From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2201) id 7E4AB3857345; Mon, 29 Aug 2022 16:54:56 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7E4AB3857345 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1661792096; bh=SNcjDlmb03qbmqulz6BpyMARYhwRUd6u5jdC0l91Ico=; h=From:To:Subject:Date:From; b=tsQdzsp+FYKI5BM62cjCZJIkC+7RQUN5RvIC8QBogUzHFdrUA8CIt67hWxuVmeLOA kbvFaqcPcPgcSIJbRbSQz7gjrDwJ3ig1mmwDohX3iT/EL4WNg9uKlS0Ujl+NPOFVvc xoZ5gyHJaUsJ1EoYf+ejKbZMYhZlgPttdEJh9eQI= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Jon TURNEY To: cygwin-cvs@sourceware.org Subject: [newlib-cygwin] Cygwin: testsuite: Add x86_64 code to "dynamically load cygwin" test X-Act-Checkin: newlib-cygwin X-Git-Author: Jon Turney X-Git-Refname: refs/heads/master X-Git-Oldrev: 6e843f7750c7b2e4f36765cae7f3027f83714e98 X-Git-Newrev: b4561c236357c32290321fade62b5c81a5a9fe3c Message-Id: <20220829165456.7E4AB3857345@sourceware.org> Date: Mon, 29 Aug 2022 16:54:56 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=3Dnewlib-cygwin.git;h=3Db4561c23635= 7c32290321fade62b5c81a5a9fe3c commit b4561c236357c32290321fade62b5c81a5a9fe3c Author: Jon Turney Date: Thu Nov 26 16:50:43 2020 +0000 Cygwin: testsuite: Add x86_64 code to "dynamically load cygwin" test =20 This still needs some more voudou to actually work. =20 Also update how-cygtls-works.txt a bit Diff: --- winsup/cygwin/DevDocs/how-cygtls-works.txt | 25 ++++++------------------- winsup/testsuite/winsup.api/cygload.cc | 22 +++++++++++++++++++--- winsup/testsuite/winsup.api/cygload.exp | 8 +++++++- 3 files changed, 32 insertions(+), 23 deletions(-) diff --git a/winsup/cygwin/DevDocs/how-cygtls-works.txt b/winsup/cygwin/Dev= Docs/how-cygtls-works.txt index e4f694118..633c4da96 100644 --- a/winsup/cygwin/DevDocs/how-cygtls-works.txt +++ b/winsup/cygwin/DevDocs/how-cygtls-works.txt @@ -4,8 +4,8 @@ All cygwin threads have separate context in an object of cl= ass _cygtls. The storage for this object is kept on the stack in the bottom __CYGTLS_PADSIZ= E__ bytes. Each thread references the storage via the Thread Environment Block (aka Thread Information Block), which Windows maintains for each user thre= ad -in the system, with the address in the FS segment register. The memory -is laid out as in the NT_TIB structure from : +in the system, with the address in a segment register (FS on x86, GS on x8= 6_64). +The memory is laid out as in the NT_TIB structure from : =20 typedef struct _NT_TIB { struct _EXCEPTION_REGISTRATION_RECORD *ExceptionList; @@ -20,16 +20,10 @@ typedef struct _NT_TIB { struct _NT_TIB *Self; } NT_TIB,*PNT_TIB; =20 -Cygwin sees it like this: - -extern exception_list *_except_list asm ("%fs:0"); // exceptions.cc -extern char *_tlsbase __asm__ ("%fs:4"); // cygtls.h -extern char *_tlstop __asm__ ("%fs:8"); // cygtls.h - -And accesses cygtls like this: - -#define _my_tls (((_cygtls *) _tlsbase)[-1]) // cygtls.h +Cygwin accesses cygtls like this (see cygtls.h): =20 +#define _my_tls (*((_cygtls *) ((PBYTE) NtCurrentTeb()->Tib.StackBase \ + - __CYGTLS_PADSIZE__))) =20 Initialization always goes through _cygtls::init_thread(). It works in the following ways: @@ -65,11 +59,4 @@ __CYGTLS_PADSIZE__ bytes down from there are overwritten. =20 Debugging =20 -You can examine the segment registers in gdb via "info w32 selector $fs" -(which is using GetThreadSelectorEntry()) to get results like this: - - Selector $fs - 0x03b: base=3D0x7ffdd000 limit=3D0x00000fff 32-bit Data (Read/Write, E= xp-up) - Priviledge level =3D 3. Byte granular. - -"x/3x 0x7ffdd000" will give you _except_list, _tlsbase, and _tlstop. +You can examine the TIB in gdb via "info w32 tib" diff --git a/winsup/testsuite/winsup.api/cygload.cc b/winsup/testsuite/wins= up.api/cygload.cc index faad5ce0e..f5ca8db9a 100644 --- a/winsup/testsuite/winsup.api/cygload.cc +++ b/winsup/testsuite/winsup.api/cygload.cc @@ -43,7 +43,10 @@ using std::string; cygwin::padding *cygwin::padding::_main =3D NULL; DWORD cygwin::padding::_mainTID =3D 0; =20 -// A few cygwin constants. +// Cygwin signal constants +#undef SIGINT +#undef SIGTERM + static const int SIGHUP =3D 1; static const int SIGINT =3D 2; static const int SIGTERM =3D 15; // Cygwin won't deliver this one to us; @@ -68,17 +71,30 @@ cygwin::padding::padding () =20 _end =3D _padding + sizeof (_padding); char *stackbase; -#ifdef __GNUC__ +#ifdef __GNUC__ /* GCC */ +# ifdef __x86_64__ + __asm__ ( + "mov %%gs:8, %0" + :"=3Dr"(stackbase) + ); +# elif __X86__ __asm__ ( "movl %%fs:4, %0" :"=3Dr"(stackbase) ); -#else +# else +# error Unknown architecture +# endif +#else /* !GCC assumed to be MSVC */ +# ifdef __X86__ __asm { mov eax, fs:[4]; mov stackbase, eax; } +#else +# error Unknown architecture +# endif #endif _stackbase =3D stackbase; =20 diff --git a/winsup/testsuite/winsup.api/cygload.exp b/winsup/testsuite/win= sup.api/cygload.exp index e7b439512..8ba8249bb 100644 --- a/winsup/testsuite/winsup.api/cygload.exp +++ b/winsup/testsuite/winsup.api/cygload.exp @@ -14,7 +14,13 @@ proc ws_spawn {cmd args} { verbose send "catchCode =3D $rv\n" } =20 -ws_spawn "$MINGW_CXX $srcdir/$subdir/cygload.cc -o mingw-cygload.exe -lstd= c++ -Wl,-e,_cygloadCRTStartup@0" +if { [string match "i686" $target_alias] } { + set entrypoint "_cygloadCRTStartup@0" +} else { + set entrypoint "cygloadCRTStartup" +} + +ws_spawn "$MINGW_CXX $srcdir/$subdir/cygload.cc -o mingw-cygload.exe -stat= ic -Wl,-e,$entrypoint" =20 if { $rv !=3D {0 {}} } { verbose -log "$rv"