]> git.saurik.com Git - apple/dyld.git/blobdiff - src/glue.c
dyld-360.19.tar.gz
[apple/dyld.git] / src / glue.c
index 69b5f2283e596f9e9afab160b9730c61f007e02d..bc7741f3ec2b864f0063e7d22e6180cff12578fa 100644 (file)
 #include <pthread.h>
 #if TARGET_IPHONE_SIMULATOR
        #include "dyldSyscallInterface.h"
+       #include "dyld_images.h"
+       #include <mach-o/loader.h>
+       #include <mach-o/nlist.h>
+       #include <mach/kern_return.h>
+       #if __LP64__
+               #define LC_SEGMENT_COMMAND                      LC_SEGMENT_64
+               typedef struct segment_command_64       macho_segment_command;
+               typedef struct mach_header_64           macho_header;
+               typedef struct nlist_64                         macho_nlist;
+       #else
+               #define LC_SEGMENT_COMMAND                      LC_SEGMENT
+               typedef struct segment_command          macho_segment_command;
+               typedef struct mach_header                      macho_header;
+               typedef struct nlist                            macho_nlist;
+       #endif
 #endif
 
 // from _simple.h in libc
@@ -108,10 +123,12 @@ void __assert_rtn(const char* func, const char* file, int line, const char* fail
 }
 
 
+int    myfprintf(FILE* file, const char* format, ...) __asm("_fprintf");
+
 // called by libuwind code before aborting
 size_t fwrite(const void* ptr, size_t size, size_t nitme, FILE* stream)
 {
-       return fprintf(stream, "%s", (char*)ptr); 
+       return myfprintf(stream, "%s", (char*)ptr); 
 }
 
 // called by libuwind code before aborting
@@ -323,6 +340,9 @@ int _ZN4dyld7my_openEPKcii(const char* path, int flag, int other)
 //
 
 #if TARGET_IPHONE_SIMULATOR
+
+#include <coreSymbolicationDyldSupport.h>
+
 int myopen(const char* path, int oflag, int extra) __asm("_open");
 int myopen(const char* path, int oflag, int extra) {
        return gSyscallHelpers->open(path, oflag, extra);
@@ -447,6 +467,177 @@ uint64_t mach_absolute_time(void) {
        return gSyscallHelpers->mach_absolute_time();
 } 
 
+kern_return_t thread_switch(mach_port_name_t thread_name,
+                                                       int option, mach_msg_timeout_t option_time) {
+       if ( gSyscallHelpers->version < 2 )
+               return KERN_FAILURE;
+       return gSyscallHelpers->thread_switch(thread_name, option, option_time);
+}
+
+DIR* opendir(const char* path) {
+       if ( gSyscallHelpers->version < 3 )
+               return NULL;
+       return gSyscallHelpers->opendir(path);
+}
+
+int    readdir_r(DIR* dirp, struct dirent* entry, struct dirent **result) {
+       if ( gSyscallHelpers->version < 3 )
+               return EPERM;
+       return gSyscallHelpers->readdir_r(dirp, entry, result);
+}
+
+int closedir(DIR* dirp) {
+       if ( gSyscallHelpers->version < 3 )
+               return EPERM;
+       return gSyscallHelpers->closedir(dirp);
+}
+
+#define SUPPORT_HOST_10_10  1
+
+#if SUPPORT_HOST_10_10
+typedef int               (*FuncPtr_nanosleep)(const struct timespec*, struct timespec*);
+typedef kern_return_t     (*FuncPtr_mach_port_allocate)(ipc_space_t, mach_port_right_t, mach_port_name_t*);
+typedef mach_msg_return_t (*FuncPtr_mach_msg)(mach_msg_header_t *, mach_msg_option_t , mach_msg_size_t , mach_msg_size_t , mach_port_name_t , mach_msg_timeout_t , mach_port_name_t);
+typedef int               (*FuncPtr_kill)(pid_t pid, int sig);
+typedef pid_t             (*FuncPtr_getpid)();
+typedef bool              (*FuncPtr_OSAtomicCompareAndSwap32)(int32_t, int32_t, volatile int32_t*);
+
+static FuncPtr_nanosleep                 proc_nanosleep = NULL;
+static FuncPtr_mach_port_allocate        proc_mach_port_allocate = NULL;
+static FuncPtr_mach_msg                  proc_mach_msg = NULL;
+static FuncPtr_kill                      proc_kill = NULL;
+static FuncPtr_getpid                    proc_getpid = NULL;
+static FuncPtr_OSAtomicCompareAndSwap32  proc_OSAtomicCompareAndSwap32 = NULL;
+
+
+int nanosleep(const struct timespec* p1, struct timespec* p2)
+{
+       return (*proc_nanosleep)(p1, p2);
+}
+
+kern_return_t mach_port_allocate(ipc_space_t p1, mach_port_right_t p2, mach_port_name_t* p3)
+{
+       return (*proc_mach_port_allocate)(p1, p2, p3);
+}
+
+mach_msg_return_t mach_msg(mach_msg_header_t* p1, mach_msg_option_t p2, mach_msg_size_t p3, mach_msg_size_t p4, mach_port_name_t p5, mach_msg_timeout_t p6, mach_port_name_t p7)
+{
+       return (*proc_mach_msg)(p1, p2, p3, p4, p5, p6, p7);
+}
+
+int kill(pid_t p1, int p2)
+{
+       return (*proc_kill)(p1, p2);
+}
+
+pid_t getpid()
+{
+       return (*proc_getpid)();
+}
+
+bool OSAtomicCompareAndSwap32(int32_t p1, int32_t p2, volatile int32_t* p3)
+{
+       return (*proc_OSAtomicCompareAndSwap32)(p1, p2, p3);
+}
+
+
+// Look up sycalls in host dyld needed by coresymbolication_ routines in dyld_sim
+static void findHostFunctions() {
+       // Only look up symbols once
+       if ( proc_nanosleep != NULL )
+               return;
+
+       struct dyld_all_image_infos* imageInfo = (struct dyld_all_image_infos*)(gSyscallHelpers->getProcessInfo());
+       const struct mach_header* hostDyldMH = imageInfo->dyldImageLoadAddress;
+
+       // find symbol table and slide of host dyld
+       uintptr_t slide = 0;
+       const macho_nlist* symbolTable = NULL;
+       const char* symbolTableStrings = NULL;
+       const struct dysymtab_command* dynSymbolTable = NULL;
+       const uint32_t cmd_count = hostDyldMH->ncmds;
+       const struct load_command* const cmds = (struct load_command*)(((char*)hostDyldMH)+sizeof(macho_header));
+       const struct load_command* cmd = cmds;
+       const uint8_t* linkEditBase = NULL;
+       for (uint32_t i = 0; i < cmd_count; ++i) {
+               switch (cmd->cmd) {
+                       case LC_SEGMENT_COMMAND:
+                               {
+                                       const macho_segment_command* seg = (macho_segment_command*)cmd;
+                                       if ( (seg->fileoff == 0) && (seg->filesize != 0) )
+                                               slide = (uintptr_t)hostDyldMH - seg->vmaddr;
+                                       if ( strcmp(seg->segname, "__LINKEDIT") == 0 )
+                                               linkEditBase = (uint8_t*)(seg->vmaddr - seg->fileoff + slide);
+                               }
+                               break;
+                       case LC_SYMTAB:
+                               {
+                                       const struct symtab_command* symtab = (struct symtab_command*)cmd;
+                                       if ( linkEditBase == NULL )
+                                               return;
+                                       symbolTableStrings = (const char*)&linkEditBase[symtab->stroff];
+                                       symbolTable = (macho_nlist*)(&linkEditBase[symtab->symoff]);
+                               }
+                               break;
+                       case LC_DYSYMTAB:
+                               dynSymbolTable = (struct dysymtab_command*)cmd;
+                               break;
+               }
+               cmd = (const struct load_command*)(((char*)cmd)+cmd->cmdsize);
+       }
+       if ( symbolTableStrings == NULL )
+               return;
+       if ( dynSymbolTable == NULL )
+               return;
+
+       // scan local symbols in host dyld looking for load/unload functions
+       const macho_nlist* const localsStart = &symbolTable[dynSymbolTable->ilocalsym];
+       const macho_nlist* const localsEnd= &localsStart[dynSymbolTable->nlocalsym];
+       for (const macho_nlist* s = localsStart; s < localsEnd; ++s) {
+               if ( ((s->n_type & N_TYPE) == N_SECT) && ((s->n_type & N_STAB) == 0) ) {
+                       const char* name = &symbolTableStrings[s->n_un.n_strx];
+                       if ( strcmp(name, "_nanosleep") == 0 )
+                               proc_nanosleep = (FuncPtr_nanosleep)(s->n_value + slide);
+                       else if ( strcmp(name, "_mach_port_allocate") == 0 )
+                               proc_mach_port_allocate = (FuncPtr_mach_port_allocate)(s->n_value + slide);
+                       else if ( strcmp(name, "_mach_msg") == 0 )
+                               proc_mach_msg = (FuncPtr_mach_msg)(s->n_value + slide);
+                       else if ( strcmp(name, "_kill") == 0 )
+                               proc_kill = (FuncPtr_kill)(s->n_value + slide);
+                       else if ( strcmp(name, "_getpid") == 0 )
+                               proc_getpid = (FuncPtr_getpid)(s->n_value + slide);
+                       else if ( strcmp(name, "_OSAtomicCompareAndSwap32") == 0 )
+                               proc_OSAtomicCompareAndSwap32 = (FuncPtr_OSAtomicCompareAndSwap32)(s->n_value + slide);
+               }
+       }
+}
+#endif
+
+void xcoresymbolication_load_notifier(void* connection, uint64_t timestamp, const char* path, const struct mach_header* mh)
+{
+       // if host dyld supports this notifier, call into host dyld
+       if ( gSyscallHelpers->version >= 4 )
+               return gSyscallHelpers->coresymbolication_load_notifier(connection, timestamp, path, mh);
+#if SUPPORT_HOST_10_10
+       // otherwise use notifier code in dyld_sim
+       findHostFunctions();
+       coresymbolication_load_notifier(connection, timestamp, path, mh);
+#endif
+}
+
+void xcoresymbolication_unload_notifier(void* connection, uint64_t timestamp, const char* path, const struct mach_header* mh)
+{
+       // if host dyld supports this notifier, call into host dyld
+       if ( gSyscallHelpers->version >= 4 )
+               return gSyscallHelpers->coresymbolication_unload_notifier(connection, timestamp, path, mh);
+#if SUPPORT_HOST_10_10
+       // otherwise use notifier code in dyld_sim
+       findHostFunctions();
+       coresymbolication_unload_notifier(connection, timestamp, path, mh);
+#endif
+}
+
+
 int* __error(void) {
        return gSyscallHelpers->errnoAddress();
 } 
@@ -454,7 +645,6 @@ int* __error(void) {
 void mach_init() {
        mach_task_self_ = task_self_trap();
        //_task_reply_port = _mach_reply_port();
-       
 }
 
 mach_port_t mach_task_self_ = MACH_PORT_NULL;