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"
37 #include <openssl/sha.h>
39 #include <plist/plist.h>
46 #define FAT_MAGIC 0xcafebabe
47 #define FAT_CIGAM 0xbebafeca
67 #define MH_MAGIC 0xfeedface
68 #define MH_CIGAM 0xcefaedfe
70 #define MH_MAGIC_64 0xfeedfacf
71 #define MH_CIGAM_64 0xcffaedfe
73 #define MH_DYLDLINK 0x4
76 #define MH_EXECUTE 0x2
79 #define MH_DYLIB_STUB 0x9
86 #define LC_REQ_DYLD uint32_t(0x80000000)
88 #define LC_SEGMENT uint32_t(0x01)
89 #define LC_SYMTAB uint32_t(0x02)
90 #define LC_DYSYMTAB uint32_t(0x0b)
91 #define LC_LOAD_DYLIB uint32_t(0x0c)
92 #define LC_ID_DYLIB uint32_t(0x0d)
93 #define LC_SEGMENT_64 uint32_t(0x19)
94 #define LC_UUID uint32_t(0x1b)
95 #define LC_CODE_SIGNATURE uint32_t(0x1d)
96 #define LC_SEGMENT_SPLIT_INFO uint32_t(0x1e)
97 #define LC_REEXPORT_DYLIB uint32_t(0x1f | LC_REQ_DYLD)
98 #define LC_ENCRYPTION_INFO uint32_t(0x21)
99 #define LC_DYLD_INFO uint32_t(0x22)
100 #define LC_DYLD_INFO_ONLY uint32_t(0x22 | LC_REQ_DYLD)
101 #define LC_ENCRYPTION_INFO_64 uint32_t(0x2c)
106 uint32_t current_version
;
107 uint32_t compatibility_version
;
110 struct dylib_command
{
116 struct uuid_command
{
122 struct symtab_command
{
131 struct dyld_info_command
{
135 uint32_t rebase_size
;
138 uint32_t weak_bind_off
;
139 uint32_t weak_bind_size
;
140 uint32_t lazy_bind_off
;
141 uint32_t lazy_bind_size
;
143 uint32_t export_size
;
146 struct dysymtab_command
{
159 uint32_t extrefsymoff
;
160 uint32_t nextrefsyms
;
161 uint32_t indirectsymoff
;
162 uint32_t nindirectsyms
;
169 struct dylib_table_of_contents
{
170 uint32_t symbol_index
;
171 uint32_t module_index
;
174 struct dylib_module
{
175 uint32_t module_name
;
184 uint32_t iinit_iterm
;
185 uint32_t ninit_nterm
;
186 uint32_t objc_module_info_addr
;
187 uint32_t objc_module_info_size
;
190 struct dylib_reference
{
195 struct relocation_info
{
197 uint32_t r_symbolnum
:24;
216 struct segment_command
{
230 struct segment_command_64
{
272 struct linkedit_data_command
{
279 struct encryption_info_command
{
287 #define BIND_OPCODE_MASK 0xf0
288 #define BIND_IMMEDIATE_MASK 0x0f
289 #define BIND_OPCODE_DONE 0x00
290 #define BIND_OPCODE_SET_DYLIB_ORDINAL_IMM 0x10
291 #define BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB 0x20
292 #define BIND_OPCODE_SET_DYLIB_SPECIAL_IMM 0x30
293 #define BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM 0x40
294 #define BIND_OPCODE_SET_TYPE_IMM 0x50
295 #define BIND_OPCODE_SET_ADDEND_SLEB 0x60
296 #define BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB 0x70
297 #define BIND_OPCODE_ADD_ADDR_ULEB 0x80
298 #define BIND_OPCODE_DO_BIND 0x90
299 #define BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB 0xa0
300 #define BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED 0xb0
301 #define BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB 0xc0
303 inline void get(std::streambuf
&stream
, void *data
, size_t size
) {
304 _assert(stream
.sgetn(static_cast<char *>(data
), size
) == size
);
307 inline void put(std::streambuf
&stream
, const void *data
, size_t size
) {
308 _assert(stream
.sputn(static_cast<const char *>(data
), size
) == size
);
311 inline void pad(std::streambuf
&stream
, size_t size
) {
313 memset(padding
, 0, size
);
314 put(stream
, padding
, size
);
317 template <typename Type_
>
318 Type_
Align(Type_ value
, size_t align
) {
325 uint16_t Swap_(uint16_t value
) {
327 ((value
>> 8) & 0x00ff) |
328 ((value
<< 8) & 0xff00);
331 uint32_t Swap_(uint32_t value
) {
332 value
= ((value
>> 8) & 0x00ff00ff) |
333 ((value
<< 8) & 0xff00ff00);
334 value
= ((value
>> 16) & 0x0000ffff) |
335 ((value
<< 16) & 0xffff0000);
339 uint64_t Swap_(uint64_t value
) {
340 value
= (value
& 0x00000000ffffffff) << 32 | (value
& 0xffffffff00000000) >> 32;
341 value
= (value
& 0x0000ffff0000ffff) << 16 | (value
& 0xffff0000ffff0000) >> 16;
342 value
= (value
& 0x00ff00ff00ff00ff) << 8 | (value
& 0xff00ff00ff00ff00) >> 8;
346 int16_t Swap_(int16_t value
) {
347 return Swap_(static_cast<uint16_t>(value
));
350 int32_t Swap_(int32_t value
) {
351 return Swap_(static_cast<uint32_t>(value
));
354 int64_t Swap_(int64_t value
) {
355 return Swap_(static_cast<uint64_t>(value
));
360 uint16_t Swap(uint16_t value
) {
361 return little_
? Swap_(value
) : value
;
364 uint32_t Swap(uint32_t value
) {
365 return little_
? Swap_(value
) : value
;
368 uint64_t Swap(uint64_t value
) {
369 return little_
? Swap_(value
) : value
;
372 int16_t Swap(int16_t value
) {
373 return Swap(static_cast<uint16_t>(value
));
376 int32_t Swap(int32_t value
) {
377 return Swap(static_cast<uint32_t>(value
));
380 int64_t Swap(int64_t value
) {
381 return Swap(static_cast<uint64_t>(value
));
384 template <typename Target_
>
397 Swapped(bool swapped
) :
402 template <typename Type_
>
403 Type_
Swap(Type_ value
) const {
404 return swapped_
? Swap_(value
) : value
;
416 Data(void *base
, size_t size
) :
422 void *GetBase() const {
426 size_t GetSize() const {
437 struct mach_header
*mach_header_
;
438 struct load_command
*load_command_
;
441 MachHeader(void *base
, size_t size
) :
444 mach_header_
= (mach_header
*) base
;
446 switch (Swap(mach_header_
->magic
)) {
448 swapped_
= !swapped_
;
454 swapped_
= !swapped_
;
463 void *post
= mach_header_
+ 1;
465 post
= (uint32_t *) post
+ 1;
466 load_command_
= (struct load_command
*) post
;
469 Swap(mach_header_
->filetype
) == MH_EXECUTE
||
470 Swap(mach_header_
->filetype
) == MH_DYLIB
||
471 Swap(mach_header_
->filetype
) == MH_BUNDLE
475 bool Bits64() const {
479 struct mach_header
*operator ->() const {
483 operator struct mach_header
*() const {
487 uint32_t GetCPUType() const {
488 return Swap(mach_header_
->cputype
);
491 uint32_t GetCPUSubtype() const {
492 return Swap(mach_header_
->cpusubtype
) & 0xff;
495 struct load_command
*GetLoadCommand() const {
496 return load_command_
;
499 std::vector
<struct load_command
*> GetLoadCommands() const {
500 std::vector
<struct load_command
*> load_commands
;
502 struct load_command
*load_command
= load_command_
;
503 for (uint32_t cmd
= 0; cmd
!= Swap(mach_header_
->ncmds
); ++cmd
) {
504 load_commands
.push_back(load_command
);
505 load_command
= (struct load_command
*) ((uint8_t *) load_command
+ Swap(load_command
->cmdsize
));
508 return load_commands
;
511 std::vector
<segment_command
*> GetSegments(const char *segment_name
) const {
512 std::vector
<struct segment_command
*> segment_commands
;
514 _foreach (load_command
, GetLoadCommands()) {
515 if (Swap(load_command
->cmd
) == LC_SEGMENT
) {
516 segment_command
*segment_command
= reinterpret_cast<struct segment_command
*>(load_command
);
517 if (strncmp(segment_command
->segname
, segment_name
, 16) == 0)
518 segment_commands
.push_back(segment_command
);
522 return segment_commands
;
525 std::vector
<segment_command_64
*> GetSegments64(const char *segment_name
) const {
526 std::vector
<struct segment_command_64
*> segment_commands
;
528 _foreach (load_command
, GetLoadCommands()) {
529 if (Swap(load_command
->cmd
) == LC_SEGMENT_64
) {
530 segment_command_64
*segment_command
= reinterpret_cast<struct segment_command_64
*>(load_command
);
531 if (strncmp(segment_command
->segname
, segment_name
, 16) == 0)
532 segment_commands
.push_back(segment_command
);
536 return segment_commands
;
539 std::vector
<section
*> GetSections(const char *segment_name
, const char *section_name
) const {
540 std::vector
<section
*> sections
;
542 _foreach (segment
, GetSegments(segment_name
)) {
543 section
*section
= (struct section
*) (segment
+ 1);
546 for (sect
= 0; sect
!= Swap(segment
->nsects
); ++sect
) {
547 if (strncmp(section
->sectname
, section_name
, 16) == 0)
548 sections
.push_back(section
);
556 template <typename Target_
>
557 Pointer
<Target_
> GetPointer(uint32_t address
, const char *segment_name
= NULL
) const {
558 load_command
*load_command
= (struct load_command
*) (mach_header_
+ 1);
561 for (cmd
= 0; cmd
!= Swap(mach_header_
->ncmds
); ++cmd
) {
562 if (Swap(load_command
->cmd
) == LC_SEGMENT
) {
563 segment_command
*segment_command
= (struct segment_command
*) load_command
;
564 if (segment_name
!= NULL
&& strncmp(segment_command
->segname
, segment_name
, 16) != 0)
567 section
*sections
= (struct section
*) (segment_command
+ 1);
570 for (sect
= 0; sect
!= Swap(segment_command
->nsects
); ++sect
) {
571 section
*section
= §ions
[sect
];
572 //printf("%s %u %p %p %u\n", segment_command->segname, sect, address, section->addr, section->size);
573 if (address
>= Swap(section
->addr
) && address
< Swap(section
->addr
) + Swap(section
->size
)) {
574 //printf("0x%.8x %s\n", address, segment_command->segname);
575 return Pointer
<Target_
>(this, reinterpret_cast<Target_
*>(address
- Swap(section
->addr
) + Swap(section
->offset
) + (char *) mach_header_
));
581 load_command
= (struct load_command
*) ((char *) load_command
+ Swap(load_command
->cmdsize
));
584 return Pointer
<Target_
>(this);
587 template <typename Target_
>
588 Pointer
<Target_
> GetOffset(uint32_t offset
) {
589 return Pointer
<Target_
>(this, reinterpret_cast<Target_
*>(offset
+ (uint8_t *) mach_header_
));
593 class FatMachHeader
:
600 FatMachHeader(void *base
, size_t size
, fat_arch
*fat_arch
) :
601 MachHeader(base
, size
),
606 fat_arch
*GetFatArch() const {
615 fat_header
*fat_header_
;
616 std::vector
<FatMachHeader
> mach_headers_
;
619 FatHeader(void *base
, size_t size
) :
622 fat_header_
= reinterpret_cast<struct fat_header
*>(base
);
624 if (Swap(fat_header_
->magic
) == FAT_CIGAM
) {
625 swapped_
= !swapped_
;
627 } else if (Swap(fat_header_
->magic
) != FAT_MAGIC
) {
629 mach_headers_
.push_back(FatMachHeader(base
, size
, NULL
));
631 size_t fat_narch
= Swap(fat_header_
->nfat_arch
);
632 fat_arch
*fat_arch
= reinterpret_cast<struct fat_arch
*>(fat_header_
+ 1);
634 for (arch
= 0; arch
!= fat_narch
; ++arch
) {
635 uint32_t arch_offset
= Swap(fat_arch
->offset
);
636 uint32_t arch_size
= Swap(fat_arch
->size
);
637 mach_headers_
.push_back(FatMachHeader((uint8_t *) base
+ arch_offset
, arch_size
, fat_arch
));
643 std::vector
<FatMachHeader
> &GetMachHeaders() {
644 return mach_headers_
;
648 return fat_header_
!= NULL
;
651 struct fat_header
*operator ->() const {
655 operator struct fat_header
*() const {
660 template <typename Target_
>
663 const MachHeader
*framework_
;
664 const Target_
*pointer_
;
667 Pointer(const MachHeader
*framework
= NULL
, const Target_
*pointer
= NULL
) :
668 framework_(framework
),
673 operator const Target_
*() const {
677 const Target_
*operator ->() const {
681 Pointer
<Target_
> &operator ++() {
686 template <typename Value_
>
687 Value_
Swap(Value_ value
) {
688 return framework_
->Swap(value
);
692 #define CSMAGIC_REQUIREMENT uint32_t(0xfade0c00)
693 #define CSMAGIC_REQUIREMENTS uint32_t(0xfade0c01)
694 #define CSMAGIC_CODEDIRECTORY uint32_t(0xfade0c02)
695 #define CSMAGIC_EMBEDDED_SIGNATURE uint32_t(0xfade0cc0)
696 #define CSMAGIC_EMBEDDED_SIGNATURE_OLD uint32_t(0xfade0b02)
697 #define CSMAGIC_EMBEDDED_ENTITLEMENTS uint32_t(0xfade7171)
698 #define CSMAGIC_DETACHED_SIGNATURE uint32_t(0xfade0cc1)
699 #define CSMAGIC_BLOBWRAPPER uint32_t(0xfade0b01)
701 #define CSSLOT_CODEDIRECTORY uint32_t(0x00000)
702 #define CSSLOT_INFOSLOT uint32_t(0x00001)
703 #define CSSLOT_REQUIREMENTS uint32_t(0x00002)
704 #define CSSLOT_RESOURCEDIR uint32_t(0x00003)
705 #define CSSLOT_APPLICATION uint32_t(0x00004)
706 #define CSSLOT_ENTITLEMENTS uint32_t(0x00005)
708 #define CSSLOT_SIGNATURESLOT uint32_t(0x10000)
710 #define CS_HASHTYPE_SHA1 1
725 struct BlobIndex index
[];
728 struct CodeDirectory
{
732 uint32_t identOffset
;
733 uint32_t nSpecialSlots
;
743 extern "C" uint32_t hash(uint8_t *k
, uint32_t length
, uint32_t initval
);
745 void sha1(uint8_t *hash
, const void *data
, size_t size
) {
746 SHA1(static_cast<const uint8_t *>(data
), size
, hash
);
749 struct CodesignAllocation
{
750 FatMachHeader mach_header_
;
758 CodesignAllocation(FatMachHeader mach_header
, size_t offset
, size_t size
, size_t limit
, size_t alloc
, size_t special
, size_t align
) :
759 mach_header_(mach_header
),
782 _syscall(close(file_
));
785 void open(const char *path
, int flags
) {
786 _assert(file_
== -1);
787 _syscall(file_
= ::open(path
, flags
));
804 _syscall(munmap(data_
, size_
));
816 Map(const char *path
, int oflag
, int pflag
, int mflag
) :
819 open(path
, oflag
, pflag
, mflag
);
822 Map(const char *path
, bool edit
) :
832 void open(const char *path
, int oflag
, int pflag
, int mflag
) {
835 file_
.open(path
, oflag
);
836 int file(file_
.file());
839 _syscall(fstat(file
, &stat
));
840 size_
= stat
.st_size
;
842 _syscall(data_
= mmap(NULL
, size_
, pflag
, mflag
, file
, 0));
845 void open(const char *path
, bool edit
) {
847 open(path
, O_RDWR
, PROT_READ
| PROT_WRITE
, MAP_SHARED
);
849 open(path
, O_RDONLY
, PROT_READ
, MAP_PRIVATE
);
856 size_t size() const {
860 operator std::string() const {
861 return std::string(static_cast<char *>(data_
), size_
);
865 void resign(void *idata
, size_t isize
, std::streambuf
&output
, const char *name
, const std::string
&entitlements
) {
866 FatHeader
source(idata
, isize
);
868 uint8_t pageshift(0x0c);
869 uint32_t pagesize(1 << pageshift
);
873 offset
+= sizeof(fat_header
) + sizeof(fat_arch
) * source
.Swap(source
->nfat_arch
);
875 std::vector
<CodesignAllocation
> allocations
;
876 _foreach (mach_header
, source
.GetMachHeaders()) {
877 struct linkedit_data_command
*signature(NULL
);
878 struct symtab_command
*symtab(NULL
);
880 _foreach (load_command
, mach_header
.GetLoadCommands()) {
881 uint32_t cmd(mach_header
.Swap(load_command
->cmd
));
883 else if (cmd
== LC_CODE_SIGNATURE
)
884 signature
= reinterpret_cast<struct linkedit_data_command
*>(load_command
);
885 else if (cmd
== LC_SYMTAB
)
886 symtab
= reinterpret_cast<struct symtab_command
*>(load_command
);
890 if (signature
== NULL
)
891 size
= mach_header
.GetSize();
893 size
= mach_header
.Swap(signature
->dataoff
);
894 _assert(size
<= mach_header
.GetSize());
897 if (symtab
!= NULL
) {
898 auto end(mach_header
.Swap(symtab
->stroff
) + mach_header
.Swap(symtab
->strsize
));
899 _assert(end
<= size
);
900 _assert(end
>= size
- 0x10);
908 alloc
+= sizeof(struct SuperBlob
);
910 special
= std::max(special
, CSSLOT_REQUIREMENTS
);
911 alloc
+= sizeof(struct BlobIndex
);
914 if (entitlements
.size() != 0) {
915 special
= std::max(special
, CSSLOT_ENTITLEMENTS
);
916 alloc
+= sizeof(struct BlobIndex
);
917 alloc
+= sizeof(struct Blob
);
918 alloc
+= entitlements
.size();
921 special
= std::max(special
, CSSLOT_CODEDIRECTORY
);
922 alloc
+= sizeof(struct BlobIndex
);
923 alloc
+= sizeof(struct Blob
);
924 alloc
+= sizeof(struct CodeDirectory
);
925 alloc
+= strlen(name
) + 1;
927 uint32_t normal((size
+ pagesize
- 1) / pagesize
);
928 alloc
= Align(alloc
+ (special
+ normal
) * SHA_DIGEST_LENGTH
, 16);
931 auto *fat_arch(mach_header
.GetFatArch());
932 uint32_t align(fat_arch
== NULL
? 0 : source
.Swap(fat_arch
->align
));
933 offset
= Align(offset
, 1 << align
);
935 uint32_t limit(size
);
937 limit
= Align(limit
, 0x10);
939 allocations
.push_back(CodesignAllocation(mach_header
, offset
, size
, limit
, alloc
, special
, align
));
940 offset
+= size
+ alloc
;
941 offset
= Align(offset
, 16);
946 if (source
.IsFat()) {
947 fat_header fat_header
;
948 fat_header
.magic
= Swap(FAT_MAGIC
);
949 fat_header
.nfat_arch
= Swap(uint32_t(allocations
.size()));
950 put(output
, &fat_header
, sizeof(fat_header
));
951 position
+= sizeof(fat_header
);
953 _foreach (allocation
, allocations
) {
954 auto &mach_header(allocation
.mach_header_
);
957 fat_arch
.cputype
= Swap(mach_header
->cputype
);
958 fat_arch
.cpusubtype
= Swap(mach_header
->cpusubtype
);
959 fat_arch
.offset
= Swap(allocation
.offset_
);
960 fat_arch
.size
= Swap(allocation
.limit_
+ allocation
.alloc_
);
961 fat_arch
.align
= Swap(allocation
.align_
);
962 put(output
, &fat_arch
, sizeof(fat_arch
));
963 position
+= sizeof(fat_arch
);
967 _foreach (allocation
, allocations
) {
968 auto &mach_header(allocation
.mach_header_
);
970 pad(output
, allocation
.offset_
- position
);
971 position
= allocation
.offset_
;
973 std::vector
<std::string
> commands
;
975 _foreach (load_command
, mach_header
.GetLoadCommands()) {
976 std::string
copy(reinterpret_cast<const char *>(load_command
), load_command
->cmdsize
);
978 switch (uint32_t cmd
= mach_header
.Swap(load_command
->cmd
)) {
979 case LC_CODE_SIGNATURE
:
984 auto segment_command(reinterpret_cast<struct segment_command
*>(©
[0]));
985 if (strncmp(segment_command
->segname
, "__LINKEDIT", 16) != 0)
987 size_t size(mach_header
.Swap(allocation
.limit_
+ allocation
.alloc_
- mach_header
.Swap(segment_command
->fileoff
)));
988 segment_command
->filesize
= size
;
989 segment_command
->vmsize
= Align(size
, 0x1000);
992 case LC_SEGMENT_64
: {
993 auto segment_command(reinterpret_cast<struct segment_command_64
*>(©
[0]));
994 if (strncmp(segment_command
->segname
, "__LINKEDIT", 16) != 0)
996 size_t size(mach_header
.Swap(allocation
.limit_
+ allocation
.alloc_
- mach_header
.Swap(segment_command
->fileoff
)));
997 segment_command
->filesize
= size
;
998 segment_command
->vmsize
= Align(size
, 0x1000);
1002 commands
.push_back(copy
);
1006 linkedit_data_command signature
;
1007 signature
.cmd
= mach_header
.Swap(LC_CODE_SIGNATURE
);
1008 signature
.cmdsize
= mach_header
.Swap(uint32_t(sizeof(signature
)));
1009 signature
.dataoff
= mach_header
.Swap(allocation
.limit_
);
1010 signature
.datasize
= mach_header
.Swap(allocation
.alloc_
);
1011 commands
.push_back(std::string(reinterpret_cast<const char *>(&signature
), sizeof(signature
)));
1014 size_t begin(position
);
1017 _foreach(command
, commands
)
1018 after
+= command
.size();
1020 std::stringbuf altern
;
1022 struct mach_header
header(*mach_header
);
1023 header
.ncmds
= mach_header
.Swap(uint32_t(commands
.size()));
1024 header
.sizeofcmds
= mach_header
.Swap(after
);
1025 put(output
, &header
, sizeof(header
));
1026 put(altern
, &header
, sizeof(header
));
1027 position
+= sizeof(header
);
1029 if (mach_header
.Bits64()) {
1030 auto pad(mach_header
.Swap(uint32_t(0)));
1031 put(output
, &pad
, sizeof(pad
));
1032 put(altern
, &pad
, sizeof(pad
));
1033 position
+= sizeof(pad
);
1036 _foreach(command
, commands
) {
1037 put(output
, command
.data(), command
.size());
1038 put(altern
, command
.data(), command
.size());
1039 position
+= command
.size();
1042 uint32_t before(mach_header
.Swap(mach_header
->sizeofcmds
));
1043 if (before
> after
) {
1044 pad(output
, before
- after
);
1045 pad(altern
, before
- after
);
1046 position
+= before
- after
;
1049 auto top(reinterpret_cast<char *>(mach_header
.GetBase()));
1051 std::string
overlap(altern
.str());
1052 overlap
.append(top
+ overlap
.size(), Align(overlap
.size(), 0x1000) - overlap
.size());
1054 put(output
, top
+ (position
- begin
), allocation
.size_
- (position
- begin
));
1055 position
= begin
+ allocation
.size_
;
1057 pad(output
, allocation
.limit_
- allocation
.size_
);
1058 position
+= allocation
.limit_
- allocation
.size_
;
1061 std::map
<uint32_t, std::string
> blobs
;
1064 std::stringbuf data
;
1067 blob
.magic
= Swap(CSMAGIC_REQUIREMENTS
);
1068 blob
.length
= Swap(uint32_t(sizeof(Blob
) + sizeof(uint32_t)));
1069 put(data
, &blob
, sizeof(blob
));
1071 uint32_t requirements(0);
1072 requirements
= Swap(0);
1073 put(data
, &requirements
, sizeof(requirements
));
1075 blobs
.insert(std::make_pair(CSSLOT_REQUIREMENTS
, data
.str()));
1078 if (entitlements
.size() != 0) {
1079 std::stringbuf data
;
1082 blob
.magic
= Swap(CSMAGIC_EMBEDDED_ENTITLEMENTS
);
1083 blob
.length
= Swap(uint32_t(sizeof(blob
) + entitlements
.size()));
1084 put(data
, &blob
, sizeof(blob
));
1086 put(data
, entitlements
.data(), entitlements
.size());
1088 blobs
.insert(std::make_pair(CSSLOT_ENTITLEMENTS
, data
.str()));
1092 std::stringbuf data
;
1094 uint32_t normal((allocation
.limit_
+ pagesize
- 1) / pagesize
);
1097 blob
.magic
= Swap(CSMAGIC_CODEDIRECTORY
);
1098 blob
.length
= Swap(uint32_t(sizeof(blob
) + sizeof(CodeDirectory
) + strlen(name
) + 1 + SHA_DIGEST_LENGTH
* (allocation
.special_
+ normal
)));
1099 put(data
, &blob
, sizeof(blob
));
1101 CodeDirectory directory
;
1102 directory
.version
= Swap(uint32_t(0x00020001));
1103 directory
.flags
= Swap(uint32_t(0));
1104 directory
.hashOffset
= Swap(uint32_t(sizeof(blob
) + sizeof(CodeDirectory
) + strlen(name
) + 1 + SHA_DIGEST_LENGTH
* allocation
.special_
));
1105 directory
.identOffset
= Swap(uint32_t(sizeof(blob
) + sizeof(CodeDirectory
)));
1106 directory
.nSpecialSlots
= Swap(allocation
.special_
);
1107 directory
.codeLimit
= Swap(allocation
.limit_
);
1108 directory
.nCodeSlots
= Swap(normal
);
1109 directory
.hashSize
= SHA_DIGEST_LENGTH
;
1110 directory
.hashType
= CS_HASHTYPE_SHA1
;
1111 directory
.spare1
= 0x00;
1112 directory
.pageSize
= pageshift
;
1113 directory
.spare2
= Swap(uint32_t(0));
1114 put(data
, &directory
, sizeof(directory
));
1116 put(data
, name
, strlen(name
) + 1);
1118 uint8_t storage
[allocation
.special_
+ normal
][SHA_DIGEST_LENGTH
];
1119 uint8_t (*hashes
)[SHA_DIGEST_LENGTH
] = storage
+ allocation
.special_
;
1121 memset(storage
, 0, sizeof(*storage
) * allocation
.special_
);
1123 _foreach (blob
, blobs
) {
1124 auto local(reinterpret_cast<const Blob
*>(&blob
.second
[0]));
1125 sha1((uint8_t *) (hashes
- blob
.first
), local
, Swap(local
->length
));
1129 for (size_t i
= 0; i
!= normal
- 1; ++i
)
1130 sha1(hashes
[i
], (pagesize
* i
< overlap
.size() ? overlap
.data() : top
) + pagesize
* i
, pagesize
);
1132 sha1(hashes
[normal
- 1], top
+ pagesize
* (normal
- 1), ((allocation
.limit_
- 1) % pagesize
) + 1);
1134 put(data
, storage
, sizeof(storage
));
1136 blobs
.insert(std::make_pair(CSSLOT_CODEDIRECTORY
, data
.str()));
1140 _foreach (blob
, blobs
)
1141 total
+= blob
.second
.size();
1143 struct SuperBlob super
;
1144 super
.blob
.magic
= Swap(CSMAGIC_EMBEDDED_SIGNATURE
);
1145 super
.blob
.length
= Swap(uint32_t(sizeof(SuperBlob
) + blobs
.size() * sizeof(BlobIndex
) + total
));
1146 super
.count
= Swap(uint32_t(blobs
.size()));
1147 put(output
, &super
, sizeof(super
));
1149 uint32_t offset(sizeof(SuperBlob
) + sizeof(BlobIndex
) * blobs
.size());
1150 position
+= offset
+ total
;
1152 _foreach (blob
, blobs
) {
1154 index
.type
= Swap(blob
.first
);
1155 index
.offset
= Swap(offset
);
1156 put(output
, &index
, sizeof(index
));
1157 offset
+= blob
.second
.size();
1160 _foreach (blob
, blobs
)
1161 put(output
, blob
.second
.data(), blob
.second
.size());
1163 if (allocation
.alloc_
> position
)
1164 pad(output
, allocation
.alloc_
- position
);
1169 int main(int argc
, const char *argv
[]) {
1175 little_
= endian
.byte
[0];
1190 uint32_t flag_CPUType(_not(uint32_t));
1191 uint32_t flag_CPUSubtype(_not(uint32_t));
1193 const char *flag_I(NULL
);
1200 std::vector
<std::string
> files
;
1203 fprintf(stderr
, "usage: %s -S[entitlements.xml] <binary>\n", argv
[0]);
1204 fprintf(stderr
, " %s -e MobileSafari\n", argv
[0]);
1205 fprintf(stderr
, " %s -S cat\n", argv
[0]);
1206 fprintf(stderr
, " %s -Stfp.xml gdb\n", argv
[0]);
1210 for (int argi(1); argi
!= argc
; ++argi
)
1211 if (argv
[argi
][0] != '-')
1212 files
.push_back(argv
[argi
]);
1213 else switch (argv
[argi
][1]) {
1214 case 'r': flag_r
= true; break;
1215 case 'e': flag_e
= true; break;
1217 case 'D': flag_D
= true; break;
1219 case 'a': flag_a
= true; break;
1223 if (argv
[argi
][2] != '\0') {
1224 const char *cpu
= argv
[argi
] + 2;
1225 const char *colon
= strchr(cpu
, ':');
1226 _assert(colon
!= NULL
);
1228 flag_CPUType
= strtoul(cpu
, &arge
, 0);
1229 _assert(arge
== colon
);
1230 flag_CPUSubtype
= strtoul(colon
+ 1, &arge
, 0);
1231 _assert(arge
== argv
[argi
] + strlen(argv
[argi
]));
1243 if (argv
[argi
][2] != '\0') {
1244 const char *xml
= argv
[argi
] + 2;
1245 entitlements
.open(xml
, O_RDONLY
, PROT_READ
, MAP_PRIVATE
);
1251 if (argv
[argi
][2] == '-')
1255 timev
= strtoul(argv
[argi
] + 2, &arge
, 0);
1256 _assert(arge
== argv
[argi
] + strlen(argv
[argi
]));
1261 flag_I
= argv
[argi
] + 2;
1269 _assert(!flag_S
|| !flag_r
);
1271 if (files
.empty()) usage
: {
1275 size_t filei(0), filee(0);
1276 _foreach (file
, files
) try {
1277 const char *path(file
.c_str());
1278 const char *base
= strrchr(path
, '/');
1282 dir
.assign(path
, base
++ - path
+ 1);
1286 const char *name(flag_I
?: base
);
1289 if (flag_S
|| flag_r
) {
1290 Map
input(path
, O_RDONLY
, PROT_READ
, MAP_PRIVATE
);
1292 std::filebuf output
;
1293 asprintf(&temp
, "%s.%s.cs", dir
.c_str(), base
);
1294 _assert(output
.open(temp
, std::ios::out
| std::ios::trunc
| std::ios::binary
) == &output
);
1296 resign(input
.data(), input
.size(), output
, flag_S
? name
: NULL
, entitlements
);
1299 Map
mapping(temp
?: path
, flag_T
|| flag_s
);
1300 FatHeader
fat_header(mapping
.data(), mapping
.size());
1302 _foreach (mach_header
, fat_header
.GetMachHeaders()) {
1303 struct linkedit_data_command
*signature(NULL
);
1304 struct encryption_info_command
*encryption(NULL
);
1307 if (mach_header
.GetCPUType() != flag_CPUType
)
1309 if (mach_header
.GetCPUSubtype() != flag_CPUSubtype
)
1314 printf("cpu=0x%x:0x%x\n", mach_header
.GetCPUType(), mach_header
.GetCPUSubtype());
1316 _foreach (load_command
, mach_header
.GetLoadCommands()) {
1317 uint32_t cmd(mach_header
.Swap(load_command
->cmd
));
1320 else if (cmd
== LC_CODE_SIGNATURE
)
1321 signature
= reinterpret_cast<struct linkedit_data_command
*>(load_command
);
1322 else if (cmd
== LC_ENCRYPTION_INFO
|| cmd
== LC_ENCRYPTION_INFO_64
)
1323 encryption
= reinterpret_cast<struct encryption_info_command
*>(load_command
);
1324 else if (cmd
== LC_ID_DYLIB
) {
1325 volatile struct dylib_command
*dylib_command(reinterpret_cast<struct dylib_command
*>(load_command
));
1333 dylib_command
->dylib
.timestamp
= 0;
1334 timed
= hash(reinterpret_cast<uint8_t *>(mach_header
.GetBase()), mach_header
.GetSize(), timev
);
1337 dylib_command
->dylib
.timestamp
= mach_header
.Swap(timed
);
1343 _assert(encryption
!= NULL
);
1344 encryption
->cryptid
= mach_header
.Swap(0);
1348 _assert(signature
!= NULL
);
1350 uint32_t data
= mach_header
.Swap(signature
->dataoff
);
1352 uint8_t *top
= reinterpret_cast<uint8_t *>(mach_header
.GetBase());
1353 uint8_t *blob
= top
+ data
;
1354 struct SuperBlob
*super
= reinterpret_cast<struct SuperBlob
*>(blob
);
1356 for (size_t index(0); index
!= Swap(super
->count
); ++index
)
1357 if (Swap(super
->index
[index
].type
) == CSSLOT_ENTITLEMENTS
) {
1358 uint32_t begin
= Swap(super
->index
[index
].offset
);
1359 struct Blob
*entitlements
= reinterpret_cast<struct Blob
*>(blob
+ begin
);
1360 fwrite(entitlements
+ 1, 1, Swap(entitlements
->length
) - sizeof(struct Blob
), stdout
);
1365 _assert(signature
!= NULL
);
1367 uint32_t data
= mach_header
.Swap(signature
->dataoff
);
1369 uint8_t *top
= reinterpret_cast<uint8_t *>(mach_header
.GetBase());
1370 uint8_t *blob
= top
+ data
;
1371 struct SuperBlob
*super
= reinterpret_cast<struct SuperBlob
*>(blob
);
1373 for (size_t index(0); index
!= Swap(super
->count
); ++index
)
1374 if (Swap(super
->index
[index
].type
) == CSSLOT_CODEDIRECTORY
) {
1375 uint32_t begin
= Swap(super
->index
[index
].offset
);
1376 struct CodeDirectory
*directory
= reinterpret_cast<struct CodeDirectory
*>(blob
+ begin
);
1378 uint8_t (*hashes
)[SHA_DIGEST_LENGTH
] = reinterpret_cast<uint8_t (*)[SHA_DIGEST_LENGTH
]>(blob
+ begin
+ Swap(directory
->hashOffset
));
1379 uint32_t pages
= Swap(directory
->nCodeSlots
);
1382 for (size_t i
= 0; i
!= pages
- 1; ++i
)
1383 sha1(hashes
[i
], top
+ 0x1000 * i
, 0x1000);
1385 sha1(hashes
[pages
- 1], top
+ 0x1000 * (pages
- 1), ((data
- 1) % 0x1000) + 1);
1392 _syscall(stat(path
, &info
));
1393 _syscall(chown(temp
, info
.st_uid
, info
.st_gid
));
1394 _syscall(chmod(temp
, info
.st_mode
));
1395 _syscall(unlink(path
));
1396 _syscall(rename(temp
, path
));
1401 } catch (const char *) {