1 /* ldid - (Mach-O) Link-Loader Identity Editor
2 * Copyright (C) 2007-2012 Jay Freeman (saurik)
5 /* GNU Lesser General Public License, Version 3 {{{ */
6 /* This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "minimal/stdlib.h"
22 #include "minimal/string.h"
23 #include "minimal/mapping.h"
32 #include <sys/types.h>
40 #define FAT_MAGIC 0xcafebabe
41 #define FAT_CIGAM 0xbebafeca
61 #define MH_MAGIC 0xfeedface
62 #define MH_CIGAM 0xcefaedfe
64 #define MH_MAGIC_64 0xfeedfacf
65 #define MH_CIGAM_64 0xcffaedfe
67 #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 uint16_t Swap_(uint16_t value
) {
281 ((value
>> 8) & 0x00ff) |
282 ((value
<< 8) & 0xff00);
285 uint32_t Swap_(uint32_t value
) {
286 value
= ((value
>> 8) & 0x00ff00ff) |
287 ((value
<< 8) & 0xff00ff00);
288 value
= ((value
>> 16) & 0x0000ffff) |
289 ((value
<< 16) & 0xffff0000);
293 int16_t Swap_(int16_t value
) {
294 return Swap_(static_cast<uint16_t>(value
));
297 int32_t Swap_(int32_t value
) {
298 return Swap_(static_cast<uint32_t>(value
));
303 uint16_t Swap(uint16_t value
) {
304 return little_
? Swap_(value
) : value
;
307 uint32_t Swap(uint32_t value
) {
308 return little_
? Swap_(value
) : value
;
311 int16_t Swap(int16_t value
) {
312 return Swap(static_cast<uint16_t>(value
));
315 int32_t Swap(int32_t value
) {
316 return Swap(static_cast<uint32_t>(value
));
319 template <typename Target_
>
331 Data(void *base
, size_t size
) :
338 uint16_t Swap(uint16_t value
) const {
339 return swapped_
? Swap_(value
) : value
;
342 uint32_t Swap(uint32_t value
) const {
343 return swapped_
? Swap_(value
) : value
;
346 int16_t Swap(int16_t value
) const {
347 return Swap(static_cast<uint16_t>(value
));
350 int32_t Swap(int32_t value
) const {
351 return Swap(static_cast<uint32_t>(value
));
354 void *GetBase() const {
358 size_t GetSize() const {
369 struct mach_header
*mach_header_
;
370 struct load_command
*load_command_
;
373 MachHeader(void *base
, size_t size
) :
376 mach_header_
= (mach_header
*) base
;
378 switch (Swap(mach_header_
->magic
)) {
380 swapped_
= !swapped_
;
386 swapped_
= !swapped_
;
395 void *post
= mach_header_
+ 1;
397 post
= (uint32_t *) post
+ 1;
398 load_command_
= (struct load_command
*) post
;
401 Swap(mach_header_
->filetype
) == MH_EXECUTE
||
402 Swap(mach_header_
->filetype
) == MH_DYLIB
||
403 Swap(mach_header_
->filetype
) == MH_BUNDLE
407 struct mach_header
*operator ->() const {
411 uint32_t GetCPUType() const {
412 return Swap(mach_header_
->cputype
);
415 uint16_t GetCPUSubtype() const {
416 return Swap(mach_header_
->cpusubtype
) & 0xff;
419 std::vector
<struct load_command
*> GetLoadCommands() const {
420 std::vector
<struct load_command
*> load_commands
;
422 struct load_command
*load_command
= load_command_
;
423 for (uint32_t cmd
= 0; cmd
!= Swap(mach_header_
->ncmds
); ++cmd
) {
424 load_commands
.push_back(load_command
);
425 load_command
= (struct load_command
*) ((uint8_t *) load_command
+ Swap(load_command
->cmdsize
));
428 return load_commands
;
431 std::vector
<segment_command
*> GetSegments(const char *segment_name
) const {
432 std::vector
<struct segment_command
*> segment_commands
;
434 _foreach (load_command
, GetLoadCommands()) {
435 if (Swap(load_command
->cmd
) == LC_SEGMENT
) {
436 segment_command
*segment_command
= reinterpret_cast<struct segment_command
*>(load_command
);
437 if (strncmp(segment_command
->segname
, segment_name
, 16) == 0)
438 segment_commands
.push_back(segment_command
);
442 return segment_commands
;
445 std::vector
<segment_command_64
*> GetSegments64(const char *segment_name
) {
446 std::vector
<struct segment_command_64
*> segment_commands
;
448 _foreach (load_command
, GetLoadCommands()) {
449 if (Swap(load_command
->cmd
) == LC_SEGMENT_64
) {
450 segment_command_64
*segment_command
= reinterpret_cast<struct segment_command_64
*>(load_command
);
451 if (strncmp(segment_command
->segname
, segment_name
, 16) == 0)
452 segment_commands
.push_back(segment_command
);
456 return segment_commands
;
459 std::vector
<section
*> GetSections(const char *segment_name
, const char *section_name
) const {
460 std::vector
<section
*> sections
;
462 _foreach (segment
, GetSegments(segment_name
)) {
463 section
*section
= (struct section
*) (segment
+ 1);
466 for (sect
= 0; sect
!= Swap(segment
->nsects
); ++sect
) {
467 if (strncmp(section
->sectname
, section_name
, 16) == 0)
468 sections
.push_back(section
);
476 template <typename Target_
>
477 Pointer
<Target_
> GetPointer(uint32_t address
, const char *segment_name
= NULL
) const {
478 load_command
*load_command
= (struct load_command
*) (mach_header_
+ 1);
481 for (cmd
= 0; cmd
!= Swap(mach_header_
->ncmds
); ++cmd
) {
482 if (Swap(load_command
->cmd
) == LC_SEGMENT
) {
483 segment_command
*segment_command
= (struct segment_command
*) load_command
;
484 if (segment_name
!= NULL
&& strncmp(segment_command
->segname
, segment_name
, 16) != 0)
487 section
*sections
= (struct section
*) (segment_command
+ 1);
490 for (sect
= 0; sect
!= Swap(segment_command
->nsects
); ++sect
) {
491 section
*section
= §ions
[sect
];
492 //printf("%s %u %p %p %u\n", segment_command->segname, sect, address, section->addr, section->size);
493 if (address
>= Swap(section
->addr
) && address
< Swap(section
->addr
) + Swap(section
->size
)) {
494 //printf("0x%.8x %s\n", address, segment_command->segname);
495 return Pointer
<Target_
>(this, reinterpret_cast<Target_
*>(address
- Swap(section
->addr
) + Swap(section
->offset
) + (char *) mach_header_
));
501 load_command
= (struct load_command
*) ((char *) load_command
+ Swap(load_command
->cmdsize
));
504 return Pointer
<Target_
>(this);
507 template <typename Target_
>
508 Pointer
<Target_
> GetOffset(uint32_t offset
) {
509 return Pointer
<Target_
>(this, reinterpret_cast<Target_
*>(offset
+ (uint8_t *) mach_header_
));
513 class FatMachHeader
:
520 FatMachHeader(void *base
, size_t size
, fat_arch
*fat_arch
) :
521 MachHeader(base
, size
),
526 fat_arch
*GetFatArch() const {
535 fat_header
*fat_header_
;
536 std::vector
<FatMachHeader
> mach_headers_
;
539 FatHeader(void *base
, size_t size
) :
542 fat_header_
= reinterpret_cast<struct fat_header
*>(base
);
544 if (Swap(fat_header_
->magic
) == FAT_CIGAM
) {
545 swapped_
= !swapped_
;
547 } else if (Swap(fat_header_
->magic
) != FAT_MAGIC
) {
549 mach_headers_
.push_back(FatMachHeader(base
, size
, NULL
));
551 size_t fat_narch
= Swap(fat_header_
->nfat_arch
);
552 fat_arch
*fat_arch
= reinterpret_cast<struct fat_arch
*>(fat_header_
+ 1);
554 for (arch
= 0; arch
!= fat_narch
; ++arch
) {
555 uint32_t arch_offset
= Swap(fat_arch
->offset
);
556 uint32_t arch_size
= Swap(fat_arch
->size
);
557 mach_headers_
.push_back(FatMachHeader((uint8_t *) base
+ arch_offset
, arch_size
, fat_arch
));
563 std::vector
<FatMachHeader
> &GetMachHeaders() {
564 return mach_headers_
;
568 return fat_header_
!= NULL
;
571 struct fat_header
*operator ->() const {
576 FatHeader
Map(const char *path
, bool ro
= false) {
578 void *base(map(path
, 0, _not(size_t), &size
, ro
));
579 return FatHeader(base
, size
);
582 template <typename Target_
>
585 const MachHeader
*framework_
;
586 const Target_
*pointer_
;
589 Pointer(const MachHeader
*framework
= NULL
, const Target_
*pointer
= NULL
) :
590 framework_(framework
),
595 operator const Target_
*() const {
599 const Target_
*operator ->() const {
603 Pointer
<Target_
> &operator ++() {
608 template <typename Value_
>
609 Value_
Swap(Value_ value
) {
610 return framework_
->Swap(value
);
614 #define CSMAGIC_CODEDIRECTORY uint32_t(0xfade0c02)
615 #define CSMAGIC_EMBEDDED_SIGNATURE uint32_t(0xfade0cc0)
616 #define CSMAGIC_ENTITLEMENTS uint32_t(0xfade7171)
618 #define CSSLOT_CODEDIRECTORY uint32_t(0)
619 #define CSSLOT_REQUIREMENTS uint32_t(2)
620 #define CSSLOT_ENTITLEMENTS uint32_t(5)
635 struct BlobIndex index
[];
638 struct CodeDirectory
{
643 uint32_t identOffset
;
644 uint32_t nSpecialSlots
;
654 extern "C" uint32_t hash(uint8_t *k
, uint32_t length
, uint32_t initval
);
656 void sha1(uint8_t *hash
, uint8_t *data
, size_t size
) {
659 SHA1Input(&context
, data
, size
);
660 SHA1Result(&context
, hash
);
663 struct CodesignAllocation
{
668 CodesignAllocation(uint32_t type
, uint16_t subtype
, size_t size
) :
676 int main(int argc
, const char *argv
[]) {
682 little_
= endian
.byte
[0];
705 uint32_t flag_CPUType(_not(uint32_t));
706 uint32_t flag_CPUSubtype(_not(uint32_t));
711 const void *xmld(NULL
);
714 uintptr_t noffset(_not(uintptr_t));
715 uintptr_t woffset(_not(uintptr_t));
717 std::vector
<std::string
> files
;
720 fprintf(stderr
, "usage: %s -S[entitlements.xml] <binary>\n", argv
[0]);
721 fprintf(stderr
, " %s -e MobileSafari\n", argv
[0]);
722 fprintf(stderr
, " %s -S cat\n", argv
[0]);
723 fprintf(stderr
, " %s -Stfp.xml gdb\n", argv
[0]);
727 for (int argi(1); argi
!= argc
; ++argi
)
728 if (argv
[argi
][0] != '-')
729 files
.push_back(argv
[argi
]);
730 else switch (argv
[argi
][1]) {
731 case 'R': flag_R
= true; break;
732 case 'r': flag_r
= true; break;
734 case 't': flag_t
= true; break;
735 case 'u': flag_u
= true; break;
736 case 'p': flag_p
= true; break;
737 case 'e': flag_e
= true; break;
738 case 'O': flag_O
= true; break;
740 case 'D': flag_D
= true; break;
741 case 'd': flag_d
= true; break;
743 case 'a': flag_a
= true; break;
747 if (argv
[argi
][2] != '\0') {
748 const char *cpu
= argv
[argi
] + 2;
749 const char *colon
= strchr(cpu
, ':');
750 _assert(colon
!= NULL
);
752 flag_CPUType
= strtoul(cpu
, &arge
, 0);
753 _assert(arge
== colon
);
754 flag_CPUSubtype
= strtoul(colon
+ 1, &arge
, 0);
755 _assert(arge
== argv
[argi
] + strlen(argv
[argi
]));
767 if (argv
[argi
][2] != '\0') {
768 const char *xml
= argv
[argi
] + 2;
769 xmld
= map(xml
, 0, _not(size_t), &xmls
, true);
775 if (argv
[argi
][2] == '-')
779 timev
= strtoul(argv
[argi
] + 2, &arge
, 0);
780 _assert(arge
== argv
[argi
] + strlen(argv
[argi
]));
786 noffset
= strtoul(argv
[argi
] + 2, &arge
, 0);
787 _assert(arge
== argv
[argi
] + strlen(argv
[argi
]));
792 woffset
= strtoul(argv
[argi
] + 2, &arge
, 0);
793 _assert(arge
== argv
[argi
] + strlen(argv
[argi
]));
801 if (files
.empty()) usage
: {
805 size_t filei(0), filee(0);
806 _foreach (file
, files
) try {
807 const char *path(file
.c_str());
808 const char *base
= strrchr(path
, '/');
809 char *temp(NULL
), *dir
;
812 dir
= strndup_(path
, base
++ - path
+ 1);
820 FatHeader
fat_header(Map(path
));
821 _foreach (mach_header
, fat_header
.GetMachHeaders()) {
823 if (mach_header
.GetCPUType() != flag_CPUType
)
825 if (mach_header
.GetCPUSubtype() != flag_CPUSubtype
)
829 mach_header
->flags
= mach_header
.Swap(mach_header
.Swap(mach_header
->flags
) | MH_DYLDLINK
);
831 uint32_t size(_not(uint32_t)); {
832 _foreach (load_command
, mach_header
.GetLoadCommands()) {
833 switch (mach_header
.Swap(load_command
->cmd
)) {
834 case LC_CODE_SIGNATURE
: {
835 struct linkedit_data_command
*signature
= reinterpret_cast<struct linkedit_data_command
*>(load_command
);
836 memset(reinterpret_cast<uint8_t *>(mach_header
.GetBase()) + mach_header
.Swap(signature
->dataoff
), 0, mach_header
.Swap(signature
->datasize
));
837 memset(signature
, 0, sizeof(struct linkedit_data_command
));
839 mach_header
->ncmds
= mach_header
.Swap(mach_header
.Swap(mach_header
->ncmds
) - 1);
840 mach_header
->sizeofcmds
= mach_header
.Swap(uint32_t(mach_header
.Swap(mach_header
->sizeofcmds
) - sizeof(struct linkedit_data_command
)));
844 struct symtab_command
*symtab
= reinterpret_cast<struct symtab_command
*>(load_command
);
845 size
= mach_header
.Swap(symtab
->stroff
) + mach_header
.Swap(symtab
->strsize
);
851 _assert(size
!= _not(uint32_t));
853 _foreach (segment
, const_cast<FatMachHeader
&>(mach_header
).GetSegments("__LINKEDIT")) {
854 segment
->filesize
-= mach_header
.GetSize() - size
;
856 if (fat_arch
*fat_arch
= mach_header
.GetFatArch()) {
857 fat_arch
->size
= fat_header
.Swap(size
);
858 clip
= std::max(clip
, fat_header
.Swap(fat_arch
->offset
) + size
);
860 clip
= std::max(clip
, size
);
863 _foreach (segment
, const_cast<FatMachHeader
&>(mach_header
).GetSegments64("__LINKEDIT")) {
864 segment
->filesize
-= mach_header
.GetSize() - size
;
866 if (fat_arch
*fat_arch
= mach_header
.GetFatArch()) {
867 fat_arch
->size
= fat_header
.Swap(size
);
868 clip
= std::max(clip
, fat_header
.Swap(fat_arch
->offset
) + size
);
870 clip
= std::max(clip
, size
);
876 _syscall(truncate(path
, clip
));
880 asprintf(&temp
, "%s.%s.cs", dir
, base
);
881 const char *allocate
= getenv("CODESIGN_ALLOCATE");
882 if (allocate
== NULL
)
883 allocate
= "codesign_allocate";
885 std::vector
<CodesignAllocation
> allocations
; {
886 FatHeader
fat_header(Map(path
));
887 _foreach (mach_header
, fat_header
.GetMachHeaders()) {
889 if (mach_header
.GetCPUType() != flag_CPUType
)
891 if (mach_header
.GetCPUSubtype() != flag_CPUSubtype
)
895 mach_header
->flags
= mach_header
.Swap(mach_header
.Swap(mach_header
->flags
) | MH_DYLDLINK
);
897 size_t size(_not(size_t)); {
898 _foreach (load_command
, mach_header
.GetLoadCommands()) {
899 uint32_t cmd(mach_header
.Swap(load_command
->cmd
));
900 if (cmd
== LC_CODE_SIGNATURE
) {
901 struct linkedit_data_command
*signature
= reinterpret_cast<struct linkedit_data_command
*>(load_command
);
902 size
= mach_header
.Swap(signature
->dataoff
);
903 _assert(size
< mach_header
.GetSize());
908 if (size
== _not(size_t))
909 size
= mach_header
.GetSize();
912 allocations
.push_back(CodesignAllocation(mach_header
.GetCPUType(), mach_header
.GetCPUSubtype(), size
));
916 if (!allocations
.empty()) {
921 // XXX: this leaks memory, but it doesn't really matter
922 std::vector
<const char *> args
;
925 args
.push_back(allocate
);
927 args
.push_back("-i");
928 args
.push_back(path
);
930 _foreach (allocation
, allocations
) {
931 args
.push_back("-A");
933 asprintf(&arg
, "%u", allocation
.type_
);
936 asprintf(&arg
, "%u", allocation
.subtype_
);
940 alloc
+= sizeof(struct SuperBlob
);
943 special
= std::max(special
, CSSLOT_CODEDIRECTORY
);
944 alloc
+= sizeof(struct BlobIndex
);
945 alloc
+= sizeof(struct CodeDirectory
);
946 alloc
+= strlen(base
) + 1;
948 special
= std::max(special
, CSSLOT_REQUIREMENTS
);
949 alloc
+= sizeof(struct BlobIndex
);
953 special
= std::max(special
, CSSLOT_ENTITLEMENTS
);
954 alloc
+= sizeof(struct BlobIndex
);
955 alloc
+= sizeof(struct Blob
);
959 size_t normal((allocation
.size_
+ 0x1000 - 1) / 0x1000);
960 alloc
+= (special
+ normal
) * 0x14;
966 asprintf(&arg
, "%zu", alloc
);
970 args
.push_back("-o");
971 args
.push_back(temp
);
973 args
.push_back(NULL
);
982 execvp(allocate
, (char **) &args
[0]);
987 _syscall(waitpid(pid
, &status
, 0));
988 _assert(WIFEXITED(status
));
989 _assert(WEXITSTATUS(status
) == 0);
995 printf("path%zu='%s'\n", filei
, file
.c_str());
997 FatHeader
fat_header(Map(temp
== NULL
? path
: temp
, !(flag_R
| flag_T
| flag_s
| flag_S
| flag_O
| flag_D
)));
998 struct linkedit_data_command
*signature(NULL
);
1000 _foreach (mach_header
, fat_header
.GetMachHeaders()) {
1002 if (mach_header
.GetCPUType() != flag_CPUType
)
1004 if (mach_header
.GetCPUSubtype() != flag_CPUSubtype
)
1009 printf("cpu=0x%x:0x%x\n", mach_header
.GetCPUType(), mach_header
.GetCPUSubtype());
1012 if (struct fat_arch
*fat_arch
= mach_header
.GetFatArch())
1013 printf("offset=0x%x\n", Swap(fat_arch
->offset
));
1015 printf("offset=0x0\n");
1018 if (woffset
!= _not(uintptr_t)) {
1019 Pointer
<uint32_t> wvalue(mach_header
.GetPointer
<uint32_t>(woffset
));
1021 printf("(null) %p\n", reinterpret_cast<void *>(woffset
));
1023 printf("0x%.08x\n", *wvalue
);
1026 if (noffset
!= _not(uintptr_t))
1027 printf("%s\n", &*mach_header
.GetPointer
<char>(noffset
));
1030 _foreach(segment
, mach_header
.GetSegments("__TEXT")) {
1031 printf("vmaddr=0x%x\n", mach_header
.Swap(segment
->vmaddr
));
1032 printf("fileoff=0x%x\n", mach_header
.Swap(segment
->fileoff
));
1036 _foreach(section
, mach_header
.GetSections("__TEXT", "__text"))
1037 section
->addr
= mach_header
.Swap(0);
1040 _foreach (load_command
, mach_header
.GetLoadCommands()) {
1041 uint32_t cmd(mach_header
.Swap(load_command
->cmd
));
1043 if (flag_R
&& cmd
== LC_REEXPORT_DYLIB
)
1044 load_command
->cmd
= mach_header
.Swap(LC_LOAD_DYLIB
);
1045 else if (cmd
== LC_CODE_SIGNATURE
)
1046 signature
= reinterpret_cast<struct linkedit_data_command
*>(load_command
);
1047 else if (cmd
== LC_UUID
) {
1048 volatile struct uuid_command
*uuid_command(reinterpret_cast<struct uuid_command
*>(load_command
));
1051 printf("uuid%zu=%.2x%.2x%.2x%.2x-%.2x%.2x-%.2x%.2x-%.2x%.2x-%.2x%.2x%.2x%.2x%.2x%.2x\n", filei
,
1052 uuid_command
->uuid
[ 0], uuid_command
->uuid
[ 1], uuid_command
->uuid
[ 2], uuid_command
->uuid
[ 3],
1053 uuid_command
->uuid
[ 4], uuid_command
->uuid
[ 5], uuid_command
->uuid
[ 6], uuid_command
->uuid
[ 7],
1054 uuid_command
->uuid
[ 8], uuid_command
->uuid
[ 9], uuid_command
->uuid
[10], uuid_command
->uuid
[11],
1055 uuid_command
->uuid
[12], uuid_command
->uuid
[13], uuid_command
->uuid
[14], uuid_command
->uuid
[15]
1058 } else if (cmd
== LC_ID_DYLIB
) {
1059 volatile struct dylib_command
*dylib_command(reinterpret_cast<struct dylib_command
*>(load_command
));
1062 printf("time%zu=0x%.8x\n", filei
, mach_header
.Swap(dylib_command
->dylib
.timestamp
));
1070 dylib_command
->dylib
.timestamp
= 0;
1071 timed
= hash(reinterpret_cast<uint8_t *>(mach_header
.GetBase()), mach_header
.GetSize(), timev
);
1074 dylib_command
->dylib
.timestamp
= mach_header
.Swap(timed
);
1076 } else if (cmd
== LC_ENCRYPTION_INFO
) {
1077 volatile struct encryption_info_command
*encryption_info_command(reinterpret_cast<struct encryption_info_command
*>(load_command
));
1080 encryption_info_command
->cryptid
= mach_header
.Swap(0);
1083 printf("cryptoff=0x%x\n", mach_header
.Swap(encryption_info_command
->cryptoff
));
1084 printf("cryptsize=0x%x\n", mach_header
.Swap(encryption_info_command
->cryptsize
));
1085 printf("cryptid=0x%x\n", mach_header
.Swap(encryption_info_command
->cryptid
));
1091 _assert(signature
!= NULL
);
1093 uint32_t data
= mach_header
.Swap(signature
->dataoff
);
1095 uint8_t *top
= reinterpret_cast<uint8_t *>(mach_header
.GetBase());
1096 uint8_t *blob
= top
+ data
;
1097 struct SuperBlob
*super
= reinterpret_cast<struct SuperBlob
*>(blob
);
1099 for (size_t index(0); index
!= Swap(super
->count
); ++index
)
1100 if (Swap(super
->index
[index
].type
) == CSSLOT_ENTITLEMENTS
) {
1101 uint32_t begin
= Swap(super
->index
[index
].offset
);
1102 struct Blob
*entitlements
= reinterpret_cast<struct Blob
*>(blob
+ begin
);
1103 fwrite(entitlements
+ 1, 1, Swap(entitlements
->length
) - sizeof(struct Blob
), stdout
);
1108 _assert(signature
!= NULL
);
1110 uint32_t data
= mach_header
.Swap(signature
->dataoff
);
1112 uint8_t *top
= reinterpret_cast<uint8_t *>(mach_header
.GetBase());
1113 uint8_t *blob
= top
+ data
;
1114 struct SuperBlob
*super
= reinterpret_cast<struct SuperBlob
*>(blob
);
1116 for (size_t index(0); index
!= Swap(super
->count
); ++index
)
1117 if (Swap(super
->index
[index
].type
) == CSSLOT_CODEDIRECTORY
) {
1118 uint32_t begin
= Swap(super
->index
[index
].offset
);
1119 struct CodeDirectory
*directory
= reinterpret_cast<struct CodeDirectory
*>(blob
+ begin
);
1121 uint8_t (*hashes
)[20] = reinterpret_cast<uint8_t (*)[20]>(blob
+ begin
+ Swap(directory
->hashOffset
));
1122 uint32_t pages
= Swap(directory
->nCodeSlots
);
1125 for (size_t i
= 0; i
!= pages
- 1; ++i
)
1126 sha1(hashes
[i
], top
+ 0x1000 * i
, 0x1000);
1128 sha1(hashes
[pages
- 1], top
+ 0x1000 * (pages
- 1), ((data
- 1) % 0x1000) + 1);
1133 _assert(signature
!= NULL
);
1135 uint32_t data
= mach_header
.Swap(signature
->dataoff
);
1136 uint32_t size
= mach_header
.Swap(signature
->datasize
);
1138 uint8_t *top
= reinterpret_cast<uint8_t *>(mach_header
.GetBase());
1139 uint8_t *blob
= top
+ data
;
1140 struct SuperBlob
*super
= reinterpret_cast<struct SuperBlob
*>(blob
);
1141 super
->blob
.magic
= Swap(CSMAGIC_EMBEDDED_SIGNATURE
);
1143 uint32_t count
= xmld
== NULL
? 2 : 3;
1144 uint32_t offset
= sizeof(struct SuperBlob
) + count
* sizeof(struct BlobIndex
);
1146 super
->index
[0].type
= Swap(CSSLOT_CODEDIRECTORY
);
1147 super
->index
[0].offset
= Swap(offset
);
1149 uint32_t begin
= offset
;
1150 struct CodeDirectory
*directory
= reinterpret_cast<struct CodeDirectory
*>(blob
+ begin
);
1151 offset
+= sizeof(struct CodeDirectory
);
1153 directory
->blob
.magic
= Swap(CSMAGIC_CODEDIRECTORY
);
1154 directory
->version
= Swap(uint32_t(0x00020001));
1155 directory
->flags
= Swap(uint32_t(0));
1156 directory
->codeLimit
= Swap(data
);
1157 directory
->hashSize
= 0x14;
1158 directory
->hashType
= 0x01;
1159 directory
->spare1
= 0x00;
1160 directory
->pageSize
= 0x0c;
1161 directory
->spare2
= Swap(uint32_t(0));
1163 directory
->identOffset
= Swap(offset
- begin
);
1164 strcpy(reinterpret_cast<char *>(blob
+ offset
), base
);
1165 offset
+= strlen(base
) + 1;
1167 uint32_t special
= xmld
== NULL
? CSSLOT_REQUIREMENTS
: CSSLOT_ENTITLEMENTS
;
1168 directory
->nSpecialSlots
= Swap(special
);
1170 uint8_t (*hashes
)[20] = reinterpret_cast<uint8_t (*)[20]>(blob
+ offset
);
1171 memset(hashes
, 0, sizeof(*hashes
) * special
);
1173 offset
+= sizeof(*hashes
) * special
;
1176 uint32_t pages
= (data
+ 0x1000 - 1) / 0x1000;
1177 directory
->nCodeSlots
= Swap(pages
);
1180 for (size_t i
= 0; i
!= pages
- 1; ++i
)
1181 sha1(hashes
[i
], top
+ 0x1000 * i
, 0x1000);
1183 sha1(hashes
[pages
- 1], top
+ 0x1000 * (pages
- 1), ((data
- 1) % 0x1000) + 1);
1185 directory
->hashOffset
= Swap(offset
- begin
);
1186 offset
+= sizeof(*hashes
) * pages
;
1187 directory
->blob
.length
= Swap(offset
- begin
);
1189 super
->index
[1].type
= Swap(CSSLOT_REQUIREMENTS
);
1190 super
->index
[1].offset
= Swap(offset
);
1192 memcpy(blob
+ offset
, "\xfa\xde\x0c\x01\x00\x00\x00\x0c\x00\x00\x00\x00", 0xc);
1196 super
->index
[2].type
= Swap(CSSLOT_ENTITLEMENTS
);
1197 super
->index
[2].offset
= Swap(offset
);
1199 uint32_t begin
= offset
;
1200 struct Blob
*entitlements
= reinterpret_cast<struct Blob
*>(blob
+ begin
);
1201 offset
+= sizeof(struct Blob
);
1203 memcpy(blob
+ offset
, xmld
, xmls
);
1206 entitlements
->magic
= Swap(CSMAGIC_ENTITLEMENTS
);
1207 entitlements
->length
= Swap(offset
- begin
);
1210 for (size_t index(0); index
!= count
; ++index
) {
1211 uint32_t type
= Swap(super
->index
[index
].type
);
1212 if (type
!= 0 && type
<= special
) {
1213 uint32_t offset
= Swap(super
->index
[index
].offset
);
1214 struct Blob
*local
= (struct Blob
*) (blob
+ offset
);
1215 sha1((uint8_t *) (hashes
- type
), (uint8_t *) local
, Swap(local
->length
));
1219 super
->count
= Swap(count
);
1220 super
->blob
.length
= Swap(offset
);
1222 if (offset
> size
) {
1223 fprintf(stderr
, "offset (%u) > size (%u)\n", offset
, size
);
1225 } //else fprintf(stderr, "offset (%zu) <= size (%zu)\n", offset, size);
1227 memset(blob
+ offset
, 0, size
- offset
);
1232 uint8_t *top
= reinterpret_cast<uint8_t *>(fat_header
.GetBase());
1233 size_t size
= fat_header
.GetSize();
1236 asprintf(©
, "%s.%s.cp", dir
, base
);
1237 FILE *file
= fopen(copy
, "w+");
1238 size_t writ
= fwrite(top
, 1, size
, file
);
1239 _assert(writ
== size
);
1242 _syscall(unlink(temp
));
1249 _syscall(stat(path
, &info
));
1250 _syscall(chown(temp
, info
.st_uid
, info
.st_gid
));
1251 _syscall(chmod(temp
, info
.st_mode
));
1252 _syscall(unlink(path
));
1253 _syscall(rename(temp
, path
));
1259 } catch (const char *) {