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"
34 #include <openssl/sha.h>
36 #include <plist/plist.h>
43 #define FAT_MAGIC 0xcafebabe
44 #define FAT_CIGAM 0xbebafeca
64 #define MH_MAGIC 0xfeedface
65 #define MH_CIGAM 0xcefaedfe
67 #define MH_MAGIC_64 0xfeedfacf
68 #define MH_CIGAM_64 0xcffaedfe
70 #define MH_DYLDLINK 0x4
73 #define MH_EXECUTE 0x2
76 #define MH_DYLIB_STUB 0x9
83 #define LC_REQ_DYLD uint32_t(0x80000000)
85 #define LC_SEGMENT uint32_t(0x01)
86 #define LC_SYMTAB uint32_t(0x02)
87 #define LC_DYSYMTAB uint32_t(0x0b)
88 #define LC_LOAD_DYLIB uint32_t(0x0c)
89 #define LC_ID_DYLIB uint32_t(0x0d)
90 #define LC_SEGMENT_64 uint32_t(0x19)
91 #define LC_UUID uint32_t(0x1b)
92 #define LC_CODE_SIGNATURE uint32_t(0x1d)
93 #define LC_SEGMENT_SPLIT_INFO uint32_t(0x1e)
94 #define LC_REEXPORT_DYLIB uint32_t(0x1f | LC_REQ_DYLD)
95 #define LC_ENCRYPTION_INFO uint32_t(0x21)
96 #define LC_DYLD_INFO uint32_t(0x22)
97 #define LC_DYLD_INFO_ONLY uint32_t(0x22 | LC_REQ_DYLD)
102 uint32_t current_version
;
103 uint32_t compatibility_version
;
106 struct dylib_command
{
112 struct uuid_command
{
118 struct symtab_command
{
127 struct dyld_info_command
{
131 uint32_t rebase_size
;
134 uint32_t weak_bind_off
;
135 uint32_t weak_bind_size
;
136 uint32_t lazy_bind_off
;
137 uint32_t lazy_bind_size
;
139 uint32_t export_size
;
142 struct dysymtab_command
{
155 uint32_t extrefsymoff
;
156 uint32_t nextrefsyms
;
157 uint32_t indirectsymoff
;
158 uint32_t nindirectsyms
;
165 struct dylib_table_of_contents
{
166 uint32_t symbol_index
;
167 uint32_t module_index
;
170 struct dylib_module
{
171 uint32_t module_name
;
180 uint32_t iinit_iterm
;
181 uint32_t ninit_nterm
;
182 uint32_t objc_module_info_addr
;
183 uint32_t objc_module_info_size
;
186 struct dylib_reference
{
191 struct relocation_info
{
193 uint32_t r_symbolnum
:24;
212 struct segment_command
{
226 struct segment_command_64
{
268 struct linkedit_data_command
{
275 struct encryption_info_command
{
283 #define BIND_OPCODE_MASK 0xf0
284 #define BIND_IMMEDIATE_MASK 0x0f
285 #define BIND_OPCODE_DONE 0x00
286 #define BIND_OPCODE_SET_DYLIB_ORDINAL_IMM 0x10
287 #define BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB 0x20
288 #define BIND_OPCODE_SET_DYLIB_SPECIAL_IMM 0x30
289 #define BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM 0x40
290 #define BIND_OPCODE_SET_TYPE_IMM 0x50
291 #define BIND_OPCODE_SET_ADDEND_SLEB 0x60
292 #define BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB 0x70
293 #define BIND_OPCODE_ADD_ADDR_ULEB 0x80
294 #define BIND_OPCODE_DO_BIND 0x90
295 #define BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB 0xa0
296 #define BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED 0xb0
297 #define BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB 0xc0
299 template <typename Type_
>
300 Type_
Align(Type_ value
, size_t align
) {
307 uint16_t Swap_(uint16_t value
) {
309 ((value
>> 8) & 0x00ff) |
310 ((value
<< 8) & 0xff00);
313 uint32_t Swap_(uint32_t value
) {
314 value
= ((value
>> 8) & 0x00ff00ff) |
315 ((value
<< 8) & 0xff00ff00);
316 value
= ((value
>> 16) & 0x0000ffff) |
317 ((value
<< 16) & 0xffff0000);
321 uint64_t Swap_(uint64_t value
) {
322 value
= (value
& 0x00000000ffffffff) << 32 | (value
& 0xffffffff00000000) >> 32;
323 value
= (value
& 0x0000ffff0000ffff) << 16 | (value
& 0xffff0000ffff0000) >> 16;
324 value
= (value
& 0x00ff00ff00ff00ff) << 8 | (value
& 0xff00ff00ff00ff00) >> 8;
328 int16_t Swap_(int16_t value
) {
329 return Swap_(static_cast<uint16_t>(value
));
332 int32_t Swap_(int32_t value
) {
333 return Swap_(static_cast<uint32_t>(value
));
336 int64_t Swap_(int64_t value
) {
337 return Swap_(static_cast<uint64_t>(value
));
342 uint16_t Swap(uint16_t value
) {
343 return little_
? Swap_(value
) : value
;
346 uint32_t Swap(uint32_t value
) {
347 return little_
? Swap_(value
) : value
;
350 uint64_t Swap(uint64_t value
) {
351 return little_
? Swap_(value
) : value
;
354 int16_t Swap(int16_t value
) {
355 return Swap(static_cast<uint16_t>(value
));
358 int32_t Swap(int32_t value
) {
359 return Swap(static_cast<uint32_t>(value
));
362 int64_t Swap(int64_t value
) {
363 return Swap(static_cast<uint64_t>(value
));
366 template <typename Target_
>
378 Data(void *base
, size_t size
) :
385 uint16_t Swap(uint16_t value
) const {
386 return swapped_
? Swap_(value
) : value
;
389 uint32_t Swap(uint32_t value
) const {
390 return swapped_
? Swap_(value
) : value
;
393 uint64_t Swap(uint64_t value
) const {
394 return swapped_
? Swap_(value
) : value
;
397 int16_t Swap(int16_t value
) const {
398 return Swap(static_cast<uint16_t>(value
));
401 int32_t Swap(int32_t value
) const {
402 return Swap(static_cast<uint32_t>(value
));
405 int64_t Swap(int64_t value
) const {
406 return Swap(static_cast<uint64_t>(value
));
409 void *GetBase() const {
413 size_t GetSize() const {
424 struct mach_header
*mach_header_
;
425 struct load_command
*load_command_
;
428 MachHeader(void *base
, size_t size
) :
431 mach_header_
= (mach_header
*) base
;
433 switch (Swap(mach_header_
->magic
)) {
435 swapped_
= !swapped_
;
441 swapped_
= !swapped_
;
450 void *post
= mach_header_
+ 1;
452 post
= (uint32_t *) post
+ 1;
453 load_command_
= (struct load_command
*) post
;
456 Swap(mach_header_
->filetype
) == MH_EXECUTE
||
457 Swap(mach_header_
->filetype
) == MH_DYLIB
||
458 Swap(mach_header_
->filetype
) == MH_BUNDLE
462 struct mach_header
*operator ->() const {
466 operator struct mach_header
*() const {
470 uint32_t GetCPUType() const {
471 return Swap(mach_header_
->cputype
);
474 uint32_t GetCPUSubtype() const {
475 return Swap(mach_header_
->cpusubtype
) & 0xff;
478 struct load_command
*GetLoadCommand() const {
479 return load_command_
;
482 std::vector
<struct load_command
*> GetLoadCommands() const {
483 std::vector
<struct load_command
*> load_commands
;
485 struct load_command
*load_command
= load_command_
;
486 for (uint32_t cmd
= 0; cmd
!= Swap(mach_header_
->ncmds
); ++cmd
) {
487 load_commands
.push_back(load_command
);
488 load_command
= (struct load_command
*) ((uint8_t *) load_command
+ Swap(load_command
->cmdsize
));
491 return load_commands
;
494 std::vector
<segment_command
*> GetSegments(const char *segment_name
) const {
495 std::vector
<struct segment_command
*> segment_commands
;
497 _foreach (load_command
, GetLoadCommands()) {
498 if (Swap(load_command
->cmd
) == LC_SEGMENT
) {
499 segment_command
*segment_command
= reinterpret_cast<struct segment_command
*>(load_command
);
500 if (strncmp(segment_command
->segname
, segment_name
, 16) == 0)
501 segment_commands
.push_back(segment_command
);
505 return segment_commands
;
508 std::vector
<segment_command_64
*> GetSegments64(const char *segment_name
) const {
509 std::vector
<struct segment_command_64
*> segment_commands
;
511 _foreach (load_command
, GetLoadCommands()) {
512 if (Swap(load_command
->cmd
) == LC_SEGMENT_64
) {
513 segment_command_64
*segment_command
= reinterpret_cast<struct segment_command_64
*>(load_command
);
514 if (strncmp(segment_command
->segname
, segment_name
, 16) == 0)
515 segment_commands
.push_back(segment_command
);
519 return segment_commands
;
522 std::vector
<section
*> GetSections(const char *segment_name
, const char *section_name
) const {
523 std::vector
<section
*> sections
;
525 _foreach (segment
, GetSegments(segment_name
)) {
526 section
*section
= (struct section
*) (segment
+ 1);
529 for (sect
= 0; sect
!= Swap(segment
->nsects
); ++sect
) {
530 if (strncmp(section
->sectname
, section_name
, 16) == 0)
531 sections
.push_back(section
);
539 template <typename Target_
>
540 Pointer
<Target_
> GetPointer(uint32_t address
, const char *segment_name
= NULL
) const {
541 load_command
*load_command
= (struct load_command
*) (mach_header_
+ 1);
544 for (cmd
= 0; cmd
!= Swap(mach_header_
->ncmds
); ++cmd
) {
545 if (Swap(load_command
->cmd
) == LC_SEGMENT
) {
546 segment_command
*segment_command
= (struct segment_command
*) load_command
;
547 if (segment_name
!= NULL
&& strncmp(segment_command
->segname
, segment_name
, 16) != 0)
550 section
*sections
= (struct section
*) (segment_command
+ 1);
553 for (sect
= 0; sect
!= Swap(segment_command
->nsects
); ++sect
) {
554 section
*section
= §ions
[sect
];
555 //printf("%s %u %p %p %u\n", segment_command->segname, sect, address, section->addr, section->size);
556 if (address
>= Swap(section
->addr
) && address
< Swap(section
->addr
) + Swap(section
->size
)) {
557 //printf("0x%.8x %s\n", address, segment_command->segname);
558 return Pointer
<Target_
>(this, reinterpret_cast<Target_
*>(address
- Swap(section
->addr
) + Swap(section
->offset
) + (char *) mach_header_
));
564 load_command
= (struct load_command
*) ((char *) load_command
+ Swap(load_command
->cmdsize
));
567 return Pointer
<Target_
>(this);
570 template <typename Target_
>
571 Pointer
<Target_
> GetOffset(uint32_t offset
) {
572 return Pointer
<Target_
>(this, reinterpret_cast<Target_
*>(offset
+ (uint8_t *) mach_header_
));
576 class FatMachHeader
:
583 FatMachHeader(void *base
, size_t size
, fat_arch
*fat_arch
) :
584 MachHeader(base
, size
),
589 fat_arch
*GetFatArch() const {
598 fat_header
*fat_header_
;
599 std::vector
<FatMachHeader
> mach_headers_
;
602 FatHeader(void *base
, size_t size
) :
605 fat_header_
= reinterpret_cast<struct fat_header
*>(base
);
607 if (Swap(fat_header_
->magic
) == FAT_CIGAM
) {
608 swapped_
= !swapped_
;
610 } else if (Swap(fat_header_
->magic
) != FAT_MAGIC
) {
612 mach_headers_
.push_back(FatMachHeader(base
, size
, NULL
));
614 size_t fat_narch
= Swap(fat_header_
->nfat_arch
);
615 fat_arch
*fat_arch
= reinterpret_cast<struct fat_arch
*>(fat_header_
+ 1);
617 for (arch
= 0; arch
!= fat_narch
; ++arch
) {
618 uint32_t arch_offset
= Swap(fat_arch
->offset
);
619 uint32_t arch_size
= Swap(fat_arch
->size
);
620 mach_headers_
.push_back(FatMachHeader((uint8_t *) base
+ arch_offset
, arch_size
, fat_arch
));
626 std::vector
<FatMachHeader
> &GetMachHeaders() {
627 return mach_headers_
;
631 return fat_header_
!= NULL
;
634 struct fat_header
*operator ->() const {
638 operator struct fat_header
*() const {
643 template <typename Target_
>
646 const MachHeader
*framework_
;
647 const Target_
*pointer_
;
650 Pointer(const MachHeader
*framework
= NULL
, const Target_
*pointer
= NULL
) :
651 framework_(framework
),
656 operator const Target_
*() const {
660 const Target_
*operator ->() const {
664 Pointer
<Target_
> &operator ++() {
669 template <typename Value_
>
670 Value_
Swap(Value_ value
) {
671 return framework_
->Swap(value
);
675 #define CSMAGIC_CODEDIRECTORY uint32_t(0xfade0c02)
676 #define CSMAGIC_EMBEDDED_SIGNATURE uint32_t(0xfade0cc0)
677 #define CSMAGIC_ENTITLEMENTS uint32_t(0xfade7171)
679 #define CSSLOT_CODEDIRECTORY uint32_t(0)
680 #define CSSLOT_REQUIREMENTS uint32_t(2)
681 #define CSSLOT_ENTITLEMENTS uint32_t(5)
696 struct BlobIndex index
[];
699 struct CodeDirectory
{
704 uint32_t identOffset
;
705 uint32_t nSpecialSlots
;
715 extern "C" uint32_t hash(uint8_t *k
, uint32_t length
, uint32_t initval
);
717 void sha1(uint8_t *hash
, uint8_t *data
, size_t size
) {
718 SHA1(data
, size
, hash
);
721 struct CodesignAllocation
{
722 FatMachHeader mach_header_
;
728 CodesignAllocation(FatMachHeader mach_header
, size_t offset
, size_t size
, size_t alloc
, size_t align
) :
729 mach_header_(mach_header
),
750 _syscall(close(file_
));
753 void open(const char *path
, int flags
) {
754 _assert(file_
== -1);
755 _syscall(file_
= ::open(path
, flags
));
772 _syscall(munmap(data_
, size_
));
784 Map(const char *path
, int oflag
, int pflag
, int mflag
) :
787 open(path
, oflag
, pflag
, mflag
);
790 Map(const char *path
, bool edit
) :
800 void open(const char *path
, int oflag
, int pflag
, int mflag
) {
803 file_
.open(path
, oflag
);
804 int file(file_
.file());
807 _syscall(fstat(file
, &stat
));
808 size_
= stat
.st_size
;
810 _syscall(data_
= mmap(NULL
, size_
, pflag
, mflag
, file
, 0));
813 void open(const char *path
, bool edit
) {
815 open(path
, O_RDWR
, PROT_READ
| PROT_WRITE
, MAP_SHARED
);
817 open(path
, O_RDONLY
, PROT_READ
, MAP_PRIVATE
);
824 size_t size() const {
829 int main(int argc
, const char *argv
[]) {
835 little_
= endian
.byte
[0];
850 uint32_t flag_CPUType(_not(uint32_t));
851 uint32_t flag_CPUSubtype(_not(uint32_t));
853 const char *flag_I(NULL
);
859 const void *xmld(NULL
);
862 std::vector
<std::string
> files
;
865 fprintf(stderr
, "usage: %s -S[entitlements.xml] <binary>\n", argv
[0]);
866 fprintf(stderr
, " %s -e MobileSafari\n", argv
[0]);
867 fprintf(stderr
, " %s -S cat\n", argv
[0]);
868 fprintf(stderr
, " %s -Stfp.xml gdb\n", argv
[0]);
872 for (int argi(1); argi
!= argc
; ++argi
)
873 if (argv
[argi
][0] != '-')
874 files
.push_back(argv
[argi
]);
875 else switch (argv
[argi
][1]) {
876 case 'r': flag_r
= true; break;
877 case 'e': flag_e
= true; break;
879 case 'D': flag_D
= true; break;
881 case 'a': flag_a
= true; break;
885 if (argv
[argi
][2] != '\0') {
886 const char *cpu
= argv
[argi
] + 2;
887 const char *colon
= strchr(cpu
, ':');
888 _assert(colon
!= NULL
);
890 flag_CPUType
= strtoul(cpu
, &arge
, 0);
891 _assert(arge
== colon
);
892 flag_CPUSubtype
= strtoul(colon
+ 1, &arge
, 0);
893 _assert(arge
== argv
[argi
] + strlen(argv
[argi
]));
905 if (argv
[argi
][2] != '\0') {
906 const char *xml
= argv
[argi
] + 2;
907 xmlm
.open(xml
, O_RDONLY
, PROT_READ
, MAP_PRIVATE
);
915 if (argv
[argi
][2] == '-')
919 timev
= strtoul(argv
[argi
] + 2, &arge
, 0);
920 _assert(arge
== argv
[argi
] + strlen(argv
[argi
]));
925 flag_I
= argv
[argi
] + 2;
933 if (files
.empty()) usage
: {
937 size_t filei(0), filee(0);
938 _foreach (file
, files
) try {
939 const char *path(file
.c_str());
940 const char *base
= strrchr(path
, '/');
944 dir
.assign(path
, base
++ - path
+ 1);
948 const char *name(flag_I
?: base
);
951 if (flag_S
|| flag_r
) {
952 Map
input(path
, O_RDONLY
, PROT_READ
| PROT_WRITE
, MAP_PRIVATE
);
953 FatHeader
source(input
.data(), input
.size());
957 offset
+= sizeof(fat_header
) + sizeof(fat_arch
) * source
.Swap(source
->nfat_arch
);
959 std::vector
<CodesignAllocation
> allocations
;
960 _foreach (mach_header
, source
.GetMachHeaders()) {
961 struct linkedit_data_command
*signature(NULL
);
962 struct symtab_command
*symtab(NULL
);
964 _foreach (load_command
, mach_header
.GetLoadCommands()) {
965 uint32_t cmd(mach_header
.Swap(load_command
->cmd
));
967 else if (cmd
== LC_CODE_SIGNATURE
)
968 signature
= reinterpret_cast<struct linkedit_data_command
*>(load_command
);
969 else if (cmd
== LC_SYMTAB
)
970 symtab
= reinterpret_cast<struct symtab_command
*>(load_command
);
974 if (signature
== NULL
)
975 size
= mach_header
.GetSize();
977 size
= mach_header
.Swap(signature
->dataoff
);
978 _assert(size
<= mach_header
.GetSize());
981 if (symtab
!= NULL
) {
982 auto end(mach_header
.Swap(symtab
->stroff
) + mach_header
.Swap(symtab
->strsize
));
983 _assert(end
<= size
);
984 _assert(end
>= size
- 0x10);
990 alloc
+= sizeof(struct SuperBlob
);
993 special
= std::max(special
, CSSLOT_CODEDIRECTORY
);
994 alloc
+= sizeof(struct BlobIndex
);
995 alloc
+= sizeof(struct CodeDirectory
);
996 alloc
+= strlen(name
) + 1;
998 special
= std::max(special
, CSSLOT_REQUIREMENTS
);
999 alloc
+= sizeof(struct BlobIndex
);
1003 special
= std::max(special
, CSSLOT_ENTITLEMENTS
);
1004 alloc
+= sizeof(struct BlobIndex
);
1005 alloc
+= sizeof(struct Blob
);
1009 size_t normal((size
+ 0x1000 - 1) / 0x1000);
1010 alloc
= Align(alloc
+ (special
+ normal
) * 0x14, 16);
1013 auto *fat_arch(mach_header
.GetFatArch());
1014 uint32_t align(fat_arch
== NULL
? 0 : source
.Swap(fat_arch
->align
));
1015 offset
= Align(offset
, 1 << align
);
1017 allocations
.push_back(CodesignAllocation(mach_header
, offset
, size
, alloc
, align
));
1018 offset
+= size
+ alloc
;
1019 offset
= Align(offset
, 16);
1022 asprintf(&temp
, "%s.%s.cs", dir
.c_str(), base
);
1023 fclose(fopen(temp
, "w+"));
1024 _syscall(truncate(temp
, offset
));
1026 Map
output(temp
, O_RDWR
, PROT_READ
| PROT_WRITE
, MAP_SHARED
);
1027 _assert(output
.size() == offset
);
1028 void *file(output
.data());
1029 memset(file
, 0, offset
);
1032 if (!source
.IsFat())
1035 auto *fat_header(reinterpret_cast<struct fat_header
*>(file
));
1036 fat_header
->magic
= Swap(FAT_MAGIC
);
1037 fat_header
->nfat_arch
= Swap(source
.Swap(source
->nfat_arch
));
1038 fat_arch
= reinterpret_cast<struct fat_arch
*>(fat_header
+ 1);
1041 _foreach (allocation
, allocations
) {
1042 auto &source(allocation
.mach_header_
);
1044 uint32_t align(allocation
.size_
);
1045 if (allocation
.alloc_
!= 0)
1046 align
= Align(align
, 0x10);
1048 if (fat_arch
!= NULL
) {
1049 fat_arch
->cputype
= Swap(source
->cputype
);
1050 fat_arch
->cpusubtype
= Swap(source
->cpusubtype
);
1051 fat_arch
->offset
= Swap(allocation
.offset_
);
1052 fat_arch
->size
= Swap(align
+ allocation
.alloc_
);
1053 fat_arch
->align
= Swap(allocation
.align_
);
1057 void *target(reinterpret_cast<uint8_t *>(file
) + allocation
.offset_
);
1058 memcpy(target
, source
, allocation
.size_
);
1059 MachHeader
mach_header(target
, align
+ allocation
.alloc_
);
1061 struct linkedit_data_command
*signature(NULL
);
1062 _foreach (load_command
, mach_header
.GetLoadCommands()) {
1063 uint32_t cmd(mach_header
.Swap(load_command
->cmd
));
1064 if (cmd
!= LC_CODE_SIGNATURE
)
1066 signature
= reinterpret_cast<struct linkedit_data_command
*>(load_command
);
1070 if (flag_r
&& signature
!= NULL
) {
1071 auto before(reinterpret_cast<uint8_t *>(mach_header
.GetLoadCommand()));
1072 auto after(reinterpret_cast<uint8_t *>(signature
));
1073 auto next(mach_header
.Swap(signature
->cmdsize
));
1074 auto total(mach_header
.Swap(mach_header
->sizeofcmds
));
1075 memmove(signature
, after
+ next
, before
+ total
- after
- next
);
1076 memset(before
+ total
- next
, 0, next
);
1077 mach_header
->ncmds
= mach_header
.Swap(mach_header
.Swap(mach_header
->ncmds
) - 1);
1078 mach_header
->sizeofcmds
= mach_header
.Swap(total
- next
);
1083 if (signature
== NULL
) {
1084 signature
= reinterpret_cast<struct linkedit_data_command
*>(reinterpret_cast<uint8_t *>(mach_header
.GetLoadCommand()) + mach_header
.Swap(mach_header
->sizeofcmds
));
1085 signature
->cmd
= mach_header
.Swap(LC_CODE_SIGNATURE
);
1086 signature
->cmdsize
= mach_header
.Swap(uint32_t(sizeof(*signature
)));
1087 mach_header
->ncmds
= mach_header
.Swap(mach_header
.Swap(mach_header
->ncmds
) + 1);
1088 mach_header
->sizeofcmds
= mach_header
.Swap(mach_header
.Swap(mach_header
->sizeofcmds
) + uint32_t(sizeof(*signature
)));
1091 signature
->dataoff
= mach_header
.Swap(align
);
1092 signature
->datasize
= mach_header
.Swap(allocation
.alloc_
);
1095 _foreach (segment
, mach_header
.GetSegments("__LINKEDIT")) {
1096 size_t size(mach_header
.Swap(align
+ allocation
.alloc_
- mach_header
.Swap(segment
->fileoff
)));
1097 segment
->filesize
= size
;
1098 segment
->vmsize
= Align(size
, 0x1000);
1101 _foreach (segment
, mach_header
.GetSegments64("__LINKEDIT")) {
1102 size_t size(mach_header
.Swap(align
+ allocation
.alloc_
- mach_header
.Swap(segment
->fileoff
)));
1103 segment
->filesize
= size
;
1104 segment
->vmsize
= Align(size
, 0x1000);
1109 Map
mapping(temp
?: path
, flag_T
|| flag_s
|| flag_S
);
1110 FatHeader
fat_header(mapping
.data(), mapping
.size());
1112 _foreach (mach_header
, fat_header
.GetMachHeaders()) {
1113 struct linkedit_data_command
*signature(NULL
);
1114 struct encryption_info_command
*encryption(NULL
);
1117 if (mach_header
.GetCPUType() != flag_CPUType
)
1119 if (mach_header
.GetCPUSubtype() != flag_CPUSubtype
)
1124 printf("cpu=0x%x:0x%x\n", mach_header
.GetCPUType(), mach_header
.GetCPUSubtype());
1126 _foreach (load_command
, mach_header
.GetLoadCommands()) {
1127 uint32_t cmd(mach_header
.Swap(load_command
->cmd
));
1130 else if (cmd
== LC_CODE_SIGNATURE
)
1131 signature
= reinterpret_cast<struct linkedit_data_command
*>(load_command
);
1132 else if (cmd
== LC_ENCRYPTION_INFO
)
1133 encryption
= reinterpret_cast<struct encryption_info_command
*>(load_command
);
1134 else if (cmd
== LC_ID_DYLIB
) {
1135 volatile struct dylib_command
*dylib_command(reinterpret_cast<struct dylib_command
*>(load_command
));
1143 dylib_command
->dylib
.timestamp
= 0;
1144 timed
= hash(reinterpret_cast<uint8_t *>(mach_header
.GetBase()), mach_header
.GetSize(), timev
);
1147 dylib_command
->dylib
.timestamp
= mach_header
.Swap(timed
);
1153 _assert(encryption
!= NULL
);
1154 encryption
->cryptid
= mach_header
.Swap(0);
1158 _assert(signature
!= NULL
);
1160 uint32_t data
= mach_header
.Swap(signature
->dataoff
);
1162 uint8_t *top
= reinterpret_cast<uint8_t *>(mach_header
.GetBase());
1163 uint8_t *blob
= top
+ data
;
1164 struct SuperBlob
*super
= reinterpret_cast<struct SuperBlob
*>(blob
);
1166 for (size_t index(0); index
!= Swap(super
->count
); ++index
)
1167 if (Swap(super
->index
[index
].type
) == CSSLOT_ENTITLEMENTS
) {
1168 uint32_t begin
= Swap(super
->index
[index
].offset
);
1169 struct Blob
*entitlements
= reinterpret_cast<struct Blob
*>(blob
+ begin
);
1170 fwrite(entitlements
+ 1, 1, Swap(entitlements
->length
) - sizeof(struct Blob
), stdout
);
1175 _assert(signature
!= NULL
);
1177 uint32_t data
= mach_header
.Swap(signature
->dataoff
);
1179 uint8_t *top
= reinterpret_cast<uint8_t *>(mach_header
.GetBase());
1180 uint8_t *blob
= top
+ data
;
1181 struct SuperBlob
*super
= reinterpret_cast<struct SuperBlob
*>(blob
);
1183 for (size_t index(0); index
!= Swap(super
->count
); ++index
)
1184 if (Swap(super
->index
[index
].type
) == CSSLOT_CODEDIRECTORY
) {
1185 uint32_t begin
= Swap(super
->index
[index
].offset
);
1186 struct CodeDirectory
*directory
= reinterpret_cast<struct CodeDirectory
*>(blob
+ begin
);
1188 uint8_t (*hashes
)[20] = reinterpret_cast<uint8_t (*)[20]>(blob
+ begin
+ Swap(directory
->hashOffset
));
1189 uint32_t pages
= Swap(directory
->nCodeSlots
);
1192 for (size_t i
= 0; i
!= pages
- 1; ++i
)
1193 sha1(hashes
[i
], top
+ 0x1000 * i
, 0x1000);
1195 sha1(hashes
[pages
- 1], top
+ 0x1000 * (pages
- 1), ((data
- 1) % 0x1000) + 1);
1200 _assert(signature
!= NULL
);
1202 uint32_t data
= mach_header
.Swap(signature
->dataoff
);
1203 uint32_t size
= mach_header
.Swap(signature
->datasize
);
1205 uint8_t *top
= reinterpret_cast<uint8_t *>(mach_header
.GetBase());
1206 uint8_t *blob
= top
+ data
;
1207 struct SuperBlob
*super
= reinterpret_cast<struct SuperBlob
*>(blob
);
1208 super
->blob
.magic
= Swap(CSMAGIC_EMBEDDED_SIGNATURE
);
1210 uint32_t count
= xmld
== NULL
? 2 : 3;
1211 uint32_t offset
= sizeof(struct SuperBlob
) + count
* sizeof(struct BlobIndex
);
1213 super
->index
[0].type
= Swap(CSSLOT_CODEDIRECTORY
);
1214 super
->index
[0].offset
= Swap(offset
);
1216 uint32_t begin
= offset
;
1217 struct CodeDirectory
*directory
= reinterpret_cast<struct CodeDirectory
*>(blob
+ begin
);
1218 offset
+= sizeof(struct CodeDirectory
);
1220 directory
->blob
.magic
= Swap(CSMAGIC_CODEDIRECTORY
);
1221 directory
->version
= Swap(uint32_t(0x00020001));
1222 directory
->flags
= Swap(uint32_t(0));
1223 directory
->codeLimit
= Swap(data
);
1224 directory
->hashSize
= 0x14;
1225 directory
->hashType
= 0x01;
1226 directory
->spare1
= 0x00;
1227 directory
->pageSize
= 0x0c;
1228 directory
->spare2
= Swap(uint32_t(0));
1230 directory
->identOffset
= Swap(offset
- begin
);
1231 strcpy(reinterpret_cast<char *>(blob
+ offset
), name
);
1232 offset
+= strlen(name
) + 1;
1234 uint32_t special
= xmld
== NULL
? CSSLOT_REQUIREMENTS
: CSSLOT_ENTITLEMENTS
;
1235 directory
->nSpecialSlots
= Swap(special
);
1237 uint8_t (*hashes
)[20] = reinterpret_cast<uint8_t (*)[20]>(blob
+ offset
);
1238 memset(hashes
, 0, sizeof(*hashes
) * special
);
1240 offset
+= sizeof(*hashes
) * special
;
1243 uint32_t pages
= (data
+ 0x1000 - 1) / 0x1000;
1244 directory
->nCodeSlots
= Swap(pages
);
1247 for (size_t i
= 0; i
!= pages
- 1; ++i
)
1248 sha1(hashes
[i
], top
+ 0x1000 * i
, 0x1000);
1250 sha1(hashes
[pages
- 1], top
+ 0x1000 * (pages
- 1), ((data
- 1) % 0x1000) + 1);
1252 directory
->hashOffset
= Swap(offset
- begin
);
1253 offset
+= sizeof(*hashes
) * pages
;
1254 directory
->blob
.length
= Swap(offset
- begin
);
1256 super
->index
[1].type
= Swap(CSSLOT_REQUIREMENTS
);
1257 super
->index
[1].offset
= Swap(offset
);
1259 memcpy(blob
+ offset
, "\xfa\xde\x0c\x01\x00\x00\x00\x0c\x00\x00\x00\x00", 0xc);
1263 super
->index
[2].type
= Swap(CSSLOT_ENTITLEMENTS
);
1264 super
->index
[2].offset
= Swap(offset
);
1266 uint32_t begin
= offset
;
1267 struct Blob
*entitlements
= reinterpret_cast<struct Blob
*>(blob
+ begin
);
1268 offset
+= sizeof(struct Blob
);
1270 memcpy(blob
+ offset
, xmld
, xmls
);
1273 entitlements
->magic
= Swap(CSMAGIC_ENTITLEMENTS
);
1274 entitlements
->length
= Swap(offset
- begin
);
1277 for (size_t index(0); index
!= count
; ++index
) {
1278 uint32_t type
= Swap(super
->index
[index
].type
);
1279 if (type
!= 0 && type
<= special
) {
1280 uint32_t offset
= Swap(super
->index
[index
].offset
);
1281 struct Blob
*local
= (struct Blob
*) (blob
+ offset
);
1282 sha1((uint8_t *) (hashes
- type
), (uint8_t *) local
, Swap(local
->length
));
1286 super
->count
= Swap(count
);
1287 super
->blob
.length
= Swap(offset
);
1289 if (offset
> size
) {
1290 fprintf(stderr
, "offset (%u) > size (%u)\n", offset
, size
);
1292 } //else fprintf(stderr, "offset (%zu) <= size (%zu)\n", offset, size);
1294 memset(blob
+ offset
, 0, size
- offset
);
1300 _syscall(stat(path
, &info
));
1301 _syscall(chown(temp
, info
.st_uid
, info
.st_gid
));
1302 _syscall(chmod(temp
, info
.st_mode
));
1303 _syscall(unlink(path
));
1304 _syscall(rename(temp
, path
));
1309 } catch (const char *) {