]>
Commit | Line | Data |
---|---|---|
ab113d22 JF |
1 | /* ldid - (Mach-O) Link-Loader Identity Editor |
2 | * Copyright (C) 2007-2010 Jay Freeman (saurik) | |
a362a82f JF |
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; | |
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 | |
91 | struct 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 | 98 | #define LC_SEGMENT uint32_t(0x01) |
20e7eb29 | 99 | #define LC_SYMTAB uint32_t(0x02) |
645f8f12 JF |
100 | #define LC_LOAD_DYLIB uint32_t(0x0c) |
101 | #define LC_ID_DYLIB uint32_t(0x0d) | |
102 | #define LC_UUID uint32_t(0x1b) | |
103 | #define LC_CODE_SIGNATURE uint32_t(0x1d) | |
104 | #define LC_REEXPORT_DYLIB uint32_t(0x1f | LC_REQ_DYLD) | |
a362a82f JF |
105 | |
106 | struct dylib { | |
107 | uint32_t name; | |
108 | uint32_t timestamp; | |
109 | uint32_t current_version; | |
110 | uint32_t compatibility_version; | |
c05d9758 | 111 | } _packed; |
a362a82f JF |
112 | |
113 | struct dylib_command { | |
114 | uint32_t cmd; | |
115 | uint32_t cmdsize; | |
116 | struct dylib dylib; | |
c05d9758 | 117 | } _packed; |
a362a82f JF |
118 | |
119 | struct uuid_command { | |
120 | uint32_t cmd; | |
121 | uint32_t cmdsize; | |
122 | uint8_t uuid[16]; | |
c05d9758 | 123 | } _packed; |
a362a82f | 124 | |
20e7eb29 JF |
125 | struct symtab_command { |
126 | uint32_t cmd; | |
127 | uint32_t cmdsize; | |
128 | uint32_t symoff; | |
129 | uint32_t nsyms; | |
130 | uint32_t stroff; | |
131 | uint32_t strsize; | |
132 | } _packed; | |
133 | ||
6e83315b JF |
134 | struct segment_command { |
135 | uint32_t cmd; | |
136 | uint32_t cmdsize; | |
137 | char segname[16]; | |
138 | uint32_t vmaddr; | |
139 | uint32_t vmsize; | |
140 | uint32_t fileoff; | |
141 | uint32_t filesize; | |
142 | uint32_t maxprot; | |
143 | uint32_t initprot; | |
144 | uint32_t nsects; | |
145 | uint32_t flags; | |
146 | }; | |
147 | ||
148 | struct section { | |
149 | char sectname[16]; | |
150 | char segname[16]; | |
151 | uint32_t addr; | |
152 | uint32_t size; | |
153 | uint32_t offset; | |
154 | uint32_t align; | |
155 | uint32_t reloff; | |
156 | uint32_t nreloc; | |
157 | uint32_t flags; | |
158 | uint32_t reserved1; | |
159 | uint32_t reserved2; | |
160 | }; | |
161 | ||
fdb119ef JF |
162 | struct linkedit_data_command { |
163 | uint32_t cmd; | |
164 | uint32_t cmdsize; | |
165 | uint32_t dataoff; | |
166 | uint32_t datasize; | |
c05d9758 | 167 | } _packed; |
fdb119ef JF |
168 | |
169 | uint16_t Swap_(uint16_t value) { | |
170 | return | |
171 | ((value >> 8) & 0x00ff) | | |
172 | ((value << 8) & 0xff00); | |
173 | } | |
174 | ||
175 | uint32_t Swap_(uint32_t value) { | |
176 | value = ((value >> 8) & 0x00ff00ff) | | |
177 | ((value << 8) & 0xff00ff00); | |
178 | value = ((value >> 16) & 0x0000ffff) | | |
179 | ((value << 16) & 0xffff0000); | |
180 | return value; | |
181 | } | |
182 | ||
183 | int16_t Swap_(int16_t value) { | |
184 | return Swap_(static_cast<uint16_t>(value)); | |
185 | } | |
186 | ||
187 | int32_t Swap_(int32_t value) { | |
188 | return Swap_(static_cast<uint32_t>(value)); | |
189 | } | |
190 | ||
5525a5a7 JF |
191 | bool little_(true); |
192 | ||
fdb119ef | 193 | uint16_t Swap(uint16_t value) { |
5525a5a7 | 194 | return little_ ? Swap_(value) : value; |
fdb119ef JF |
195 | } |
196 | ||
197 | uint32_t Swap(uint32_t value) { | |
5525a5a7 | 198 | return little_ ? Swap_(value) : value; |
fdb119ef JF |
199 | } |
200 | ||
201 | int16_t Swap(int16_t value) { | |
202 | return Swap(static_cast<uint16_t>(value)); | |
203 | } | |
204 | ||
205 | int32_t Swap(int32_t value) { | |
206 | return Swap(static_cast<uint32_t>(value)); | |
207 | } | |
208 | ||
6e83315b JF |
209 | template <typename Target_> |
210 | class Pointer; | |
211 | ||
0a033524 | 212 | class Data { |
a362a82f JF |
213 | private: |
214 | void *base_; | |
215 | size_t size_; | |
3cbc6463 | 216 | |
0a033524 | 217 | protected: |
a362a82f JF |
218 | bool swapped_; |
219 | ||
220 | public: | |
0a033524 JF |
221 | Data(void *base, size_t size) : |
222 | base_(base), | |
223 | size_(size), | |
224 | swapped_(false) | |
225 | { | |
226 | } | |
227 | ||
fdb119ef JF |
228 | uint16_t Swap(uint16_t value) const { |
229 | return swapped_ ? Swap_(value) : value; | |
a362a82f JF |
230 | } |
231 | ||
fdb119ef JF |
232 | uint32_t Swap(uint32_t value) const { |
233 | return swapped_ ? Swap_(value) : value; | |
a362a82f JF |
234 | } |
235 | ||
fdb119ef JF |
236 | int16_t Swap(int16_t value) const { |
237 | return Swap(static_cast<uint16_t>(value)); | |
a362a82f JF |
238 | } |
239 | ||
fdb119ef JF |
240 | int32_t Swap(int32_t value) const { |
241 | return Swap(static_cast<uint32_t>(value)); | |
a362a82f JF |
242 | } |
243 | ||
0a033524 JF |
244 | void *GetBase() const { |
245 | return base_; | |
246 | } | |
a362a82f | 247 | |
0a033524 JF |
248 | size_t GetSize() const { |
249 | return size_; | |
250 | } | |
251 | }; | |
a362a82f | 252 | |
0a033524 JF |
253 | class MachHeader : |
254 | public Data | |
255 | { | |
256 | private: | |
257 | bool bits64_; | |
258 | ||
259 | struct mach_header *mach_header_; | |
260 | struct load_command *load_command_; | |
261 | ||
262 | public: | |
263 | MachHeader(void *base, size_t size) : | |
264 | Data(base, size) | |
265 | { | |
266 | mach_header_ = (mach_header *) base; | |
a362a82f | 267 | |
3cbc6463 JF |
268 | switch (Swap(mach_header_->magic)) { |
269 | case MH_CIGAM: | |
270 | swapped_ = !swapped_; | |
271 | case MH_MAGIC: | |
272 | bits64_ = false; | |
273 | break; | |
274 | ||
275 | case MH_CIGAM_64: | |
276 | swapped_ = !swapped_; | |
277 | case MH_MAGIC_64: | |
278 | bits64_ = true; | |
279 | break; | |
280 | ||
281 | default: | |
282 | _assert(false); | |
283 | } | |
284 | ||
285 | void *post = mach_header_ + 1; | |
286 | if (bits64_) | |
287 | post = (uint32_t *) post + 1; | |
288 | load_command_ = (struct load_command *) post; | |
a362a82f JF |
289 | |
290 | _assert( | |
291 | Swap(mach_header_->filetype) == MH_EXECUTE || | |
292 | Swap(mach_header_->filetype) == MH_DYLIB || | |
293 | Swap(mach_header_->filetype) == MH_BUNDLE | |
294 | ); | |
295 | } | |
296 | ||
afbb7c8e JF |
297 | struct mach_header *operator ->() const { |
298 | return mach_header_; | |
299 | } | |
300 | ||
04802ab1 JF |
301 | uint32_t GetCPUType() const { |
302 | return Swap(mach_header_->cputype); | |
303 | } | |
304 | ||
305 | uint16_t GetCPUSubtype() const { | |
306 | return Swap(mach_header_->cpusubtype) & 0xff; | |
307 | } | |
308 | ||
0a033524 | 309 | std::vector<struct load_command *> GetLoadCommands() const { |
a362a82f JF |
310 | std::vector<struct load_command *> load_commands; |
311 | ||
3cbc6463 | 312 | struct load_command *load_command = load_command_; |
a362a82f JF |
313 | for (uint32_t cmd = 0; cmd != Swap(mach_header_->ncmds); ++cmd) { |
314 | load_commands.push_back(load_command); | |
315 | load_command = (struct load_command *) ((uint8_t *) load_command + Swap(load_command->cmdsize)); | |
316 | } | |
317 | ||
318 | return load_commands; | |
319 | } | |
6e83315b JF |
320 | |
321 | std::vector<segment_command *> GetSegments(const char *segment_name) { | |
322 | std::vector<struct segment_command *> segment_commands; | |
323 | ||
324 | _foreach (load_command, GetLoadCommands()) | |
0a033524 JF |
325 | if (Swap(load_command->cmd) == LC_SEGMENT) { |
326 | segment_command *segment_command = reinterpret_cast<struct segment_command *>(load_command); | |
6e83315b JF |
327 | if (strncmp(segment_command->segname, segment_name, 16) == 0) |
328 | segment_commands.push_back(segment_command); | |
329 | } | |
330 | ||
331 | return segment_commands; | |
332 | } | |
333 | ||
334 | std::vector<section *> GetSections(const char *segment_name, const char *section_name) { | |
335 | std::vector<section *> sections; | |
336 | ||
337 | _foreach (segment, GetSegments(segment_name)) { | |
0a033524 | 338 | section *section = (struct section *) (segment + 1); |
6e83315b JF |
339 | |
340 | uint32_t sect; | |
0a033524 | 341 | for (sect = 0; sect != Swap(segment->nsects); ++sect) { |
6e83315b JF |
342 | if (strncmp(section->sectname, section_name, 16) == 0) |
343 | sections.push_back(section); | |
344 | ++section; | |
345 | } | |
346 | } | |
347 | ||
348 | return sections; | |
349 | } | |
350 | ||
351 | template <typename Target_> | |
0a033524 | 352 | Pointer<Target_> GetPointer(uint32_t address, const char *segment_name = NULL) const { |
6e83315b JF |
353 | load_command *load_command = (struct load_command *) (mach_header_ + 1); |
354 | uint32_t cmd; | |
355 | ||
356 | for (cmd = 0; cmd != Swap(mach_header_->ncmds); ++cmd) { | |
357 | if (Swap(load_command->cmd) == LC_SEGMENT) { | |
358 | segment_command *segment_command = (struct segment_command *) load_command; | |
359 | if (segment_name != NULL && strncmp(segment_command->segname, segment_name, 16) != 0) | |
360 | goto next_command; | |
361 | ||
362 | section *sections = (struct section *) (segment_command + 1); | |
363 | ||
364 | uint32_t sect; | |
365 | for (sect = 0; sect != Swap(segment_command->nsects); ++sect) { | |
366 | section *section = §ions[sect]; | |
367 | //printf("%s %u %p %p %u\n", segment_command->segname, sect, address, section->addr, section->size); | |
368 | if (address >= Swap(section->addr) && address < Swap(section->addr) + Swap(section->size)) { | |
369 | //printf("0x%.8x %s\n", address, segment_command->segname); | |
370 | return Pointer<Target_>(this, reinterpret_cast<Target_ *>(address - Swap(section->addr) + Swap(section->offset) + (char *) mach_header_)); | |
371 | } | |
372 | } | |
373 | } | |
374 | ||
375 | next_command: | |
376 | load_command = (struct load_command *) ((char *) load_command + Swap(load_command->cmdsize)); | |
377 | } | |
378 | ||
379 | return Pointer<Target_>(this); | |
380 | } | |
381 | ||
382 | template <typename Target_> | |
383 | Pointer<Target_> GetOffset(uint32_t offset) { | |
384 | return Pointer<Target_>(this, reinterpret_cast<Target_ *>(offset + (uint8_t *) mach_header_)); | |
385 | } | |
386 | }; | |
387 | ||
0a033524 JF |
388 | class FatHeader : |
389 | public Data | |
390 | { | |
391 | private: | |
392 | fat_header *fat_header_; | |
393 | std::vector<MachHeader> mach_headers_; | |
394 | ||
395 | public: | |
396 | FatHeader(void *base, size_t size) : | |
397 | Data(base, size) | |
398 | { | |
399 | fat_header_ = reinterpret_cast<struct fat_header *>(base); | |
400 | ||
401 | if (Swap(fat_header_->magic) == FAT_CIGAM) { | |
402 | swapped_ = !swapped_; | |
403 | goto fat; | |
404 | } else if (Swap(fat_header_->magic) != FAT_MAGIC) { | |
405 | fat_header_ = NULL; | |
406 | mach_headers_.push_back(MachHeader(base, size)); | |
407 | } else fat: { | |
408 | size_t fat_narch = Swap(fat_header_->nfat_arch); | |
409 | fat_arch *fat_arch = reinterpret_cast<struct fat_arch *>(fat_header_ + 1); | |
410 | size_t arch; | |
411 | for (arch = 0; arch != fat_narch; ++arch) { | |
412 | uint32_t arch_offset = Swap(fat_arch->offset); | |
413 | uint32_t arch_size = Swap(fat_arch->size); | |
414 | mach_headers_.push_back(MachHeader((uint8_t *) base + arch_offset, size)); | |
415 | ++fat_arch; | |
416 | } | |
417 | } | |
418 | } | |
419 | ||
420 | std::vector<MachHeader> &GetMachHeaders() { | |
421 | return mach_headers_; | |
422 | } | |
20e7eb29 JF |
423 | |
424 | bool IsFat() const { | |
425 | return fat_header_ != NULL; | |
426 | } | |
0a033524 JF |
427 | }; |
428 | ||
429 | FatHeader Map(const char *path) { | |
430 | size_t size; | |
431 | void *base(map(path, 0, _not(size_t), &size, false)); | |
432 | return FatHeader(base, size); | |
433 | } | |
434 | ||
6e83315b JF |
435 | template <typename Target_> |
436 | class Pointer { | |
437 | private: | |
0a033524 | 438 | const MachHeader *framework_; |
6e83315b JF |
439 | const Target_ *pointer_; |
440 | ||
441 | public: | |
0a033524 | 442 | Pointer(const MachHeader *framework = NULL, const Target_ *pointer = NULL) : |
6e83315b JF |
443 | framework_(framework), |
444 | pointer_(pointer) | |
445 | { | |
446 | } | |
447 | ||
448 | operator const Target_ *() const { | |
449 | return pointer_; | |
450 | } | |
451 | ||
452 | const Target_ *operator ->() const { | |
453 | return pointer_; | |
454 | } | |
455 | ||
456 | Pointer<Target_> &operator ++() { | |
457 | ++pointer_; | |
458 | return *this; | |
459 | } | |
460 | ||
461 | template <typename Value_> | |
462 | Value_ Swap(Value_ value) { | |
463 | return framework_->Swap(value); | |
464 | } | |
a362a82f JF |
465 | }; |
466 | ||
645f8f12 JF |
467 | #define CSMAGIC_CODEDIRECTORY uint32_t(0xfade0c02) |
468 | #define CSMAGIC_EMBEDDED_SIGNATURE uint32_t(0xfade0cc0) | |
469 | #define CSMAGIC_ENTITLEMENTS uint32_t(0xfade7171) | |
c05d9758 | 470 | |
645f8f12 JF |
471 | #define CSSLOT_CODEDIRECTORY uint32_t(0) |
472 | #define CSSLOT_REQUIREMENTS uint32_t(2) | |
473 | #define CSSLOT_ENTITLEMENTS uint32_t(5) | |
fdb119ef JF |
474 | |
475 | struct BlobIndex { | |
476 | uint32_t type; | |
477 | uint32_t offset; | |
c05d9758 | 478 | } _packed; |
fdb119ef | 479 | |
c05d9758 | 480 | struct Blob { |
fdb119ef JF |
481 | uint32_t magic; |
482 | uint32_t length; | |
c05d9758 JF |
483 | } _packed; |
484 | ||
485 | struct SuperBlob { | |
486 | struct Blob blob; | |
fdb119ef JF |
487 | uint32_t count; |
488 | struct BlobIndex index[]; | |
c05d9758 | 489 | } _packed; |
fdb119ef JF |
490 | |
491 | struct CodeDirectory { | |
c05d9758 | 492 | struct Blob blob; |
fdb119ef JF |
493 | uint32_t version; |
494 | uint32_t flags; | |
495 | uint32_t hashOffset; | |
496 | uint32_t identOffset; | |
497 | uint32_t nSpecialSlots; | |
498 | uint32_t nCodeSlots; | |
499 | uint32_t codeLimit; | |
500 | uint8_t hashSize; | |
501 | uint8_t hashType; | |
502 | uint8_t spare1; | |
503 | uint8_t pageSize; | |
504 | uint32_t spare2; | |
c05d9758 | 505 | } _packed; |
fdb119ef | 506 | |
a362a82f JF |
507 | extern "C" uint32_t hash(uint8_t *k, uint32_t length, uint32_t initval); |
508 | ||
fdb119ef JF |
509 | void sha1(uint8_t *hash, uint8_t *data, size_t size) { |
510 | SHA1Context context; | |
511 | SHA1Reset(&context); | |
512 | SHA1Input(&context, data, size); | |
513 | SHA1Result(&context, hash); | |
514 | } | |
515 | ||
0a033524 JF |
516 | struct CodesignAllocation { |
517 | uint32_t type_; | |
518 | uint16_t subtype_; | |
519 | size_t size_; | |
520 | ||
521 | CodesignAllocation(uint32_t type, uint16_t subtype, size_t size) : | |
522 | type_(type), | |
523 | subtype_(subtype), | |
524 | size_(size) | |
525 | { | |
526 | } | |
527 | }; | |
528 | ||
a362a82f | 529 | int main(int argc, const char *argv[]) { |
5525a5a7 JF |
530 | union { |
531 | uint16_t word; | |
532 | uint8_t byte[2]; | |
533 | } endian = {1}; | |
534 | ||
535 | little_ = endian.byte[0]; | |
536 | ||
a362a82f | 537 | bool flag_R(false); |
20e7eb29 JF |
538 | bool flag_r(false); |
539 | ||
a362a82f JF |
540 | bool flag_t(false); |
541 | bool flag_p(false); | |
542 | bool flag_u(false); | |
9c83be90 | 543 | bool flag_e(false); |
a362a82f JF |
544 | |
545 | bool flag_T(false); | |
20c5f1e8 | 546 | |
fdb119ef | 547 | bool flag_S(false); |
20c5f1e8 | 548 | bool flag_s(false); |
a362a82f JF |
549 | |
550 | bool timeh(false); | |
551 | uint32_t timev(0); | |
552 | ||
c05d9758 JF |
553 | const void *xmld(NULL); |
554 | size_t xmls(0); | |
555 | ||
6e83315b JF |
556 | uintptr_t noffset(_not(uintptr_t)); |
557 | uintptr_t woffset(_not(uintptr_t)); | |
558 | ||
a362a82f JF |
559 | std::vector<std::string> files; |
560 | ||
a960f392 JF |
561 | if (argc == 1) { |
562 | fprintf(stderr, "usage: %s -S[entitlements.xml] <binary>\n", argv[0]); | |
9c83be90 | 563 | fprintf(stderr, " %s -e MobileSafari\n", argv[0]); |
a960f392 JF |
564 | fprintf(stderr, " %s -S cat\n", argv[0]); |
565 | fprintf(stderr, " %s -Stfp.xml gdb\n", argv[0]); | |
566 | exit(0); | |
567 | } | |
568 | ||
a362a82f JF |
569 | for (int argi(1); argi != argc; ++argi) |
570 | if (argv[argi][0] != '-') | |
571 | files.push_back(argv[argi]); | |
572 | else switch (argv[argi][1]) { | |
573 | case 'R': flag_R = true; break; | |
20e7eb29 JF |
574 | case 'r': flag_r = true; break; |
575 | ||
a362a82f JF |
576 | case 't': flag_t = true; break; |
577 | case 'u': flag_u = true; break; | |
578 | case 'p': flag_p = true; break; | |
9c83be90 | 579 | case 'e': flag_e = true; break; |
c05d9758 | 580 | |
20c5f1e8 JF |
581 | case 's': |
582 | _assert(!flag_S); | |
583 | flag_s = true; | |
584 | break; | |
585 | ||
c05d9758 | 586 | case 'S': |
20c5f1e8 | 587 | _assert(!flag_s); |
c05d9758 JF |
588 | flag_S = true; |
589 | if (argv[argi][2] != '\0') { | |
590 | const char *xml = argv[argi] + 2; | |
591 | xmld = map(xml, 0, _not(size_t), &xmls, true); | |
592 | } | |
593 | break; | |
a362a82f JF |
594 | |
595 | case 'T': { | |
596 | flag_T = true; | |
597 | if (argv[argi][2] == '-') | |
598 | timeh = true; | |
599 | else { | |
600 | char *arge; | |
601 | timev = strtoul(argv[argi] + 2, &arge, 0); | |
602 | _assert(arge == argv[argi] + strlen(argv[argi])); | |
603 | } | |
604 | } break; | |
605 | ||
6e83315b JF |
606 | case 'n': { |
607 | char *arge; | |
608 | noffset = strtoul(argv[argi] + 2, &arge, 0); | |
609 | _assert(arge == argv[argi] + strlen(argv[argi])); | |
610 | } break; | |
611 | ||
612 | case 'w': { | |
613 | char *arge; | |
614 | woffset = strtoul(argv[argi] + 2, &arge, 0); | |
615 | _assert(arge == argv[argi] + strlen(argv[argi])); | |
616 | } break; | |
617 | ||
a362a82f JF |
618 | default: |
619 | goto usage; | |
620 | break; | |
621 | } | |
622 | ||
623 | if (files.empty()) usage: { | |
624 | exit(0); | |
625 | } | |
626 | ||
627 | size_t filei(0), filee(0); | |
628 | _foreach (file, files) try { | |
0a033524 | 629 | const char *path(file.c_str()); |
fdb119ef JF |
630 | const char *base = strrchr(path, '/'); |
631 | char *temp(NULL), *dir; | |
632 | ||
633 | if (base != NULL) | |
b8187ac7 | 634 | dir = strndup_(path, base++ - path + 1); |
fdb119ef JF |
635 | else { |
636 | dir = strdup(""); | |
637 | base = path; | |
638 | } | |
639 | ||
20e7eb29 JF |
640 | if (flag_r) { |
641 | size_t clip(_not(size_t)); { | |
642 | FatHeader fat_header(Map(path)); | |
643 | _foreach (mach_header, fat_header.GetMachHeaders()) { | |
644 | mach_header->flags = mach_header.Swap(mach_header.Swap(mach_header->flags) | MH_DYLDLINK); | |
645 | ||
646 | size_t size(_not(size_t)); { | |
647 | _foreach (load_command, mach_header.GetLoadCommands()) { | |
648 | switch (mach_header.Swap(load_command->cmd)) { | |
649 | case LC_CODE_SIGNATURE: { | |
650 | struct linkedit_data_command *signature = reinterpret_cast<struct linkedit_data_command *>(load_command); | |
651 | memset(signature, 0, sizeof(struct linkedit_data_command)); | |
652 | ||
653 | mach_header->ncmds -= 1; | |
654 | mach_header->sizeofcmds -= sizeof(struct linkedit_data_command); | |
655 | } break; | |
656 | ||
657 | case LC_SYMTAB: { | |
658 | struct symtab_command *symtab = reinterpret_cast<struct symtab_command *>(load_command); | |
659 | size = symtab->stroff + symtab->strsize; | |
660 | } break; | |
661 | } | |
662 | } | |
663 | ||
664 | _foreach (segment, const_cast<MachHeader &>(mach_header).GetSegments("__LINKEDIT")) { | |
665 | segment->filesize -= mach_header.GetSize() - size; | |
666 | ||
667 | if (!fat_header.IsFat()) | |
668 | clip = size; | |
669 | else | |
670 | _assert(false); | |
671 | } | |
672 | } | |
673 | } | |
674 | } | |
675 | ||
676 | if (clip != _not(size_t)) | |
677 | _syscall(truncate(path, clip)); | |
678 | } | |
679 | ||
fdb119ef JF |
680 | if (flag_S) { |
681 | asprintf(&temp, "%s.%s.cs", dir, base); | |
682 | const char *allocate = getenv("CODESIGN_ALLOCATE"); | |
683 | if (allocate == NULL) | |
684 | allocate = "codesign_allocate"; | |
685 | ||
0a033524 JF |
686 | std::vector<CodesignAllocation> allocations; { |
687 | FatHeader fat_header(Map(path)); | |
688 | _foreach (mach_header, fat_header.GetMachHeaders()) { | |
689 | mach_header->flags = mach_header.Swap(mach_header.Swap(mach_header->flags) | MH_DYLDLINK); | |
690 | ||
691 | size_t size(_not(size_t)); { | |
692 | _foreach (load_command, mach_header.GetLoadCommands()) { | |
693 | uint32_t cmd(mach_header.Swap(load_command->cmd)); | |
694 | if (cmd == LC_CODE_SIGNATURE) { | |
695 | struct linkedit_data_command *signature = reinterpret_cast<struct linkedit_data_command *>(load_command); | |
696 | size = mach_header.Swap(signature->dataoff); | |
697 | _assert(size < mach_header.GetSize()); | |
698 | break; | |
699 | } | |
700 | } | |
701 | ||
702 | if (size == _not(size_t)) | |
703 | size = mach_header.GetSize(); | |
afbb7c8e | 704 | } |
afbb7c8e | 705 | |
0a033524 | 706 | allocations.push_back(CodesignAllocation(mach_header.GetCPUType(), mach_header.GetCPUSubtype(), size)); |
afbb7c8e | 707 | } |
fdb119ef JF |
708 | } |
709 | ||
710 | pid_t pid = fork(); | |
711 | _syscall(pid); | |
712 | if (pid == 0) { | |
0a033524 JF |
713 | // XXX: this leaks memory, but it doesn't really matter |
714 | std::vector<const char *> args; | |
715 | char *arg; | |
716 | ||
717 | args.push_back(allocate); | |
718 | ||
719 | args.push_back("-i"); | |
720 | args.push_back(path); | |
721 | ||
722 | _foreach (allocation, allocations) { | |
723 | args.push_back("-A"); | |
724 | ||
725 | asprintf(&arg, "%u", allocation.type_); | |
726 | args.push_back(arg); | |
727 | ||
728 | asprintf(&arg, "%u", allocation.subtype_); | |
729 | args.push_back(arg); | |
730 | ||
731 | size_t alloc(0); | |
732 | alloc += sizeof(struct SuperBlob); | |
733 | uint32_t special(0); | |
734 | ||
735 | special = std::max(special, CSSLOT_CODEDIRECTORY); | |
736 | alloc += sizeof(struct BlobIndex); | |
737 | alloc += sizeof(struct CodeDirectory); | |
738 | alloc += strlen(base) + 1; | |
739 | ||
740 | special = std::max(special, CSSLOT_REQUIREMENTS); | |
741 | alloc += sizeof(struct BlobIndex); | |
742 | alloc += 0xc; | |
743 | ||
744 | if (xmld != NULL) { | |
745 | special = std::max(special, CSSLOT_ENTITLEMENTS); | |
746 | alloc += sizeof(struct BlobIndex); | |
747 | alloc += sizeof(struct Blob); | |
748 | alloc += xmls; | |
749 | } | |
750 | ||
751 | size_t normal((allocation.size_ + 0x1000 - 1) / 0x1000); | |
752 | alloc += (special + normal) * 0x14; | |
753 | ||
754 | alloc += 15; | |
755 | alloc /= 16; | |
756 | alloc *= 16; | |
757 | ||
758 | asprintf(&arg, "%u", alloc); | |
759 | args.push_back(arg); | |
760 | } | |
761 | ||
762 | args.push_back("-o"); | |
763 | args.push_back(temp); | |
764 | ||
765 | args.push_back(NULL); | |
766 | ||
767 | if (false) { | |
768 | printf("run:"); | |
769 | _foreach (arg, args) | |
770 | printf(" %s", arg); | |
771 | printf("\n"); | |
772 | } | |
773 | ||
774 | execvp(allocate, (char **) &args[0]); | |
fdb119ef JF |
775 | _assert(false); |
776 | } | |
777 | ||
778 | int status; | |
779 | _syscall(waitpid(pid, &status, 0)); | |
780 | _assert(WIFEXITED(status)); | |
781 | _assert(WEXITSTATUS(status) == 0); | |
782 | } | |
783 | ||
0a033524 JF |
784 | if (flag_p) |
785 | printf("path%zu='%s'\n", filei, file.c_str()); | |
786 | ||
787 | FatHeader fat_header(Map(temp == NULL ? path : temp)); | |
fdb119ef | 788 | struct linkedit_data_command *signature(NULL); |
a362a82f | 789 | |
0a033524 JF |
790 | _foreach (mach_header, fat_header.GetMachHeaders()) { |
791 | if (woffset != _not(uintptr_t)) { | |
792 | Pointer<uint32_t> wvalue(mach_header.GetPointer<uint32_t>(woffset)); | |
793 | if (wvalue == NULL) | |
794 | printf("(null) %p\n", reinterpret_cast<void *>(woffset)); | |
795 | else | |
796 | printf("0x%.08x\n", *wvalue); | |
797 | } | |
6e83315b | 798 | |
0a033524 JF |
799 | if (noffset != _not(uintptr_t)) |
800 | printf("%s\n", &*mach_header.GetPointer<char>(noffset)); | |
801 | ||
802 | _foreach (load_command, mach_header.GetLoadCommands()) { | |
803 | uint32_t cmd(mach_header.Swap(load_command->cmd)); | |
804 | ||
805 | if (flag_R && cmd == LC_REEXPORT_DYLIB) | |
806 | load_command->cmd = mach_header.Swap(LC_LOAD_DYLIB); | |
807 | else if (cmd == LC_CODE_SIGNATURE) | |
808 | signature = reinterpret_cast<struct linkedit_data_command *>(load_command); | |
809 | else if (cmd == LC_UUID) { | |
810 | volatile struct uuid_command *uuid_command(reinterpret_cast<struct uuid_command *>(load_command)); | |
811 | ||
812 | if (flag_u) { | |
813 | printf("uuid%zu=%.2x%.2x%.2x%.2x-%.2x%.2x-%.2x%.2x-%.2x%.2x-%.2x%.2x%.2x%.2x%.2x%.2x\n", filei, | |
814 | uuid_command->uuid[ 0], uuid_command->uuid[ 1], uuid_command->uuid[ 2], uuid_command->uuid[ 3], | |
815 | uuid_command->uuid[ 4], uuid_command->uuid[ 5], uuid_command->uuid[ 6], uuid_command->uuid[ 7], | |
816 | uuid_command->uuid[ 8], uuid_command->uuid[ 9], uuid_command->uuid[10], uuid_command->uuid[11], | |
817 | uuid_command->uuid[12], uuid_command->uuid[13], uuid_command->uuid[14], uuid_command->uuid[15] | |
818 | ); | |
819 | } | |
820 | } else if (cmd == LC_ID_DYLIB) { | |
821 | volatile struct dylib_command *dylib_command(reinterpret_cast<struct dylib_command *>(load_command)); | |
a362a82f | 822 | |
0a033524 JF |
823 | if (flag_t) |
824 | printf("time%zu=0x%.8x\n", filei, mach_header.Swap(dylib_command->dylib.timestamp)); | |
a362a82f | 825 | |
0a033524 JF |
826 | if (flag_T) { |
827 | uint32_t timed; | |
a362a82f | 828 | |
0a033524 JF |
829 | if (!timeh) |
830 | timed = timev; | |
831 | else { | |
832 | dylib_command->dylib.timestamp = 0; | |
833 | timed = hash(reinterpret_cast<uint8_t *>(mach_header.GetBase()), mach_header.GetSize(), timev); | |
834 | } | |
a362a82f | 835 | |
0a033524 JF |
836 | dylib_command->dylib.timestamp = mach_header.Swap(timed); |
837 | } | |
a362a82f JF |
838 | } |
839 | } | |
a362a82f | 840 | |
0a033524 JF |
841 | if (flag_e) { |
842 | _assert(signature != NULL); | |
9c83be90 | 843 | |
0a033524 JF |
844 | uint32_t data = mach_header.Swap(signature->dataoff); |
845 | uint32_t size = mach_header.Swap(signature->datasize); | |
9c83be90 | 846 | |
0a033524 JF |
847 | uint8_t *top = reinterpret_cast<uint8_t *>(mach_header.GetBase()); |
848 | uint8_t *blob = top + data; | |
849 | struct SuperBlob *super = reinterpret_cast<struct SuperBlob *>(blob); | |
9c83be90 | 850 | |
0a033524 JF |
851 | for (size_t index(0); index != Swap(super->count); ++index) |
852 | if (Swap(super->index[index].type) == CSSLOT_ENTITLEMENTS) { | |
853 | uint32_t begin = Swap(super->index[index].offset); | |
854 | struct Blob *entitlements = reinterpret_cast<struct Blob *>(blob + begin); | |
855 | fwrite(entitlements + 1, 1, Swap(entitlements->length) - sizeof(struct Blob), stdout); | |
856 | } | |
857 | } | |
9c83be90 | 858 | |
0a033524 JF |
859 | if (flag_s) { |
860 | _assert(signature != NULL); | |
20c5f1e8 | 861 | |
0a033524 JF |
862 | uint32_t data = mach_header.Swap(signature->dataoff); |
863 | uint32_t size = mach_header.Swap(signature->datasize); | |
20c5f1e8 | 864 | |
0a033524 JF |
865 | uint8_t *top = reinterpret_cast<uint8_t *>(mach_header.GetBase()); |
866 | uint8_t *blob = top + data; | |
867 | struct SuperBlob *super = reinterpret_cast<struct SuperBlob *>(blob); | |
20c5f1e8 | 868 | |
0a033524 JF |
869 | for (size_t index(0); index != Swap(super->count); ++index) |
870 | if (Swap(super->index[index].type) == CSSLOT_CODEDIRECTORY) { | |
871 | uint32_t begin = Swap(super->index[index].offset); | |
872 | struct CodeDirectory *directory = reinterpret_cast<struct CodeDirectory *>(blob + begin); | |
20c5f1e8 | 873 | |
0a033524 JF |
874 | uint8_t (*hashes)[20] = reinterpret_cast<uint8_t (*)[20]>(blob + begin + Swap(directory->hashOffset)); |
875 | uint32_t pages = Swap(directory->nCodeSlots); | |
20c5f1e8 | 876 | |
0a033524 JF |
877 | if (pages != 1) |
878 | for (size_t i = 0; i != pages - 1; ++i) | |
879 | sha1(hashes[i], top + 0x1000 * i, 0x1000); | |
880 | if (pages != 0) | |
881 | sha1(hashes[pages - 1], top + 0x1000 * (pages - 1), ((data - 1) % 0x1000) + 1); | |
882 | } | |
883 | } | |
20c5f1e8 | 884 | |
0a033524 JF |
885 | if (flag_S) { |
886 | _assert(signature != NULL); | |
fdb119ef | 887 | |
0a033524 JF |
888 | uint32_t data = mach_header.Swap(signature->dataoff); |
889 | uint32_t size = mach_header.Swap(signature->datasize); | |
fdb119ef | 890 | |
0a033524 JF |
891 | uint8_t *top = reinterpret_cast<uint8_t *>(mach_header.GetBase()); |
892 | uint8_t *blob = top + data; | |
893 | struct SuperBlob *super = reinterpret_cast<struct SuperBlob *>(blob); | |
894 | super->blob.magic = Swap(CSMAGIC_EMBEDDED_SIGNATURE); | |
fdb119ef | 895 | |
0a033524 JF |
896 | uint32_t count = xmld == NULL ? 2 : 3; |
897 | uint32_t offset = sizeof(struct SuperBlob) + count * sizeof(struct BlobIndex); | |
fdb119ef | 898 | |
0a033524 JF |
899 | super->index[0].type = Swap(CSSLOT_CODEDIRECTORY); |
900 | super->index[0].offset = Swap(offset); | |
fdb119ef | 901 | |
0a033524 JF |
902 | uint32_t begin = offset; |
903 | struct CodeDirectory *directory = reinterpret_cast<struct CodeDirectory *>(blob + begin); | |
904 | offset += sizeof(struct CodeDirectory); | |
fdb119ef | 905 | |
0a033524 JF |
906 | directory->blob.magic = Swap(CSMAGIC_CODEDIRECTORY); |
907 | directory->version = Swap(uint32_t(0x00020001)); | |
908 | directory->flags = Swap(uint32_t(0)); | |
909 | directory->codeLimit = Swap(data); | |
910 | directory->hashSize = 0x14; | |
911 | directory->hashType = 0x01; | |
912 | directory->spare1 = 0x00; | |
913 | directory->pageSize = 0x0c; | |
914 | directory->spare2 = Swap(uint32_t(0)); | |
fdb119ef | 915 | |
0a033524 JF |
916 | directory->identOffset = Swap(offset - begin); |
917 | strcpy(reinterpret_cast<char *>(blob + offset), base); | |
918 | offset += strlen(base) + 1; | |
fdb119ef | 919 | |
0a033524 JF |
920 | uint32_t special = xmld == NULL ? CSSLOT_REQUIREMENTS : CSSLOT_ENTITLEMENTS; |
921 | directory->nSpecialSlots = Swap(special); | |
20c5f1e8 | 922 | |
0a033524 JF |
923 | uint8_t (*hashes)[20] = reinterpret_cast<uint8_t (*)[20]>(blob + offset); |
924 | memset(hashes, 0, sizeof(*hashes) * special); | |
20c5f1e8 | 925 | |
0a033524 JF |
926 | offset += sizeof(*hashes) * special; |
927 | hashes += special; | |
fdb119ef | 928 | |
0a033524 JF |
929 | uint32_t pages = (data + 0x1000 - 1) / 0x1000; |
930 | directory->nCodeSlots = Swap(pages); | |
fdb119ef | 931 | |
0a033524 JF |
932 | if (pages != 1) |
933 | for (size_t i = 0; i != pages - 1; ++i) | |
934 | sha1(hashes[i], top + 0x1000 * i, 0x1000); | |
935 | if (pages != 0) | |
936 | sha1(hashes[pages - 1], top + 0x1000 * (pages - 1), ((data - 1) % 0x1000) + 1); | |
fdb119ef | 937 | |
0a033524 JF |
938 | directory->hashOffset = Swap(offset - begin); |
939 | offset += sizeof(*hashes) * pages; | |
940 | directory->blob.length = Swap(offset - begin); | |
fdb119ef | 941 | |
0a033524 JF |
942 | super->index[1].type = Swap(CSSLOT_REQUIREMENTS); |
943 | super->index[1].offset = Swap(offset); | |
afbb7c8e | 944 | |
0a033524 JF |
945 | memcpy(blob + offset, "\xfa\xde\x0c\x01\x00\x00\x00\x0c\x00\x00\x00\x00", 0xc); |
946 | offset += 0xc; | |
afbb7c8e | 947 | |
0a033524 JF |
948 | if (xmld != NULL) { |
949 | super->index[2].type = Swap(CSSLOT_ENTITLEMENTS); | |
950 | super->index[2].offset = Swap(offset); | |
c05d9758 | 951 | |
0a033524 JF |
952 | uint32_t begin = offset; |
953 | struct Blob *entitlements = reinterpret_cast<struct Blob *>(blob + begin); | |
954 | offset += sizeof(struct Blob); | |
c05d9758 | 955 | |
0a033524 JF |
956 | memcpy(blob + offset, xmld, xmls); |
957 | offset += xmls; | |
c05d9758 | 958 | |
0a033524 JF |
959 | entitlements->magic = Swap(CSMAGIC_ENTITLEMENTS); |
960 | entitlements->length = Swap(offset - begin); | |
961 | } | |
c05d9758 | 962 | |
0a033524 JF |
963 | for (size_t index(0); index != count; ++index) { |
964 | uint32_t type = Swap(super->index[index].type); | |
965 | if (type != 0 && type <= special) { | |
966 | uint32_t offset = Swap(super->index[index].offset); | |
967 | struct Blob *local = (struct Blob *) (blob + offset); | |
968 | sha1((uint8_t *) (hashes - type), (uint8_t *) local, Swap(local->length)); | |
969 | } | |
20c5f1e8 | 970 | } |
20c5f1e8 | 971 | |
0a033524 JF |
972 | super->count = Swap(count); |
973 | super->blob.length = Swap(offset); | |
c05d9758 | 974 | |
0a033524 JF |
975 | if (offset > size) { |
976 | fprintf(stderr, "offset (%u) > size (%u)\n", offset, size); | |
977 | _assert(false); | |
978 | } //else fprintf(stderr, "offset (%zu) <= size (%zu)\n", offset, size); | |
fdb119ef | 979 | |
0a033524 JF |
980 | memset(blob + offset, 0, size - offset); |
981 | } | |
fdb119ef JF |
982 | } |
983 | ||
122621c5 | 984 | if (flag_S) { |
0a033524 JF |
985 | uint8_t *top = reinterpret_cast<uint8_t *>(fat_header.GetBase()); |
986 | size_t size = fat_header.GetSize(); | |
122621c5 JF |
987 | |
988 | char *copy; | |
989 | asprintf(©, "%s.%s.cp", dir, base); | |
990 | FILE *file = fopen(copy, "w+"); | |
991 | size_t writ = fwrite(top, 1, size, file); | |
992 | _assert(writ == size); | |
993 | fclose(file); | |
994 | ||
995 | _syscall(unlink(temp)); | |
996 | free(temp); | |
997 | temp = copy; | |
998 | } | |
999 | ||
0a033524 | 1000 | if (temp != NULL) { |
31ad673b JF |
1001 | struct stat info; |
1002 | _syscall(stat(path, &info)); | |
1003 | _syscall(chown(temp, info.st_uid, info.st_gid)); | |
1004 | _syscall(chmod(temp, info.st_mode)); | |
fdb119ef JF |
1005 | _syscall(unlink(path)); |
1006 | _syscall(rename(temp, path)); | |
1007 | free(temp); | |
1008 | } | |
1009 | ||
1010 | free(dir); | |
a362a82f JF |
1011 | ++filei; |
1012 | } catch (const char *) { | |
1013 | ++filee; | |
1014 | ++filei; | |
1015 | } | |
1016 | ||
1017 | return filee; | |
1018 | } |