llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
29.36k stars 12.14k forks source link

TLS segment offsets different for target i386 and i386-unknown-linux #109087

Open prabhjotsingh3 opened 2 months ago

prabhjotsingh3 commented 2 months ago

Hi,

The tls offsets used is different for target i386 and i386-unknonwn-linux for local-exec model.

Example code :-

#include <stdio.h>
__thread int myvar;
int main(void)
{
        myvar = 0;
        return 0;
}

For target i386 :- Command - clang -ftls-model=local-exec --target=i386 -v thread.c

080484a0 <main>:
 80484a3:       50                      push   %eax
 80484a4:       c7 45 fc 00 00 00 00    movl   $0x0,-0x4(%ebp)
 80484ab:       65 a1 00 00 00 00       mov    %gs:0x0,%eax
 80484b1:       c7 80 fc ff ff ff 00    movl   $0x0,-0x4(%eax)
 80484b8:       00 00 00 
 80484bb:       31 c0                   xor    %eax,%eax
 80484bd:       83 c4 04                add    $0x4,%esp
 80484c0:       5d                      pop    %ebp
 80484c1:       c3                      ret

For target i386-unknown-linux :- Command - clang -ftls-model=local-exec --target=i386-unknown-linux -v thread.c

000011a0 <main>:
    11a0:       55                      push   %ebp
    11a1:       89 e5                   mov    %esp,%ebp
    11a3:       50                      push   %eax
    11a4:       c7 45 fc 00 00 00 00    movl   $0x0,-0x4(%ebp)
    11ab:       65 c7 05 fc ff ff ff    movl   $0x0,%gs:0xfffffffc
    11b2:       00 00 00 00 
    11b6:       31 c0                   xor    %eax,%eax
    11b8:       83 c4 04                add    $0x4,%esp
    11bb:       5d                      pop    %ebp
    11bc:       c3                      ret
    11bd:       66 90                   xchg   %ax,%ax
    11bf:       90                      nop

Would be grateful if anyway could please let me know the reason for this and is there anyway to control this via clang arguments?

prabhjotsingh3 commented 2 months ago

Closed this issue by mistake. Is there any way to reopen it?

llvmbot commented 2 months ago

@llvm/issue-subscribers-backend-x86

Author: None (prabhjotsingh3)

Hi, The tls offsets used is different for target i386 and i386-unknonwn-linux for local-exec model. Example code :- ``` #include <stdio.h> __thread int myvar; int main(void) { myvar = 0; return 0; } ``` For target i386 :- Command - `clang -ftls-model=local-exec --target=i386 -v thread.c` ``` 080484a0 <main>: 80484a3: 50 push %eax 80484a4: c7 45 fc 00 00 00 00 movl $0x0,-0x4(%ebp) 80484ab: 65 a1 00 00 00 00 mov %gs:0x0,%eax 80484b1: c7 80 fc ff ff ff 00 movl $0x0,-0x4(%eax) 80484b8: 00 00 00 80484bb: 31 c0 xor %eax,%eax 80484bd: 83 c4 04 add $0x4,%esp 80484c0: 5d pop %ebp 80484c1: c3 ret ``` For target i386-unknown-linux :- Command - `clang -ftls-model=local-exec --target=i386-unknown-linux -v thread.c` ``` 000011a0 <main>: 11a0: 55 push %ebp 11a1: 89 e5 mov %esp,%ebp 11a3: 50 push %eax 11a4: c7 45 fc 00 00 00 00 movl $0x0,-0x4(%ebp) 11ab: 65 c7 05 fc ff ff ff movl $0x0,%gs:0xfffffffc 11b2: 00 00 00 00 11b6: 31 c0 xor %eax,%eax 11b8: 83 c4 04 add $0x4,%esp 11bb: 5d pop %ebp 11bc: c3 ret 11bd: 66 90 xchg %ax,%ax 11bf: 90 nop ``` Would be grateful if anyway could please let me know the reason for this and is there anyway to control this via clang arguments?
prabhjotsingh3 commented 2 months ago

Hi, Can anyone please provide some insights on the difference and how "movl $0x0,%gs:0xfffffffc" instruction works?