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_DYLD_INFO uint32_t(0x22)
92 #define LC_DYLD_INFO_ONLY uint32_t(0x22 | LC_REQ_DYLD)
97 uint32_t current_version
;
98 uint32_t compatibility_version
;
101 struct dylib_command
{
107 struct uuid_command
{
113 struct symtab_command
{
122 struct dyld_info_command
{
126 uint32_t rebase_size
;
129 uint32_t weak_bind_off
;
130 uint32_t weak_bind_size
;
131 uint32_t lazy_bind_off
;
132 uint32_t lazy_bind_size
;
134 uint32_t export_size
;
137 struct dysymtab_command
{
150 uint32_t extrefsymoff
;
151 uint32_t nextrefsyms
;
152 uint32_t indirectsymoff
;
153 uint32_t nindirectsyms
;
160 struct dylib_table_of_contents
{
161 uint32_t symbol_index
;
162 uint32_t module_index
;
165 struct dylib_module
{
166 uint32_t module_name
;
175 uint32_t iinit_iterm
;
176 uint32_t ninit_nterm
;
177 uint32_t objc_module_info_addr
;
178 uint32_t objc_module_info_size
;
181 struct dylib_reference
{
186 struct relocation_info
{
188 uint32_t r_symbolnum
:24;
207 struct segment_command
{
221 struct segment_command_64
{
263 struct linkedit_data_command
{
270 uint16_t Swap_(uint16_t value
) {
272 ((value
>> 8) & 0x00ff) |
273 ((value
<< 8) & 0xff00);
276 uint32_t Swap_(uint32_t value
) {
277 value
= ((value
>> 8) & 0x00ff00ff) |
278 ((value
<< 8) & 0xff00ff00);
279 value
= ((value
>> 16) & 0x0000ffff) |
280 ((value
<< 16) & 0xffff0000);
284 int16_t Swap_(int16_t value
) {
285 return Swap_(static_cast<uint16_t>(value
));
288 int32_t Swap_(int32_t value
) {
289 return Swap_(static_cast<uint32_t>(value
));
294 uint16_t Swap(uint16_t value
) {
295 return little_
? Swap_(value
) : value
;
298 uint32_t Swap(uint32_t value
) {
299 return little_
? Swap_(value
) : value
;
302 int16_t Swap(int16_t value
) {
303 return Swap(static_cast<uint16_t>(value
));
306 int32_t Swap(int32_t value
) {
307 return Swap(static_cast<uint32_t>(value
));
310 template <typename Target_
>
322 Data(void *base
, size_t size
) :
329 uint16_t Swap(uint16_t value
) const {
330 return swapped_
? Swap_(value
) : value
;
333 uint32_t Swap(uint32_t value
) const {
334 return swapped_
? Swap_(value
) : value
;
337 int16_t Swap(int16_t value
) const {
338 return Swap(static_cast<uint16_t>(value
));
341 int32_t Swap(int32_t value
) const {
342 return Swap(static_cast<uint32_t>(value
));
345 void *GetBase() const {
349 size_t GetSize() const {
360 struct mach_header
*mach_header_
;
361 struct load_command
*load_command_
;
364 MachHeader(void *base
, size_t size
) :
367 mach_header_
= (mach_header
*) base
;
369 switch (Swap(mach_header_
->magic
)) {
371 swapped_
= !swapped_
;
377 swapped_
= !swapped_
;
386 void *post
= mach_header_
+ 1;
388 post
= (uint32_t *) post
+ 1;
389 load_command_
= (struct load_command
*) post
;
392 Swap(mach_header_
->filetype
) == MH_EXECUTE
||
393 Swap(mach_header_
->filetype
) == MH_DYLIB
||
394 Swap(mach_header_
->filetype
) == MH_BUNDLE
398 struct mach_header
*operator ->() const {
402 uint32_t GetCPUType() const {
403 return Swap(mach_header_
->cputype
);
406 uint16_t GetCPUSubtype() const {
407 return Swap(mach_header_
->cpusubtype
) & 0xff;
410 std::vector
<struct load_command
*> GetLoadCommands() const {
411 std::vector
<struct load_command
*> load_commands
;
413 struct load_command
*load_command
= load_command_
;
414 for (uint32_t cmd
= 0; cmd
!= Swap(mach_header_
->ncmds
); ++cmd
) {
415 load_commands
.push_back(load_command
);
416 load_command
= (struct load_command
*) ((uint8_t *) load_command
+ Swap(load_command
->cmdsize
));
419 return load_commands
;
422 std::vector
<segment_command
*> GetSegments(const char *segment_name
) const {
423 std::vector
<struct segment_command
*> segment_commands
;
425 _foreach (load_command
, GetLoadCommands()) {
426 if (Swap(load_command
->cmd
) == LC_SEGMENT
) {
427 segment_command
*segment_command
= reinterpret_cast<struct segment_command
*>(load_command
);
428 if (strncmp(segment_command
->segname
, segment_name
, 16) == 0)
429 segment_commands
.push_back(segment_command
);
433 return segment_commands
;
436 std::vector
<segment_command_64
*> GetSegments64(const char *segment_name
) {
437 std::vector
<struct segment_command_64
*> segment_commands
;
439 _foreach (load_command
, GetLoadCommands()) {
440 if (Swap(load_command
->cmd
) == LC_SEGMENT_64
) {
441 segment_command_64
*segment_command
= reinterpret_cast<struct segment_command_64
*>(load_command
);
442 if (strncmp(segment_command
->segname
, segment_name
, 16) == 0)
443 segment_commands
.push_back(segment_command
);
447 return segment_commands
;
450 std::vector
<section
*> GetSections(const char *segment_name
, const char *section_name
) const {
451 std::vector
<section
*> sections
;
453 _foreach (segment
, GetSegments(segment_name
)) {
454 section
*section
= (struct section
*) (segment
+ 1);
457 for (sect
= 0; sect
!= Swap(segment
->nsects
); ++sect
) {
458 if (strncmp(section
->sectname
, section_name
, 16) == 0)
459 sections
.push_back(section
);
467 template <typename Target_
>
468 Pointer
<Target_
> GetPointer(uint32_t address
, const char *segment_name
= NULL
) const {
469 load_command
*load_command
= (struct load_command
*) (mach_header_
+ 1);
472 for (cmd
= 0; cmd
!= Swap(mach_header_
->ncmds
); ++cmd
) {
473 if (Swap(load_command
->cmd
) == LC_SEGMENT
) {
474 segment_command
*segment_command
= (struct segment_command
*) load_command
;
475 if (segment_name
!= NULL
&& strncmp(segment_command
->segname
, segment_name
, 16) != 0)
478 section
*sections
= (struct section
*) (segment_command
+ 1);
481 for (sect
= 0; sect
!= Swap(segment_command
->nsects
); ++sect
) {
482 section
*section
= §ions
[sect
];
483 //printf("%s %u %p %p %u\n", segment_command->segname, sect, address, section->addr, section->size);
484 if (address
>= Swap(section
->addr
) && address
< Swap(section
->addr
) + Swap(section
->size
)) {
485 //printf("0x%.8x %s\n", address, segment_command->segname);
486 return Pointer
<Target_
>(this, reinterpret_cast<Target_
*>(address
- Swap(section
->addr
) + Swap(section
->offset
) + (char *) mach_header_
));
492 load_command
= (struct load_command
*) ((char *) load_command
+ Swap(load_command
->cmdsize
));
495 return Pointer
<Target_
>(this);
498 template <typename Target_
>
499 Pointer
<Target_
> GetOffset(uint32_t offset
) {
500 return Pointer
<Target_
>(this, reinterpret_cast<Target_
*>(offset
+ (uint8_t *) mach_header_
));
504 class FatMachHeader
:
511 FatMachHeader(void *base
, size_t size
, fat_arch
*fat_arch
) :
512 MachHeader(base
, size
),
517 fat_arch
*GetFatArch() const {
526 fat_header
*fat_header_
;
527 std::vector
<FatMachHeader
> mach_headers_
;
530 FatHeader(void *base
, size_t size
) :
533 fat_header_
= reinterpret_cast<struct fat_header
*>(base
);
535 if (Swap(fat_header_
->magic
) == FAT_CIGAM
) {
536 swapped_
= !swapped_
;
538 } else if (Swap(fat_header_
->magic
) != FAT_MAGIC
) {
540 mach_headers_
.push_back(FatMachHeader(base
, size
, NULL
));
542 size_t fat_narch
= Swap(fat_header_
->nfat_arch
);
543 fat_arch
*fat_arch
= reinterpret_cast<struct fat_arch
*>(fat_header_
+ 1);
545 for (arch
= 0; arch
!= fat_narch
; ++arch
) {
546 uint32_t arch_offset
= Swap(fat_arch
->offset
);
547 uint32_t arch_size
= Swap(fat_arch
->size
);
548 mach_headers_
.push_back(FatMachHeader((uint8_t *) base
+ arch_offset
, arch_size
, fat_arch
));
554 std::vector
<FatMachHeader
> &GetMachHeaders() {
555 return mach_headers_
;
559 return fat_header_
!= NULL
;
562 struct fat_header
*operator ->() const {
567 FatHeader
Map(const char *path
, bool ro
= false) {
569 void *base(map(path
, 0, _not(size_t), &size
, ro
));
570 return FatHeader(base
, size
);
573 template <typename Target_
>
576 const MachHeader
*framework_
;
577 const Target_
*pointer_
;
580 Pointer(const MachHeader
*framework
= NULL
, const Target_
*pointer
= NULL
) :
581 framework_(framework
),
586 operator const Target_
*() const {
590 const Target_
*operator ->() const {
594 Pointer
<Target_
> &operator ++() {
599 template <typename Value_
>
600 Value_
Swap(Value_ value
) {
601 return framework_
->Swap(value
);
605 #define CSMAGIC_CODEDIRECTORY uint32_t(0xfade0c02)
606 #define CSMAGIC_EMBEDDED_SIGNATURE uint32_t(0xfade0cc0)
607 #define CSMAGIC_ENTITLEMENTS uint32_t(0xfade7171)
609 #define CSSLOT_CODEDIRECTORY uint32_t(0)
610 #define CSSLOT_REQUIREMENTS uint32_t(2)
611 #define CSSLOT_ENTITLEMENTS uint32_t(5)
626 struct BlobIndex index
[];
629 struct CodeDirectory
{
634 uint32_t identOffset
;
635 uint32_t nSpecialSlots
;
645 extern "C" uint32_t hash(uint8_t *k
, uint32_t length
, uint32_t initval
);
647 void sha1(uint8_t *hash
, uint8_t *data
, size_t size
) {
650 SHA1Input(&context
, data
, size
);
651 SHA1Result(&context
, hash
);
654 struct CodesignAllocation
{
659 CodesignAllocation(uint32_t type
, uint16_t subtype
, size_t size
) :
667 int main(int argc
, const char *argv
[]) {
673 little_
= endian
.byte
[0];
693 const void *xmld(NULL
);
696 uintptr_t noffset(_not(uintptr_t));
697 uintptr_t woffset(_not(uintptr_t));
699 std::vector
<std::string
> files
;
702 fprintf(stderr
, "usage: %s -S[entitlements.xml] <binary>\n", argv
[0]);
703 fprintf(stderr
, " %s -e MobileSafari\n", argv
[0]);
704 fprintf(stderr
, " %s -S cat\n", argv
[0]);
705 fprintf(stderr
, " %s -Stfp.xml gdb\n", argv
[0]);
709 for (int argi(1); argi
!= argc
; ++argi
)
710 if (argv
[argi
][0] != '-')
711 files
.push_back(argv
[argi
]);
712 else switch (argv
[argi
][1]) {
713 case 'R': flag_R
= true; break;
714 case 'r': flag_r
= true; break;
716 case 't': flag_t
= true; break;
717 case 'u': flag_u
= true; break;
718 case 'p': flag_p
= true; break;
719 case 'e': flag_e
= true; break;
720 case 'O': flag_O
= true; break;
730 if (argv
[argi
][2] != '\0') {
731 const char *xml
= argv
[argi
] + 2;
732 xmld
= map(xml
, 0, _not(size_t), &xmls
, true);
738 if (argv
[argi
][2] == '-')
742 timev
= strtoul(argv
[argi
] + 2, &arge
, 0);
743 _assert(arge
== argv
[argi
] + strlen(argv
[argi
]));
749 noffset
= strtoul(argv
[argi
] + 2, &arge
, 0);
750 _assert(arge
== argv
[argi
] + strlen(argv
[argi
]));
755 woffset
= strtoul(argv
[argi
] + 2, &arge
, 0);
756 _assert(arge
== argv
[argi
] + strlen(argv
[argi
]));
764 if (files
.empty()) usage
: {
768 size_t filei(0), filee(0);
769 _foreach (file
, files
) try {
770 const char *path(file
.c_str());
771 const char *base
= strrchr(path
, '/');
772 char *temp(NULL
), *dir
;
775 dir
= strndup_(path
, base
++ - path
+ 1);
783 FatHeader
fat_header(Map(path
));
784 _foreach (mach_header
, fat_header
.GetMachHeaders()) {
785 mach_header
->flags
= mach_header
.Swap(mach_header
.Swap(mach_header
->flags
) | MH_DYLDLINK
);
787 uint32_t size(_not(uint32_t)); {
788 _foreach (load_command
, mach_header
.GetLoadCommands()) {
789 switch (mach_header
.Swap(load_command
->cmd
)) {
790 case LC_CODE_SIGNATURE
: {
791 struct linkedit_data_command
*signature
= reinterpret_cast<struct linkedit_data_command
*>(load_command
);
792 memset(reinterpret_cast<uint8_t *>(mach_header
.GetBase()) + mach_header
.Swap(signature
->dataoff
), 0, mach_header
.Swap(signature
->datasize
));
793 memset(signature
, 0, sizeof(struct linkedit_data_command
));
795 mach_header
->ncmds
= mach_header
.Swap(mach_header
.Swap(mach_header
->ncmds
) - 1);
796 mach_header
->sizeofcmds
= mach_header
.Swap(uint32_t(mach_header
.Swap(mach_header
->sizeofcmds
) - sizeof(struct linkedit_data_command
)));
800 struct symtab_command
*symtab
= reinterpret_cast<struct symtab_command
*>(load_command
);
801 size
= mach_header
.Swap(symtab
->stroff
) + mach_header
.Swap(symtab
->strsize
);
807 _assert(size
!= _not(uint32_t));
809 _foreach (segment
, const_cast<FatMachHeader
&>(mach_header
).GetSegments("__LINKEDIT")) {
810 segment
->filesize
-= mach_header
.GetSize() - size
;
812 if (fat_arch
*fat_arch
= mach_header
.GetFatArch()) {
813 fat_arch
->size
= fat_header
.Swap(size
);
814 clip
= std::max(clip
, fat_header
.Swap(fat_arch
->offset
) + size
);
816 clip
= std::max(clip
, size
);
819 _foreach (segment
, const_cast<FatMachHeader
&>(mach_header
).GetSegments64("__LINKEDIT")) {
820 segment
->filesize
-= mach_header
.GetSize() - size
;
822 if (fat_arch
*fat_arch
= mach_header
.GetFatArch()) {
823 fat_arch
->size
= fat_header
.Swap(size
);
824 clip
= std::max(clip
, fat_header
.Swap(fat_arch
->offset
) + size
);
826 clip
= std::max(clip
, size
);
832 _syscall(truncate(path
, clip
));
836 asprintf(&temp
, "%s.%s.cs", dir
, base
);
837 const char *allocate
= getenv("CODESIGN_ALLOCATE");
838 if (allocate
== NULL
)
839 allocate
= "codesign_allocate";
841 std::vector
<CodesignAllocation
> allocations
; {
842 FatHeader
fat_header(Map(path
));
843 _foreach (mach_header
, fat_header
.GetMachHeaders()) {
844 mach_header
->flags
= mach_header
.Swap(mach_header
.Swap(mach_header
->flags
) | MH_DYLDLINK
);
846 size_t size(_not(size_t)); {
847 _foreach (load_command
, mach_header
.GetLoadCommands()) {
848 uint32_t cmd(mach_header
.Swap(load_command
->cmd
));
849 if (cmd
== LC_CODE_SIGNATURE
) {
850 struct linkedit_data_command
*signature
= reinterpret_cast<struct linkedit_data_command
*>(load_command
);
851 size
= mach_header
.Swap(signature
->dataoff
);
852 _assert(size
< mach_header
.GetSize());
857 if (size
== _not(size_t))
858 size
= mach_header
.GetSize();
861 allocations
.push_back(CodesignAllocation(mach_header
.GetCPUType(), mach_header
.GetCPUSubtype(), size
));
868 // XXX: this leaks memory, but it doesn't really matter
869 std::vector
<const char *> args
;
872 args
.push_back(allocate
);
874 args
.push_back("-i");
875 args
.push_back(path
);
877 _foreach (allocation
, allocations
) {
878 args
.push_back("-A");
880 asprintf(&arg
, "%u", allocation
.type_
);
883 asprintf(&arg
, "%u", allocation
.subtype_
);
887 alloc
+= sizeof(struct SuperBlob
);
890 special
= std::max(special
, CSSLOT_CODEDIRECTORY
);
891 alloc
+= sizeof(struct BlobIndex
);
892 alloc
+= sizeof(struct CodeDirectory
);
893 alloc
+= strlen(base
) + 1;
895 special
= std::max(special
, CSSLOT_REQUIREMENTS
);
896 alloc
+= sizeof(struct BlobIndex
);
900 special
= std::max(special
, CSSLOT_ENTITLEMENTS
);
901 alloc
+= sizeof(struct BlobIndex
);
902 alloc
+= sizeof(struct Blob
);
906 size_t normal((allocation
.size_
+ 0x1000 - 1) / 0x1000);
907 alloc
+= (special
+ normal
) * 0x14;
913 asprintf(&arg
, "%zu", alloc
);
917 args
.push_back("-o");
918 args
.push_back(temp
);
920 args
.push_back(NULL
);
929 execvp(allocate
, (char **) &args
[0]);
934 _syscall(waitpid(pid
, &status
, 0));
935 _assert(WIFEXITED(status
));
936 _assert(WEXITSTATUS(status
) == 0);
940 printf("path%zu='%s'\n", filei
, file
.c_str());
942 FatHeader
fat_header(Map(temp
== NULL
? path
: temp
, !(flag_R
| flag_T
| flag_s
| flag_S
| flag_O
)));
943 struct linkedit_data_command
*signature(NULL
);
945 _foreach (mach_header
, fat_header
.GetMachHeaders()) {
946 if (woffset
!= _not(uintptr_t)) {
947 Pointer
<uint32_t> wvalue(mach_header
.GetPointer
<uint32_t>(woffset
));
949 printf("(null) %p\n", reinterpret_cast<void *>(woffset
));
951 printf("0x%.08x\n", *wvalue
);
954 if (noffset
!= _not(uintptr_t))
955 printf("%s\n", &*mach_header
.GetPointer
<char>(noffset
));
958 _foreach(section
, mach_header
.GetSections("__TEXT", "__text"))
959 section
->addr
= mach_header
.Swap(0);
962 _foreach (load_command
, mach_header
.GetLoadCommands()) {
963 uint32_t cmd(mach_header
.Swap(load_command
->cmd
));
965 if (flag_R
&& cmd
== LC_REEXPORT_DYLIB
)
966 load_command
->cmd
= mach_header
.Swap(LC_LOAD_DYLIB
);
967 else if (cmd
== LC_CODE_SIGNATURE
)
968 signature
= reinterpret_cast<struct linkedit_data_command
*>(load_command
);
969 else if (cmd
== LC_UUID
) {
970 volatile struct uuid_command
*uuid_command(reinterpret_cast<struct uuid_command
*>(load_command
));
973 printf("uuid%zu=%.2x%.2x%.2x%.2x-%.2x%.2x-%.2x%.2x-%.2x%.2x-%.2x%.2x%.2x%.2x%.2x%.2x\n", filei
,
974 uuid_command
->uuid
[ 0], uuid_command
->uuid
[ 1], uuid_command
->uuid
[ 2], uuid_command
->uuid
[ 3],
975 uuid_command
->uuid
[ 4], uuid_command
->uuid
[ 5], uuid_command
->uuid
[ 6], uuid_command
->uuid
[ 7],
976 uuid_command
->uuid
[ 8], uuid_command
->uuid
[ 9], uuid_command
->uuid
[10], uuid_command
->uuid
[11],
977 uuid_command
->uuid
[12], uuid_command
->uuid
[13], uuid_command
->uuid
[14], uuid_command
->uuid
[15]
980 } else if (cmd
== LC_ID_DYLIB
) {
981 volatile struct dylib_command
*dylib_command(reinterpret_cast<struct dylib_command
*>(load_command
));
984 printf("time%zu=0x%.8x\n", filei
, mach_header
.Swap(dylib_command
->dylib
.timestamp
));
992 dylib_command
->dylib
.timestamp
= 0;
993 timed
= hash(reinterpret_cast<uint8_t *>(mach_header
.GetBase()), mach_header
.GetSize(), timev
);
996 dylib_command
->dylib
.timestamp
= mach_header
.Swap(timed
);
1002 _assert(signature
!= NULL
);
1004 uint32_t data
= mach_header
.Swap(signature
->dataoff
);
1006 uint8_t *top
= reinterpret_cast<uint8_t *>(mach_header
.GetBase());
1007 uint8_t *blob
= top
+ data
;
1008 struct SuperBlob
*super
= reinterpret_cast<struct SuperBlob
*>(blob
);
1010 for (size_t index(0); index
!= Swap(super
->count
); ++index
)
1011 if (Swap(super
->index
[index
].type
) == CSSLOT_ENTITLEMENTS
) {
1012 uint32_t begin
= Swap(super
->index
[index
].offset
);
1013 struct Blob
*entitlements
= reinterpret_cast<struct Blob
*>(blob
+ begin
);
1014 fwrite(entitlements
+ 1, 1, Swap(entitlements
->length
) - sizeof(struct Blob
), stdout
);
1019 _assert(signature
!= NULL
);
1021 uint32_t data
= mach_header
.Swap(signature
->dataoff
);
1023 uint8_t *top
= reinterpret_cast<uint8_t *>(mach_header
.GetBase());
1024 uint8_t *blob
= top
+ data
;
1025 struct SuperBlob
*super
= reinterpret_cast<struct SuperBlob
*>(blob
);
1027 for (size_t index(0); index
!= Swap(super
->count
); ++index
)
1028 if (Swap(super
->index
[index
].type
) == CSSLOT_CODEDIRECTORY
) {
1029 uint32_t begin
= Swap(super
->index
[index
].offset
);
1030 struct CodeDirectory
*directory
= reinterpret_cast<struct CodeDirectory
*>(blob
+ begin
);
1032 uint8_t (*hashes
)[20] = reinterpret_cast<uint8_t (*)[20]>(blob
+ begin
+ Swap(directory
->hashOffset
));
1033 uint32_t pages
= Swap(directory
->nCodeSlots
);
1036 for (size_t i
= 0; i
!= pages
- 1; ++i
)
1037 sha1(hashes
[i
], top
+ 0x1000 * i
, 0x1000);
1039 sha1(hashes
[pages
- 1], top
+ 0x1000 * (pages
- 1), ((data
- 1) % 0x1000) + 1);
1044 _assert(signature
!= NULL
);
1046 uint32_t data
= mach_header
.Swap(signature
->dataoff
);
1047 uint32_t size
= mach_header
.Swap(signature
->datasize
);
1049 uint8_t *top
= reinterpret_cast<uint8_t *>(mach_header
.GetBase());
1050 uint8_t *blob
= top
+ data
;
1051 struct SuperBlob
*super
= reinterpret_cast<struct SuperBlob
*>(blob
);
1052 super
->blob
.magic
= Swap(CSMAGIC_EMBEDDED_SIGNATURE
);
1054 uint32_t count
= xmld
== NULL
? 2 : 3;
1055 uint32_t offset
= sizeof(struct SuperBlob
) + count
* sizeof(struct BlobIndex
);
1057 super
->index
[0].type
= Swap(CSSLOT_CODEDIRECTORY
);
1058 super
->index
[0].offset
= Swap(offset
);
1060 uint32_t begin
= offset
;
1061 struct CodeDirectory
*directory
= reinterpret_cast<struct CodeDirectory
*>(blob
+ begin
);
1062 offset
+= sizeof(struct CodeDirectory
);
1064 directory
->blob
.magic
= Swap(CSMAGIC_CODEDIRECTORY
);
1065 directory
->version
= Swap(uint32_t(0x00020001));
1066 directory
->flags
= Swap(uint32_t(0));
1067 directory
->codeLimit
= Swap(data
);
1068 directory
->hashSize
= 0x14;
1069 directory
->hashType
= 0x01;
1070 directory
->spare1
= 0x00;
1071 directory
->pageSize
= 0x0c;
1072 directory
->spare2
= Swap(uint32_t(0));
1074 directory
->identOffset
= Swap(offset
- begin
);
1075 strcpy(reinterpret_cast<char *>(blob
+ offset
), base
);
1076 offset
+= strlen(base
) + 1;
1078 uint32_t special
= xmld
== NULL
? CSSLOT_REQUIREMENTS
: CSSLOT_ENTITLEMENTS
;
1079 directory
->nSpecialSlots
= Swap(special
);
1081 uint8_t (*hashes
)[20] = reinterpret_cast<uint8_t (*)[20]>(blob
+ offset
);
1082 memset(hashes
, 0, sizeof(*hashes
) * special
);
1084 offset
+= sizeof(*hashes
) * special
;
1087 uint32_t pages
= (data
+ 0x1000 - 1) / 0x1000;
1088 directory
->nCodeSlots
= Swap(pages
);
1091 for (size_t i
= 0; i
!= pages
- 1; ++i
)
1092 sha1(hashes
[i
], top
+ 0x1000 * i
, 0x1000);
1094 sha1(hashes
[pages
- 1], top
+ 0x1000 * (pages
- 1), ((data
- 1) % 0x1000) + 1);
1096 directory
->hashOffset
= Swap(offset
- begin
);
1097 offset
+= sizeof(*hashes
) * pages
;
1098 directory
->blob
.length
= Swap(offset
- begin
);
1100 super
->index
[1].type
= Swap(CSSLOT_REQUIREMENTS
);
1101 super
->index
[1].offset
= Swap(offset
);
1103 memcpy(blob
+ offset
, "\xfa\xde\x0c\x01\x00\x00\x00\x0c\x00\x00\x00\x00", 0xc);
1107 super
->index
[2].type
= Swap(CSSLOT_ENTITLEMENTS
);
1108 super
->index
[2].offset
= Swap(offset
);
1110 uint32_t begin
= offset
;
1111 struct Blob
*entitlements
= reinterpret_cast<struct Blob
*>(blob
+ begin
);
1112 offset
+= sizeof(struct Blob
);
1114 memcpy(blob
+ offset
, xmld
, xmls
);
1117 entitlements
->magic
= Swap(CSMAGIC_ENTITLEMENTS
);
1118 entitlements
->length
= Swap(offset
- begin
);
1121 for (size_t index(0); index
!= count
; ++index
) {
1122 uint32_t type
= Swap(super
->index
[index
].type
);
1123 if (type
!= 0 && type
<= special
) {
1124 uint32_t offset
= Swap(super
->index
[index
].offset
);
1125 struct Blob
*local
= (struct Blob
*) (blob
+ offset
);
1126 sha1((uint8_t *) (hashes
- type
), (uint8_t *) local
, Swap(local
->length
));
1130 super
->count
= Swap(count
);
1131 super
->blob
.length
= Swap(offset
);
1133 if (offset
> size
) {
1134 fprintf(stderr
, "offset (%u) > size (%u)\n", offset
, size
);
1136 } //else fprintf(stderr, "offset (%zu) <= size (%zu)\n", offset, size);
1138 memset(blob
+ offset
, 0, size
- offset
);
1143 uint8_t *top
= reinterpret_cast<uint8_t *>(fat_header
.GetBase());
1144 size_t size
= fat_header
.GetSize();
1147 asprintf(©
, "%s.%s.cp", dir
, base
);
1148 FILE *file
= fopen(copy
, "w+");
1149 size_t writ
= fwrite(top
, 1, size
, file
);
1150 _assert(writ
== size
);
1153 _syscall(unlink(temp
));
1160 _syscall(stat(path
, &info
));
1161 _syscall(chown(temp
, info
.st_uid
, info
.st_gid
));
1162 _syscall(chmod(temp
, info
.st_mode
));
1163 _syscall(unlink(path
));
1164 _syscall(rename(temp
, path
));
1170 } catch (const char *) {