Cygwin: revamp TLS offsets computation

- convert gentls_offsets to a shell script, only running the target
  compiler and gawk.

- Simplify cygtls.h.  The new gentls_offsets script only requires two
  lines with the "public:" keyword as markers.  The comments are not
  used anymore, the output is a preprocesses file without comments.
  Align Makefile rules accordingly.

- Rather than generating perl variables and C #defines, just generate
  .ecu statements and .include the TLS offsets file right from the
  generated assembler file sigfe.s.  It's the only place we really
  need (some of) the offsets.

- Drop the target-specific name of the TLS offsets file and generate
  it on the fly in the build dir.  Fix configure and Makefile rules
  accordingly.

Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
This commit is contained in:
Corinna Vinschen 2022-02-02 17:27:34 +01:00
parent c0d5bb262d
commit 0597c84b9b
6 changed files with 172 additions and 335 deletions

View File

@ -67,7 +67,6 @@ esac
DLL_ENTRY="dll_entry" DLL_ENTRY="dll_entry"
DIN_FILE="${target_cpu}.din" DIN_FILE="${target_cpu}.din"
TLSOFFSETS_H="tlsoffsets-${target_cpu}.h"
case "$target_cpu" in case "$target_cpu" in
x86_64) ;; x86_64) ;;
@ -76,7 +75,6 @@ esac
AC_SUBST(DLL_ENTRY) AC_SUBST(DLL_ENTRY)
AC_SUBST(DIN_FILE) AC_SUBST(DIN_FILE)
AC_SUBST(TLSOFFSETS_H)
AM_CONDITIONAL(TARGET_X86_64, [test $target_cpu = "x86_64"]) AM_CONDITIONAL(TARGET_X86_64, [test $target_cpu = "x86_64"])

View File

@ -45,7 +45,6 @@ DBG_DLL_NAME=cygwin1.dbg
NEW_DLL_NAME=new-cygwin1.dll NEW_DLL_NAME=new-cygwin1.dll
DIN_FILE=@DIN_FILE@ DIN_FILE=@DIN_FILE@
DEF_FILE=cygwin.def DEF_FILE=cygwin.def
TLSOFFSETS_H=@TLSOFFSETS_H@
LIB_NAME=libcygwin.a LIB_NAME=libcygwin.a
TEST_LIB_NAME=libcygwin0.a TEST_LIB_NAME=libcygwin0.a
@ -386,8 +385,8 @@ localtime.patched.c: tzcode/localtime.c tzcode/localtime.c.patch
$(srcdir)/devices.cc: gendevices devices.in devices.h $(srcdir)/devices.cc: gendevices devices.in devices.h
$(wordlist 1,2,$^) $@ $(wordlist 1,2,$^) $@
$(srcdir)/$(TLSOFFSETS_H): gentls_offsets cygtls.h tlsoffsets: gentls_offsets cygtls.h
$^ $@ $(target_cpu) $(CC) $(AM_CFLAGS) -c || rm $@ $(AM_V_GEN)CXXCOMPILE="$(CXXCOMPILE)" $^ $@
BUILT_SOURCES = \ BUILT_SOURCES = \
child_info_magic.h \ child_info_magic.h \
@ -609,10 +608,10 @@ $(NEW_DLL_NAME): $(PRE_DLL_NAME) $(DBG_DLL_NAME)
# cygwin import library # cygwin import library
toolopts=--cpu=@target_cpu@ --ar=@AR@ --as=@AS@ --nm=@NM@ --objcopy=@OBJCOPY@ toolopts=--cpu=@target_cpu@ --ar=@AR@ --as=@AS@ --nm=@NM@ --objcopy=@OBJCOPY@
$(DEF_FILE): gendef $(srcdir)/$(TLSOFFSETS_H) $(DIN_FILE) common.din $(DEF_FILE): gendef $(DIN_FILE) common.din
$(AM_V_GEN)$(srcdir)/gendef --cpu=@target_cpu@ --output-def=$(DEF_FILE) --tlsoffsets=$(srcdir)/$(TLSOFFSETS_H) $(srcdir)/$(DIN_FILE) $(srcdir)/common.din $(AM_V_GEN)$(srcdir)/gendef --cpu=@target_cpu@ --output-def=$(DEF_FILE) $(srcdir)/$(DIN_FILE) $(srcdir)/common.din
sigfe.s: $(DEF_FILE) sigfe.s: $(DEF_FILE) tlsoffsets
@[ -s $@ ] || \ @[ -s $@ ] || \
{ rm -f $(DEF_FILE); $(MAKE) -s -j1 $(DEF_FILE); }; \ { rm -f $(DEF_FILE); $(MAKE) -s -j1 $(DEF_FILE); }; \
[ -s $@ ] && touch $@ [ -s $@ ] && touch $@
@ -680,9 +679,10 @@ clean-local:
-rm -f $(PRE_DLL_NAME) $(DBG_DLL_NAME) $(NEW_DLL_NAME) -rm -f $(PRE_DLL_NAME) $(DBG_DLL_NAME) $(NEW_DLL_NAME)
-rm -f $(LIB_NAME) $(TEST_LIB_NAME) $(SUBLIBS) -rm -f $(LIB_NAME) $(TEST_LIB_NAME) $(SUBLIBS)
-rm -f version.cc -rm -f version.cc
-rm -f tlsoffsets
maintainer-clean-local: maintainer-clean-local:
-rm -f $(srcdir)/$(TLSOFFSETS_H) $(srcdir)/devices.cc -rm -f $(srcdir)/devices.cc
# #
# install # install

View File

@ -152,33 +152,22 @@ typedef struct struct_waitq
HANDLE thread_ev; HANDLE thread_ev;
} waitq; } waitq;
/* Changes to the below structure may require acompanying changes to the very /* Changes to the below structure may require acompanying changes to the
simple parser in the perl script 'gentls_offsets' (<<-- start parsing here). gawk parser in the shell script 'gentls_offsets' */
The union in this structure is used to force alignment between the version
of the compiler used to generate tlsoffsets.h and the cygwin cross compiler.
*/
/*gentls_offsets*/
extern "C" int __sjfault (jmp_buf); extern "C" int __sjfault (jmp_buf);
extern "C" int __ljfault (jmp_buf, int); extern "C" int __ljfault (jmp_buf, int);
/*gentls_offsets*/
typedef uintptr_t __tlsstack_t; typedef uintptr_t __tlsstack_t;
class _cygtls class _cygtls
{ {
public: public: /* Do NOT remove this public: line, it's a marker for gentls_offsets. */
/* Keep these two declarations first, keep local_clib first. */ /* offsetoff (class _cygtls, local_clib) *must* be 0. */
union
{
struct _reent local_clib; struct _reent local_clib;
char __dontuse[8 * ((sizeof(struct _reent) + 4) / 8)];
};
struct _local_storage locals; struct _local_storage locals;
/**/ /**/
void (*func) /*gentls_offsets*/(int, siginfo_t *, void *)/*gentls_offsets*/; void (*func) (int, siginfo_t *, void *);
int saved_errno; int saved_errno;
int sa_flags; int sa_flags;
sigset_t oldmask; sigset_t oldmask;
@ -209,7 +198,7 @@ public:
__tlsstack_t stack[TLS_STACK_SIZE]; __tlsstack_t stack[TLS_STACK_SIZE];
unsigned initialized; unsigned initialized;
/*gentls_offsets*/ public: /* Do NOT remove this public: line, it's a marker for gentls_offsets. */
void init_thread (void *, DWORD (*) (void *, void *)); void init_thread (void *, DWORD (*) (void *, void *));
static void call (DWORD (*) (void *, void *), void *); static void call (DWORD (*) (void *, void *), void *);
void remove (DWORD); void remove (DWORD);
@ -276,12 +265,9 @@ public:
private: private:
void __reg3 call2 (DWORD (*) (void *, void *), void *, void *); void __reg3 call2 (DWORD (*) (void *, void *), void *, void *);
void remove_pending_sigs (); void remove_pending_sigs ();
/*gentls_offsets*/
}; };
#pragma pack(pop) #pragma pack(pop)
/*gentls_offsets*/
#include "cygerrno.h" #include "cygerrno.h"
#include "ntdll.h" #include "ntdll.h"

View File

@ -14,16 +14,13 @@ sub cleanup(@);
my $cpu; my $cpu;
my $output_def; my $output_def;
my $tls_offsets; GetOptions('cpu=s'=>\$cpu, 'output-def=s'=>\$output_def);
GetOptions('cpu=s'=>\$cpu, 'output-def=s'=>\$output_def, 'tlsoffsets=s'=>\$tls_offsets);
$main::first = 0; $main::first = 0;
if (!defined($cpu) || !defined($output_def) || !defined($tls_offsets)) { if (!defined($cpu) || !defined($output_def)) {
die "$0: missing required option\n"; die "$0: missing required option\n";
} }
require $tls_offsets;
my $is64bit = $cpu eq 'x86_64'; my $is64bit = $cpu eq 'x86_64';
my $sym_prefix = $is64bit ? '' : '_'; my $sym_prefix = $is64bit ? '' : '_';
@ -125,16 +122,17 @@ EOF
if (!$main::first++) { if (!$main::first++) {
if ($is64bit) { if ($is64bit) {
$res = <<EOF . longjmp () . $res; $res = <<EOF . longjmp () . $res;
.include "tlsoffsets"
.text .text
.seh_proc _sigfe_maybe .seh_proc _sigfe_maybe
_sigfe_maybe: # stack is aligned on entry! _sigfe_maybe: # stack is aligned on entry!
.seh_endprologue .seh_endprologue
movq %gs:8,%r10 # location of bottom of stack movq %gs:8,%r10 # location of bottom of stack
leaq $tls::initialized(%r10),%r11 # where we will be looking leaq _cygtls.initialized(%r10),%r11 # where we will be looking
cmpq %r11,%rsp # stack loc > than tls cmpq %r11,%rsp # stack loc > than tls
jge 0f # yep. we don't have a tls. jge 0f # yep. we don't have a tls.
movl $tls::initialized(%r10),%r11d movl _cygtls.initialized(%r10),%r11d
cmpl \$0xc763173f,%r11d # initialized? cmpl \$0xc763173f,%r11d # initialized?
je 1f je 1f
0: ret 0: ret
@ -145,19 +143,19 @@ _sigfe: # stack is aligned on entry!
.seh_endprologue .seh_endprologue
movq %gs:8,%r10 # location of bottom of stack movq %gs:8,%r10 # location of bottom of stack
1: movl \$1,%r11d # potential lock value 1: movl \$1,%r11d # potential lock value
xchgl %r11d,$tls::stacklock(%r10) # see if we can grab it xchgl %r11d,_cygtls.stacklock(%r10) # see if we can grab it
movl %r11d,$tls::spinning(%r10) # flag if we are waiting for lock movl %r11d,_cygtls.spinning(%r10) # flag if we are waiting for lock
testl %r11d,%r11d # it will be zero testl %r11d,%r11d # it will be zero
jz 2f # if so jz 2f # if so
pause pause
jmp 1b # loop jmp 1b # loop
2: movq \$8,%rax # have the lock, now increment the 2: movq \$8,%rax # have the lock, now increment the
xaddq %rax,$tls::stackptr(%r10) # stack pointer and get pointer xaddq %rax,_cygtls.stackptr(%r10) # stack pointer and get pointer
leaq _sigbe(%rip),%r11 # new place to return to leaq _sigbe(%rip),%r11 # new place to return to
xchgq %r11,8(%rsp) # exchange with real return value xchgq %r11,8(%rsp) # exchange with real return value
movq %r11,(%rax) # store real return value on alt stack movq %r11,(%rax) # store real return value on alt stack
incl $tls::incyg(%r10) incl _cygtls.incyg(%r10)
decl $tls::stacklock(%r10) # remove lock decl _cygtls.stacklock(%r10) # remove lock
popq %rax # pop real function address from stack popq %rax # pop real function address from stack
jmp *%rax # and jmp to it jmp *%rax # and jmp to it
.seh_endproc .seh_endproc
@ -169,17 +167,17 @@ _sigbe: # return here after cygwin syscall
.seh_endprologue .seh_endprologue
movq %gs:8,%r10 # address of bottom of tls movq %gs:8,%r10 # address of bottom of tls
1: movl \$1,%r11d # potential lock value 1: movl \$1,%r11d # potential lock value
xchgl %r11d,$tls::stacklock(%r10) # see if we can grab it xchgl %r11d,_cygtls.stacklock(%r10) # see if we can grab it
movl %r11d,$tls::spinning(%r10) # flag if we are waiting for lock movl %r11d,_cygtls.spinning(%r10) # flag if we are waiting for lock
testl %r11d,%r11d # it will be zero testl %r11d,%r11d # it will be zero
jz 2f # if so jz 2f # if so
pause pause
jmp 1b # and loop jmp 1b # and loop
2: movq \$-8,%r11 # now decrement aux stack 2: movq \$-8,%r11 # now decrement aux stack
xaddq %r11,$tls::stackptr(%r10) # and get pointer xaddq %r11,_cygtls.stackptr(%r10) # and get pointer
movq -8(%r11),%r11 # get return address from signal stack movq -8(%r11),%r11 # get return address from signal stack
decl $tls::incyg(%r10) decl _cygtls.incyg(%r10)
decl $tls::stacklock(%r10) # release lock decl _cygtls.stacklock(%r10) # release lock
jmp *%r11 # "return" to caller jmp *%r11 # "return" to caller
.seh_endproc .seh_endproc
@ -248,29 +246,29 @@ sigdelayed:
.seh_endprologue .seh_endprologue
movq %gs:8,%r12 # get tls movq %gs:8,%r12 # get tls
movl $tls::saved_errno(%r12),%r15d # temporarily save saved_errno movl _cygtls.saved_errno(%r12),%r15d # temporarily save saved_errno
movq \$$tls::start_offset,%rcx # point to beginning of tls block movq \$_cygtls.start_offset,%rcx # point to beginning of tls block
addq %r12,%rcx # and store as first arg to method addq %r12,%rcx # and store as first arg to method
call _ZN7_cygtls19call_signal_handlerEv # call handler call _ZN7_cygtls19call_signal_handlerEv # call handler
1: movl \$1,%r11d # potential lock value 1: movl \$1,%r11d # potential lock value
xchgl %r11d,$tls::stacklock(%r12) # see if we can grab it xchgl %r11d,_cygtls.stacklock(%r12) # see if we can grab it
movl %r11d,$tls::spinning(%r12) # flag if we are waiting for lock movl %r11d,_cygtls.spinning(%r12) # flag if we are waiting for lock
testl %r11d,%r11d # it will be zero testl %r11d,%r11d # it will be zero
jz 2f # if so jz 2f # if so
pause pause
jmp 1b # and loop jmp 1b # and loop
2: testl %r15d,%r15d # was saved_errno < 0 2: testl %r15d,%r15d # was saved_errno < 0
jl 3f # yup. ignore it jl 3f # yup. ignore it
movq $tls::errno_addr(%r12),%r11 movq _cygtls.errno_addr(%r12),%r11
movl %r15d,(%r11) movl %r15d,(%r11)
3: movq \$-8,%r11 # now decrement aux stack 3: movq \$-8,%r11 # now decrement aux stack
xaddq %r11,$tls::stackptr(%r12) # and get pointer xaddq %r11,_cygtls.stackptr(%r12) # and get pointer
xorq %r10,%r10 xorq %r10,%r10
xchgq %r10,-8(%r11) # get return address from signal stack xchgq %r10,-8(%r11) # get return address from signal stack
xorl %r11d,%r11d xorl %r11d,%r11d
movl %r11d,$tls::incyg(%r12) movl %r11d,_cygtls.incyg(%r12)
movl %r11d,$tls::stacklock(%r12) # unlock movl %r11d,_cygtls.stacklock(%r12) # unlock
movdqa 0x20(%rsp),%xmm0 movdqa 0x20(%rsp),%xmm0
movdqa 0x30(%rsp),%xmm1 movdqa 0x30(%rsp),%xmm1
movdqa 0x40(%rsp),%xmm2 movdqa 0x40(%rsp),%xmm2
@ -320,7 +318,7 @@ _sigdelayed_end:
_ZN7_cygtls3popEv: _ZN7_cygtls3popEv:
.seh_endprologue .seh_endprologue
movq \$-8,%r11 movq \$-8,%r11
xaddq %r11,$tls::pstackptr(%rcx) xaddq %r11,_cygtls.pstackptr(%rcx)
movq -8(%r11),%rax movq -8(%r11),%rax
ret ret
.seh_endproc .seh_endproc
@ -334,7 +332,7 @@ _ZN7_cygtls4lockEv:
.seh_endprologue .seh_endprologue
movq %rcx,%r12 movq %rcx,%r12
1: movl \$1,%r11d 1: movl \$1,%r11d
xchgl %r11d,$tls::pstacklock(%r12) xchgl %r11d,_cygtls.pstacklock(%r12)
testl %r11d,%r11d testl %r11d,%r11d
jz 2f jz 2f
pause pause
@ -348,7 +346,7 @@ _ZN7_cygtls4lockEv:
.seh_proc _ZN7_cygtls6unlockEv .seh_proc _ZN7_cygtls6unlockEv
_ZN7_cygtls6unlockEv: _ZN7_cygtls6unlockEv:
.seh_endprologue .seh_endprologue
decl $tls::pstacklock(%rcx) decl _cygtls.pstacklock(%rcx)
ret ret
.seh_endproc .seh_endproc
@ -357,7 +355,7 @@ _ZN7_cygtls6unlockEv:
.seh_proc _ZN7_cygtls6lockedEv .seh_proc _ZN7_cygtls6lockedEv
_ZN7_cygtls6lockedEv: _ZN7_cygtls6lockedEv:
.seh_endprologue .seh_endprologue
movl $tls::pstacklock(%rcx),%eax movl _cygtls.pstacklock(%rcx),%eax
ret ret
.seh_endproc .seh_endproc
@ -370,21 +368,21 @@ stabilize_sig_stack:
.seh_endprologue .seh_endprologue
movq %gs:8,%r12 movq %gs:8,%r12
1: movl \$1,%r10d 1: movl \$1,%r10d
xchgl %r10d,$tls::stacklock(%r12) xchgl %r10d,_cygtls.stacklock(%r12)
movl %r10d,$tls::spinning(%r12) # flag if we are waiting for lock movl %r10d,_cygtls.spinning(%r12) # flag if we are waiting for lock
testl %r10d,%r10d testl %r10d,%r10d
jz 2f jz 2f
pause pause
jmp 1b jmp 1b
2: incl $tls::incyg(%r12) 2: incl _cygtls.incyg(%r12)
cmpl \$0,$tls::sig(%r12) cmpl \$0,_cygtls.sig(%r12)
jz 3f jz 3f
decl $tls::stacklock(%r12) # unlock decl _cygtls.stacklock(%r12) # unlock
movq \$$tls::start_offset,%rcx # point to beginning movq \$_cygtls.start_offset,%rcx # point to beginning
addq %r12,%rcx # of tls block addq %r12,%rcx # of tls block
call _ZN7_cygtls19call_signal_handlerEv call _ZN7_cygtls19call_signal_handlerEv
jmp 1b jmp 1b
3: decl $tls::incyg(%r12) 3: decl _cygtls.incyg(%r12)
addq \$0x20,%rsp addq \$0x20,%rsp
movq %r12,%r11 # return tls addr in r11 movq %r12,%r11 # return tls addr in r11
popq %r12 popq %r12
@ -399,11 +397,11 @@ __sigfe_maybe:
pushl %ebx pushl %ebx
pushl %edx pushl %edx
movl %fs:4,%ebx # location of bottom of stack movl %fs:4,%ebx # location of bottom of stack
addl \$$tls::initialized,%ebx # where we will be looking addl \$_cygtls.initialized,%ebx # where we will be looking
cmpl %ebx,%esp # stack loc > than tls cmpl %ebx,%esp # stack loc > than tls
jge 0f # yep. we don't have a tls. jge 0f # yep. we don't have a tls.
subl \$$tls::initialized,%ebx # where we will be looking subl \$_cygtls.initialized,%ebx # where we will be looking
movl $tls::initialized(%ebx),%eax movl _cygtls.initialized(%ebx),%eax
cmpl \$0xc763173f,%eax # initialized? cmpl \$0xc763173f,%eax # initialized?
je 1f je 1f
0: popl %edx 0: popl %edx
@ -415,19 +413,19 @@ __sigfe:
pushl %edx pushl %edx
movl %fs:4,%ebx # location of bottom of stack movl %fs:4,%ebx # location of bottom of stack
1: movl \$1,%eax # potential lock value 1: movl \$1,%eax # potential lock value
xchgl %eax,$tls::stacklock(%ebx) # see if we can grab it xchgl %eax,_cygtls.stacklock(%ebx) # see if we can grab it
movl %eax,$tls::spinning(%ebx) # flag if we are waiting for lock movl %eax,_cygtls.spinning(%ebx) # flag if we are waiting for lock
testl %eax,%eax # it will be zero testl %eax,%eax # it will be zero
jz 2f # if so jz 2f # if so
call _yield # should be a short-time thing, so call _yield # should be a short-time thing, so
jmp 1b # sleep and loop jmp 1b # sleep and loop
2: movl \$4,%eax # have the lock, now increment the 2: movl \$4,%eax # have the lock, now increment the
xadd %eax,$tls::stackptr(%ebx) # stack pointer and get pointer xadd %eax,_cygtls.stackptr(%ebx) # stack pointer and get pointer
leal __sigbe,%edx # new place to return to leal __sigbe,%edx # new place to return to
xchgl %edx,12(%esp) # exchange with real return value xchgl %edx,12(%esp) # exchange with real return value
movl %edx,(%eax) # store real return value on alt stack movl %edx,(%eax) # store real return value on alt stack
incl $tls::incyg(%ebx) incl _cygtls.incyg(%ebx)
decl $tls::stacklock(%ebx) # remove lock decl _cygtls.stacklock(%ebx) # remove lock
popl %edx # restore saved value popl %edx # restore saved value
popl %ebx popl %ebx
ret ret
@ -438,18 +436,18 @@ __sigbe: # return here after cygwin syscall
pushl %ebx # tls pointer pushl %ebx # tls pointer
1: movl %fs:4,%ebx # address of bottom of tls 1: movl %fs:4,%ebx # address of bottom of tls
movl \$1,%eax # potential lock value movl \$1,%eax # potential lock value
xchgl %eax,$tls::stacklock(%ebx) # see if we can grab it xchgl %eax,_cygtls.stacklock(%ebx) # see if we can grab it
movl %eax,$tls::spinning(%ebx) # flag if we are waiting for lock movl %eax,_cygtls.spinning(%ebx) # flag if we are waiting for lock
testl %eax,%eax # it will be zero testl %eax,%eax # it will be zero
jz 2f # if so jz 2f # if so
call _yield # sleep call _yield # sleep
jmp 1b # and loop jmp 1b # and loop
2: movl \$-4,%eax # now decrement aux stack 2: movl \$-4,%eax # now decrement aux stack
xadd %eax,$tls::stackptr(%ebx) # and get pointer xadd %eax,_cygtls.stackptr(%ebx) # and get pointer
movl -4(%eax),%eax # get return address from signal stack movl -4(%eax),%eax # get return address from signal stack
xchgl %eax,4(%esp) # swap return address with saved eax xchgl %eax,4(%esp) # swap return address with saved eax
decl $tls::incyg(%ebx) decl _cygtls.incyg(%ebx)
decl $tls::stacklock(%ebx) # release lock decl _cygtls.stacklock(%ebx) # release lock
popl %ebx popl %ebx
ret ret
@ -477,16 +475,16 @@ _sigdelayed:
movdqu %xmm1,0x10(%esp) movdqu %xmm1,0x10(%esp)
movdqu %xmm0,(%esp) movdqu %xmm0,(%esp)
movl %fs:4,%ebx # get tls movl %fs:4,%ebx # get tls
pushl $tls::saved_errno(%ebx) # saved errno pushl _cygtls.saved_errno(%ebx) # saved errno
movl \$$tls::start_offset,%eax # point to beginning movl \$_cygtls.start_offset,%eax # point to beginning
addl %ebx,%eax # of tls block addl %ebx,%eax # of tls block
call __ZN7_cygtls19call_signal_handlerEv\@4 # call handler call __ZN7_cygtls19call_signal_handlerEv\@4 # call handler
movl %fs:4,%ebx # reget tls movl %fs:4,%ebx # reget tls
1: movl \$1,%eax # potential lock value 1: movl \$1,%eax # potential lock value
xchgl %eax,$tls::stacklock(%ebx) # see if we can grab it xchgl %eax,_cygtls.stacklock(%ebx) # see if we can grab it
movl %eax,$tls::spinning(%ebx) # flag if we are waiting for lock movl %eax,_cygtls.spinning(%ebx) # flag if we are waiting for lock
testl %eax,%eax # it will be zero testl %eax,%eax # it will be zero
jz 2f # if so jz 2f # if so
call _yield # sleep call _yield # sleep
@ -494,16 +492,16 @@ _sigdelayed:
2: popl %edx # saved errno 2: popl %edx # saved errno
testl %edx,%edx # Is it < 0 testl %edx,%edx # Is it < 0
jl 3f # yup. ignore it jl 3f # yup. ignore it
movl $tls::errno_addr(%ebx),%eax movl _cygtls.errno_addr(%ebx),%eax
movl %edx,(%eax) movl %edx,(%eax)
3: movl \$-4,%eax # now decrement aux stack 3: movl \$-4,%eax # now decrement aux stack
xadd %eax,$tls::stackptr(%ebx) # and get pointer xadd %eax,_cygtls.stackptr(%ebx) # and get pointer
xorl %ebp,%ebp xorl %ebp,%ebp
xchgl %ebp,-4(%eax) # get return address from signal stack xchgl %ebp,-4(%eax) # get return address from signal stack
xchgl %ebp,0xa0(%esp) # store real return address xchgl %ebp,0xa0(%esp) # store real return address
leave: xorl %eax,%eax leave: xorl %eax,%eax
movl %eax,$tls::incyg(%ebx) movl %eax,_cygtls.incyg(%ebx)
movl %eax,$tls::stacklock(%ebx) # unlock movl %eax,_cygtls.stacklock(%ebx) # unlock
movdqu (%esp),%xmm0 movdqu (%esp),%xmm0
movdqu 0x10(%esp),%xmm1 movdqu 0x10(%esp),%xmm1
@ -530,7 +528,7 @@ __ZN7_cygtls3popEv\@4:
1: pushl %ebx 1: pushl %ebx
movl %eax,%ebx # this movl %eax,%ebx # this
movl \$-4,%eax movl \$-4,%eax
xadd %eax,$tls::pstackptr(%ebx) xadd %eax,_cygtls.pstackptr(%ebx)
movl -4(%eax),%eax movl -4(%eax),%eax
popl %ebx popl %ebx
ret ret
@ -541,7 +539,7 @@ __ZN7_cygtls4lockEv\@4:
pushl %ebx pushl %ebx
movl %eax,%ebx movl %eax,%ebx
1: movl \$1,%eax 1: movl \$1,%eax
xchgl %eax,$tls::pstacklock(%ebx) xchgl %eax,_cygtls.pstacklock(%ebx)
testl %eax,%eax testl %eax,%eax
jz 2f jz 2f
call _yield call _yield
@ -552,33 +550,33 @@ __ZN7_cygtls4lockEv\@4:
# _cygtls::unlock # _cygtls::unlock
.global __ZN7_cygtls6unlockEv\@4 .global __ZN7_cygtls6unlockEv\@4
__ZN7_cygtls6unlockEv\@4: __ZN7_cygtls6unlockEv\@4:
decl $tls::pstacklock(%eax) decl _cygtls.pstacklock(%eax)
ret ret
.global __ZN7_cygtls6lockedEv .global __ZN7_cygtls6lockedEv
__ZN7_cygtls6lockedEv: __ZN7_cygtls6lockedEv:
movl $tls::pstacklock(%eax),%eax movl _cygtls.pstacklock(%eax),%eax
ret ret
.extern __ZN7_cygtls19call_signal_handlerEv\@4 .extern __ZN7_cygtls19call_signal_handlerEv\@4
stabilize_sig_stack: stabilize_sig_stack:
movl %fs:4,%ebx movl %fs:4,%ebx
1: movl \$1,%eax 1: movl \$1,%eax
xchgl %eax,$tls::stacklock(%ebx) xchgl %eax,_cygtls.stacklock(%ebx)
movl %eax,$tls::spinning(%ebx) # flag if we are waiting for lock movl %eax,_cygtls.spinning(%ebx) # flag if we are waiting for lock
testl %eax,%eax testl %eax,%eax
jz 2f jz 2f
call _yield call _yield
jmp 1b jmp 1b
2: incl $tls::incyg(%ebx) 2: incl _cygtls.incyg(%ebx)
cmpl \$0,$tls::sig(%ebx) cmpl \$0,_cygtls.sig(%ebx)
jz 3f jz 3f
decl $tls::stacklock(%ebx) # unlock decl _cygtls.stacklock(%ebx) # unlock
movl \$$tls::start_offset,%eax # point to beginning movl \$_cygtls.start_offset,%eax # point to beginning
addl %ebx,%eax # of tls block addl %ebx,%eax # of tls block
call __ZN7_cygtls19call_signal_handlerEv\@4 call __ZN7_cygtls19call_signal_handlerEv\@4
jmp 1b jmp 1b
3: decl $tls::incyg(%ebx) 3: decl _cygtls.incyg(%ebx)
ret ret
EOF EOF
} }
@ -644,9 +642,9 @@ setjmp:
.seh_pushreg %rcx .seh_pushreg %rcx
call stabilize_sig_stack # returns tls in r11 call stabilize_sig_stack # returns tls in r11
popq %rcx popq %rcx
movq $tls::stackptr(%r11),%r10 movq _cygtls.stackptr(%r11),%r10
movq %r10,(%rcx) movq %r10,(%rcx)
decl $tls::stacklock(%r11) decl _cygtls.stacklock(%r11)
xorl %eax,%eax xorl %eax,%eax
ret ret
.seh_endproc .seh_endproc
@ -682,10 +680,10 @@ longjmp:
popq %rcx popq %rcx
movl %r12d,%eax # restore return value movl %r12d,%eax # restore return value
movq (%rcx),%r10 # get old signal stack movq (%rcx),%r10 # get old signal stack
movq %r10,$tls::stackptr(%r11) # restore movq %r10,_cygtls.stackptr(%r11) # restore
decl $tls::stacklock(%r11) # relinquish lock decl _cygtls.stacklock(%r11) # relinquish lock
xorl %r10d,%r10d xorl %r10d,%r10d
movl %r10d,$tls::incyg(%r11) # we're not in cygwin anymore movl %r10d,_cygtls.incyg(%r11) # we're not in cygwin anymore
movq 0x8(%rcx),%rbx movq 0x8(%rcx),%rbx
movq 0x10(%rcx),%rsp movq 0x10(%rcx),%rsp
movq 0x18(%rcx),%rbp movq 0x18(%rcx),%rbp
@ -774,8 +772,8 @@ _setjmp:
fnstcw 48(%edi) fnstcw 48(%edi)
pushl %ebx pushl %ebx
call stabilize_sig_stack call stabilize_sig_stack
movl $tls::stackptr(%ebx),%eax # save stack pointer contents movl _cygtls.stackptr(%ebx),%eax # save stack pointer contents
decl $tls::stacklock(%ebx) decl _cygtls.stacklock(%ebx)
popl %ebx popl %ebx
movl %eax,52(%edi) movl %eax,52(%edi)
popl %edi popl %edi
@ -879,10 +877,10 @@ _longjmp:
1: 1:
call stabilize_sig_stack call stabilize_sig_stack
movl 52(%edi),%eax # get old signal stack movl 52(%edi),%eax # get old signal stack
movl %eax,$tls::stackptr(%ebx) # restore movl %eax,_cygtls.stackptr(%ebx) # restore
decl $tls::stacklock(%ebx) # relinquish lock decl _cygtls.stacklock(%ebx) # relinquish lock
xorl %eax,%eax xorl %eax,%eax
movl %eax,$tls::incyg(%ebx) # we're not in cygwin anymore movl %eax,_cygtls.incyg(%ebx) # we're not in cygwin anymore
movl 12(%ebp),%eax movl 12(%ebp),%eax
testl %eax,%eax testl %eax,%eax

View File

@ -1,103 +1,83 @@
#!/usr/bin/perl -s #!/usr/bin/env bash
# #set -x
# This file is part of Cygwin. input_file=$1
# output_file=$2
# This software is a copyrighted work licensed under the terms of the
# Cygwin license. Please consult the file "CYGWIN_LICENSE" for # Preprocess cygtls.h and filter out only the member lines from
# details. # class _cygtls to generate an input file for the cross compiler
# # to generate the member offsets for tlsoffsets-$(target_cpu).h.
my $tls = shift; ${CXXCOMPILE} -E -P "${input_file}" 2> /dev/null | \
my $tls_out = shift; gawk '
my $tgt = shift; BEGIN {
# FIXME? This method obviously requires a 64 bit OS to build tlsoffsets64.h # marker is used to split out the member lines from class _cygtls
# Another method which doesn't requires to run an executable would be to marker=0;
# generate assembler code accessing the various struct members and analyzing # Prepare the input file for the subsequent compiler run.
# it, but that's arguably a lot more effort. # Prepend value of __CYGTLS_PADSIZE__ so we can compute the offsets
my $tgt_opt = $tgt eq 'x86_64' ? '-m64' : '-m32'; # up and down at the same time
open TLS, '<', $tls or die "$0: couldn't open tls file \"$tls\" - $!\n"; print "#include \"winsup.h\"";
my $struct = ''; print "#include \"cygtls.h\"";
my @fields = (); print "extern \"C\" const uint32_t __CYGTLS__start_offset = __CYGTLS_PADSIZE__;";
my $def = '';
$tls = join('', <TLS>);
$tls =~ s/\A.*\n#pragma once\n//os;
$tls =~ s/\n[^\n]*gentls_offsets[^\n]*\n(.+)\Z/$1/os;
my $pre = $`;
substr($tls, 0, length($pre)) = '';
$pre .= "\n//*/";
$tls =~ s%/\*\s*gentls_offsets.*?/\*\s*gentls_offsets\s*\*/%%ogs;
foreach ($tls =~ /^.*\n/mg) {
/^}|\s*(?:typedef|const)/ and do {
$def .= $_ ;
next;
};
$def .= $_ if $struct;
if (!s/;.*$//) {
if (!$struct && /^\s*(?:struct|class)\s*([a-z_0-9]+)/) {
$def .= $_;
$struct = $1
} }
next; /^class _cygtls$/ {
# Ok, bump marker, next we are expecting a "public:" line
marker=1;
} }
s/(?:\[[^\]]*\]|struct|class)//; /^public:/ {
s/^\s+\S+\s+//; # We are only interested in the lines between the first (marker == 2)
s/[\*\s()]+//g; # and the second (marker == 3) "public:" line in class _cygtls. These
for my $f (split(/,/)) { # are where the members are defined.
push(@fields, $f); if (marker > 0) ++marker;
if (marker > 2) exit;
} }
}
close TLS;
open TMP, '>', "/tmp/$$.cc" or die "$0: couldn't open temporary index file \"/tmp/$$.c\" - $!\n";
print TMP <<EOF;
#define __INSIDE_CYGWIN__
#ifndef __x86_64__
#define __attribute__(X)
#endif
#define __reg1
#define __reg2
#define __reg3
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <windows.h>
$pre
$def
int
main(int argc, char **argv)
{ {
$struct *foo; if (marker == 2 && $1 != "public:") {
# define foo_beg ((char *) foo) # Filter out function names
# define offset(f) ((unsigned)((int) (((char *) &(foo->f)) - foo_beg) - __CYGTLS_PADSIZE__)) $0 = gensub (/\(\*(\w+)\)\s*\([^\)]*\)/, "\\1", "g");
# define poffset(f) ((unsigned)(((char *) &(foo->f)) - ((char *) foo))) # Filter out leading asterisk
EOF $NF = gensub (/^\**(\w+)/, "\\1", "g", $NF);
print TMP 'puts ("//;# autogenerated: Do not edit.\n");', "\n\n"; # Filter out trailing array expression
print TMP "printf (\"//; \$tls::start_offset = -%d;\\n\", __CYGTLS_PADSIZE__);\n"; $NF = gensub (/(\w+)\s*\[[^\]]*\]/, "\\1", "g", $NF);
for my $f (@fields) { $NF = gensub (/(\w+);/, "\\1", "g", $NF);
print TMP ' printf ("//; $tls::', $f, ' = %d;\n", ', "offset($f));\n"; print "extern \"C\" const uint32_t __CYGTLS__" $NF " = offsetof (class _cygtls, " $NF ");";
print TMP ' printf ("//; $tls::p', $f, ' = %d;\n", ', "poffset($f));\n";
} }
print TMP ' puts ("//; __DATA__\n");', "\n";
for my $f (@fields) {
print TMP ' printf ("#define tls_', $f, ' (%d)\n", ', "offset($f));\n";
print TMP ' printf ("#define tls_p', $f, ' (%d)\n", ', "poffset($f));\n";
} }
' | \
print TMP <<EOF; # Now run the compiler to generate an assembler file.
${CXXCOMPILE} -x c++ -g0 -S - -o - | \
exit (0); # The assembler file consists of lines like these:
#
# __CYGTLS__foo
# .long 42
# .globl __CYGTLS__foo
# .align 4
#
# From this info, generate the tlsoffsets file.
gawk '\
BEGIN {
varname=""
start_offset = 0
} }
EOF /^__CYGTLS__/ {
close TMP; varname = gensub (/__CYGTLS__(\w+):/, "\\1", "g");
my @avoid_headers = qw'-D_XMMINTRIN_H_INCLUDED -D_ADXINTRIN_H_INCLUDED -D_EMMINTRIN_H_INCLUDED -D_X86INTRIN_H_INCLUDED'; }
my @cmd = (@ARGV, @avoid_headers, '-o', "/tmp/$$-1.cc", '-E', "/tmp/$$.cc"); /\s*\.space\s*4/ {
if (length (varname) > 0) {
system @cmd; printf (".equ _cygtls.%s, %d\n", varname, -start_offset);
system 'g++', "$tgt_opt", '-o', "/tmp/$$.a.out", "/tmp/$$-1.cc" and printf (".equ _cygtls.p%s, 0\n", varname);
($? == 127 && system 'c++', "$tgt_opt", '-o', "/tmp/$$.a.out", "/tmp/$$-1.cc") and varname = "";
die "$0: couldn't generate executable for offset calculation \"/tmp/$$.a.out\" - $!\n"; }
open TLS_OUT, '>', $tls_out or die "$0: couldn't open tls index file \"$tls_out\" - $!\n"; }
open OFFS, '-|', "/tmp/$$.a.out" or die "$0: couldn't run \"/tmp/$$.a.out\" - $!\n"; /\s*\.long\s+/ {
print TLS_OUT <OFFS>; if (length (varname) > 0) {
close OFFS; if (varname == "start_offset") {
close TLS_OUT; start_offset = $2;
unlink "/tmp/$$.cc", "/tmp/$$-1.cc", "/tmp/$$-1.d", "/tmp/$$.a.out"; printf (".equ _cygtls.%s, -%u\n", varname, start_offset);
exit(0); } else {
value = $2;
printf (".equ _cygtls.%s, %d\n", varname, value - start_offset);
printf (".equ _cygtls.p%s, %d\n", varname, value);
}
varname = "";
}
}
' > "${output_file}"

View File

@ -1,125 +0,0 @@
//;# autogenerated: Do not edit.
//; $tls::start_offset = -12800;
//; $tls::local_clib = -12800;
//; $tls::plocal_clib = 0;
//; $tls::__dontuse = -12800;
//; $tls::p__dontuse = 0;
//; $tls::locals = -11488;
//; $tls::plocals = 1312;
//; $tls::func = -9312;
//; $tls::pfunc = 3488;
//; $tls::saved_errno = -9304;
//; $tls::psaved_errno = 3496;
//; $tls::sa_flags = -9300;
//; $tls::psa_flags = 3500;
//; $tls::oldmask = -9296;
//; $tls::poldmask = 3504;
//; $tls::deltamask = -9288;
//; $tls::pdeltamask = 3512;
//; $tls::errno_addr = -9280;
//; $tls::perrno_addr = 3520;
//; $tls::sigmask = -9272;
//; $tls::psigmask = 3528;
//; $tls::sigwait_mask = -9264;
//; $tls::psigwait_mask = 3536;
//; $tls::altstack = -9256;
//; $tls::paltstack = 3544;
//; $tls::sigwait_info = -9232;
//; $tls::psigwait_info = 3568;
//; $tls::signal_arrived = -9224;
//; $tls::psignal_arrived = 3576;
//; $tls::will_wait_for_signal = -9216;
//; $tls::pwill_wait_for_signal = 3584;
//; $tls::__align = -9208;
//; $tls::p__align = 3592;
//; $tls::context = -9200;
//; $tls::pcontext = 3600;
//; $tls::thread_id = -7904;
//; $tls::pthread_id = 4896;
//; $tls::infodata = -7900;
//; $tls::pinfodata = 4900;
//; $tls::tid = -7752;
//; $tls::ptid = 5048;
//; $tls::_ctinfo = -7744;
//; $tls::p_ctinfo = 5056;
//; $tls::andreas = -7736;
//; $tls::pandreas = 5064;
//; $tls::wq = -7728;
//; $tls::pwq = 5072;
//; $tls::sig = -7680;
//; $tls::psig = 5120;
//; $tls::incyg = -7676;
//; $tls::pincyg = 5124;
//; $tls::spinning = -7672;
//; $tls::pspinning = 5128;
//; $tls::stacklock = -7668;
//; $tls::pstacklock = 5132;
//; $tls::stackptr = -7664;
//; $tls::pstackptr = 5136;
//; $tls::stack = -7656;
//; $tls::pstack = 5144;
//; $tls::initialized = -5608;
//; $tls::pinitialized = 7192;
//; __DATA__
#define tls_local_clib (-12800)
#define tls_plocal_clib (0)
#define tls___dontuse (-12800)
#define tls_p__dontuse (0)
#define tls_locals (-11488)
#define tls_plocals (1312)
#define tls_func (-9312)
#define tls_pfunc (3488)
#define tls_saved_errno (-9304)
#define tls_psaved_errno (3496)
#define tls_sa_flags (-9300)
#define tls_psa_flags (3500)
#define tls_oldmask (-9296)
#define tls_poldmask (3504)
#define tls_deltamask (-9288)
#define tls_pdeltamask (3512)
#define tls_errno_addr (-9280)
#define tls_perrno_addr (3520)
#define tls_sigmask (-9272)
#define tls_psigmask (3528)
#define tls_sigwait_mask (-9264)
#define tls_psigwait_mask (3536)
#define tls_altstack (-9256)
#define tls_paltstack (3544)
#define tls_sigwait_info (-9232)
#define tls_psigwait_info (3568)
#define tls_signal_arrived (-9224)
#define tls_psignal_arrived (3576)
#define tls_will_wait_for_signal (-9216)
#define tls_pwill_wait_for_signal (3584)
#define tls___align (-9208)
#define tls_p__align (3592)
#define tls_context (-9200)
#define tls_pcontext (3600)
#define tls_thread_id (-7904)
#define tls_pthread_id (4896)
#define tls_infodata (-7900)
#define tls_pinfodata (4900)
#define tls_tid (-7752)
#define tls_ptid (5048)
#define tls__ctinfo (-7744)
#define tls_p_ctinfo (5056)
#define tls_andreas (-7736)
#define tls_pandreas (5064)
#define tls_wq (-7728)
#define tls_pwq (5072)
#define tls_sig (-7680)
#define tls_psig (5120)
#define tls_incyg (-7676)
#define tls_pincyg (5124)
#define tls_spinning (-7672)
#define tls_pspinning (5128)
#define tls_stacklock (-7668)
#define tls_pstacklock (5132)
#define tls_stackptr (-7664)
#define tls_pstackptr (5136)
#define tls_stack (-7656)
#define tls_pstack (5144)
#define tls_initialized (-5608)
#define tls_pinitialized (7192)