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"
35 #include <sys/types.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 uint16_t Swap_(uint16_t value
) {
301 ((value
>> 8) & 0x00ff) |
302 ((value
<< 8) & 0xff00);
305 uint32_t Swap_(uint32_t value
) {
306 value
= ((value
>> 8) & 0x00ff00ff) |
307 ((value
<< 8) & 0xff00ff00);
308 value
= ((value
>> 16) & 0x0000ffff) |
309 ((value
<< 16) & 0xffff0000);
313 int16_t Swap_(int16_t value
) {
314 return Swap_(static_cast<uint16_t>(value
));
317 int32_t Swap_(int32_t value
) {
318 return Swap_(static_cast<uint32_t>(value
));
323 uint16_t Swap(uint16_t value
) {
324 return little_
? Swap_(value
) : value
;
327 uint32_t Swap(uint32_t value
) {
328 return little_
? Swap_(value
) : value
;
331 int16_t Swap(int16_t value
) {
332 return Swap(static_cast<uint16_t>(value
));
335 int32_t Swap(int32_t value
) {
336 return Swap(static_cast<uint32_t>(value
));
339 template <typename Target_
>
351 Data(void *base
, size_t size
) :
358 uint16_t Swap(uint16_t value
) const {
359 return swapped_
? Swap_(value
) : value
;
362 uint32_t Swap(uint32_t value
) const {
363 return swapped_
? Swap_(value
) : value
;
366 int16_t Swap(int16_t value
) const {
367 return Swap(static_cast<uint16_t>(value
));
370 int32_t Swap(int32_t value
) const {
371 return Swap(static_cast<uint32_t>(value
));
374 void *GetBase() const {
378 size_t GetSize() const {
389 struct mach_header
*mach_header_
;
390 struct load_command
*load_command_
;
393 MachHeader(void *base
, size_t size
) :
396 mach_header_
= (mach_header
*) base
;
398 switch (Swap(mach_header_
->magic
)) {
400 swapped_
= !swapped_
;
406 swapped_
= !swapped_
;
415 void *post
= mach_header_
+ 1;
417 post
= (uint32_t *) post
+ 1;
418 load_command_
= (struct load_command
*) post
;
421 Swap(mach_header_
->filetype
) == MH_EXECUTE
||
422 Swap(mach_header_
->filetype
) == MH_DYLIB
||
423 Swap(mach_header_
->filetype
) == MH_BUNDLE
427 struct mach_header
*operator ->() const {
431 uint32_t GetCPUType() const {
432 return Swap(mach_header_
->cputype
);
435 uint16_t GetCPUSubtype() const {
436 return Swap(mach_header_
->cpusubtype
) & 0xff;
439 std::vector
<struct load_command
*> GetLoadCommands() const {
440 std::vector
<struct load_command
*> load_commands
;
442 struct load_command
*load_command
= load_command_
;
443 for (uint32_t cmd
= 0; cmd
!= Swap(mach_header_
->ncmds
); ++cmd
) {
444 load_commands
.push_back(load_command
);
445 load_command
= (struct load_command
*) ((uint8_t *) load_command
+ Swap(load_command
->cmdsize
));
448 return load_commands
;
451 std::vector
<segment_command
*> GetSegments(const char *segment_name
) const {
452 std::vector
<struct segment_command
*> segment_commands
;
454 _foreach (load_command
, GetLoadCommands()) {
455 if (Swap(load_command
->cmd
) == LC_SEGMENT
) {
456 segment_command
*segment_command
= reinterpret_cast<struct segment_command
*>(load_command
);
457 if (strncmp(segment_command
->segname
, segment_name
, 16) == 0)
458 segment_commands
.push_back(segment_command
);
462 return segment_commands
;
465 std::vector
<segment_command_64
*> GetSegments64(const char *segment_name
) {
466 std::vector
<struct segment_command_64
*> segment_commands
;
468 _foreach (load_command
, GetLoadCommands()) {
469 if (Swap(load_command
->cmd
) == LC_SEGMENT_64
) {
470 segment_command_64
*segment_command
= reinterpret_cast<struct segment_command_64
*>(load_command
);
471 if (strncmp(segment_command
->segname
, segment_name
, 16) == 0)
472 segment_commands
.push_back(segment_command
);
476 return segment_commands
;
479 std::vector
<section
*> GetSections(const char *segment_name
, const char *section_name
) const {
480 std::vector
<section
*> sections
;
482 _foreach (segment
, GetSegments(segment_name
)) {
483 section
*section
= (struct section
*) (segment
+ 1);
486 for (sect
= 0; sect
!= Swap(segment
->nsects
); ++sect
) {
487 if (strncmp(section
->sectname
, section_name
, 16) == 0)
488 sections
.push_back(section
);
496 template <typename Target_
>
497 Pointer
<Target_
> GetPointer(uint32_t address
, const char *segment_name
= NULL
) const {
498 load_command
*load_command
= (struct load_command
*) (mach_header_
+ 1);
501 for (cmd
= 0; cmd
!= Swap(mach_header_
->ncmds
); ++cmd
) {
502 if (Swap(load_command
->cmd
) == LC_SEGMENT
) {
503 segment_command
*segment_command
= (struct segment_command
*) load_command
;
504 if (segment_name
!= NULL
&& strncmp(segment_command
->segname
, segment_name
, 16) != 0)
507 section
*sections
= (struct section
*) (segment_command
+ 1);
510 for (sect
= 0; sect
!= Swap(segment_command
->nsects
); ++sect
) {
511 section
*section
= §ions
[sect
];
512 //printf("%s %u %p %p %u\n", segment_command->segname, sect, address, section->addr, section->size);
513 if (address
>= Swap(section
->addr
) && address
< Swap(section
->addr
) + Swap(section
->size
)) {
514 //printf("0x%.8x %s\n", address, segment_command->segname);
515 return Pointer
<Target_
>(this, reinterpret_cast<Target_
*>(address
- Swap(section
->addr
) + Swap(section
->offset
) + (char *) mach_header_
));
521 load_command
= (struct load_command
*) ((char *) load_command
+ Swap(load_command
->cmdsize
));
524 return Pointer
<Target_
>(this);
527 template <typename Target_
>
528 Pointer
<Target_
> GetOffset(uint32_t offset
) {
529 return Pointer
<Target_
>(this, reinterpret_cast<Target_
*>(offset
+ (uint8_t *) mach_header_
));
533 class FatMachHeader
:
540 FatMachHeader(void *base
, size_t size
, fat_arch
*fat_arch
) :
541 MachHeader(base
, size
),
546 fat_arch
*GetFatArch() const {
555 fat_header
*fat_header_
;
556 std::vector
<FatMachHeader
> mach_headers_
;
559 FatHeader(void *base
, size_t size
) :
562 fat_header_
= reinterpret_cast<struct fat_header
*>(base
);
564 if (Swap(fat_header_
->magic
) == FAT_CIGAM
) {
565 swapped_
= !swapped_
;
567 } else if (Swap(fat_header_
->magic
) != FAT_MAGIC
) {
569 mach_headers_
.push_back(FatMachHeader(base
, size
, NULL
));
571 size_t fat_narch
= Swap(fat_header_
->nfat_arch
);
572 fat_arch
*fat_arch
= reinterpret_cast<struct fat_arch
*>(fat_header_
+ 1);
574 for (arch
= 0; arch
!= fat_narch
; ++arch
) {
575 uint32_t arch_offset
= Swap(fat_arch
->offset
);
576 uint32_t arch_size
= Swap(fat_arch
->size
);
577 mach_headers_
.push_back(FatMachHeader((uint8_t *) base
+ arch_offset
, arch_size
, fat_arch
));
583 std::vector
<FatMachHeader
> &GetMachHeaders() {
584 return mach_headers_
;
588 return fat_header_
!= NULL
;
591 struct fat_header
*operator ->() const {
596 FatHeader
Map(const char *path
, bool ro
= false) {
598 void *base(map(path
, 0, _not(size_t), &size
, ro
));
599 return FatHeader(base
, size
);
602 template <typename Target_
>
605 const MachHeader
*framework_
;
606 const Target_
*pointer_
;
609 Pointer(const MachHeader
*framework
= NULL
, const Target_
*pointer
= NULL
) :
610 framework_(framework
),
615 operator const Target_
*() const {
619 const Target_
*operator ->() const {
623 Pointer
<Target_
> &operator ++() {
628 template <typename Value_
>
629 Value_
Swap(Value_ value
) {
630 return framework_
->Swap(value
);
634 #define CSMAGIC_CODEDIRECTORY uint32_t(0xfade0c02)
635 #define CSMAGIC_EMBEDDED_SIGNATURE uint32_t(0xfade0cc0)
636 #define CSMAGIC_ENTITLEMENTS uint32_t(0xfade7171)
638 #define CSSLOT_CODEDIRECTORY uint32_t(0)
639 #define CSSLOT_REQUIREMENTS uint32_t(2)
640 #define CSSLOT_ENTITLEMENTS uint32_t(5)
655 struct BlobIndex index
[];
658 struct CodeDirectory
{
663 uint32_t identOffset
;
664 uint32_t nSpecialSlots
;
674 extern "C" uint32_t hash(uint8_t *k
, uint32_t length
, uint32_t initval
);
676 void sha1(uint8_t *hash
, uint8_t *data
, size_t size
) {
679 SHA1Input(&context
, data
, size
);
680 SHA1Result(&context
, hash
);
683 struct CodesignAllocation
{
688 CodesignAllocation(uint32_t type
, uint16_t subtype
, size_t size
) :
696 int main(int argc
, const char *argv
[]) {
702 little_
= endian
.byte
[0];
725 uint32_t flag_CPUType(_not(uint32_t));
726 uint32_t flag_CPUSubtype(_not(uint32_t));
731 const void *xmld(NULL
);
734 uintptr_t noffset(_not(uintptr_t));
735 uintptr_t woffset(_not(uintptr_t));
737 std::vector
<std::string
> files
;
740 fprintf(stderr
, "usage: %s -S[entitlements.xml] <binary>\n", argv
[0]);
741 fprintf(stderr
, " %s -e MobileSafari\n", argv
[0]);
742 fprintf(stderr
, " %s -S cat\n", argv
[0]);
743 fprintf(stderr
, " %s -Stfp.xml gdb\n", argv
[0]);
747 for (int argi(1); argi
!= argc
; ++argi
)
748 if (argv
[argi
][0] != '-')
749 files
.push_back(argv
[argi
]);
750 else switch (argv
[argi
][1]) {
751 case 'R': flag_R
= true; break;
752 case 'r': flag_r
= true; break;
754 case 't': flag_t
= true; break;
755 case 'u': flag_u
= true; break;
756 case 'p': flag_p
= true; break;
757 case 'e': flag_e
= true; break;
758 case 'O': flag_O
= true; break;
760 case 'D': flag_D
= true; break;
761 case 'd': flag_d
= true; break;
763 case 'a': flag_a
= true; break;
767 if (argv
[argi
][2] != '\0') {
768 const char *cpu
= argv
[argi
] + 2;
769 const char *colon
= strchr(cpu
, ':');
770 _assert(colon
!= NULL
);
772 flag_CPUType
= strtoul(cpu
, &arge
, 0);
773 _assert(arge
== colon
);
774 flag_CPUSubtype
= strtoul(colon
+ 1, &arge
, 0);
775 _assert(arge
== argv
[argi
] + strlen(argv
[argi
]));
787 if (argv
[argi
][2] != '\0') {
788 const char *xml
= argv
[argi
] + 2;
789 xmld
= map(xml
, 0, _not(size_t), &xmls
, true);
795 if (argv
[argi
][2] == '-')
799 timev
= strtoul(argv
[argi
] + 2, &arge
, 0);
800 _assert(arge
== argv
[argi
] + strlen(argv
[argi
]));
806 noffset
= strtoul(argv
[argi
] + 2, &arge
, 0);
807 _assert(arge
== argv
[argi
] + strlen(argv
[argi
]));
812 woffset
= strtoul(argv
[argi
] + 2, &arge
, 0);
813 _assert(arge
== argv
[argi
] + strlen(argv
[argi
]));
821 if (files
.empty()) usage
: {
825 size_t filei(0), filee(0);
826 _foreach (file
, files
) try {
827 const char *path(file
.c_str());
828 const char *base
= strrchr(path
, '/');
829 char *temp(NULL
), *dir
;
832 dir
= strndup_(path
, base
++ - path
+ 1);
840 FatHeader
fat_header(Map(path
));
841 _foreach (mach_header
, fat_header
.GetMachHeaders()) {
843 if (mach_header
.GetCPUType() != flag_CPUType
)
845 if (mach_header
.GetCPUSubtype() != flag_CPUSubtype
)
849 mach_header
->flags
= mach_header
.Swap(mach_header
.Swap(mach_header
->flags
) | MH_DYLDLINK
);
851 uint32_t size(_not(uint32_t)); {
852 _foreach (load_command
, mach_header
.GetLoadCommands()) {
853 switch (mach_header
.Swap(load_command
->cmd
)) {
854 case LC_CODE_SIGNATURE
: {
855 struct linkedit_data_command
*signature
= reinterpret_cast<struct linkedit_data_command
*>(load_command
);
856 memset(reinterpret_cast<uint8_t *>(mach_header
.GetBase()) + mach_header
.Swap(signature
->dataoff
), 0, mach_header
.Swap(signature
->datasize
));
857 memset(signature
, 0, sizeof(struct linkedit_data_command
));
859 mach_header
->ncmds
= mach_header
.Swap(mach_header
.Swap(mach_header
->ncmds
) - 1);
860 mach_header
->sizeofcmds
= mach_header
.Swap(uint32_t(mach_header
.Swap(mach_header
->sizeofcmds
) - sizeof(struct linkedit_data_command
)));
864 struct symtab_command
*symtab
= reinterpret_cast<struct symtab_command
*>(load_command
);
865 size
= mach_header
.Swap(symtab
->stroff
) + mach_header
.Swap(symtab
->strsize
);
871 _assert(size
!= _not(uint32_t));
873 _foreach (segment
, const_cast<FatMachHeader
&>(mach_header
).GetSegments("__LINKEDIT")) {
874 segment
->filesize
-= mach_header
.GetSize() - size
;
876 if (fat_arch
*fat_arch
= mach_header
.GetFatArch()) {
877 fat_arch
->size
= fat_header
.Swap(size
);
878 clip
= std::max(clip
, fat_header
.Swap(fat_arch
->offset
) + size
);
880 clip
= std::max(clip
, size
);
883 _foreach (segment
, const_cast<FatMachHeader
&>(mach_header
).GetSegments64("__LINKEDIT")) {
884 segment
->filesize
-= mach_header
.GetSize() - size
;
886 if (fat_arch
*fat_arch
= mach_header
.GetFatArch()) {
887 fat_arch
->size
= fat_header
.Swap(size
);
888 clip
= std::max(clip
, fat_header
.Swap(fat_arch
->offset
) + size
);
890 clip
= std::max(clip
, size
);
896 _syscall(truncate(path
, clip
));
900 asprintf(&temp
, "%s.%s.cs", dir
, base
);
901 const char *allocate
= getenv("CODESIGN_ALLOCATE");
902 if (allocate
== NULL
)
903 allocate
= "codesign_allocate";
905 std::vector
<CodesignAllocation
> allocations
; {
906 FatHeader
fat_header(Map(path
));
907 _foreach (mach_header
, fat_header
.GetMachHeaders()) {
909 if (mach_header
.GetCPUType() != flag_CPUType
)
911 if (mach_header
.GetCPUSubtype() != flag_CPUSubtype
)
915 mach_header
->flags
= mach_header
.Swap(mach_header
.Swap(mach_header
->flags
) | MH_DYLDLINK
);
917 size_t size(_not(size_t)); {
918 _foreach (load_command
, mach_header
.GetLoadCommands()) {
919 uint32_t cmd(mach_header
.Swap(load_command
->cmd
));
920 if (cmd
== LC_CODE_SIGNATURE
) {
921 struct linkedit_data_command
*signature
= reinterpret_cast<struct linkedit_data_command
*>(load_command
);
922 size
= mach_header
.Swap(signature
->dataoff
);
923 _assert(size
< mach_header
.GetSize());
928 if (size
== _not(size_t))
929 size
= mach_header
.GetSize();
932 allocations
.push_back(CodesignAllocation(mach_header
.GetCPUType(), mach_header
.GetCPUSubtype(), size
));
936 if (!allocations
.empty()) {
941 // XXX: this leaks memory, but it doesn't really matter
942 std::vector
<const char *> args
;
945 args
.push_back(allocate
);
947 args
.push_back("-i");
948 args
.push_back(path
);
950 _foreach (allocation
, allocations
) {
951 if (allocation
.type_
== 12 && (
952 allocation
.subtype_
== 0 ||
953 allocation
.subtype_
== 6 ||
955 // Telesphoreo codesign_allocate
956 args
.push_back("-a");
959 switch (allocation
.subtype_
) {
971 _assert(arch
!= NULL
);
972 args
.push_back(arch
);
974 args
.push_back("-A");
976 asprintf(&arg
, "%u", allocation
.type_
);
979 asprintf(&arg
, "%u", allocation
.subtype_
);
984 alloc
+= sizeof(struct SuperBlob
);
987 special
= std::max(special
, CSSLOT_CODEDIRECTORY
);
988 alloc
+= sizeof(struct BlobIndex
);
989 alloc
+= sizeof(struct CodeDirectory
);
990 alloc
+= strlen(base
) + 1;
992 special
= std::max(special
, CSSLOT_REQUIREMENTS
);
993 alloc
+= sizeof(struct BlobIndex
);
997 special
= std::max(special
, CSSLOT_ENTITLEMENTS
);
998 alloc
+= sizeof(struct BlobIndex
);
999 alloc
+= sizeof(struct Blob
);
1003 size_t normal((allocation
.size_
+ 0x1000 - 1) / 0x1000);
1004 alloc
+= (special
+ normal
) * 0x14;
1010 asprintf(&arg
, "%zu", alloc
);
1011 args
.push_back(arg
);
1014 args
.push_back("-o");
1015 args
.push_back(temp
);
1017 args
.push_back(NULL
);
1021 _foreach (arg
, args
)
1026 execvp(allocate
, (char **) &args
[0]);
1031 _syscall(waitpid(pid
, &status
, 0));
1032 _assert(WIFEXITED(status
));
1033 _assert(WEXITSTATUS(status
) == 0);
1039 printf("path%zu='%s'\n", filei
, file
.c_str());
1041 FatHeader
fat_header(Map(temp
== NULL
? path
: temp
, !(flag_R
|| flag_T
|| flag_s
|| flag_S
|| flag_O
|| flag_D
)));
1042 struct linkedit_data_command
*signature(NULL
);
1044 _foreach (mach_header
, fat_header
.GetMachHeaders()) {
1046 if (mach_header
.GetCPUType() != flag_CPUType
)
1048 if (mach_header
.GetCPUSubtype() != flag_CPUSubtype
)
1053 printf("cpu=0x%x:0x%x\n", mach_header
.GetCPUType(), mach_header
.GetCPUSubtype());
1056 if (struct fat_arch
*fat_arch
= mach_header
.GetFatArch())
1057 printf("offset=0x%x\n", Swap(fat_arch
->offset
));
1059 printf("offset=0x0\n");
1062 if (woffset
!= _not(uintptr_t)) {
1063 Pointer
<uint32_t> wvalue(mach_header
.GetPointer
<uint32_t>(woffset
));
1065 printf("(null) %p\n", reinterpret_cast<void *>(woffset
));
1067 printf("0x%.08x\n", *wvalue
);
1070 if (noffset
!= _not(uintptr_t))
1071 printf("%s\n", &*mach_header
.GetPointer
<char>(noffset
));
1074 _foreach(segment
, mach_header
.GetSegments("__TEXT")) {
1075 printf("vmaddr=0x%x\n", mach_header
.Swap(segment
->vmaddr
));
1076 printf("fileoff=0x%x\n", mach_header
.Swap(segment
->fileoff
));
1080 _foreach(section
, mach_header
.GetSections("__TEXT", "__text"))
1081 section
->addr
= mach_header
.Swap(0);
1084 _foreach (load_command
, mach_header
.GetLoadCommands()) {
1085 uint32_t cmd(mach_header
.Swap(load_command
->cmd
));
1087 if (flag_R
&& cmd
== LC_REEXPORT_DYLIB
)
1088 load_command
->cmd
= mach_header
.Swap(LC_LOAD_DYLIB
);
1089 else if (cmd
== LC_CODE_SIGNATURE
)
1090 signature
= reinterpret_cast<struct linkedit_data_command
*>(load_command
);
1091 else if (cmd
== LC_UUID
) {
1092 volatile struct uuid_command
*uuid_command(reinterpret_cast<struct uuid_command
*>(load_command
));
1095 printf("uuid%zu=%.2x%.2x%.2x%.2x-%.2x%.2x-%.2x%.2x-%.2x%.2x-%.2x%.2x%.2x%.2x%.2x%.2x\n", filei
,
1096 uuid_command
->uuid
[ 0], uuid_command
->uuid
[ 1], uuid_command
->uuid
[ 2], uuid_command
->uuid
[ 3],
1097 uuid_command
->uuid
[ 4], uuid_command
->uuid
[ 5], uuid_command
->uuid
[ 6], uuid_command
->uuid
[ 7],
1098 uuid_command
->uuid
[ 8], uuid_command
->uuid
[ 9], uuid_command
->uuid
[10], uuid_command
->uuid
[11],
1099 uuid_command
->uuid
[12], uuid_command
->uuid
[13], uuid_command
->uuid
[14], uuid_command
->uuid
[15]
1102 } else if (cmd
== LC_ID_DYLIB
) {
1103 volatile struct dylib_command
*dylib_command(reinterpret_cast<struct dylib_command
*>(load_command
));
1106 printf("time%zu=0x%.8x\n", filei
, mach_header
.Swap(dylib_command
->dylib
.timestamp
));
1114 dylib_command
->dylib
.timestamp
= 0;
1115 timed
= hash(reinterpret_cast<uint8_t *>(mach_header
.GetBase()), mach_header
.GetSize(), timev
);
1118 dylib_command
->dylib
.timestamp
= mach_header
.Swap(timed
);
1120 } else if (cmd
== LC_ENCRYPTION_INFO
) {
1121 volatile struct encryption_info_command
*encryption_info_command(reinterpret_cast<struct encryption_info_command
*>(load_command
));
1124 encryption_info_command
->cryptid
= mach_header
.Swap(0);
1127 printf("cryptoff=0x%x\n", mach_header
.Swap(encryption_info_command
->cryptoff
));
1128 printf("cryptsize=0x%x\n", mach_header
.Swap(encryption_info_command
->cryptsize
));
1129 printf("cryptid=0x%x\n", mach_header
.Swap(encryption_info_command
->cryptid
));
1135 _assert(signature
!= NULL
);
1137 uint32_t data
= mach_header
.Swap(signature
->dataoff
);
1139 uint8_t *top
= reinterpret_cast<uint8_t *>(mach_header
.GetBase());
1140 uint8_t *blob
= top
+ data
;
1141 struct SuperBlob
*super
= reinterpret_cast<struct SuperBlob
*>(blob
);
1143 for (size_t index(0); index
!= Swap(super
->count
); ++index
)
1144 if (Swap(super
->index
[index
].type
) == CSSLOT_ENTITLEMENTS
) {
1145 uint32_t begin
= Swap(super
->index
[index
].offset
);
1146 struct Blob
*entitlements
= reinterpret_cast<struct Blob
*>(blob
+ begin
);
1147 fwrite(entitlements
+ 1, 1, Swap(entitlements
->length
) - sizeof(struct Blob
), stdout
);
1152 _assert(signature
!= NULL
);
1154 uint32_t data
= mach_header
.Swap(signature
->dataoff
);
1156 uint8_t *top
= reinterpret_cast<uint8_t *>(mach_header
.GetBase());
1157 uint8_t *blob
= top
+ data
;
1158 struct SuperBlob
*super
= reinterpret_cast<struct SuperBlob
*>(blob
);
1160 for (size_t index(0); index
!= Swap(super
->count
); ++index
)
1161 if (Swap(super
->index
[index
].type
) == CSSLOT_CODEDIRECTORY
) {
1162 uint32_t begin
= Swap(super
->index
[index
].offset
);
1163 struct CodeDirectory
*directory
= reinterpret_cast<struct CodeDirectory
*>(blob
+ begin
);
1165 uint8_t (*hashes
)[20] = reinterpret_cast<uint8_t (*)[20]>(blob
+ begin
+ Swap(directory
->hashOffset
));
1166 uint32_t pages
= Swap(directory
->nCodeSlots
);
1169 for (size_t i
= 0; i
!= pages
- 1; ++i
)
1170 sha1(hashes
[i
], top
+ 0x1000 * i
, 0x1000);
1172 sha1(hashes
[pages
- 1], top
+ 0x1000 * (pages
- 1), ((data
- 1) % 0x1000) + 1);
1177 _assert(signature
!= NULL
);
1179 uint32_t data
= mach_header
.Swap(signature
->dataoff
);
1180 uint32_t size
= mach_header
.Swap(signature
->datasize
);
1182 uint8_t *top
= reinterpret_cast<uint8_t *>(mach_header
.GetBase());
1183 uint8_t *blob
= top
+ data
;
1184 struct SuperBlob
*super
= reinterpret_cast<struct SuperBlob
*>(blob
);
1185 super
->blob
.magic
= Swap(CSMAGIC_EMBEDDED_SIGNATURE
);
1187 uint32_t count
= xmld
== NULL
? 2 : 3;
1188 uint32_t offset
= sizeof(struct SuperBlob
) + count
* sizeof(struct BlobIndex
);
1190 super
->index
[0].type
= Swap(CSSLOT_CODEDIRECTORY
);
1191 super
->index
[0].offset
= Swap(offset
);
1193 uint32_t begin
= offset
;
1194 struct CodeDirectory
*directory
= reinterpret_cast<struct CodeDirectory
*>(blob
+ begin
);
1195 offset
+= sizeof(struct CodeDirectory
);
1197 directory
->blob
.magic
= Swap(CSMAGIC_CODEDIRECTORY
);
1198 directory
->version
= Swap(uint32_t(0x00020001));
1199 directory
->flags
= Swap(uint32_t(0));
1200 directory
->codeLimit
= Swap(data
);
1201 directory
->hashSize
= 0x14;
1202 directory
->hashType
= 0x01;
1203 directory
->spare1
= 0x00;
1204 directory
->pageSize
= 0x0c;
1205 directory
->spare2
= Swap(uint32_t(0));
1207 directory
->identOffset
= Swap(offset
- begin
);
1208 strcpy(reinterpret_cast<char *>(blob
+ offset
), base
);
1209 offset
+= strlen(base
) + 1;
1211 uint32_t special
= xmld
== NULL
? CSSLOT_REQUIREMENTS
: CSSLOT_ENTITLEMENTS
;
1212 directory
->nSpecialSlots
= Swap(special
);
1214 uint8_t (*hashes
)[20] = reinterpret_cast<uint8_t (*)[20]>(blob
+ offset
);
1215 memset(hashes
, 0, sizeof(*hashes
) * special
);
1217 offset
+= sizeof(*hashes
) * special
;
1220 uint32_t pages
= (data
+ 0x1000 - 1) / 0x1000;
1221 directory
->nCodeSlots
= Swap(pages
);
1224 for (size_t i
= 0; i
!= pages
- 1; ++i
)
1225 sha1(hashes
[i
], top
+ 0x1000 * i
, 0x1000);
1227 sha1(hashes
[pages
- 1], top
+ 0x1000 * (pages
- 1), ((data
- 1) % 0x1000) + 1);
1229 directory
->hashOffset
= Swap(offset
- begin
);
1230 offset
+= sizeof(*hashes
) * pages
;
1231 directory
->blob
.length
= Swap(offset
- begin
);
1233 super
->index
[1].type
= Swap(CSSLOT_REQUIREMENTS
);
1234 super
->index
[1].offset
= Swap(offset
);
1236 memcpy(blob
+ offset
, "\xfa\xde\x0c\x01\x00\x00\x00\x0c\x00\x00\x00\x00", 0xc);
1240 super
->index
[2].type
= Swap(CSSLOT_ENTITLEMENTS
);
1241 super
->index
[2].offset
= Swap(offset
);
1243 uint32_t begin
= offset
;
1244 struct Blob
*entitlements
= reinterpret_cast<struct Blob
*>(blob
+ begin
);
1245 offset
+= sizeof(struct Blob
);
1247 memcpy(blob
+ offset
, xmld
, xmls
);
1250 entitlements
->magic
= Swap(CSMAGIC_ENTITLEMENTS
);
1251 entitlements
->length
= Swap(offset
- begin
);
1254 for (size_t index(0); index
!= count
; ++index
) {
1255 uint32_t type
= Swap(super
->index
[index
].type
);
1256 if (type
!= 0 && type
<= special
) {
1257 uint32_t offset
= Swap(super
->index
[index
].offset
);
1258 struct Blob
*local
= (struct Blob
*) (blob
+ offset
);
1259 sha1((uint8_t *) (hashes
- type
), (uint8_t *) local
, Swap(local
->length
));
1263 super
->count
= Swap(count
);
1264 super
->blob
.length
= Swap(offset
);
1266 if (offset
> size
) {
1267 fprintf(stderr
, "offset (%u) > size (%u)\n", offset
, size
);
1269 } //else fprintf(stderr, "offset (%zu) <= size (%zu)\n", offset, size);
1271 memset(blob
+ offset
, 0, size
- offset
);
1276 uint8_t *top
= reinterpret_cast<uint8_t *>(fat_header
.GetBase());
1277 size_t size
= fat_header
.GetSize();
1280 asprintf(©
, "%s.%s.cp", dir
, base
);
1281 FILE *file
= fopen(copy
, "w+");
1282 size_t writ
= fwrite(top
, 1, size
, file
);
1283 _assert(writ
== size
);
1286 _syscall(unlink(temp
));
1293 _syscall(stat(path
, &info
));
1294 _syscall(chown(temp
, info
.st_uid
, info
.st_gid
));
1295 _syscall(chmod(temp
, info
.st_mode
));
1296 _syscall(unlink(path
));
1297 _syscall(rename(temp
, path
));
1303 } catch (const char *) {