https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128846
--- Comment #16 from ro at CeBiTec dot Uni-Bielefeld.DE <ro at CeBiTec dot
Uni-Bielefeld.DE> ---
I've finally found what's going on. Consider this minimal testcase:
$ cat min.c
__thread double thrtest[3];
void main ()
{
thrtest[0] = 1.0;
thrtest[1] = 2.0;
}
For ease of comparion between different targets and gcc/clang, compile
and link with
gcc -m32 -O2 -fomit-frame-pointer -fno-PIC -mno-tls-direct-seg-refs
-fno-asynchronous-unwind-tables -fno-dwarf2-cfi-asm -Wl,-no-pie
The assembler output is identical between Solaris and Linux
.file "min.c"
.text
.section .text.startup,"ax",@progbits
.p2align 4
.globl main
.type main, @function
main:
movapd .LC0, %xmm0
movl %gs:0, %eax
movaps %xmm0, thrtest@ntpoff(%eax)
ret
.size main, .-main
.globl thrtest
.section .tbss,"awT",@nobits
.align 16
.type thrtest, @object
.size thrtest, 24
thrtest:
.zero 24
.section .rodata.cst16,"aM",@progbits,16
.align 16
.LC0:
.long 0
.long 1072693248
.long 0
.long 1073741824
as are the object files, which have
8: 65 a1 00 00 00 00 mov %gs:0x0,%eax
e: 0f 29 80 00 00 00 00 movaps %xmm0,0x0(%eax)
11: R_386_TLS_LE thrtest
However, the resulting executables differ in one important respect:
Solaris has
main+0x8: 65 a1 00 00 00 00 movl %gs:0x0,%eax
main+0xe: 0f 29 80 e8 ff ff movaps %xmm0,-0x18(%eax)
ff
while Linux is
main()
main+0x8: 65 a1 00 00 00 00 movl %gs:0x0,%eax
main+0xe: 0f 29 80 e0 ff ff movaps %xmm0,-0x20(%eax)
ff
i.e. the ntpoff reloc results in different offsets:
movaps %xmm0, thrtest@ntpoff(%eax)
On Linux, the static TLS offset is 16-byte aligned, on Solaris it's only
8 bytes, so we get the SEGV for an unaligned access. Solaris ld and gld
handle this in the same way.
This issue was fixed in gld 16 years ago as described in the thread
starting at
https://sourceware.org/legacy-ml/binutils/2010-09/msg00485.html
The Solaris ABI requires that 8-byte alignment:
https://docs.oracle.com/en/operating-systems/solaris/oracle-solaris/11.4/linkers-libraries/thread-local-storage-section.html
For relocations against static TLS items, the relocation address
is encoded as a negative offset from the end of the static TLS
template. This offset is calculated by first rounding the
template size to the nearest 8-byte boundary in a 32-bit object,
and to the nearest 16-byte boundary in a 64-bit object. This
rounding ensures that the static TLS template is suitably
aligned for any use.
so both linkers follow suit. The binutils side is commit
7dc98aeaf1f3fe256e284347a41f1c150955c37d.
clang avoids this problem by not requiring the alignment. It generates
main: # @main
movl %gs:0, %eax
movaps .LCPI0_0, %xmm0 # xmm0 = [1.0E+0,2.0E+0]
movups %xmm0, thrtest@NTPOFF(%eax)
retl
on both Solaris and Linux, i.e. uses movups instead of movaps.