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"
33 #include <sys/types.h>
41 #define FAT_MAGIC 0xcafebabe
42 #define FAT_CIGAM 0xbebafeca
62 #define MH_MAGIC 0xfeedface
63 #define MH_CIGAM 0xcefaedfe
65 #define MH_MAGIC_64 0xfeedfacf
66 #define MH_CIGAM_64 0xcffaedfe
68 #define MH_DYLDLINK 0x4
71 #define MH_EXECUTE 0x2
74 #define MH_DYLIB_STUB 0x9
81 #define LC_REQ_DYLD uint32_t(0x80000000)
83 #define LC_SEGMENT uint32_t(0x01)
84 #define LC_SYMTAB uint32_t(0x02)
85 #define LC_DYSYMTAB uint32_t(0x0b)
86 #define LC_LOAD_DYLIB uint32_t(0x0c)
87 #define LC_ID_DYLIB uint32_t(0x0d)
88 #define LC_SEGMENT_64 uint32_t(0x19)
89 #define LC_UUID uint32_t(0x1b)
90 #define LC_CODE_SIGNATURE uint32_t(0x1d)
91 #define LC_SEGMENT_SPLIT_INFO uint32_t(0x1e)
92 #define LC_REEXPORT_DYLIB uint32_t(0x1f | LC_REQ_DYLD)
93 #define LC_ENCRYPTION_INFO uint32_t(0x21)
94 #define LC_DYLD_INFO uint32_t(0x22)
95 #define LC_DYLD_INFO_ONLY uint32_t(0x22 | LC_REQ_DYLD)
100 uint32_t current_version
;
101 uint32_t compatibility_version
;
104 struct dylib_command
{
110 struct uuid_command
{
116 struct symtab_command
{
125 struct dyld_info_command
{
129 uint32_t rebase_size
;
132 uint32_t weak_bind_off
;
133 uint32_t weak_bind_size
;
134 uint32_t lazy_bind_off
;
135 uint32_t lazy_bind_size
;
137 uint32_t export_size
;
140 struct dysymtab_command
{
153 uint32_t extrefsymoff
;
154 uint32_t nextrefsyms
;
155 uint32_t indirectsymoff
;
156 uint32_t nindirectsyms
;
163 struct dylib_table_of_contents
{
164 uint32_t symbol_index
;
165 uint32_t module_index
;
168 struct dylib_module
{
169 uint32_t module_name
;
178 uint32_t iinit_iterm
;
179 uint32_t ninit_nterm
;
180 uint32_t objc_module_info_addr
;
181 uint32_t objc_module_info_size
;
184 struct dylib_reference
{
189 struct relocation_info
{
191 uint32_t r_symbolnum
:24;
210 struct segment_command
{
224 struct segment_command_64
{
266 struct linkedit_data_command
{
273 struct encryption_info_command
{
281 #define BIND_OPCODE_MASK 0xf0
282 #define BIND_IMMEDIATE_MASK 0x0f
283 #define BIND_OPCODE_DONE 0x00
284 #define BIND_OPCODE_SET_DYLIB_ORDINAL_IMM 0x10
285 #define BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB 0x20
286 #define BIND_OPCODE_SET_DYLIB_SPECIAL_IMM 0x30
287 #define BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM 0x40
288 #define BIND_OPCODE_SET_TYPE_IMM 0x50
289 #define BIND_OPCODE_SET_ADDEND_SLEB 0x60
290 #define BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB 0x70
291 #define BIND_OPCODE_ADD_ADDR_ULEB 0x80
292 #define BIND_OPCODE_DO_BIND 0x90
293 #define BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB 0xa0
294 #define BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED 0xb0
295 #define BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB 0xc0
297 uint16_t Swap_(uint16_t value
) {
299 ((value
>> 8) & 0x00ff) |
300 ((value
<< 8) & 0xff00);
303 uint32_t Swap_(uint32_t value
) {
304 value
= ((value
>> 8) & 0x00ff00ff) |
305 ((value
<< 8) & 0xff00ff00);
306 value
= ((value
>> 16) & 0x0000ffff) |
307 ((value
<< 16) & 0xffff0000);
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
));
321 uint16_t Swap(uint16_t value
) {
322 return little_
? Swap_(value
) : value
;
325 uint32_t Swap(uint32_t value
) {
326 return little_
? Swap_(value
) : value
;
329 int16_t Swap(int16_t value
) {
330 return Swap(static_cast<uint16_t>(value
));
333 int32_t Swap(int32_t value
) {
334 return Swap(static_cast<uint32_t>(value
));
337 template <typename Target_
>
349 Data(void *base
, size_t size
) :
356 uint16_t Swap(uint16_t value
) const {
357 return swapped_
? Swap_(value
) : value
;
360 uint32_t Swap(uint32_t value
) const {
361 return swapped_
? Swap_(value
) : value
;
364 int16_t Swap(int16_t value
) const {
365 return Swap(static_cast<uint16_t>(value
));
368 int32_t Swap(int32_t value
) const {
369 return Swap(static_cast<uint32_t>(value
));
372 void *GetBase() const {
376 size_t GetSize() const {
387 struct mach_header
*mach_header_
;
388 struct load_command
*load_command_
;
391 MachHeader(void *base
, size_t size
) :
394 mach_header_
= (mach_header
*) base
;
396 switch (Swap(mach_header_
->magic
)) {
398 swapped_
= !swapped_
;
404 swapped_
= !swapped_
;
413 void *post
= mach_header_
+ 1;
415 post
= (uint32_t *) post
+ 1;
416 load_command_
= (struct load_command
*) post
;
419 Swap(mach_header_
->filetype
) == MH_EXECUTE
||
420 Swap(mach_header_
->filetype
) == MH_DYLIB
||
421 Swap(mach_header_
->filetype
) == MH_BUNDLE
425 struct mach_header
*operator ->() const {
429 uint32_t GetCPUType() const {
430 return Swap(mach_header_
->cputype
);
433 uint16_t GetCPUSubtype() const {
434 return Swap(mach_header_
->cpusubtype
) & 0xff;
437 std::vector
<struct load_command
*> GetLoadCommands() const {
438 std::vector
<struct load_command
*> load_commands
;
440 struct load_command
*load_command
= load_command_
;
441 for (uint32_t cmd
= 0; cmd
!= Swap(mach_header_
->ncmds
); ++cmd
) {
442 load_commands
.push_back(load_command
);
443 load_command
= (struct load_command
*) ((uint8_t *) load_command
+ Swap(load_command
->cmdsize
));
446 return load_commands
;
449 std::vector
<segment_command
*> GetSegments(const char *segment_name
) const {
450 std::vector
<struct segment_command
*> segment_commands
;
452 _foreach (load_command
, GetLoadCommands()) {
453 if (Swap(load_command
->cmd
) == LC_SEGMENT
) {
454 segment_command
*segment_command
= reinterpret_cast<struct segment_command
*>(load_command
);
455 if (strncmp(segment_command
->segname
, segment_name
, 16) == 0)
456 segment_commands
.push_back(segment_command
);
460 return segment_commands
;
463 std::vector
<segment_command_64
*> GetSegments64(const char *segment_name
) {
464 std::vector
<struct segment_command_64
*> segment_commands
;
466 _foreach (load_command
, GetLoadCommands()) {
467 if (Swap(load_command
->cmd
) == LC_SEGMENT_64
) {
468 segment_command_64
*segment_command
= reinterpret_cast<struct segment_command_64
*>(load_command
);
469 if (strncmp(segment_command
->segname
, segment_name
, 16) == 0)
470 segment_commands
.push_back(segment_command
);
474 return segment_commands
;
477 std::vector
<section
*> GetSections(const char *segment_name
, const char *section_name
) const {
478 std::vector
<section
*> sections
;
480 _foreach (segment
, GetSegments(segment_name
)) {
481 section
*section
= (struct section
*) (segment
+ 1);
484 for (sect
= 0; sect
!= Swap(segment
->nsects
); ++sect
) {
485 if (strncmp(section
->sectname
, section_name
, 16) == 0)
486 sections
.push_back(section
);
494 template <typename Target_
>
495 Pointer
<Target_
> GetPointer(uint32_t address
, const char *segment_name
= NULL
) const {
496 load_command
*load_command
= (struct load_command
*) (mach_header_
+ 1);
499 for (cmd
= 0; cmd
!= Swap(mach_header_
->ncmds
); ++cmd
) {
500 if (Swap(load_command
->cmd
) == LC_SEGMENT
) {
501 segment_command
*segment_command
= (struct segment_command
*) load_command
;
502 if (segment_name
!= NULL
&& strncmp(segment_command
->segname
, segment_name
, 16) != 0)
505 section
*sections
= (struct section
*) (segment_command
+ 1);
508 for (sect
= 0; sect
!= Swap(segment_command
->nsects
); ++sect
) {
509 section
*section
= §ions
[sect
];
510 //printf("%s %u %p %p %u\n", segment_command->segname, sect, address, section->addr, section->size);
511 if (address
>= Swap(section
->addr
) && address
< Swap(section
->addr
) + Swap(section
->size
)) {
512 //printf("0x%.8x %s\n", address, segment_command->segname);
513 return Pointer
<Target_
>(this, reinterpret_cast<Target_
*>(address
- Swap(section
->addr
) + Swap(section
->offset
) + (char *) mach_header_
));
519 load_command
= (struct load_command
*) ((char *) load_command
+ Swap(load_command
->cmdsize
));
522 return Pointer
<Target_
>(this);
525 template <typename Target_
>
526 Pointer
<Target_
> GetOffset(uint32_t offset
) {
527 return Pointer
<Target_
>(this, reinterpret_cast<Target_
*>(offset
+ (uint8_t *) mach_header_
));
531 class FatMachHeader
:
538 FatMachHeader(void *base
, size_t size
, fat_arch
*fat_arch
) :
539 MachHeader(base
, size
),
544 fat_arch
*GetFatArch() const {
553 fat_header
*fat_header_
;
554 std::vector
<FatMachHeader
> mach_headers_
;
557 FatHeader(void *base
, size_t size
) :
560 fat_header_
= reinterpret_cast<struct fat_header
*>(base
);
562 if (Swap(fat_header_
->magic
) == FAT_CIGAM
) {
563 swapped_
= !swapped_
;
565 } else if (Swap(fat_header_
->magic
) != FAT_MAGIC
) {
567 mach_headers_
.push_back(FatMachHeader(base
, size
, NULL
));
569 size_t fat_narch
= Swap(fat_header_
->nfat_arch
);
570 fat_arch
*fat_arch
= reinterpret_cast<struct fat_arch
*>(fat_header_
+ 1);
572 for (arch
= 0; arch
!= fat_narch
; ++arch
) {
573 uint32_t arch_offset
= Swap(fat_arch
->offset
);
574 uint32_t arch_size
= Swap(fat_arch
->size
);
575 mach_headers_
.push_back(FatMachHeader((uint8_t *) base
+ arch_offset
, arch_size
, fat_arch
));
581 std::vector
<FatMachHeader
> &GetMachHeaders() {
582 return mach_headers_
;
586 return fat_header_
!= NULL
;
589 struct fat_header
*operator ->() const {
594 FatHeader
Map(const char *path
, bool ro
= false) {
596 void *base(map(path
, 0, _not(size_t), &size
, ro
));
597 return FatHeader(base
, size
);
600 template <typename Target_
>
603 const MachHeader
*framework_
;
604 const Target_
*pointer_
;
607 Pointer(const MachHeader
*framework
= NULL
, const Target_
*pointer
= NULL
) :
608 framework_(framework
),
613 operator const Target_
*() const {
617 const Target_
*operator ->() const {
621 Pointer
<Target_
> &operator ++() {
626 template <typename Value_
>
627 Value_
Swap(Value_ value
) {
628 return framework_
->Swap(value
);
632 #define CSMAGIC_CODEDIRECTORY uint32_t(0xfade0c02)
633 #define CSMAGIC_EMBEDDED_SIGNATURE uint32_t(0xfade0cc0)
634 #define CSMAGIC_ENTITLEMENTS uint32_t(0xfade7171)
636 #define CSSLOT_CODEDIRECTORY uint32_t(0)
637 #define CSSLOT_REQUIREMENTS uint32_t(2)
638 #define CSSLOT_ENTITLEMENTS uint32_t(5)
653 struct BlobIndex index
[];
656 struct CodeDirectory
{
661 uint32_t identOffset
;
662 uint32_t nSpecialSlots
;
672 extern "C" uint32_t hash(uint8_t *k
, uint32_t length
, uint32_t initval
);
674 void sha1(uint8_t *hash
, uint8_t *data
, size_t size
) {
677 SHA1Input(&context
, data
, size
);
678 SHA1Result(&context
, hash
);
681 struct CodesignAllocation
{
686 CodesignAllocation(uint32_t type
, uint16_t subtype
, size_t size
) :
694 int main(int argc
, const char *argv
[]) {
700 little_
= endian
.byte
[0];
723 uint32_t flag_CPUType(_not(uint32_t));
724 uint32_t flag_CPUSubtype(_not(uint32_t));
729 const void *xmld(NULL
);
732 uintptr_t noffset(_not(uintptr_t));
733 uintptr_t woffset(_not(uintptr_t));
735 std::vector
<std::string
> files
;
738 fprintf(stderr
, "usage: %s -S[entitlements.xml] <binary>\n", argv
[0]);
739 fprintf(stderr
, " %s -e MobileSafari\n", argv
[0]);
740 fprintf(stderr
, " %s -S cat\n", argv
[0]);
741 fprintf(stderr
, " %s -Stfp.xml gdb\n", argv
[0]);
745 for (int argi(1); argi
!= argc
; ++argi
)
746 if (argv
[argi
][0] != '-')
747 files
.push_back(argv
[argi
]);
748 else switch (argv
[argi
][1]) {
749 case 'R': flag_R
= true; break;
750 case 'r': flag_r
= true; break;
752 case 't': flag_t
= true; break;
753 case 'u': flag_u
= true; break;
754 case 'p': flag_p
= true; break;
755 case 'e': flag_e
= true; break;
756 case 'O': flag_O
= true; break;
758 case 'D': flag_D
= true; break;
759 case 'd': flag_d
= true; break;
761 case 'a': flag_a
= true; break;
765 if (argv
[argi
][2] != '\0') {
766 const char *cpu
= argv
[argi
] + 2;
767 const char *colon
= strchr(cpu
, ':');
768 _assert(colon
!= NULL
);
770 flag_CPUType
= strtoul(cpu
, &arge
, 0);
771 _assert(arge
== colon
);
772 flag_CPUSubtype
= strtoul(colon
+ 1, &arge
, 0);
773 _assert(arge
== argv
[argi
] + strlen(argv
[argi
]));
785 if (argv
[argi
][2] != '\0') {
786 const char *xml
= argv
[argi
] + 2;
787 xmld
= map(xml
, 0, _not(size_t), &xmls
, true);
793 if (argv
[argi
][2] == '-')
797 timev
= strtoul(argv
[argi
] + 2, &arge
, 0);
798 _assert(arge
== argv
[argi
] + strlen(argv
[argi
]));
804 noffset
= strtoul(argv
[argi
] + 2, &arge
, 0);
805 _assert(arge
== argv
[argi
] + strlen(argv
[argi
]));
810 woffset
= strtoul(argv
[argi
] + 2, &arge
, 0);
811 _assert(arge
== argv
[argi
] + strlen(argv
[argi
]));
819 if (files
.empty()) usage
: {
823 size_t filei(0), filee(0);
824 _foreach (file
, files
) try {
825 const char *path(file
.c_str());
826 const char *base
= strrchr(path
, '/');
827 char *temp(NULL
), *dir
;
830 dir
= strndup_(path
, base
++ - path
+ 1);
838 FatHeader
fat_header(Map(path
));
839 _foreach (mach_header
, fat_header
.GetMachHeaders()) {
841 if (mach_header
.GetCPUType() != flag_CPUType
)
843 if (mach_header
.GetCPUSubtype() != flag_CPUSubtype
)
847 mach_header
->flags
= mach_header
.Swap(mach_header
.Swap(mach_header
->flags
) | MH_DYLDLINK
);
849 uint32_t size(_not(uint32_t)); {
850 _foreach (load_command
, mach_header
.GetLoadCommands()) {
851 switch (mach_header
.Swap(load_command
->cmd
)) {
852 case LC_CODE_SIGNATURE
: {
853 struct linkedit_data_command
*signature
= reinterpret_cast<struct linkedit_data_command
*>(load_command
);
854 memset(reinterpret_cast<uint8_t *>(mach_header
.GetBase()) + mach_header
.Swap(signature
->dataoff
), 0, mach_header
.Swap(signature
->datasize
));
855 memset(signature
, 0, sizeof(struct linkedit_data_command
));
857 mach_header
->ncmds
= mach_header
.Swap(mach_header
.Swap(mach_header
->ncmds
) - 1);
858 mach_header
->sizeofcmds
= mach_header
.Swap(uint32_t(mach_header
.Swap(mach_header
->sizeofcmds
) - sizeof(struct linkedit_data_command
)));
862 struct symtab_command
*symtab
= reinterpret_cast<struct symtab_command
*>(load_command
);
863 size
= mach_header
.Swap(symtab
->stroff
) + mach_header
.Swap(symtab
->strsize
);
869 _assert(size
!= _not(uint32_t));
871 _foreach (segment
, const_cast<FatMachHeader
&>(mach_header
).GetSegments("__LINKEDIT")) {
872 segment
->filesize
-= mach_header
.GetSize() - size
;
874 if (fat_arch
*fat_arch
= mach_header
.GetFatArch()) {
875 fat_arch
->size
= fat_header
.Swap(size
);
876 clip
= std::max(clip
, fat_header
.Swap(fat_arch
->offset
) + size
);
878 clip
= std::max(clip
, size
);
881 _foreach (segment
, const_cast<FatMachHeader
&>(mach_header
).GetSegments64("__LINKEDIT")) {
882 segment
->filesize
-= mach_header
.GetSize() - size
;
884 if (fat_arch
*fat_arch
= mach_header
.GetFatArch()) {
885 fat_arch
->size
= fat_header
.Swap(size
);
886 clip
= std::max(clip
, fat_header
.Swap(fat_arch
->offset
) + size
);
888 clip
= std::max(clip
, size
);
894 _syscall(truncate(path
, clip
));
898 asprintf(&temp
, "%s.%s.cs", dir
, base
);
899 const char *allocate
= getenv("CODESIGN_ALLOCATE");
900 if (allocate
== NULL
)
901 allocate
= "codesign_allocate";
903 std::vector
<CodesignAllocation
> allocations
; {
904 FatHeader
fat_header(Map(path
));
905 _foreach (mach_header
, fat_header
.GetMachHeaders()) {
907 if (mach_header
.GetCPUType() != flag_CPUType
)
909 if (mach_header
.GetCPUSubtype() != flag_CPUSubtype
)
913 mach_header
->flags
= mach_header
.Swap(mach_header
.Swap(mach_header
->flags
) | MH_DYLDLINK
);
915 size_t size(_not(size_t)); {
916 _foreach (load_command
, mach_header
.GetLoadCommands()) {
917 uint32_t cmd(mach_header
.Swap(load_command
->cmd
));
918 if (cmd
== LC_CODE_SIGNATURE
) {
919 struct linkedit_data_command
*signature
= reinterpret_cast<struct linkedit_data_command
*>(load_command
);
920 size
= mach_header
.Swap(signature
->dataoff
);
921 _assert(size
< mach_header
.GetSize());
926 if (size
== _not(size_t))
927 size
= mach_header
.GetSize();
930 allocations
.push_back(CodesignAllocation(mach_header
.GetCPUType(), mach_header
.GetCPUSubtype(), size
));
934 if (!allocations
.empty()) {
939 // XXX: this leaks memory, but it doesn't really matter
940 std::vector
<const char *> args
;
943 args
.push_back(allocate
);
945 args
.push_back("-i");
946 args
.push_back(path
);
948 _foreach (allocation
, allocations
) {
949 if (allocation
.type_
== 12 && (
950 allocation
.subtype_
== 0 ||
951 allocation
.subtype_
== 6 ||
953 // Telesphoreo codesign_allocate
954 args
.push_back("-a");
957 switch (allocation
.subtype_
) {
969 _assert(arch
!= NULL
);
970 args
.push_back(arch
);
972 args
.push_back("-A");
974 asprintf(&arg
, "%u", allocation
.type_
);
977 asprintf(&arg
, "%u", allocation
.subtype_
);
982 alloc
+= sizeof(struct SuperBlob
);
985 special
= std::max(special
, CSSLOT_CODEDIRECTORY
);
986 alloc
+= sizeof(struct BlobIndex
);
987 alloc
+= sizeof(struct CodeDirectory
);
988 alloc
+= strlen(base
) + 1;
990 special
= std::max(special
, CSSLOT_REQUIREMENTS
);
991 alloc
+= sizeof(struct BlobIndex
);
995 special
= std::max(special
, CSSLOT_ENTITLEMENTS
);
996 alloc
+= sizeof(struct BlobIndex
);
997 alloc
+= sizeof(struct Blob
);
1001 size_t normal((allocation
.size_
+ 0x1000 - 1) / 0x1000);
1002 alloc
+= (special
+ normal
) * 0x14;
1008 asprintf(&arg
, "%zu", alloc
);
1009 args
.push_back(arg
);
1012 args
.push_back("-o");
1013 args
.push_back(temp
);
1015 args
.push_back(NULL
);
1019 _foreach (arg
, args
)
1024 execvp(allocate
, (char **) &args
[0]);
1029 _syscall(waitpid(pid
, &status
, 0));
1030 _assert(WIFEXITED(status
));
1031 _assert(WEXITSTATUS(status
) == 0);
1037 printf("path%zu='%s'\n", filei
, file
.c_str());
1039 FatHeader
fat_header(Map(temp
== NULL
? path
: temp
, !(flag_R
|| flag_T
|| flag_s
|| flag_S
|| flag_O
|| flag_D
)));
1040 struct linkedit_data_command
*signature(NULL
);
1042 _foreach (mach_header
, fat_header
.GetMachHeaders()) {
1044 if (mach_header
.GetCPUType() != flag_CPUType
)
1046 if (mach_header
.GetCPUSubtype() != flag_CPUSubtype
)
1051 printf("cpu=0x%x:0x%x\n", mach_header
.GetCPUType(), mach_header
.GetCPUSubtype());
1054 if (struct fat_arch
*fat_arch
= mach_header
.GetFatArch())
1055 printf("offset=0x%x\n", Swap(fat_arch
->offset
));
1057 printf("offset=0x0\n");
1060 if (woffset
!= _not(uintptr_t)) {
1061 Pointer
<uint32_t> wvalue(mach_header
.GetPointer
<uint32_t>(woffset
));
1063 printf("(null) %p\n", reinterpret_cast<void *>(woffset
));
1065 printf("0x%.08x\n", *wvalue
);
1068 if (noffset
!= _not(uintptr_t))
1069 printf("%s\n", &*mach_header
.GetPointer
<char>(noffset
));
1072 _foreach(segment
, mach_header
.GetSegments("__TEXT")) {
1073 printf("vmaddr=0x%x\n", mach_header
.Swap(segment
->vmaddr
));
1074 printf("fileoff=0x%x\n", mach_header
.Swap(segment
->fileoff
));
1078 _foreach(section
, mach_header
.GetSections("__TEXT", "__text"))
1079 section
->addr
= mach_header
.Swap(0);
1082 _foreach (load_command
, mach_header
.GetLoadCommands()) {
1083 uint32_t cmd(mach_header
.Swap(load_command
->cmd
));
1085 if (flag_R
&& cmd
== LC_REEXPORT_DYLIB
)
1086 load_command
->cmd
= mach_header
.Swap(LC_LOAD_DYLIB
);
1087 else if (cmd
== LC_CODE_SIGNATURE
)
1088 signature
= reinterpret_cast<struct linkedit_data_command
*>(load_command
);
1089 else if (cmd
== LC_UUID
) {
1090 volatile struct uuid_command
*uuid_command(reinterpret_cast<struct uuid_command
*>(load_command
));
1093 printf("uuid%zu=%.2x%.2x%.2x%.2x-%.2x%.2x-%.2x%.2x-%.2x%.2x-%.2x%.2x%.2x%.2x%.2x%.2x\n", filei
,
1094 uuid_command
->uuid
[ 0], uuid_command
->uuid
[ 1], uuid_command
->uuid
[ 2], uuid_command
->uuid
[ 3],
1095 uuid_command
->uuid
[ 4], uuid_command
->uuid
[ 5], uuid_command
->uuid
[ 6], uuid_command
->uuid
[ 7],
1096 uuid_command
->uuid
[ 8], uuid_command
->uuid
[ 9], uuid_command
->uuid
[10], uuid_command
->uuid
[11],
1097 uuid_command
->uuid
[12], uuid_command
->uuid
[13], uuid_command
->uuid
[14], uuid_command
->uuid
[15]
1100 } else if (cmd
== LC_ID_DYLIB
) {
1101 volatile struct dylib_command
*dylib_command(reinterpret_cast<struct dylib_command
*>(load_command
));
1104 printf("time%zu=0x%.8x\n", filei
, mach_header
.Swap(dylib_command
->dylib
.timestamp
));
1112 dylib_command
->dylib
.timestamp
= 0;
1113 timed
= hash(reinterpret_cast<uint8_t *>(mach_header
.GetBase()), mach_header
.GetSize(), timev
);
1116 dylib_command
->dylib
.timestamp
= mach_header
.Swap(timed
);
1118 } else if (cmd
== LC_ENCRYPTION_INFO
) {
1119 volatile struct encryption_info_command
*encryption_info_command(reinterpret_cast<struct encryption_info_command
*>(load_command
));
1122 encryption_info_command
->cryptid
= mach_header
.Swap(0);
1125 printf("cryptoff=0x%x\n", mach_header
.Swap(encryption_info_command
->cryptoff
));
1126 printf("cryptsize=0x%x\n", mach_header
.Swap(encryption_info_command
->cryptsize
));
1127 printf("cryptid=0x%x\n", mach_header
.Swap(encryption_info_command
->cryptid
));
1133 _assert(signature
!= NULL
);
1135 uint32_t data
= mach_header
.Swap(signature
->dataoff
);
1137 uint8_t *top
= reinterpret_cast<uint8_t *>(mach_header
.GetBase());
1138 uint8_t *blob
= top
+ data
;
1139 struct SuperBlob
*super
= reinterpret_cast<struct SuperBlob
*>(blob
);
1141 for (size_t index(0); index
!= Swap(super
->count
); ++index
)
1142 if (Swap(super
->index
[index
].type
) == CSSLOT_ENTITLEMENTS
) {
1143 uint32_t begin
= Swap(super
->index
[index
].offset
);
1144 struct Blob
*entitlements
= reinterpret_cast<struct Blob
*>(blob
+ begin
);
1145 fwrite(entitlements
+ 1, 1, Swap(entitlements
->length
) - sizeof(struct Blob
), stdout
);
1150 _assert(signature
!= NULL
);
1152 uint32_t data
= mach_header
.Swap(signature
->dataoff
);
1154 uint8_t *top
= reinterpret_cast<uint8_t *>(mach_header
.GetBase());
1155 uint8_t *blob
= top
+ data
;
1156 struct SuperBlob
*super
= reinterpret_cast<struct SuperBlob
*>(blob
);
1158 for (size_t index(0); index
!= Swap(super
->count
); ++index
)
1159 if (Swap(super
->index
[index
].type
) == CSSLOT_CODEDIRECTORY
) {
1160 uint32_t begin
= Swap(super
->index
[index
].offset
);
1161 struct CodeDirectory
*directory
= reinterpret_cast<struct CodeDirectory
*>(blob
+ begin
);
1163 uint8_t (*hashes
)[20] = reinterpret_cast<uint8_t (*)[20]>(blob
+ begin
+ Swap(directory
->hashOffset
));
1164 uint32_t pages
= Swap(directory
->nCodeSlots
);
1167 for (size_t i
= 0; i
!= pages
- 1; ++i
)
1168 sha1(hashes
[i
], top
+ 0x1000 * i
, 0x1000);
1170 sha1(hashes
[pages
- 1], top
+ 0x1000 * (pages
- 1), ((data
- 1) % 0x1000) + 1);
1175 _assert(signature
!= NULL
);
1177 uint32_t data
= mach_header
.Swap(signature
->dataoff
);
1178 uint32_t size
= mach_header
.Swap(signature
->datasize
);
1180 uint8_t *top
= reinterpret_cast<uint8_t *>(mach_header
.GetBase());
1181 uint8_t *blob
= top
+ data
;
1182 struct SuperBlob
*super
= reinterpret_cast<struct SuperBlob
*>(blob
);
1183 super
->blob
.magic
= Swap(CSMAGIC_EMBEDDED_SIGNATURE
);
1185 uint32_t count
= xmld
== NULL
? 2 : 3;
1186 uint32_t offset
= sizeof(struct SuperBlob
) + count
* sizeof(struct BlobIndex
);
1188 super
->index
[0].type
= Swap(CSSLOT_CODEDIRECTORY
);
1189 super
->index
[0].offset
= Swap(offset
);
1191 uint32_t begin
= offset
;
1192 struct CodeDirectory
*directory
= reinterpret_cast<struct CodeDirectory
*>(blob
+ begin
);
1193 offset
+= sizeof(struct CodeDirectory
);
1195 directory
->blob
.magic
= Swap(CSMAGIC_CODEDIRECTORY
);
1196 directory
->version
= Swap(uint32_t(0x00020001));
1197 directory
->flags
= Swap(uint32_t(0));
1198 directory
->codeLimit
= Swap(data
);
1199 directory
->hashSize
= 0x14;
1200 directory
->hashType
= 0x01;
1201 directory
->spare1
= 0x00;
1202 directory
->pageSize
= 0x0c;
1203 directory
->spare2
= Swap(uint32_t(0));
1205 directory
->identOffset
= Swap(offset
- begin
);
1206 strcpy(reinterpret_cast<char *>(blob
+ offset
), base
);
1207 offset
+= strlen(base
) + 1;
1209 uint32_t special
= xmld
== NULL
? CSSLOT_REQUIREMENTS
: CSSLOT_ENTITLEMENTS
;
1210 directory
->nSpecialSlots
= Swap(special
);
1212 uint8_t (*hashes
)[20] = reinterpret_cast<uint8_t (*)[20]>(blob
+ offset
);
1213 memset(hashes
, 0, sizeof(*hashes
) * special
);
1215 offset
+= sizeof(*hashes
) * special
;
1218 uint32_t pages
= (data
+ 0x1000 - 1) / 0x1000;
1219 directory
->nCodeSlots
= Swap(pages
);
1222 for (size_t i
= 0; i
!= pages
- 1; ++i
)
1223 sha1(hashes
[i
], top
+ 0x1000 * i
, 0x1000);
1225 sha1(hashes
[pages
- 1], top
+ 0x1000 * (pages
- 1), ((data
- 1) % 0x1000) + 1);
1227 directory
->hashOffset
= Swap(offset
- begin
);
1228 offset
+= sizeof(*hashes
) * pages
;
1229 directory
->blob
.length
= Swap(offset
- begin
);
1231 super
->index
[1].type
= Swap(CSSLOT_REQUIREMENTS
);
1232 super
->index
[1].offset
= Swap(offset
);
1234 memcpy(blob
+ offset
, "\xfa\xde\x0c\x01\x00\x00\x00\x0c\x00\x00\x00\x00", 0xc);
1238 super
->index
[2].type
= Swap(CSSLOT_ENTITLEMENTS
);
1239 super
->index
[2].offset
= Swap(offset
);
1241 uint32_t begin
= offset
;
1242 struct Blob
*entitlements
= reinterpret_cast<struct Blob
*>(blob
+ begin
);
1243 offset
+= sizeof(struct Blob
);
1245 memcpy(blob
+ offset
, xmld
, xmls
);
1248 entitlements
->magic
= Swap(CSMAGIC_ENTITLEMENTS
);
1249 entitlements
->length
= Swap(offset
- begin
);
1252 for (size_t index(0); index
!= count
; ++index
) {
1253 uint32_t type
= Swap(super
->index
[index
].type
);
1254 if (type
!= 0 && type
<= special
) {
1255 uint32_t offset
= Swap(super
->index
[index
].offset
);
1256 struct Blob
*local
= (struct Blob
*) (blob
+ offset
);
1257 sha1((uint8_t *) (hashes
- type
), (uint8_t *) local
, Swap(local
->length
));
1261 super
->count
= Swap(count
);
1262 super
->blob
.length
= Swap(offset
);
1264 if (offset
> size
) {
1265 fprintf(stderr
, "offset (%u) > size (%u)\n", offset
, size
);
1267 } //else fprintf(stderr, "offset (%zu) <= size (%zu)\n", offset, size);
1269 memset(blob
+ offset
, 0, size
- offset
);
1274 uint8_t *top
= reinterpret_cast<uint8_t *>(fat_header
.GetBase());
1275 size_t size
= fat_header
.GetSize();
1278 asprintf(©
, "%s.%s.cp", dir
, base
);
1279 FILE *file
= fopen(copy
, "w+");
1280 size_t writ
= fwrite(top
, 1, size
, file
);
1281 _assert(writ
== size
);
1284 _syscall(unlink(temp
));
1291 _syscall(stat(path
, &info
));
1292 _syscall(chown(temp
, info
.st_uid
, info
.st_gid
));
1293 _syscall(chmod(temp
, info
.st_mode
));
1294 _syscall(unlink(path
));
1295 _syscall(rename(temp
, path
));
1301 } catch (const char *) {