3 Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 2006, 2007, 2008, 2009, 2010, 2011 Red Hat, Inc.
6 This file is part of Cygwin.
8 This software is a copyrighted work licensed under the terms of the
9 Cygwin license. Please consult the file "CYGWIN_LICENSE" for
15 #include "shared_info.h"
17 static DWORD _my_oldfunc;
19 static char *search_for __attribute__((section (".cygwin_dll_common"), shared)) = (char *) cygthread::stub;
20 unsigned threadfunc_ix[8] __attribute__((section (".cygwin_dll_common"), shared));
22 static bool dll_finished_loading;
23 #define OLDFUNC_OFFSET -1
26 threadfunc_fe (VOID *arg)
28 (void)__builtin_return_address(1);
29 asm volatile ("andl $-16,%%esp" ::: "%esp");
30 _cygtls::call ((DWORD (*) (void *, void *)) TlsGetValue (_my_oldfunc), arg);
33 /* If possible, redirect the thread entry point to a cygwin routine which
34 adds tls stuff to the stack. */
39 char **ebp = (char **) __builtin_frame_address (0);
40 if (!threadfunc_ix[0])
43 char **top = (char **) _tlsbase;
44 for (peb = ebp, i = 0; peb < top && i < 7; peb++)
45 if (*peb == search_for)
46 threadfunc_ix[i++] = peb - ebp;
47 if (0 && !threadfunc_ix[0])
56 char *threadfunc = ebp[threadfunc_ix[0]];
57 if (!search_for || threadfunc == search_for)
60 for (i = 0; threadfunc_ix[i]; i++)
61 ebp[threadfunc_ix[i]] = (char *) threadfunc_fe;
62 TlsSetValue (_my_oldfunc, threadfunc);
68 respawn_wow64_process ()
71 PROCESS_BASIC_INFORMATION pbi;
74 ULONG wow64 = TRUE; /* Opt on the safe side. */
76 /* Unfortunately there's no simpler way to retrieve the
77 parent process in NT, as far as I know. Hints welcome. */
78 ret = NtQueryInformationProcess (NtCurrentProcess (),
79 ProcessBasicInformation,
80 &pbi, sizeof pbi, NULL);
82 && (parent = OpenProcess (PROCESS_QUERY_INFORMATION,
84 pbi.InheritedFromUniqueProcessId)))
86 NtQueryInformationProcess (parent, ProcessWow64Information,
87 &wow64, sizeof wow64, NULL);
91 /* The parent is a real 64 bit process? Respawn! */
94 PROCESS_INFORMATION pi;
98 GetStartupInfoW (&si);
99 if (!CreateProcessW (NULL, GetCommandLineW (), NULL, NULL, TRUE,
100 CREATE_DEFAULT_ERROR_MODE
101 | GetPriorityClass (GetCurrentProcess ()),
102 NULL, NULL, &si, &pi))
103 api_fatal ("Failed to create process <%s>, %E", GetCommandLineA ());
104 CloseHandle (pi.hThread);
105 if (WaitForSingleObject (pi.hProcess, INFINITE) == WAIT_FAILED)
106 api_fatal ("Waiting for process %d failed, %E", pi.dwProcessId);
107 GetExitCodeProcess (pi.hProcess, &ret);
108 CloseHandle (pi.hProcess);
115 extern "C" BOOL WINAPI
116 dll_entry (HANDLE h, DWORD reason, void *static_load)
118 BOOL wow64_test_stack_marker;
122 case DLL_PROCESS_ATTACH:
124 init_console_handler (false);
126 cygwin_hmodule = (HMODULE) h;
127 dynamically_loaded = (static_load == NULL);
129 /* Is the stack at an unusual address? That is, an address which
130 is in the usual space occupied by the process image, but below
131 the auto load address of DLLs?
132 Check if we're running in WOW64 on a 64 bit machine *and* are
133 spawned by a genuine 64 bit process. If so, respawn. */
134 if (wincap.is_wow64 ()
135 && &wow64_test_stack_marker >= (PBOOL) 0x400000
136 && &wow64_test_stack_marker <= (PBOOL) 0x10000000)
137 respawn_wow64_process ();
140 _my_oldfunc = TlsAlloc ();
141 dll_finished_loading = true;
143 case DLL_PROCESS_DETACH:
144 if (dynamically_loaded)
147 case DLL_THREAD_ATTACH:
148 if (dll_finished_loading)
151 case DLL_THREAD_DETACH:
152 if (dll_finished_loading
153 && (PVOID) &_my_tls > (PVOID) &wow64_test_stack_marker
154 && _my_tls.isinitialized ())
156 /* Windows 2000 has a bug in NtTerminateThread. Instead of releasing
157 the stack at teb->DeallocationStack it uses the value of
158 teb->Tib.StackLimit to evaluate the stack address. So we just claim
159 there is no stack. */
160 if (NtCurrentTeb ()->DeallocationStack == NULL
161 && !wincap.has_stack_size_param_is_a_reservation ())
162 NtCurrentTeb ()->Tib.StackLimit = NULL;