From a0b7fb9bb2a6f446a0018a89bd8b50f756a0fe1c Mon Sep 17 00:00:00 2001 From: Alexandre Julliard Date: Thu, 18 Jun 2020 14:14:19 +0200 Subject: [PATCH] ntdll: Move the image address initialization to the Unix library. Signed-off-by: Alexandre Julliard --- dlls/ntdll/thread.c | 76 ---------------------------------- dlls/ntdll/unix/loader.c | 40 ++++++++++++++++++ dlls/ntdll/unix/unix_private.h | 3 +- dlls/ntdll/unix/virtual.c | 1 + 4 files changed, 43 insertions(+), 77 deletions(-) diff --git a/dlls/ntdll/thread.c b/dlls/ntdll/thread.c index e60018fb47d..7329aa177ba 100644 --- a/dlls/ntdll/thread.c +++ b/dlls/ntdll/thread.c @@ -25,15 +25,6 @@ #include #include #include -#ifdef HAVE_SYS_MMAN_H -#include -#endif -#ifdef HAVE_SYS_TIMES_H -#include -#endif -#ifdef HAVE_SYS_SYSCALL_H -#include -#endif #define NONAMELESSUNION #include "ntstatus.h" @@ -67,72 +58,6 @@ static RTL_CRITICAL_SECTION_DEBUG critsect_debug = }; static RTL_CRITICAL_SECTION peb_lock = { &critsect_debug, -1, 0, 0, 0, 0 }; -#ifdef __linux__ - -#ifdef HAVE_ELF_H -# include -#endif -#ifdef HAVE_LINK_H -# include -#endif -#ifdef HAVE_SYS_AUXV_H -# include -#endif -#ifndef HAVE_GETAUXVAL -static unsigned long getauxval( unsigned long id ) -{ - extern char **__wine_main_environ; - char **ptr = __wine_main_environ; - ElfW(auxv_t) *auxv; - - while (*ptr) ptr++; - while (!*ptr) ptr++; - for (auxv = (ElfW(auxv_t) *)ptr; auxv->a_type; auxv++) - if (auxv->a_type == id) return auxv->a_un.a_val; - return 0; -} -#endif - -static ULONG_PTR get_image_addr(void) -{ - ULONG_PTR size, num, phdr_addr = getauxval( AT_PHDR ); - ElfW(Phdr) *phdr; - - if (!phdr_addr) return 0; - phdr = (ElfW(Phdr) *)phdr_addr; - size = getauxval( AT_PHENT ); - num = getauxval( AT_PHNUM ); - while (num--) - { - if (phdr->p_type == PT_PHDR) return phdr_addr - phdr->p_offset; - phdr = (ElfW(Phdr) *)((char *)phdr + size); - } - return 0; -} - -#elif defined(__APPLE__) -#include -#include - -static ULONG_PTR get_image_addr(void) -{ - ULONG_PTR ret = 0; -#ifdef TASK_DYLD_INFO - struct task_dyld_info dyld_info; - mach_msg_type_number_t size = TASK_DYLD_INFO_COUNT; - if (task_info(mach_task_self(), TASK_DYLD_INFO, (task_info_t)&dyld_info, &size) == KERN_SUCCESS) - ret = dyld_info.all_image_info_addr; -#endif - return ret; -} - -#else -static ULONG_PTR get_image_addr(void) -{ - return 0; -} -#endif - /*********************************************************************** * __wine_dbg_get_channel_flags (NTDLL.@) @@ -204,7 +129,6 @@ TEB *thread_init( SIZE_T *info_size, BOOL *suspend ) InitializeListHead( &ldr.InLoadOrderModuleList ); InitializeListHead( &ldr.InMemoryOrderModuleList ); InitializeListHead( &ldr.InInitializationOrderModuleList ); - *(ULONG_PTR *)peb->Reserved = get_image_addr(); /* * Starting with Vista, the first user to log on has session id 1. diff --git a/dlls/ntdll/unix/loader.c b/dlls/ntdll/unix/loader.c index 091951163d7..4ae51cb2f49 100644 --- a/dlls/ntdll/unix/loader.c +++ b/dlls/ntdll/unix/loader.c @@ -33,6 +33,15 @@ #ifdef HAVE_PWD_H # include #endif +#ifdef HAVE_ELF_H +# include +#endif +#ifdef HAVE_LINK_H +# include +#endif +#ifdef HAVE_SYS_AUXV_H +# include +#endif #ifdef HAVE_SYS_MMAN_H # include #endif @@ -53,6 +62,8 @@ # undef LoadResource # undef GetCurrentThread # include +# include +# include # include # include # include @@ -829,6 +840,35 @@ static HMODULE load_ntdll(void) } +/*********************************************************************** + * get_image_address + */ +ULONG_PTR get_image_address(void) +{ +#ifdef HAVE_GETAUXVAL + ULONG_PTR size, num, phdr_addr = getauxval( AT_PHDR ); + ElfW(Phdr) *phdr; + + if (!phdr_addr) return 0; + phdr = (ElfW(Phdr) *)phdr_addr; + size = getauxval( AT_PHENT ); + num = getauxval( AT_PHNUM ); + while (num--) + { + if (phdr->p_type == PT_PHDR) return phdr_addr - phdr->p_offset; + phdr = (ElfW(Phdr) *)((char *)phdr + size); + } +#elif defined(__APPLE__) && defined(TASK_DYLD_INFO) + struct task_dyld_info dyld_info; + mach_msg_type_number_t size = TASK_DYLD_INFO_COUNT; + + if (task_info(mach_task_self(), TASK_DYLD_INFO, (task_info_t)&dyld_info, &size) == KERN_SUCCESS) + return dyld_info.all_image_info_addr; +#endif + return 0; +} + + /*********************************************************************** * unix_funcs */ diff --git a/dlls/ntdll/unix/unix_private.h b/dlls/ntdll/unix/unix_private.h index dbd8ce31528..9904ef7bfd5 100644 --- a/dlls/ntdll/unix/unix_private.h +++ b/dlls/ntdll/unix/unix_private.h @@ -142,11 +142,12 @@ extern int ntdll_wcstoumbs( const WCHAR *src, DWORD srclen, char *dst, DWORD dst extern char **build_envp( const WCHAR *envW ) DECLSPEC_HIDDEN; extern NTSTATUS exec_wineloader( char **argv, int socketfd, int is_child_64bit, ULONGLONG res_start, ULONGLONG res_end ) DECLSPEC_HIDDEN; +extern void start_server( BOOL debug ) DECLSPEC_HIDDEN; +extern ULONG_PTR get_image_address(void) DECLSPEC_HIDDEN; extern unsigned int server_call_unlocked( void *req_ptr ) DECLSPEC_HIDDEN; extern void server_enter_uninterrupted_section( RTL_CRITICAL_SECTION *cs, sigset_t *sigset ) DECLSPEC_HIDDEN; extern void server_leave_uninterrupted_section( RTL_CRITICAL_SECTION *cs, sigset_t *sigset ) DECLSPEC_HIDDEN; -extern void start_server( BOOL debug ) DECLSPEC_HIDDEN; extern unsigned int server_select( const select_op_t *select_op, data_size_t size, UINT flags, timeout_t abs_timeout, CONTEXT *context, RTL_CRITICAL_SECTION *cs, user_apc_t *user_apc ) DECLSPEC_HIDDEN; diff --git a/dlls/ntdll/unix/virtual.c b/dlls/ntdll/unix/virtual.c index a32059f0197..6166507871c 100644 --- a/dlls/ntdll/unix/virtual.c +++ b/dlls/ntdll/unix/virtual.c @@ -2587,6 +2587,7 @@ TEB *virtual_alloc_first_teb(void) NtAllocateVirtualMemory( NtCurrentProcess(), (void **)&teb, 0, &teb_size, MEM_COMMIT, PAGE_READWRITE ); NtAllocateVirtualMemory( NtCurrentProcess(), (void **)&peb, 0, &peb_size, MEM_COMMIT, PAGE_READWRITE ); init_teb( teb, peb ); + *(ULONG_PTR *)peb->Reserved = get_image_address(); use_locks = TRUE; return teb; }