https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66631

            Bug ID: 66631
           Summary: inability to clobber segment regs makes tls
                    problematic
           Product: gcc
           Version: 5.1.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: inline-asm
          Assignee: unassigned at gcc dot gnu.org
          Reporter: stsp at users dot sourceforge.net
  Target Milestone: ---

Hello.

On x86 (32/64) gcc uses FS register for TLS.
When writing a signal handler, a special care
must be taken: FS register must be restored by
hands, together with all other segment registers,
because linux unfortunately does not do that
(except cs and ds).

Then signal handler can access the thread-local
data safely... except that it can't.
After calling the function that restores the
segment registers, I need to specify that FS was
clobbered, which is currently not possible with
the inline asm.
As a result, the following code is generated:

   0x00000000004438c0 <+0>:     push   %r14
   0x00000000004438c2 <+2>:     mov    %edi,%r14d
   0x00000000004438c5 <+5>:     push   %r13
   0x00000000004438c7 <+7>:     mov    %rdx,%r13
   0x00000000004438ca <+10>:    push   %r12
   0x00000000004438cc <+12>:    lea    0x28(%rdx),%r12
   0x00000000004438d0 <+16>:    push   %rbp
   0x00000000004438d1 <+17>:    mov    %r12,%rdi
   0x00000000004438d4 <+20>:    mov    %fs:0x0,%rbp
   0x00000000004438dd <+29>:    push   %rbx
   0x00000000004438de <+30>:    add    $0xffffffffffffff80,%rsp
   0x00000000004438e2 <+34>:    callq  0x4441b0 <init_handler>
   0x00000000004438e7 <+39>:    mov    0x35070a(%rip),%rbx        # 0x793ff8
   0x00000000004438ee <+46>:    mov    0x0(%rbp,%rbx,1),%eax  <-- STACK FAULT

init_handler() is a function that restore the segregs -
it is called first in a function:
---
{
  init_handler(scp);
//  asm volatile("":::"fs");
  fault_cnt++;
---

fault_cnt is a thread-local var:
extern volatile __thread int fault_cnt;

As you can see, gcc doesn't move the access to
fault_cnt below init_handler(), but, unfortunately,
it moved the address calculation. To prevent this,
I need to clobber the FS, but it doesn't seem to be
possible.

Please let me know if there are any work-arounds to that.
It seems to be a bug that gcc does not allow clobbering
the segregs that it use.

Reply via email to