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)
98 #define LC_ENCRYPTION_INFO_64 uint32_t(0x2c)
103 uint32_t current_version
;
104 uint32_t compatibility_version
;
107 struct dylib_command
{
113 struct uuid_command
{
119 struct symtab_command
{
128 struct dyld_info_command
{
132 uint32_t rebase_size
;
135 uint32_t weak_bind_off
;
136 uint32_t weak_bind_size
;
137 uint32_t lazy_bind_off
;
138 uint32_t lazy_bind_size
;
140 uint32_t export_size
;
143 struct dysymtab_command
{
156 uint32_t extrefsymoff
;
157 uint32_t nextrefsyms
;
158 uint32_t indirectsymoff
;
159 uint32_t nindirectsyms
;
166 struct dylib_table_of_contents
{
167 uint32_t symbol_index
;
168 uint32_t module_index
;
171 struct dylib_module
{
172 uint32_t module_name
;
181 uint32_t iinit_iterm
;
182 uint32_t ninit_nterm
;
183 uint32_t objc_module_info_addr
;
184 uint32_t objc_module_info_size
;
187 struct dylib_reference
{
192 struct relocation_info
{
194 uint32_t r_symbolnum
:24;
213 struct segment_command
{
227 struct segment_command_64
{
269 struct linkedit_data_command
{
276 struct encryption_info_command
{
284 #define BIND_OPCODE_MASK 0xf0
285 #define BIND_IMMEDIATE_MASK 0x0f
286 #define BIND_OPCODE_DONE 0x00
287 #define BIND_OPCODE_SET_DYLIB_ORDINAL_IMM 0x10
288 #define BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB 0x20
289 #define BIND_OPCODE_SET_DYLIB_SPECIAL_IMM 0x30
290 #define BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM 0x40
291 #define BIND_OPCODE_SET_TYPE_IMM 0x50
292 #define BIND_OPCODE_SET_ADDEND_SLEB 0x60
293 #define BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB 0x70
294 #define BIND_OPCODE_ADD_ADDR_ULEB 0x80
295 #define BIND_OPCODE_DO_BIND 0x90
296 #define BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB 0xa0
297 #define BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED 0xb0
298 #define BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB 0xc0
300 template <typename Type_
>
301 Type_
Align(Type_ value
, size_t align
) {
308 uint16_t Swap_(uint16_t value
) {
310 ((value
>> 8) & 0x00ff) |
311 ((value
<< 8) & 0xff00);
314 uint32_t Swap_(uint32_t value
) {
315 value
= ((value
>> 8) & 0x00ff00ff) |
316 ((value
<< 8) & 0xff00ff00);
317 value
= ((value
>> 16) & 0x0000ffff) |
318 ((value
<< 16) & 0xffff0000);
322 uint64_t Swap_(uint64_t value
) {
323 value
= (value
& 0x00000000ffffffff) << 32 | (value
& 0xffffffff00000000) >> 32;
324 value
= (value
& 0x0000ffff0000ffff) << 16 | (value
& 0xffff0000ffff0000) >> 16;
325 value
= (value
& 0x00ff00ff00ff00ff) << 8 | (value
& 0xff00ff00ff00ff00) >> 8;
329 int16_t Swap_(int16_t value
) {
330 return Swap_(static_cast<uint16_t>(value
));
333 int32_t Swap_(int32_t value
) {
334 return Swap_(static_cast<uint32_t>(value
));
337 int64_t Swap_(int64_t value
) {
338 return Swap_(static_cast<uint64_t>(value
));
343 uint16_t Swap(uint16_t value
) {
344 return little_
? Swap_(value
) : value
;
347 uint32_t Swap(uint32_t value
) {
348 return little_
? Swap_(value
) : value
;
351 uint64_t Swap(uint64_t value
) {
352 return little_
? Swap_(value
) : value
;
355 int16_t Swap(int16_t value
) {
356 return Swap(static_cast<uint16_t>(value
));
359 int32_t Swap(int32_t value
) {
360 return Swap(static_cast<uint32_t>(value
));
363 int64_t Swap(int64_t value
) {
364 return Swap(static_cast<uint64_t>(value
));
367 template <typename Target_
>
379 Data(void *base
, size_t size
) :
386 uint16_t Swap(uint16_t value
) const {
387 return swapped_
? Swap_(value
) : value
;
390 uint32_t Swap(uint32_t value
) const {
391 return swapped_
? Swap_(value
) : value
;
394 uint64_t Swap(uint64_t value
) const {
395 return swapped_
? Swap_(value
) : value
;
398 int16_t Swap(int16_t value
) const {
399 return Swap(static_cast<uint16_t>(value
));
402 int32_t Swap(int32_t value
) const {
403 return Swap(static_cast<uint32_t>(value
));
406 int64_t Swap(int64_t value
) const {
407 return Swap(static_cast<uint64_t>(value
));
410 void *GetBase() const {
414 size_t GetSize() const {
425 struct mach_header
*mach_header_
;
426 struct load_command
*load_command_
;
429 MachHeader(void *base
, size_t size
) :
432 mach_header_
= (mach_header
*) base
;
434 switch (Swap(mach_header_
->magic
)) {
436 swapped_
= !swapped_
;
442 swapped_
= !swapped_
;
451 void *post
= mach_header_
+ 1;
453 post
= (uint32_t *) post
+ 1;
454 load_command_
= (struct load_command
*) post
;
457 Swap(mach_header_
->filetype
) == MH_EXECUTE
||
458 Swap(mach_header_
->filetype
) == MH_DYLIB
||
459 Swap(mach_header_
->filetype
) == MH_BUNDLE
463 struct mach_header
*operator ->() const {
467 operator struct mach_header
*() const {
471 uint32_t GetCPUType() const {
472 return Swap(mach_header_
->cputype
);
475 uint32_t GetCPUSubtype() const {
476 return Swap(mach_header_
->cpusubtype
) & 0xff;
479 struct load_command
*GetLoadCommand() const {
480 return load_command_
;
483 std::vector
<struct load_command
*> GetLoadCommands() const {
484 std::vector
<struct load_command
*> load_commands
;
486 struct load_command
*load_command
= load_command_
;
487 for (uint32_t cmd
= 0; cmd
!= Swap(mach_header_
->ncmds
); ++cmd
) {
488 load_commands
.push_back(load_command
);
489 load_command
= (struct load_command
*) ((uint8_t *) load_command
+ Swap(load_command
->cmdsize
));
492 return load_commands
;
495 std::vector
<segment_command
*> GetSegments(const char *segment_name
) const {
496 std::vector
<struct segment_command
*> segment_commands
;
498 _foreach (load_command
, GetLoadCommands()) {
499 if (Swap(load_command
->cmd
) == LC_SEGMENT
) {
500 segment_command
*segment_command
= reinterpret_cast<struct segment_command
*>(load_command
);
501 if (strncmp(segment_command
->segname
, segment_name
, 16) == 0)
502 segment_commands
.push_back(segment_command
);
506 return segment_commands
;
509 std::vector
<segment_command_64
*> GetSegments64(const char *segment_name
) const {
510 std::vector
<struct segment_command_64
*> segment_commands
;
512 _foreach (load_command
, GetLoadCommands()) {
513 if (Swap(load_command
->cmd
) == LC_SEGMENT_64
) {
514 segment_command_64
*segment_command
= reinterpret_cast<struct segment_command_64
*>(load_command
);
515 if (strncmp(segment_command
->segname
, segment_name
, 16) == 0)
516 segment_commands
.push_back(segment_command
);
520 return segment_commands
;
523 std::vector
<section
*> GetSections(const char *segment_name
, const char *section_name
) const {
524 std::vector
<section
*> sections
;
526 _foreach (segment
, GetSegments(segment_name
)) {
527 section
*section
= (struct section
*) (segment
+ 1);
530 for (sect
= 0; sect
!= Swap(segment
->nsects
); ++sect
) {
531 if (strncmp(section
->sectname
, section_name
, 16) == 0)
532 sections
.push_back(section
);
540 template <typename Target_
>
541 Pointer
<Target_
> GetPointer(uint32_t address
, const char *segment_name
= NULL
) const {
542 load_command
*load_command
= (struct load_command
*) (mach_header_
+ 1);
545 for (cmd
= 0; cmd
!= Swap(mach_header_
->ncmds
); ++cmd
) {
546 if (Swap(load_command
->cmd
) == LC_SEGMENT
) {
547 segment_command
*segment_command
= (struct segment_command
*) load_command
;
548 if (segment_name
!= NULL
&& strncmp(segment_command
->segname
, segment_name
, 16) != 0)
551 section
*sections
= (struct section
*) (segment_command
+ 1);
554 for (sect
= 0; sect
!= Swap(segment_command
->nsects
); ++sect
) {
555 section
*section
= §ions
[sect
];
556 //printf("%s %u %p %p %u\n", segment_command->segname, sect, address, section->addr, section->size);
557 if (address
>= Swap(section
->addr
) && address
< Swap(section
->addr
) + Swap(section
->size
)) {
558 //printf("0x%.8x %s\n", address, segment_command->segname);
559 return Pointer
<Target_
>(this, reinterpret_cast<Target_
*>(address
- Swap(section
->addr
) + Swap(section
->offset
) + (char *) mach_header_
));
565 load_command
= (struct load_command
*) ((char *) load_command
+ Swap(load_command
->cmdsize
));
568 return Pointer
<Target_
>(this);
571 template <typename Target_
>
572 Pointer
<Target_
> GetOffset(uint32_t offset
) {
573 return Pointer
<Target_
>(this, reinterpret_cast<Target_
*>(offset
+ (uint8_t *) mach_header_
));
577 class FatMachHeader
:
584 FatMachHeader(void *base
, size_t size
, fat_arch
*fat_arch
) :
585 MachHeader(base
, size
),
590 fat_arch
*GetFatArch() const {
599 fat_header
*fat_header_
;
600 std::vector
<FatMachHeader
> mach_headers_
;
603 FatHeader(void *base
, size_t size
) :
606 fat_header_
= reinterpret_cast<struct fat_header
*>(base
);
608 if (Swap(fat_header_
->magic
) == FAT_CIGAM
) {
609 swapped_
= !swapped_
;
611 } else if (Swap(fat_header_
->magic
) != FAT_MAGIC
) {
613 mach_headers_
.push_back(FatMachHeader(base
, size
, NULL
));
615 size_t fat_narch
= Swap(fat_header_
->nfat_arch
);
616 fat_arch
*fat_arch
= reinterpret_cast<struct fat_arch
*>(fat_header_
+ 1);
618 for (arch
= 0; arch
!= fat_narch
; ++arch
) {
619 uint32_t arch_offset
= Swap(fat_arch
->offset
);
620 uint32_t arch_size
= Swap(fat_arch
->size
);
621 mach_headers_
.push_back(FatMachHeader((uint8_t *) base
+ arch_offset
, arch_size
, fat_arch
));
627 std::vector
<FatMachHeader
> &GetMachHeaders() {
628 return mach_headers_
;
632 return fat_header_
!= NULL
;
635 struct fat_header
*operator ->() const {
639 operator struct fat_header
*() const {
644 template <typename Target_
>
647 const MachHeader
*framework_
;
648 const Target_
*pointer_
;
651 Pointer(const MachHeader
*framework
= NULL
, const Target_
*pointer
= NULL
) :
652 framework_(framework
),
657 operator const Target_
*() const {
661 const Target_
*operator ->() const {
665 Pointer
<Target_
> &operator ++() {
670 template <typename Value_
>
671 Value_
Swap(Value_ value
) {
672 return framework_
->Swap(value
);
676 #define CSMAGIC_CODEDIRECTORY uint32_t(0xfade0c02)
677 #define CSMAGIC_EMBEDDED_SIGNATURE uint32_t(0xfade0cc0)
678 #define CSMAGIC_ENTITLEMENTS uint32_t(0xfade7171)
680 #define CSSLOT_CODEDIRECTORY uint32_t(0)
681 #define CSSLOT_REQUIREMENTS uint32_t(2)
682 #define CSSLOT_ENTITLEMENTS uint32_t(5)
697 struct BlobIndex index
[];
700 struct CodeDirectory
{
705 uint32_t identOffset
;
706 uint32_t nSpecialSlots
;
716 extern "C" uint32_t hash(uint8_t *k
, uint32_t length
, uint32_t initval
);
718 void sha1(uint8_t *hash
, uint8_t *data
, size_t size
) {
719 SHA1(data
, size
, hash
);
722 struct CodesignAllocation
{
723 FatMachHeader mach_header_
;
729 CodesignAllocation(FatMachHeader mach_header
, size_t offset
, size_t size
, size_t alloc
, size_t align
) :
730 mach_header_(mach_header
),
751 _syscall(close(file_
));
754 void open(const char *path
, int flags
) {
755 _assert(file_
== -1);
756 _syscall(file_
= ::open(path
, flags
));
773 _syscall(munmap(data_
, size_
));
785 Map(const char *path
, int oflag
, int pflag
, int mflag
) :
788 open(path
, oflag
, pflag
, mflag
);
791 Map(const char *path
, bool edit
) :
801 void open(const char *path
, int oflag
, int pflag
, int mflag
) {
804 file_
.open(path
, oflag
);
805 int file(file_
.file());
808 _syscall(fstat(file
, &stat
));
809 size_
= stat
.st_size
;
811 _syscall(data_
= mmap(NULL
, size_
, pflag
, mflag
, file
, 0));
814 void open(const char *path
, bool edit
) {
816 open(path
, O_RDWR
, PROT_READ
| PROT_WRITE
, MAP_SHARED
);
818 open(path
, O_RDONLY
, PROT_READ
, MAP_PRIVATE
);
825 size_t size() const {
830 int main(int argc
, const char *argv
[]) {
836 little_
= endian
.byte
[0];
851 uint32_t flag_CPUType(_not(uint32_t));
852 uint32_t flag_CPUSubtype(_not(uint32_t));
854 const char *flag_I(NULL
);
860 const void *xmld(NULL
);
863 std::vector
<std::string
> files
;
866 fprintf(stderr
, "usage: %s -S[entitlements.xml] <binary>\n", argv
[0]);
867 fprintf(stderr
, " %s -e MobileSafari\n", argv
[0]);
868 fprintf(stderr
, " %s -S cat\n", argv
[0]);
869 fprintf(stderr
, " %s -Stfp.xml gdb\n", argv
[0]);
873 for (int argi(1); argi
!= argc
; ++argi
)
874 if (argv
[argi
][0] != '-')
875 files
.push_back(argv
[argi
]);
876 else switch (argv
[argi
][1]) {
877 case 'r': flag_r
= true; break;
878 case 'e': flag_e
= true; break;
880 case 'D': flag_D
= true; break;
882 case 'a': flag_a
= true; break;
886 if (argv
[argi
][2] != '\0') {
887 const char *cpu
= argv
[argi
] + 2;
888 const char *colon
= strchr(cpu
, ':');
889 _assert(colon
!= NULL
);
891 flag_CPUType
= strtoul(cpu
, &arge
, 0);
892 _assert(arge
== colon
);
893 flag_CPUSubtype
= strtoul(colon
+ 1, &arge
, 0);
894 _assert(arge
== argv
[argi
] + strlen(argv
[argi
]));
906 if (argv
[argi
][2] != '\0') {
907 const char *xml
= argv
[argi
] + 2;
908 xmlm
.open(xml
, O_RDONLY
, PROT_READ
, MAP_PRIVATE
);
916 if (argv
[argi
][2] == '-')
920 timev
= strtoul(argv
[argi
] + 2, &arge
, 0);
921 _assert(arge
== argv
[argi
] + strlen(argv
[argi
]));
926 flag_I
= argv
[argi
] + 2;
934 if (files
.empty()) usage
: {
938 size_t filei(0), filee(0);
939 _foreach (file
, files
) try {
940 const char *path(file
.c_str());
941 const char *base
= strrchr(path
, '/');
945 dir
.assign(path
, base
++ - path
+ 1);
949 const char *name(flag_I
?: base
);
952 if (flag_S
|| flag_r
) {
953 Map
input(path
, O_RDONLY
, PROT_READ
| PROT_WRITE
, MAP_PRIVATE
);
954 FatHeader
source(input
.data(), input
.size());
958 offset
+= sizeof(fat_header
) + sizeof(fat_arch
) * source
.Swap(source
->nfat_arch
);
960 std::vector
<CodesignAllocation
> allocations
;
961 _foreach (mach_header
, source
.GetMachHeaders()) {
962 struct linkedit_data_command
*signature(NULL
);
963 struct symtab_command
*symtab(NULL
);
965 _foreach (load_command
, mach_header
.GetLoadCommands()) {
966 uint32_t cmd(mach_header
.Swap(load_command
->cmd
));
968 else if (cmd
== LC_CODE_SIGNATURE
)
969 signature
= reinterpret_cast<struct linkedit_data_command
*>(load_command
);
970 else if (cmd
== LC_SYMTAB
)
971 symtab
= reinterpret_cast<struct symtab_command
*>(load_command
);
975 if (signature
== NULL
)
976 size
= mach_header
.GetSize();
978 size
= mach_header
.Swap(signature
->dataoff
);
979 _assert(size
<= mach_header
.GetSize());
982 if (symtab
!= NULL
) {
983 auto end(mach_header
.Swap(symtab
->stroff
) + mach_header
.Swap(symtab
->strsize
));
984 _assert(end
<= size
);
985 _assert(end
>= size
- 0x10);
991 alloc
+= sizeof(struct SuperBlob
);
994 special
= std::max(special
, CSSLOT_CODEDIRECTORY
);
995 alloc
+= sizeof(struct BlobIndex
);
996 alloc
+= sizeof(struct CodeDirectory
);
997 alloc
+= strlen(name
) + 1;
999 special
= std::max(special
, CSSLOT_REQUIREMENTS
);
1000 alloc
+= sizeof(struct BlobIndex
);
1004 special
= std::max(special
, CSSLOT_ENTITLEMENTS
);
1005 alloc
+= sizeof(struct BlobIndex
);
1006 alloc
+= sizeof(struct Blob
);
1010 size_t normal((size
+ 0x1000 - 1) / 0x1000);
1011 alloc
= Align(alloc
+ (special
+ normal
) * 0x14, 16);
1014 auto *fat_arch(mach_header
.GetFatArch());
1015 uint32_t align(fat_arch
== NULL
? 0 : source
.Swap(fat_arch
->align
));
1016 offset
= Align(offset
, 1 << align
);
1018 allocations
.push_back(CodesignAllocation(mach_header
, offset
, size
, alloc
, align
));
1019 offset
+= size
+ alloc
;
1020 offset
= Align(offset
, 16);
1023 asprintf(&temp
, "%s.%s.cs", dir
.c_str(), base
);
1024 fclose(fopen(temp
, "w+"));
1025 _syscall(truncate(temp
, offset
));
1027 Map
output(temp
, O_RDWR
, PROT_READ
| PROT_WRITE
, MAP_SHARED
);
1028 _assert(output
.size() == offset
);
1029 void *file(output
.data());
1030 memset(file
, 0, offset
);
1033 if (!source
.IsFat())
1036 auto *fat_header(reinterpret_cast<struct fat_header
*>(file
));
1037 fat_header
->magic
= Swap(FAT_MAGIC
);
1038 fat_header
->nfat_arch
= Swap(source
.Swap(source
->nfat_arch
));
1039 fat_arch
= reinterpret_cast<struct fat_arch
*>(fat_header
+ 1);
1042 _foreach (allocation
, allocations
) {
1043 auto &source(allocation
.mach_header_
);
1045 uint32_t align(allocation
.size_
);
1046 if (allocation
.alloc_
!= 0)
1047 align
= Align(align
, 0x10);
1049 if (fat_arch
!= NULL
) {
1050 fat_arch
->cputype
= Swap(source
->cputype
);
1051 fat_arch
->cpusubtype
= Swap(source
->cpusubtype
);
1052 fat_arch
->offset
= Swap(allocation
.offset_
);
1053 fat_arch
->size
= Swap(align
+ allocation
.alloc_
);
1054 fat_arch
->align
= Swap(allocation
.align_
);
1058 void *target(reinterpret_cast<uint8_t *>(file
) + allocation
.offset_
);
1059 memcpy(target
, source
, allocation
.size_
);
1060 MachHeader
mach_header(target
, align
+ allocation
.alloc_
);
1062 struct linkedit_data_command
*signature(NULL
);
1063 _foreach (load_command
, mach_header
.GetLoadCommands()) {
1064 uint32_t cmd(mach_header
.Swap(load_command
->cmd
));
1065 if (cmd
!= LC_CODE_SIGNATURE
)
1067 signature
= reinterpret_cast<struct linkedit_data_command
*>(load_command
);
1071 if (flag_r
&& signature
!= NULL
) {
1072 auto before(reinterpret_cast<uint8_t *>(mach_header
.GetLoadCommand()));
1073 auto after(reinterpret_cast<uint8_t *>(signature
));
1074 auto next(mach_header
.Swap(signature
->cmdsize
));
1075 auto total(mach_header
.Swap(mach_header
->sizeofcmds
));
1076 memmove(signature
, after
+ next
, before
+ total
- after
- next
);
1077 memset(before
+ total
- next
, 0, next
);
1078 mach_header
->ncmds
= mach_header
.Swap(mach_header
.Swap(mach_header
->ncmds
) - 1);
1079 mach_header
->sizeofcmds
= mach_header
.Swap(total
- next
);
1084 if (signature
== NULL
) {
1085 signature
= reinterpret_cast<struct linkedit_data_command
*>(reinterpret_cast<uint8_t *>(mach_header
.GetLoadCommand()) + mach_header
.Swap(mach_header
->sizeofcmds
));
1086 signature
->cmd
= mach_header
.Swap(LC_CODE_SIGNATURE
);
1087 signature
->cmdsize
= mach_header
.Swap(uint32_t(sizeof(*signature
)));
1088 mach_header
->ncmds
= mach_header
.Swap(mach_header
.Swap(mach_header
->ncmds
) + 1);
1089 mach_header
->sizeofcmds
= mach_header
.Swap(mach_header
.Swap(mach_header
->sizeofcmds
) + uint32_t(sizeof(*signature
)));
1092 signature
->dataoff
= mach_header
.Swap(align
);
1093 signature
->datasize
= mach_header
.Swap(allocation
.alloc_
);
1096 _foreach (segment
, mach_header
.GetSegments("__LINKEDIT")) {
1097 size_t size(mach_header
.Swap(align
+ allocation
.alloc_
- mach_header
.Swap(segment
->fileoff
)));
1098 segment
->filesize
= size
;
1099 segment
->vmsize
= Align(size
, 0x1000);
1102 _foreach (segment
, mach_header
.GetSegments64("__LINKEDIT")) {
1103 size_t size(mach_header
.Swap(align
+ allocation
.alloc_
- mach_header
.Swap(segment
->fileoff
)));
1104 segment
->filesize
= size
;
1105 segment
->vmsize
= Align(size
, 0x1000);
1110 Map
mapping(temp
?: path
, flag_T
|| flag_s
|| flag_S
);
1111 FatHeader
fat_header(mapping
.data(), mapping
.size());
1113 _foreach (mach_header
, fat_header
.GetMachHeaders()) {
1114 struct linkedit_data_command
*signature(NULL
);
1115 struct encryption_info_command
*encryption(NULL
);
1118 if (mach_header
.GetCPUType() != flag_CPUType
)
1120 if (mach_header
.GetCPUSubtype() != flag_CPUSubtype
)
1125 printf("cpu=0x%x:0x%x\n", mach_header
.GetCPUType(), mach_header
.GetCPUSubtype());
1127 _foreach (load_command
, mach_header
.GetLoadCommands()) {
1128 uint32_t cmd(mach_header
.Swap(load_command
->cmd
));
1131 else if (cmd
== LC_CODE_SIGNATURE
)
1132 signature
= reinterpret_cast<struct linkedit_data_command
*>(load_command
);
1133 else if (cmd
== LC_ENCRYPTION_INFO
|| cmd
== LC_ENCRYPTION_INFO_64
)
1134 encryption
= reinterpret_cast<struct encryption_info_command
*>(load_command
);
1135 else if (cmd
== LC_ID_DYLIB
) {
1136 volatile struct dylib_command
*dylib_command(reinterpret_cast<struct dylib_command
*>(load_command
));
1144 dylib_command
->dylib
.timestamp
= 0;
1145 timed
= hash(reinterpret_cast<uint8_t *>(mach_header
.GetBase()), mach_header
.GetSize(), timev
);
1148 dylib_command
->dylib
.timestamp
= mach_header
.Swap(timed
);
1154 _assert(encryption
!= NULL
);
1155 encryption
->cryptid
= mach_header
.Swap(0);
1159 _assert(signature
!= NULL
);
1161 uint32_t data
= mach_header
.Swap(signature
->dataoff
);
1163 uint8_t *top
= reinterpret_cast<uint8_t *>(mach_header
.GetBase());
1164 uint8_t *blob
= top
+ data
;
1165 struct SuperBlob
*super
= reinterpret_cast<struct SuperBlob
*>(blob
);
1167 for (size_t index(0); index
!= Swap(super
->count
); ++index
)
1168 if (Swap(super
->index
[index
].type
) == CSSLOT_ENTITLEMENTS
) {
1169 uint32_t begin
= Swap(super
->index
[index
].offset
);
1170 struct Blob
*entitlements
= reinterpret_cast<struct Blob
*>(blob
+ begin
);
1171 fwrite(entitlements
+ 1, 1, Swap(entitlements
->length
) - sizeof(struct Blob
), stdout
);
1176 _assert(signature
!= NULL
);
1178 uint32_t data
= mach_header
.Swap(signature
->dataoff
);
1180 uint8_t *top
= reinterpret_cast<uint8_t *>(mach_header
.GetBase());
1181 uint8_t *blob
= top
+ data
;
1182 struct SuperBlob
*super
= reinterpret_cast<struct SuperBlob
*>(blob
);
1184 for (size_t index(0); index
!= Swap(super
->count
); ++index
)
1185 if (Swap(super
->index
[index
].type
) == CSSLOT_CODEDIRECTORY
) {
1186 uint32_t begin
= Swap(super
->index
[index
].offset
);
1187 struct CodeDirectory
*directory
= reinterpret_cast<struct CodeDirectory
*>(blob
+ begin
);
1189 uint8_t (*hashes
)[20] = reinterpret_cast<uint8_t (*)[20]>(blob
+ begin
+ Swap(directory
->hashOffset
));
1190 uint32_t pages
= Swap(directory
->nCodeSlots
);
1193 for (size_t i
= 0; i
!= pages
- 1; ++i
)
1194 sha1(hashes
[i
], top
+ 0x1000 * i
, 0x1000);
1196 sha1(hashes
[pages
- 1], top
+ 0x1000 * (pages
- 1), ((data
- 1) % 0x1000) + 1);
1201 _assert(signature
!= NULL
);
1203 uint32_t data
= mach_header
.Swap(signature
->dataoff
);
1204 uint32_t size
= mach_header
.Swap(signature
->datasize
);
1206 uint8_t *top
= reinterpret_cast<uint8_t *>(mach_header
.GetBase());
1207 uint8_t *blob
= top
+ data
;
1208 struct SuperBlob
*super
= reinterpret_cast<struct SuperBlob
*>(blob
);
1209 super
->blob
.magic
= Swap(CSMAGIC_EMBEDDED_SIGNATURE
);
1211 uint32_t count
= xmld
== NULL
? 2 : 3;
1212 uint32_t offset
= sizeof(struct SuperBlob
) + count
* sizeof(struct BlobIndex
);
1214 super
->index
[0].type
= Swap(CSSLOT_CODEDIRECTORY
);
1215 super
->index
[0].offset
= Swap(offset
);
1217 uint32_t begin
= offset
;
1218 struct CodeDirectory
*directory
= reinterpret_cast<struct CodeDirectory
*>(blob
+ begin
);
1219 offset
+= sizeof(struct CodeDirectory
);
1221 directory
->blob
.magic
= Swap(CSMAGIC_CODEDIRECTORY
);
1222 directory
->version
= Swap(uint32_t(0x00020001));
1223 directory
->flags
= Swap(uint32_t(0));
1224 directory
->codeLimit
= Swap(data
);
1225 directory
->hashSize
= 0x14;
1226 directory
->hashType
= 0x01;
1227 directory
->spare1
= 0x00;
1228 directory
->pageSize
= 0x0c;
1229 directory
->spare2
= Swap(uint32_t(0));
1231 directory
->identOffset
= Swap(offset
- begin
);
1232 strcpy(reinterpret_cast<char *>(blob
+ offset
), name
);
1233 offset
+= strlen(name
) + 1;
1235 uint32_t special
= xmld
== NULL
? CSSLOT_REQUIREMENTS
: CSSLOT_ENTITLEMENTS
;
1236 directory
->nSpecialSlots
= Swap(special
);
1238 uint8_t (*hashes
)[20] = reinterpret_cast<uint8_t (*)[20]>(blob
+ offset
);
1239 memset(hashes
, 0, sizeof(*hashes
) * special
);
1241 offset
+= sizeof(*hashes
) * special
;
1244 uint32_t pages
= (data
+ 0x1000 - 1) / 0x1000;
1245 directory
->nCodeSlots
= Swap(pages
);
1248 for (size_t i
= 0; i
!= pages
- 1; ++i
)
1249 sha1(hashes
[i
], top
+ 0x1000 * i
, 0x1000);
1251 sha1(hashes
[pages
- 1], top
+ 0x1000 * (pages
- 1), ((data
- 1) % 0x1000) + 1);
1253 directory
->hashOffset
= Swap(offset
- begin
);
1254 offset
+= sizeof(*hashes
) * pages
;
1255 directory
->blob
.length
= Swap(offset
- begin
);
1257 super
->index
[1].type
= Swap(CSSLOT_REQUIREMENTS
);
1258 super
->index
[1].offset
= Swap(offset
);
1260 memcpy(blob
+ offset
, "\xfa\xde\x0c\x01\x00\x00\x00\x0c\x00\x00\x00\x00", 0xc);
1264 super
->index
[2].type
= Swap(CSSLOT_ENTITLEMENTS
);
1265 super
->index
[2].offset
= Swap(offset
);
1267 uint32_t begin
= offset
;
1268 struct Blob
*entitlements
= reinterpret_cast<struct Blob
*>(blob
+ begin
);
1269 offset
+= sizeof(struct Blob
);
1271 memcpy(blob
+ offset
, xmld
, xmls
);
1274 entitlements
->magic
= Swap(CSMAGIC_ENTITLEMENTS
);
1275 entitlements
->length
= Swap(offset
- begin
);
1278 for (size_t index(0); index
!= count
; ++index
) {
1279 uint32_t type
= Swap(super
->index
[index
].type
);
1280 if (type
!= 0 && type
<= special
) {
1281 uint32_t offset
= Swap(super
->index
[index
].offset
);
1282 struct Blob
*local
= (struct Blob
*) (blob
+ offset
);
1283 sha1((uint8_t *) (hashes
- type
), (uint8_t *) local
, Swap(local
->length
));
1287 super
->count
= Swap(count
);
1288 super
->blob
.length
= Swap(offset
);
1290 if (offset
> size
) {
1291 fprintf(stderr
, "offset (%u) > size (%u)\n", offset
, size
);
1293 } //else fprintf(stderr, "offset (%zu) <= size (%zu)\n", offset, size);
1295 memset(blob
+ offset
, 0, size
- offset
);
1301 _syscall(stat(path
, &info
));
1302 _syscall(chown(temp
, info
.st_uid
, info
.st_gid
));
1303 _syscall(chmod(temp
, info
.st_mode
));
1304 _syscall(unlink(path
));
1305 _syscall(rename(temp
, path
));
1310 } catch (const char *) {