Skip to content

Static executables using TLS crash with SIGSEGV on Linux

$ cat tls.c 
__thread int i;

int main() {
  i = 42;
}
(gdb) r
Starting program: /home/mateusz/a.out 

Program received signal SIGSEGV, Segmentation fault.
main () at tls.c:4
4	  i = 42;
(gdb) bt
#0  main () at tls.c:4
(gdb) disas
Dump of assembler code for function main:
   0x0000000000233030 <+0>:	push   %rbp
   0x0000000000233031 <+1>:	mov    %rsp,%rbp
   0x0000000000233034 <+4>:	xor    %eax,%eax
=> 0x0000000000233036 <+6>:	movl   $0x2a,%fs:0xffffffffffffffe8
   0x0000000000233042 <+18>:	pop    %rbp
   0x0000000000233043 <+19>:	retq   
End of assembler dump.

Convenient way to test is available at mati865/relibc@44d6c623. Running make test will fail with:

# tls #
Segmentation fault (core dumped)
Edited by Mateusz Mikuła