1 /* ldid - (Mach-O) Link-Loader Identity Editor
2 * Copyright (C) 2007-2012 Jay Freeman (saurik)
5 /* GNU Affero General Public License, Version 3 {{{ */
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "minimal/stdlib.h"
23 #include "minimal/string.h"
24 #include "minimal/mapping.h"
39 #define FAT_MAGIC 0xcafebabe
40 #define FAT_CIGAM 0xbebafeca
60 #define MH_MAGIC 0xfeedface
61 #define MH_CIGAM 0xcefaedfe
63 #define MH_MAGIC_64 0xfeedfacf
64 #define MH_CIGAM_64 0xcffaedfe
66 #define MH_DYLDLINK 0x4
69 #define MH_EXECUTE 0x2
72 #define MH_DYLIB_STUB 0x9
79 #define LC_REQ_DYLD uint32_t(0x80000000)
81 #define LC_SEGMENT uint32_t(0x01)
82 #define LC_SYMTAB uint32_t(0x02)
83 #define LC_DYSYMTAB uint32_t(0x0b)
84 #define LC_LOAD_DYLIB uint32_t(0x0c)
85 #define LC_ID_DYLIB uint32_t(0x0d)
86 #define LC_SEGMENT_64 uint32_t(0x19)
87 #define LC_UUID uint32_t(0x1b)
88 #define LC_CODE_SIGNATURE uint32_t(0x1d)
89 #define LC_SEGMENT_SPLIT_INFO uint32_t(0x1e)
90 #define LC_REEXPORT_DYLIB uint32_t(0x1f | LC_REQ_DYLD)
91 #define LC_ENCRYPTION_INFO uint32_t(0x21)
92 #define LC_DYLD_INFO uint32_t(0x22)
93 #define LC_DYLD_INFO_ONLY uint32_t(0x22 | LC_REQ_DYLD)
98 uint32_t current_version
;
99 uint32_t compatibility_version
;
102 struct dylib_command
{
108 struct uuid_command
{
114 struct symtab_command
{
123 struct dyld_info_command
{
127 uint32_t rebase_size
;
130 uint32_t weak_bind_off
;
131 uint32_t weak_bind_size
;
132 uint32_t lazy_bind_off
;
133 uint32_t lazy_bind_size
;
135 uint32_t export_size
;
138 struct dysymtab_command
{
151 uint32_t extrefsymoff
;
152 uint32_t nextrefsyms
;
153 uint32_t indirectsymoff
;
154 uint32_t nindirectsyms
;
161 struct dylib_table_of_contents
{
162 uint32_t symbol_index
;
163 uint32_t module_index
;
166 struct dylib_module
{
167 uint32_t module_name
;
176 uint32_t iinit_iterm
;
177 uint32_t ninit_nterm
;
178 uint32_t objc_module_info_addr
;
179 uint32_t objc_module_info_size
;
182 struct dylib_reference
{
187 struct relocation_info
{
189 uint32_t r_symbolnum
:24;
208 struct segment_command
{
222 struct segment_command_64
{
264 struct linkedit_data_command
{
271 struct encryption_info_command
{
279 #define BIND_OPCODE_MASK 0xf0
280 #define BIND_IMMEDIATE_MASK 0x0f
281 #define BIND_OPCODE_DONE 0x00
282 #define BIND_OPCODE_SET_DYLIB_ORDINAL_IMM 0x10
283 #define BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB 0x20
284 #define BIND_OPCODE_SET_DYLIB_SPECIAL_IMM 0x30
285 #define BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM 0x40
286 #define BIND_OPCODE_SET_TYPE_IMM 0x50
287 #define BIND_OPCODE_SET_ADDEND_SLEB 0x60
288 #define BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB 0x70
289 #define BIND_OPCODE_ADD_ADDR_ULEB 0x80
290 #define BIND_OPCODE_DO_BIND 0x90
291 #define BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB 0xa0
292 #define BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED 0xb0
293 #define BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB 0xc0
295 template <typename Type_
>
296 Type_
Align(Type_ value
, size_t align
) {
303 uint16_t Swap_(uint16_t value
) {
305 ((value
>> 8) & 0x00ff) |
306 ((value
<< 8) & 0xff00);
309 uint32_t Swap_(uint32_t value
) {
310 value
= ((value
>> 8) & 0x00ff00ff) |
311 ((value
<< 8) & 0xff00ff00);
312 value
= ((value
>> 16) & 0x0000ffff) |
313 ((value
<< 16) & 0xffff0000);
317 uint64_t Swap_(uint64_t value
) {
318 value
= (value
& 0x00000000ffffffff) << 32 | (value
& 0xffffffff00000000) >> 32;
319 value
= (value
& 0x0000ffff0000ffff) << 16 | (value
& 0xffff0000ffff0000) >> 16;
320 value
= (value
& 0x00ff00ff00ff00ff) << 8 | (value
& 0xff00ff00ff00ff00) >> 8;
324 int16_t Swap_(int16_t value
) {
325 return Swap_(static_cast<uint16_t>(value
));
328 int32_t Swap_(int32_t value
) {
329 return Swap_(static_cast<uint32_t>(value
));
332 int64_t Swap_(int64_t value
) {
333 return Swap_(static_cast<uint64_t>(value
));
338 uint16_t Swap(uint16_t value
) {
339 return little_
? Swap_(value
) : value
;
342 uint32_t Swap(uint32_t value
) {
343 return little_
? Swap_(value
) : value
;
346 uint64_t Swap(uint64_t value
) {
347 return little_
? Swap_(value
) : value
;
350 int16_t Swap(int16_t value
) {
351 return Swap(static_cast<uint16_t>(value
));
354 int32_t Swap(int32_t value
) {
355 return Swap(static_cast<uint32_t>(value
));
358 int64_t Swap(int64_t value
) {
359 return Swap(static_cast<uint64_t>(value
));
362 template <typename Target_
>
374 Data(void *base
, size_t size
) :
381 uint16_t Swap(uint16_t value
) const {
382 return swapped_
? Swap_(value
) : value
;
385 uint32_t Swap(uint32_t value
) const {
386 return swapped_
? Swap_(value
) : value
;
389 uint64_t Swap(uint64_t value
) const {
390 return swapped_
? Swap_(value
) : value
;
393 int16_t Swap(int16_t value
) const {
394 return Swap(static_cast<uint16_t>(value
));
397 int32_t Swap(int32_t value
) const {
398 return Swap(static_cast<uint32_t>(value
));
401 int64_t Swap(int64_t value
) const {
402 return Swap(static_cast<uint64_t>(value
));
405 void *GetBase() const {
409 size_t GetSize() const {
420 struct mach_header
*mach_header_
;
421 struct load_command
*load_command_
;
424 MachHeader(void *base
, size_t size
) :
427 mach_header_
= (mach_header
*) base
;
429 switch (Swap(mach_header_
->magic
)) {
431 swapped_
= !swapped_
;
437 swapped_
= !swapped_
;
446 void *post
= mach_header_
+ 1;
448 post
= (uint32_t *) post
+ 1;
449 load_command_
= (struct load_command
*) post
;
452 Swap(mach_header_
->filetype
) == MH_EXECUTE
||
453 Swap(mach_header_
->filetype
) == MH_DYLIB
||
454 Swap(mach_header_
->filetype
) == MH_BUNDLE
458 struct mach_header
*operator ->() const {
462 operator struct mach_header
*() const {
466 uint32_t GetCPUType() const {
467 return Swap(mach_header_
->cputype
);
470 uint32_t GetCPUSubtype() const {
471 return Swap(mach_header_
->cpusubtype
) & 0xff;
474 struct load_command
*GetLoadCommand() const {
475 return load_command_
;
478 std::vector
<struct load_command
*> GetLoadCommands() const {
479 std::vector
<struct load_command
*> load_commands
;
481 struct load_command
*load_command
= load_command_
;
482 for (uint32_t cmd
= 0; cmd
!= Swap(mach_header_
->ncmds
); ++cmd
) {
483 load_commands
.push_back(load_command
);
484 load_command
= (struct load_command
*) ((uint8_t *) load_command
+ Swap(load_command
->cmdsize
));
487 return load_commands
;
490 std::vector
<segment_command
*> GetSegments(const char *segment_name
) const {
491 std::vector
<struct segment_command
*> segment_commands
;
493 _foreach (load_command
, GetLoadCommands()) {
494 if (Swap(load_command
->cmd
) == LC_SEGMENT
) {
495 segment_command
*segment_command
= reinterpret_cast<struct segment_command
*>(load_command
);
496 if (strncmp(segment_command
->segname
, segment_name
, 16) == 0)
497 segment_commands
.push_back(segment_command
);
501 return segment_commands
;
504 std::vector
<segment_command_64
*> GetSegments64(const char *segment_name
) const {
505 std::vector
<struct segment_command_64
*> segment_commands
;
507 _foreach (load_command
, GetLoadCommands()) {
508 if (Swap(load_command
->cmd
) == LC_SEGMENT_64
) {
509 segment_command_64
*segment_command
= reinterpret_cast<struct segment_command_64
*>(load_command
);
510 if (strncmp(segment_command
->segname
, segment_name
, 16) == 0)
511 segment_commands
.push_back(segment_command
);
515 return segment_commands
;
518 std::vector
<section
*> GetSections(const char *segment_name
, const char *section_name
) const {
519 std::vector
<section
*> sections
;
521 _foreach (segment
, GetSegments(segment_name
)) {
522 section
*section
= (struct section
*) (segment
+ 1);
525 for (sect
= 0; sect
!= Swap(segment
->nsects
); ++sect
) {
526 if (strncmp(section
->sectname
, section_name
, 16) == 0)
527 sections
.push_back(section
);
535 template <typename Target_
>
536 Pointer
<Target_
> GetPointer(uint32_t address
, const char *segment_name
= NULL
) const {
537 load_command
*load_command
= (struct load_command
*) (mach_header_
+ 1);
540 for (cmd
= 0; cmd
!= Swap(mach_header_
->ncmds
); ++cmd
) {
541 if (Swap(load_command
->cmd
) == LC_SEGMENT
) {
542 segment_command
*segment_command
= (struct segment_command
*) load_command
;
543 if (segment_name
!= NULL
&& strncmp(segment_command
->segname
, segment_name
, 16) != 0)
546 section
*sections
= (struct section
*) (segment_command
+ 1);
549 for (sect
= 0; sect
!= Swap(segment_command
->nsects
); ++sect
) {
550 section
*section
= §ions
[sect
];
551 //printf("%s %u %p %p %u\n", segment_command->segname, sect, address, section->addr, section->size);
552 if (address
>= Swap(section
->addr
) && address
< Swap(section
->addr
) + Swap(section
->size
)) {
553 //printf("0x%.8x %s\n", address, segment_command->segname);
554 return Pointer
<Target_
>(this, reinterpret_cast<Target_
*>(address
- Swap(section
->addr
) + Swap(section
->offset
) + (char *) mach_header_
));
560 load_command
= (struct load_command
*) ((char *) load_command
+ Swap(load_command
->cmdsize
));
563 return Pointer
<Target_
>(this);
566 template <typename Target_
>
567 Pointer
<Target_
> GetOffset(uint32_t offset
) {
568 return Pointer
<Target_
>(this, reinterpret_cast<Target_
*>(offset
+ (uint8_t *) mach_header_
));
572 class FatMachHeader
:
579 FatMachHeader(void *base
, size_t size
, fat_arch
*fat_arch
) :
580 MachHeader(base
, size
),
585 fat_arch
*GetFatArch() const {
594 fat_header
*fat_header_
;
595 std::vector
<FatMachHeader
> mach_headers_
;
598 FatHeader(void *base
, size_t size
) :
601 fat_header_
= reinterpret_cast<struct fat_header
*>(base
);
603 if (Swap(fat_header_
->magic
) == FAT_CIGAM
) {
604 swapped_
= !swapped_
;
606 } else if (Swap(fat_header_
->magic
) != FAT_MAGIC
) {
608 mach_headers_
.push_back(FatMachHeader(base
, size
, NULL
));
610 size_t fat_narch
= Swap(fat_header_
->nfat_arch
);
611 fat_arch
*fat_arch
= reinterpret_cast<struct fat_arch
*>(fat_header_
+ 1);
613 for (arch
= 0; arch
!= fat_narch
; ++arch
) {
614 uint32_t arch_offset
= Swap(fat_arch
->offset
);
615 uint32_t arch_size
= Swap(fat_arch
->size
);
616 mach_headers_
.push_back(FatMachHeader((uint8_t *) base
+ arch_offset
, arch_size
, fat_arch
));
622 std::vector
<FatMachHeader
> &GetMachHeaders() {
623 return mach_headers_
;
627 return fat_header_
!= NULL
;
630 struct fat_header
*operator ->() const {
634 operator struct fat_header
*() const {
639 FatHeader
Map(const char *path
, bool ro
= false) {
641 void *base(map(path
, 0, _not(size_t), &size
, ro
));
642 return FatHeader(base
, size
);
645 template <typename Target_
>
648 const MachHeader
*framework_
;
649 const Target_
*pointer_
;
652 Pointer(const MachHeader
*framework
= NULL
, const Target_
*pointer
= NULL
) :
653 framework_(framework
),
658 operator const Target_
*() const {
662 const Target_
*operator ->() const {
666 Pointer
<Target_
> &operator ++() {
671 template <typename Value_
>
672 Value_
Swap(Value_ value
) {
673 return framework_
->Swap(value
);
677 #define CSMAGIC_CODEDIRECTORY uint32_t(0xfade0c02)
678 #define CSMAGIC_EMBEDDED_SIGNATURE uint32_t(0xfade0cc0)
679 #define CSMAGIC_ENTITLEMENTS uint32_t(0xfade7171)
681 #define CSSLOT_CODEDIRECTORY uint32_t(0)
682 #define CSSLOT_REQUIREMENTS uint32_t(2)
683 #define CSSLOT_ENTITLEMENTS uint32_t(5)
698 struct BlobIndex index
[];
701 struct CodeDirectory
{
706 uint32_t identOffset
;
707 uint32_t nSpecialSlots
;
717 extern "C" uint32_t hash(uint8_t *k
, uint32_t length
, uint32_t initval
);
719 void sha1(uint8_t *hash
, uint8_t *data
, size_t size
) {
722 SHA1Input(&context
, data
, size
);
723 SHA1Result(&context
, hash
);
726 struct CodesignAllocation
{
727 FatMachHeader mach_header_
;
733 CodesignAllocation(FatMachHeader mach_header
, size_t offset
, size_t size
, size_t alloc
, size_t align
) :
734 mach_header_(mach_header
),
743 int main(int argc
, const char *argv
[]) {
749 little_
= endian
.byte
[0];
772 uint32_t flag_CPUType(_not(uint32_t));
773 uint32_t flag_CPUSubtype(_not(uint32_t));
775 const char *flag_I(NULL
);
780 const void *xmld(NULL
);
783 uintptr_t noffset(_not(uintptr_t));
784 uintptr_t woffset(_not(uintptr_t));
786 std::vector
<std::string
> files
;
789 fprintf(stderr
, "usage: %s -S[entitlements.xml] <binary>\n", argv
[0]);
790 fprintf(stderr
, " %s -e MobileSafari\n", argv
[0]);
791 fprintf(stderr
, " %s -S cat\n", argv
[0]);
792 fprintf(stderr
, " %s -Stfp.xml gdb\n", argv
[0]);
796 for (int argi(1); argi
!= argc
; ++argi
)
797 if (argv
[argi
][0] != '-')
798 files
.push_back(argv
[argi
]);
799 else switch (argv
[argi
][1]) {
800 case 'R': flag_R
= true; break;
801 case 'r': flag_r
= true; break;
803 case 't': flag_t
= true; break;
804 case 'u': flag_u
= true; break;
805 case 'p': flag_p
= true; break;
806 case 'e': flag_e
= true; break;
807 case 'O': flag_O
= true; break;
809 case 'D': flag_D
= true; break;
810 case 'd': flag_d
= true; break;
812 case 'a': flag_a
= true; break;
816 if (argv
[argi
][2] != '\0') {
817 const char *cpu
= argv
[argi
] + 2;
818 const char *colon
= strchr(cpu
, ':');
819 _assert(colon
!= NULL
);
821 flag_CPUType
= strtoul(cpu
, &arge
, 0);
822 _assert(arge
== colon
);
823 flag_CPUSubtype
= strtoul(colon
+ 1, &arge
, 0);
824 _assert(arge
== argv
[argi
] + strlen(argv
[argi
]));
836 if (argv
[argi
][2] != '\0') {
837 const char *xml
= argv
[argi
] + 2;
838 xmld
= map(xml
, 0, _not(size_t), &xmls
, true);
844 if (argv
[argi
][2] == '-')
848 timev
= strtoul(argv
[argi
] + 2, &arge
, 0);
849 _assert(arge
== argv
[argi
] + strlen(argv
[argi
]));
854 flag_I
= argv
[argi
] + 2;
859 noffset
= strtoul(argv
[argi
] + 2, &arge
, 0);
860 _assert(arge
== argv
[argi
] + strlen(argv
[argi
]));
865 woffset
= strtoul(argv
[argi
] + 2, &arge
, 0);
866 _assert(arge
== argv
[argi
] + strlen(argv
[argi
]));
874 if (files
.empty()) usage
: {
878 size_t filei(0), filee(0);
879 _foreach (file
, files
) try {
880 const char *path(file
.c_str());
881 const char *base
= strrchr(path
, '/');
882 char *temp(NULL
), *dir
;
885 dir
= strndup_(path
, base
++ - path
+ 1);
891 const char *name(flag_I
?: base
);
895 FatHeader
fat_header(Map(path
));
896 _foreach (mach_header
, fat_header
.GetMachHeaders()) {
898 if (mach_header
.GetCPUType() != flag_CPUType
)
900 if (mach_header
.GetCPUSubtype() != flag_CPUSubtype
)
904 mach_header
->flags
= mach_header
.Swap(mach_header
.Swap(mach_header
->flags
) | MH_DYLDLINK
);
906 uint32_t size(_not(uint32_t)); {
907 _foreach (load_command
, mach_header
.GetLoadCommands()) {
908 switch (mach_header
.Swap(load_command
->cmd
)) {
909 case LC_CODE_SIGNATURE
: {
910 struct linkedit_data_command
*signature
= reinterpret_cast<struct linkedit_data_command
*>(load_command
);
911 memset(reinterpret_cast<uint8_t *>(mach_header
.GetBase()) + mach_header
.Swap(signature
->dataoff
), 0, mach_header
.Swap(signature
->datasize
));
912 memset(signature
, 0, sizeof(struct linkedit_data_command
));
914 mach_header
->ncmds
= mach_header
.Swap(mach_header
.Swap(mach_header
->ncmds
) - 1);
915 mach_header
->sizeofcmds
= mach_header
.Swap(uint32_t(mach_header
.Swap(mach_header
->sizeofcmds
) - sizeof(struct linkedit_data_command
)));
919 struct symtab_command
*symtab
= reinterpret_cast<struct symtab_command
*>(load_command
);
920 size
= mach_header
.Swap(symtab
->stroff
) + mach_header
.Swap(symtab
->strsize
);
926 _assert(size
!= _not(uint32_t));
928 _foreach (segment
, mach_header
.GetSegments("__LINKEDIT")) {
929 segment
->filesize
-= mach_header
.GetSize() - size
;
931 if (fat_arch
*fat_arch
= mach_header
.GetFatArch()) {
932 fat_arch
->size
= fat_header
.Swap(size
);
933 clip
= std::max(clip
, fat_header
.Swap(fat_arch
->offset
) + size
);
935 clip
= std::max(clip
, size
);
938 _foreach (segment
, mach_header
.GetSegments64("__LINKEDIT")) {
939 segment
->filesize
-= mach_header
.GetSize() - size
;
941 if (fat_arch
*fat_arch
= mach_header
.GetFatArch()) {
942 fat_arch
->size
= fat_header
.Swap(size
);
943 clip
= std::max(clip
, fat_header
.Swap(fat_arch
->offset
) + size
);
945 clip
= std::max(clip
, size
);
951 _syscall(truncate(path
, clip
));
955 FatHeader
source(Map(path
));
960 offset
+= sizeof(fat_header
) + sizeof(fat_arch
) * source
.Swap(source
->nfat_arch
);
962 std::vector
<CodesignAllocation
> allocations
; {
963 _foreach (mach_header
, source
.GetMachHeaders()) {
965 if (mach_header
.GetCPUType() != flag_CPUType
)
967 if (mach_header
.GetCPUSubtype() != flag_CPUSubtype
)
971 mach_header
->flags
= mach_header
.Swap(mach_header
.Swap(mach_header
->flags
) | MH_DYLDLINK
);
973 size_t size(_not(size_t)); {
974 _foreach (load_command
, mach_header
.GetLoadCommands()) {
975 uint32_t cmd(mach_header
.Swap(load_command
->cmd
));
976 if (cmd
== LC_CODE_SIGNATURE
) {
977 struct linkedit_data_command
*signature
= reinterpret_cast<struct linkedit_data_command
*>(load_command
);
978 size
= mach_header
.Swap(signature
->dataoff
);
979 _assert(size
< mach_header
.GetSize());
984 if (size
== _not(size_t))
985 size
= mach_header
.GetSize();
989 alloc
+= sizeof(struct SuperBlob
);
992 special
= std::max(special
, CSSLOT_CODEDIRECTORY
);
993 alloc
+= sizeof(struct BlobIndex
);
994 alloc
+= sizeof(struct CodeDirectory
);
995 alloc
+= strlen(name
) + 1;
997 special
= std::max(special
, CSSLOT_REQUIREMENTS
);
998 alloc
+= sizeof(struct BlobIndex
);
1002 special
= std::max(special
, CSSLOT_ENTITLEMENTS
);
1003 alloc
+= sizeof(struct BlobIndex
);
1004 alloc
+= sizeof(struct Blob
);
1008 size_t normal((size
+ 0x1000 - 1) / 0x1000);
1009 alloc
= Align(alloc
+ (special
+ normal
) * 0x14, 16);
1011 auto *fat_arch(mach_header
.GetFatArch());
1012 uint32_t align(fat_arch
== NULL
? 0 : source
.Swap(fat_arch
->align
));
1013 offset
= Align(offset
, 1 << align
);
1015 allocations
.push_back(CodesignAllocation(mach_header
, offset
, size
, alloc
, align
));
1016 offset
+= size
+ alloc
;
1017 offset
= Align(offset
, 16);
1021 asprintf(&temp
, "%s.%s.cs", dir
, base
);
1022 fclose(fopen(temp
, "w+"));
1023 _syscall(truncate(temp
, offset
));
1025 void *file(map(temp
, 0, offset
, NULL
, false));
1026 memset(file
, 0, offset
);
1029 if (!source
.IsFat())
1032 auto *fat_header(reinterpret_cast<struct fat_header
*>(file
));
1033 fat_header
->magic
= Swap(FAT_MAGIC
);
1034 fat_header
->nfat_arch
= Swap(source
.Swap(source
->nfat_arch
));
1035 fat_arch
= reinterpret_cast<struct fat_arch
*>(fat_header
+ 1);
1038 _foreach (allocation
, allocations
) {
1039 auto &source(allocation
.mach_header_
);
1041 uint32_t align(allocation
.size_
);
1042 align
= Align(align
, 0x10);
1044 if (fat_arch
!= NULL
) {
1045 fat_arch
->cputype
= Swap(source
->cputype
);
1046 fat_arch
->cpusubtype
= Swap(source
->cpusubtype
);
1047 fat_arch
->offset
= Swap(allocation
.offset_
);
1048 fat_arch
->size
= Swap(align
+ allocation
.alloc_
);
1049 fat_arch
->align
= Swap(allocation
.align_
);
1053 void *target(reinterpret_cast<uint8_t *>(file
) + allocation
.offset_
);
1054 memcpy(target
, source
, allocation
.size_
);
1055 MachHeader
mach_header(target
, align
+ allocation
.alloc_
);
1057 struct linkedit_data_command
*signature(NULL
);
1058 _foreach (load_command
, mach_header
.GetLoadCommands()) {
1059 uint32_t cmd(mach_header
.Swap(load_command
->cmd
));
1060 if (cmd
!= LC_CODE_SIGNATURE
)
1062 signature
= reinterpret_cast<struct linkedit_data_command
*>(load_command
);
1066 if (signature
== NULL
) {
1067 mach_header
->ncmds
= mach_header
.Swap(mach_header
.Swap(mach_header
->ncmds
) + 1);
1068 signature
= reinterpret_cast<struct linkedit_data_command
*>(reinterpret_cast<uint8_t *>(mach_header
.GetLoadCommand()) + mach_header
.Swap(mach_header
->sizeofcmds
));
1069 mach_header
->sizeofcmds
= mach_header
.Swap(mach_header
.Swap(mach_header
->sizeofcmds
) + uint32_t(sizeof(*signature
)));
1070 signature
->cmd
= mach_header
.Swap(LC_CODE_SIGNATURE
);
1071 signature
->cmdsize
= mach_header
.Swap(uint32_t(sizeof(*signature
)));
1074 signature
->dataoff
= mach_header
.Swap(align
);
1075 signature
->datasize
= mach_header
.Swap(allocation
.alloc_
);
1077 _foreach (segment
, mach_header
.GetSegments("__LINKEDIT")) {
1078 size_t size(mach_header
.Swap(align
+ allocation
.alloc_
- mach_header
.Swap(segment
->fileoff
)));
1079 segment
->filesize
= size
;
1080 segment
->vmsize
= Align(size
, 0x1000);
1083 _foreach (segment
, mach_header
.GetSegments64("__LINKEDIT")) {
1084 size_t size(mach_header
.Swap(align
+ allocation
.alloc_
- mach_header
.Swap(segment
->fileoff
)));
1085 segment
->filesize
= size
;
1086 segment
->vmsize
= Align(size
, 0x1000);
1092 printf("path%zu='%s'\n", filei
, file
.c_str());
1094 FatHeader
fat_header(Map(temp
== NULL
? path
: temp
, !(flag_R
|| flag_T
|| flag_s
|| flag_S
|| flag_O
|| flag_D
)));
1095 struct linkedit_data_command
*signature(NULL
);
1097 _foreach (mach_header
, fat_header
.GetMachHeaders()) {
1099 if (mach_header
.GetCPUType() != flag_CPUType
)
1101 if (mach_header
.GetCPUSubtype() != flag_CPUSubtype
)
1106 printf("cpu=0x%x:0x%x\n", mach_header
.GetCPUType(), mach_header
.GetCPUSubtype());
1109 if (struct fat_arch
*fat_arch
= mach_header
.GetFatArch())
1110 printf("offset=0x%x\n", Swap(fat_arch
->offset
));
1112 printf("offset=0x0\n");
1115 if (woffset
!= _not(uintptr_t)) {
1116 Pointer
<uint32_t> wvalue(mach_header
.GetPointer
<uint32_t>(woffset
));
1118 printf("(null) %p\n", reinterpret_cast<void *>(woffset
));
1120 printf("0x%.08x\n", *wvalue
);
1123 if (noffset
!= _not(uintptr_t))
1124 printf("%s\n", &*mach_header
.GetPointer
<char>(noffset
));
1127 _foreach(segment
, mach_header
.GetSegments("__TEXT")) {
1128 printf("vmaddr=0x%x\n", mach_header
.Swap(segment
->vmaddr
));
1129 printf("fileoff=0x%x\n", mach_header
.Swap(segment
->fileoff
));
1133 _foreach(section
, mach_header
.GetSections("__TEXT", "__text"))
1134 section
->addr
= mach_header
.Swap(0);
1137 _foreach (load_command
, mach_header
.GetLoadCommands()) {
1138 uint32_t cmd(mach_header
.Swap(load_command
->cmd
));
1140 if (flag_R
&& cmd
== LC_REEXPORT_DYLIB
)
1141 load_command
->cmd
= mach_header
.Swap(LC_LOAD_DYLIB
);
1142 else if (cmd
== LC_CODE_SIGNATURE
)
1143 signature
= reinterpret_cast<struct linkedit_data_command
*>(load_command
);
1144 else if (cmd
== LC_UUID
) {
1145 volatile struct uuid_command
*uuid_command(reinterpret_cast<struct uuid_command
*>(load_command
));
1148 printf("uuid%zu=%.2x%.2x%.2x%.2x-%.2x%.2x-%.2x%.2x-%.2x%.2x-%.2x%.2x%.2x%.2x%.2x%.2x\n", filei
,
1149 uuid_command
->uuid
[ 0], uuid_command
->uuid
[ 1], uuid_command
->uuid
[ 2], uuid_command
->uuid
[ 3],
1150 uuid_command
->uuid
[ 4], uuid_command
->uuid
[ 5], uuid_command
->uuid
[ 6], uuid_command
->uuid
[ 7],
1151 uuid_command
->uuid
[ 8], uuid_command
->uuid
[ 9], uuid_command
->uuid
[10], uuid_command
->uuid
[11],
1152 uuid_command
->uuid
[12], uuid_command
->uuid
[13], uuid_command
->uuid
[14], uuid_command
->uuid
[15]
1155 } else if (cmd
== LC_ID_DYLIB
) {
1156 volatile struct dylib_command
*dylib_command(reinterpret_cast<struct dylib_command
*>(load_command
));
1159 printf("time%zu=0x%.8x\n", filei
, mach_header
.Swap(dylib_command
->dylib
.timestamp
));
1167 dylib_command
->dylib
.timestamp
= 0;
1168 timed
= hash(reinterpret_cast<uint8_t *>(mach_header
.GetBase()), mach_header
.GetSize(), timev
);
1171 dylib_command
->dylib
.timestamp
= mach_header
.Swap(timed
);
1173 } else if (cmd
== LC_ENCRYPTION_INFO
) {
1174 volatile struct encryption_info_command
*encryption_info_command(reinterpret_cast<struct encryption_info_command
*>(load_command
));
1177 encryption_info_command
->cryptid
= mach_header
.Swap(0);
1180 printf("cryptoff=0x%x\n", mach_header
.Swap(encryption_info_command
->cryptoff
));
1181 printf("cryptsize=0x%x\n", mach_header
.Swap(encryption_info_command
->cryptsize
));
1182 printf("cryptid=0x%x\n", mach_header
.Swap(encryption_info_command
->cryptid
));
1188 _assert(signature
!= NULL
);
1190 uint32_t data
= mach_header
.Swap(signature
->dataoff
);
1192 uint8_t *top
= reinterpret_cast<uint8_t *>(mach_header
.GetBase());
1193 uint8_t *blob
= top
+ data
;
1194 struct SuperBlob
*super
= reinterpret_cast<struct SuperBlob
*>(blob
);
1196 for (size_t index(0); index
!= Swap(super
->count
); ++index
)
1197 if (Swap(super
->index
[index
].type
) == CSSLOT_ENTITLEMENTS
) {
1198 uint32_t begin
= Swap(super
->index
[index
].offset
);
1199 struct Blob
*entitlements
= reinterpret_cast<struct Blob
*>(blob
+ begin
);
1200 fwrite(entitlements
+ 1, 1, Swap(entitlements
->length
) - sizeof(struct Blob
), stdout
);
1205 _assert(signature
!= NULL
);
1207 uint32_t data
= mach_header
.Swap(signature
->dataoff
);
1209 uint8_t *top
= reinterpret_cast<uint8_t *>(mach_header
.GetBase());
1210 uint8_t *blob
= top
+ data
;
1211 struct SuperBlob
*super
= reinterpret_cast<struct SuperBlob
*>(blob
);
1213 for (size_t index(0); index
!= Swap(super
->count
); ++index
)
1214 if (Swap(super
->index
[index
].type
) == CSSLOT_CODEDIRECTORY
) {
1215 uint32_t begin
= Swap(super
->index
[index
].offset
);
1216 struct CodeDirectory
*directory
= reinterpret_cast<struct CodeDirectory
*>(blob
+ begin
);
1218 uint8_t (*hashes
)[20] = reinterpret_cast<uint8_t (*)[20]>(blob
+ begin
+ Swap(directory
->hashOffset
));
1219 uint32_t pages
= Swap(directory
->nCodeSlots
);
1222 for (size_t i
= 0; i
!= pages
- 1; ++i
)
1223 sha1(hashes
[i
], top
+ 0x1000 * i
, 0x1000);
1225 sha1(hashes
[pages
- 1], top
+ 0x1000 * (pages
- 1), ((data
- 1) % 0x1000) + 1);
1230 _assert(signature
!= NULL
);
1232 uint32_t data
= mach_header
.Swap(signature
->dataoff
);
1233 uint32_t size
= mach_header
.Swap(signature
->datasize
);
1235 uint8_t *top
= reinterpret_cast<uint8_t *>(mach_header
.GetBase());
1236 uint8_t *blob
= top
+ data
;
1237 struct SuperBlob
*super
= reinterpret_cast<struct SuperBlob
*>(blob
);
1238 super
->blob
.magic
= Swap(CSMAGIC_EMBEDDED_SIGNATURE
);
1240 uint32_t count
= xmld
== NULL
? 2 : 3;
1241 uint32_t offset
= sizeof(struct SuperBlob
) + count
* sizeof(struct BlobIndex
);
1243 super
->index
[0].type
= Swap(CSSLOT_CODEDIRECTORY
);
1244 super
->index
[0].offset
= Swap(offset
);
1246 uint32_t begin
= offset
;
1247 struct CodeDirectory
*directory
= reinterpret_cast<struct CodeDirectory
*>(blob
+ begin
);
1248 offset
+= sizeof(struct CodeDirectory
);
1250 directory
->blob
.magic
= Swap(CSMAGIC_CODEDIRECTORY
);
1251 directory
->version
= Swap(uint32_t(0x00020001));
1252 directory
->flags
= Swap(uint32_t(0));
1253 directory
->codeLimit
= Swap(data
);
1254 directory
->hashSize
= 0x14;
1255 directory
->hashType
= 0x01;
1256 directory
->spare1
= 0x00;
1257 directory
->pageSize
= 0x0c;
1258 directory
->spare2
= Swap(uint32_t(0));
1260 directory
->identOffset
= Swap(offset
- begin
);
1261 strcpy(reinterpret_cast<char *>(blob
+ offset
), name
);
1262 offset
+= strlen(name
) + 1;
1264 uint32_t special
= xmld
== NULL
? CSSLOT_REQUIREMENTS
: CSSLOT_ENTITLEMENTS
;
1265 directory
->nSpecialSlots
= Swap(special
);
1267 uint8_t (*hashes
)[20] = reinterpret_cast<uint8_t (*)[20]>(blob
+ offset
);
1268 memset(hashes
, 0, sizeof(*hashes
) * special
);
1270 offset
+= sizeof(*hashes
) * special
;
1273 uint32_t pages
= (data
+ 0x1000 - 1) / 0x1000;
1274 directory
->nCodeSlots
= Swap(pages
);
1277 for (size_t i
= 0; i
!= pages
- 1; ++i
)
1278 sha1(hashes
[i
], top
+ 0x1000 * i
, 0x1000);
1280 sha1(hashes
[pages
- 1], top
+ 0x1000 * (pages
- 1), ((data
- 1) % 0x1000) + 1);
1282 directory
->hashOffset
= Swap(offset
- begin
);
1283 offset
+= sizeof(*hashes
) * pages
;
1284 directory
->blob
.length
= Swap(offset
- begin
);
1286 super
->index
[1].type
= Swap(CSSLOT_REQUIREMENTS
);
1287 super
->index
[1].offset
= Swap(offset
);
1289 memcpy(blob
+ offset
, "\xfa\xde\x0c\x01\x00\x00\x00\x0c\x00\x00\x00\x00", 0xc);
1293 super
->index
[2].type
= Swap(CSSLOT_ENTITLEMENTS
);
1294 super
->index
[2].offset
= Swap(offset
);
1296 uint32_t begin
= offset
;
1297 struct Blob
*entitlements
= reinterpret_cast<struct Blob
*>(blob
+ begin
);
1298 offset
+= sizeof(struct Blob
);
1300 memcpy(blob
+ offset
, xmld
, xmls
);
1303 entitlements
->magic
= Swap(CSMAGIC_ENTITLEMENTS
);
1304 entitlements
->length
= Swap(offset
- begin
);
1307 for (size_t index(0); index
!= count
; ++index
) {
1308 uint32_t type
= Swap(super
->index
[index
].type
);
1309 if (type
!= 0 && type
<= special
) {
1310 uint32_t offset
= Swap(super
->index
[index
].offset
);
1311 struct Blob
*local
= (struct Blob
*) (blob
+ offset
);
1312 sha1((uint8_t *) (hashes
- type
), (uint8_t *) local
, Swap(local
->length
));
1316 super
->count
= Swap(count
);
1317 super
->blob
.length
= Swap(offset
);
1319 if (offset
> size
) {
1320 fprintf(stderr
, "offset (%u) > size (%u)\n", offset
, size
);
1322 } //else fprintf(stderr, "offset (%zu) <= size (%zu)\n", offset, size);
1324 memset(blob
+ offset
, 0, size
- offset
);
1329 uint8_t *top
= reinterpret_cast<uint8_t *>(fat_header
.GetBase());
1330 size_t size
= fat_header
.GetSize();
1333 asprintf(©
, "%s.%s.cp", dir
, base
);
1334 FILE *file
= fopen(copy
, "w+");
1335 size_t writ
= fwrite(top
, 1, size
, file
);
1336 _assert(writ
== size
);
1339 _syscall(unlink(temp
));
1346 _syscall(stat(path
, &info
));
1347 _syscall(chown(temp
, info
.st_uid
, info
.st_gid
));
1348 _syscall(chmod(temp
, info
.st_mode
));
1349 _syscall(unlink(path
));
1350 _syscall(rename(temp
, path
));
1356 } catch (const char *) {