66#include <mach-o/nlist.h>
77#include <stdint.h>
88#include <string.h>
9+ #ifndef COMPACT
10+ #include <mach/mach_error.h> // mach_error_string()
11+ #endif
912
1013int tiny_interpose (uint32_t image_index , const char * symbol_name , void * replacement ) {
1114 intptr_t image_slide = _dyld_get_image_vmaddr_slide (image_index );
1215 struct mach_header_64 * mh_header = (struct mach_header_64 * )_dyld_get_image_header (image_index );
1316 struct load_command * ld_command = (void * )mh_header + sizeof (struct mach_header_64 );
14- struct section_64 * sym_sects [2 ] = {NULL , NULL };
17+ struct section_64 * sym_sects [2 ] = {NULL , NULL }; // possible to have both!
1518 struct symtab_command * symtab_cmd = NULL ;
1619 struct dysymtab_command * dysymtab_cmd = NULL ;
1720 struct segment_command_64 * linkedit_cmd = NULL ;
@@ -24,7 +27,6 @@ int tiny_interpose(uint32_t image_index, const char *symbol_name, void *replacem
2427 for (int j = 0 ; j < segment -> nsects ; j ++ ) {
2528 if ((data_const_sect [j ].flags & SECTION_TYPE ) == S_NON_LAZY_SYMBOL_POINTERS ) {
2629 sym_sects [0 ] = data_const_sect + j ; // __nl_symbol_ptr
27- break ;
2830 }
2931 }
3032 }
@@ -34,7 +36,6 @@ int tiny_interpose(uint32_t image_index, const char *symbol_name, void *replacem
3436 for (int j = 0 ; j < segment -> nsects ; j ++ ) {
3537 if ((data_sect [j ].flags & SECTION_TYPE ) == S_LAZY_SYMBOL_POINTERS ) {
3638 sym_sects [1 ] = data_sect + j ; // __la_symbol_ptr
37- break ;
3839 }
3940 }
4041 }
@@ -78,15 +79,17 @@ int tiny_interpose(uint32_t image_index, const char *symbol_name, void *replacem
7879 if (i == 0 ) { // __nl_symbol_ptr in __DATA_CONST
7980 err = mach_vm_protect (mach_task_self (), (mach_vm_address_t )sym_ptrs , sym_sec -> size , FALSE,
8081 VM_PROT_READ | VM_PROT_WRITE | VM_PROT_COPY );
81- if (err != 0 ) goto exit ;
82+ if (err != 0 ) {
83+ LOG_ERROR ("mach_vm_protect: %s" , mach_error_string (err ));
84+ break ;
85+ }
8286 }
8387 sym_ptrs [j ] = replacement ;
84- goto exit ;
88+ break ;
8589 }
8690 }
8791 }
8892
89- exit :
9093 if (!found ) {
9194 err = -1 ;
9295 LOG_ERROR ("tiny_interpose: no matching indirect symbol found!" );
0 commit comments