1 #import <Foundation/Foundation.h>
4 #include <mach-o/nlist.h>
10 #include <mach/mach_init.h>
11 #include <mach/vm_map.h>
13 extern "C" void __clear_cache (char *beg, char *end);
15 extern "C" CFStringRef CPPhoneNumberCopyNetworkCountryCode();
17 static void ASReplace(void *symbol, void *replace) {
18 //NSLog(@"AS:Notice: Remapping %p to %p", symbol, replace);
22 int page = getpagesize();
23 uintptr_t base = (uintptr_t) symbol / page * page;
25 mach_port_t self = mach_task_self();
27 if (kern_return_t error = vm_protect(self, base, page, FALSE, VM_PROT_READ | VM_PROT_WRITE | VM_PROT_COPY)) {
28 NSLog(@"AS:Error:vm_protect():%d", error);
32 uint32_t *code = (uint32_t *) symbol;
34 code[0] = 0xe51ff004; // ldr pc, [pc, #-4]
35 code[1] = (uint32_t) replace;
37 __clear_cache(reinterpret_cast<char *>(code), reinterpret_cast<char *>(code + 2));
39 if (kern_return_t error = vm_protect(self, base, page, FALSE, VM_PROT_READ | VM_PROT_EXECUTE))
40 NSLog(@"AS:Error:vm_protect():%d", error);
43 /*static CFStringRef UIDefaultCountryCode_() {
44 return CPPhoneNumberCopyNetworkCountryCode();
47 #define UIKit "/System/Library/Frameworks/UIKit.framework/UIKit"
49 extern "C" void ASInitialize() {
50 if (dlopen(UIKit, RTLD_LAZY | RTLD_NOLOAD) == NULL)
52 //NSLog(@"AS:Notice: Installing AppSupport...");
55 memset(nl, 0, sizeof(nl));
56 nl[0].n_un.n_name = (char *) "_UIDefaultCountryCode";
58 CFStringRef (*UIDefaultCountryCode)() = reinterpret_cast<CFStringRef (*)()>(nl[0].n_value);
59 ASReplace(reinterpret_cast<void *>(UIDefaultCountryCode), reinterpret_cast<void *>(&CPPhoneNumberCopyNetworkCountryCode));
61 //NSLog(@"AS:Notice: Done");