Bug 90912 - Thread-local storage not working properly when compiling code with -fPIC and optimization on Solaris
Summary: Thread-local storage not working properly when compiling code with -fPIC and ...
Status: RESOLVED MOVED
Alias: None
Product: gcc
Classification: Unclassified
Component: target (show other bugs)
Version: 9.1.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2019-06-18 14:27 UTC by Witold Krecicki
Modified: 2019-06-21 17:31 UTC (History)
1 user (show)

See Also:
Host:
Target: x86_64-pc-solaris2.11
Build:
Known to work:
Known to fail: 5.5.0, 7.3.0, 9.1.0
Last reconfirmed:


Attachments
Preprocessed minimal testcase (1.53 KB, text/plain)
2019-06-18 14:27 UTC, Witold Krecicki
Details
Minimal testcase (164 bytes, text/x-csrc)
2019-06-18 14:28 UTC, Witold Krecicki
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Witold Krecicki 2019-06-18 14:27:52 UTC
Created attachment 46497 [details]
Preprocessed minimal testcase

When compiling with -fPIC and any level of optimization the following code doesn't work properly:

```
#include <stdio.h>

static __thread int tls[4];

void
initialize(void) {
        tls[0] = 1; tls[1] = 2; tls[2] = 3; tls[3] = 4;
        printf("initialize %u %u %u %u, ", tls[0], tls[1], tls[2], tls[3]);
}

int main() {
        initialize();
        printf("value %u %u %u %u\n", tls[0], tls[1], tls[2], tls[3]);
}
```

The result depends on the optimization level:
-O0 initialize 1 2 3 4, value 1 2 3 4
-O1 initialize 1 2 3 4, value 4 4 4 4
-O2 initialize 1 2 3 4, value 3 3 3 3
-O3 initialize 1 2 3 4, value 1 1 1 1


This makes thread-local storage unusable in libraries on Solaris.

gcc -v -O2 -fPIC -save-temps tls-test.c output:
```
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/gcc/7/lib/gcc/x86_64-pc-solaris2.11/7.3.0/lto-wrapper
Target: x86_64-pc-solaris2.11
Configured with: /builds/ulhg/workspace/Solaris_11u4/Userland/full-build/02a-build-i386/components/gcc7/gcc-7.3.0/configure --prefix=/usr/gcc/7 --mandir=/usr/gcc/7/share/man --bindir=/usr/gcc/7/bin --sbindir=/us
Thread model: posix
gcc version 7.3.0 (GCC) 
COLLECT_GCC_OPTIONS='-v' '-O2' '-fPIC' '-save-temps' '-mtune=generic' '-march=x86-64'
 /usr/gcc/7/lib/gcc/x86_64-pc-solaris2.11/7.3.0/cc1 -E -quiet -v tls-test.c -mtune=generic -march=x86-64 -fPIC -O2 -fpch-preprocess -o tls-test.i
ignoring nonexistent directory "/usr/local/include"
ignoring nonexistent directory "/usr/gcc/7/lib/gcc/x86_64-pc-solaris2.11/7.3.0/../../../../x86_64-pc-solaris2.11/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/gcc/7/lib/gcc/x86_64-pc-solaris2.11/7.3.0/include
 /usr/gcc/7/include
 /usr/gcc/7/lib/gcc/x86_64-pc-solaris2.11/7.3.0/include-fixed
 /usr/include
End of search list.
COLLECT_GCC_OPTIONS='-v' '-O2' '-fPIC' '-save-temps' '-mtune=generic' '-march=x86-64'
 /usr/gcc/7/lib/gcc/x86_64-pc-solaris2.11/7.3.0/cc1 -fpreprocessed tls-test.i -quiet -dumpbase tls-test.c -mtune=generic -march=x86-64 -auxbase tls-test -O2 -version -fPIC -o tls-test.s
GNU C11 (GCC) version 7.3.0 (x86_64-pc-solaris2.11)
        compiled by GNU C version 7.3.0, GMP version 6.1.2, MPFR version 3.1.5, MPC version 1.0.3, isl version isl-0.18-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
GNU C11 (GCC) version 7.3.0 (x86_64-pc-solaris2.11)
        compiled by GNU C version 7.3.0, GMP version 6.1.2, MPFR version 3.1.5, MPC version 1.0.3, isl version isl-0.18-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 295b202e34f5835e529741e171b5a9d2
COLLECT_GCC_OPTIONS='-v' '-O2' '-fPIC' '-save-temps' '-mtune=generic' '-march=x86-64'
 /usr/gnu/bin/as -v -V -Qy -s --64 -o tls-test.o tls-test.s
GNU assembler version 2.30 (x86_64-pc-solaris2.11) using BFD version (GNU Binutils) 2.30
COMPILER_PATH=/usr/gcc/7/lib/gcc/x86_64-pc-solaris2.11/7.3.0/:/usr/gcc/7/lib/gcc/x86_64-pc-solaris2.11/7.3.0/:/usr/gcc/7/lib/gcc/x86_64-pc-solaris2.11/:/usr/gcc/7/lib/gcc/x86_64-pc-solaris2.11/7.3.0/:/usr/gcc/7/
LIBRARY_PATH=/usr/gcc/7/lib/gcc/x86_64-pc-solaris2.11/7.3.0/:/usr/gcc/7/lib/gcc/x86_64-pc-solaris2.11/7.3.0/../../../amd64/:/lib/amd64/:/usr/lib/amd64/:/usr/gcc/7/lib/gcc/x86_64-pc-solaris2.11/7.3.0/../../../:/l
COLLECT_GCC_OPTIONS='-v' '-O2' '-fPIC' '-save-temps' '-mtune=generic' '-march=x86-64'
 /usr/gcc/7/lib/gcc/x86_64-pc-solaris2.11/7.3.0/collect2 -V -Y P,/lib/amd64:/usr/lib/amd64 -Qy /usr/lib/amd64/crt1.o /usr/gcc/7/lib/gcc/x86_64-pc-solaris2.11/7.3.0/crtp.o /usr/lib/amd64/crti.o /usr/lib/amd64/val
ld: Software Generation Utilities - Solaris Link Editors: 5.11-1.3159
COLLECT_GCC_OPTIONS='-v' '-O2' '-fPIC' '-save-temps' '-mtune=generic' '-march=x86-64'
```
Comment 1 Witold Krecicki 2019-06-18 14:28:53 UTC
Created attachment 46498 [details]
Minimal testcase
Comment 2 Witold Krecicki 2019-06-18 20:06:35 UTC
I was able to reproduce it on fresh build of gcc 9.1.0:

Using built-in specs.
COLLECT_GCC=/usr/gcc/9/bin/gcc
COLLECT_LTO_WRAPPER=/usr/gcc/9/lib/gcc/x86_64-pc-solaris2.11/9.1.0/lto-wrapper
Target: x86_64-pc-solaris2.11
Configured with: ../configure --prefix=/usr/gcc/9 --mandir=/usr/gcc/9/share/man --bindir=/usr/gcc/9/bin --sbindir=/usr/gcc/9/sbin --libdir=/usr/gcc/9/lib --infodir=/usr/gcc/9/share/info --libexecdir=/usr/gcc/9/lib --enable-languages=c,c++,fortran,objc --enable-shared --enable-initfini-array --disable-rpath --with-system-zlib --with-build-config=no --with-gmp-include=/usr/include --with-mpfr-include=/usr/include --without-gnu-ld --with-ld=/usr/bin/ld --with-gnu-as --with-as=/usr/gnu/bin/as --disable-bootstrap 'BOOT_CFLAGS=-g -O2' x86_64-pc-solaris2.11
Thread model: posix
gcc version 9.1.0 (GCC) 
COLLECT_GCC_OPTIONS='-v' '-fPIC' '-O3' '-mtune=generic' '-march=x86-64'
 /usr/gcc/9/lib/gcc/x86_64-pc-solaris2.11/9.1.0/cc1 -quiet -v tls-test.c -quiet -dumpbase tls-test.c -mtune=generic -march=x86-64 -auxbase tls-test -O3 -version -fPIC -o /var/tmp//ccDNriHa.s
GNU C17 (GCC) version 9.1.0 (x86_64-pc-solaris2.11)
        compiled by GNU C version 7.3.0, GMP version 6.1.2, MPFR version 3.1.5, MPC version 1.0.3, isl version isl-0.18-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring nonexistent directory "/usr/local/include"
ignoring nonexistent directory "/usr/gcc/9/lib/gcc/x86_64-pc-solaris2.11/9.1.0/../../../../x86_64-pc-solaris2.11/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/gcc/9/lib/gcc/x86_64-pc-solaris2.11/9.1.0/include
 /usr/gcc/9/include
 /usr/gcc/9/lib/gcc/x86_64-pc-solaris2.11/9.1.0/include-fixed
 /usr/include
End of search list.
GNU C17 (GCC) version 9.1.0 (x86_64-pc-solaris2.11)
        compiled by GNU C version 7.3.0, GMP version 6.1.2, MPFR version 3.1.5, MPC version 1.0.3, isl version isl-0.18-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: f7f8d50a0a7d1f8b10135f1417732223
COLLECT_GCC_OPTIONS='-v' '-fPIC' '-O3' '-mtune=generic' '-march=x86-64'
 /usr/gnu/bin/as -v -V -Qy -s --64 -o /var/tmp//cc0akr6a.o /var/tmp//ccDNriHa.s
GNU assembler version 2.30 (x86_64-pc-solaris2.11) using BFD version (GNU Binutils) 2.30
COMPILER_PATH=/usr/gcc/9/lib/gcc/x86_64-pc-solaris2.11/9.1.0/:/usr/gcc/9/lib/gcc/x86_64-pc-solaris2.11/9.1.0/:/usr/gcc/9/lib/gcc/x86_64-pc-solaris2.11/:/usr/gcc/9/lib/gcc/x86_64-pc-solaris2.11/9.1.0/:/usr/gcc/9/lib/gcc/x86_64-pc-solaris2.11/:/usr/ccs/bin/
LIBRARY_PATH=/usr/gcc/9/lib/gcc/x86_64-pc-solaris2.11/9.1.0/:/usr/gcc/9/lib/gcc/x86_64-pc-solaris2.11/9.1.0/../../../amd64/:/lib/amd64/:/usr/lib/amd64/:/usr/gcc/9/lib/gcc/x86_64-pc-solaris2.11/9.1.0/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-fPIC' '-O3' '-mtune=generic' '-march=x86-64'
 /usr/gcc/9/lib/gcc/x86_64-pc-solaris2.11/9.1.0/collect2 -V -Y P,/lib/amd64:/usr/lib/amd64 -Qy /usr/lib/amd64/crt1.o /usr/gcc/9/lib/gcc/x86_64-pc-solaris2.11/9.1.0/crtp.o /usr/lib/amd64/crti.o /usr/lib/amd64/values-Xa.o /usr/lib/amd64/values-xpg6.o /usr/gcc/9/lib/gcc/x86_64-pc-solaris2.11/9.1.0/crtbegin.o -L/usr/gcc/9/lib/gcc/x86_64-pc-solaris2.11/9.1.0 -L/usr/gcc/9/lib/gcc/x86_64-pc-solaris2.11/9.1.0/../../../amd64 -L/lib/amd64 -L/usr/lib/amd64 -L/usr/gcc/9/lib/gcc/x86_64-pc-solaris2.11/9.1.0/../../.. /var/tmp//cc0akr6a.o -lgcc -z ignore -lgcc_s -z record -lc -lgcc -z ignore -lgcc_s -z record /usr/gcc/9/lib/gcc/x86_64-pc-solaris2.11/9.1.0/crtend.o /usr/lib/amd64/crtn.o
ld: Software Generation Utilities - Solaris Link Editors: 5.11-1.3159
COLLECT_GCC_OPTIONS='-v' '-fPIC' '-O3' '-mtune=generic' '-march=x86-64'
Comment 3 Witold Krecicki 2019-06-18 23:18:07 UTC
It seems to be a bad interaction between GCC and Solaris linker, with GNU ld it works correctly on all optimization levels - gcc -v below. Unfortunately gcc shipped with Solaris is using Solaris linker.

```
Using built-in specs.
COLLECT_GCC=/usr/gcc/9-gnuld/bin/gcc
COLLECT_LTO_WRAPPER=/usr/gcc/9-gnuld/lib/gcc/x86_64-pc-solaris2.11/9.1.0/lto-wrapper
Target: x86_64-pc-solaris2.11
Configured with: ../configure --prefix=/usr/gcc/9-gnuld --mandir=/usr/gcc/9-gnuld/share/man --bindir=/usr/gcc/9-gnuld/bin --sbindir=/usr/gcc/9-gnuld/sbin --libdir=/usr/gcc/9-gnuld/lib --infodir=/usr/gcc/9-gnuld/share/info --libexecdir=/usr/gcc/9-gnuld/lib --enable-languages=c,c++,fortran,objc --enable-shared --enable-initfini-array --disable-rpath --with-system-zlib --with-build-config=no --with-gmp-include=/usr/include --with-mpfr-include=/usr/include --with-gnu-ld=/usr/gnu/bin/ld --with-ld=/usr/gnu/bin/ld --with-gnu-as --with-as=/usr/gnu/bin/as --disable-bootstrap 'BOOT_CFLAGS=-g -O2' x86_64-pc-solaris2.11
Thread model: posix
gcc version 9.1.0 (GCC) 
COLLECT_GCC_OPTIONS='-v' '-O2' '-fPIC' '-mtune=generic' '-march=x86-64'
 /usr/gcc/9-gnuld/lib/gcc/x86_64-pc-solaris2.11/9.1.0/cc1 -quiet -v tls-test.c -quiet -dumpbase tls-test.c -mtune=generic -march=x86-64 -auxbase tls-test -O2 -version -fPIC -o /var/tmp//ccFCnhKb.s
GNU C17 (GCC) version 9.1.0 (x86_64-pc-solaris2.11)
        compiled by GNU C version 9.1.0, GMP version 6.1.2, MPFR version 3.1.5, MPC version 1.0.3, isl version isl-0.18-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring nonexistent directory "/usr/local/include"
ignoring nonexistent directory "/usr/gcc/9-gnuld/lib/gcc/x86_64-pc-solaris2.11/9.1.0/../../../../x86_64-pc-solaris2.11/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/gcc/9-gnuld/lib/gcc/x86_64-pc-solaris2.11/9.1.0/include
 /usr/gcc/9-gnuld/include
 /usr/gcc/9-gnuld/lib/gcc/x86_64-pc-solaris2.11/9.1.0/include-fixed
 /usr/include
End of search list.
GNU C17 (GCC) version 9.1.0 (x86_64-pc-solaris2.11)
        compiled by GNU C version 9.1.0, GMP version 6.1.2, MPFR version 3.1.5, MPC version 1.0.3, isl version isl-0.18-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 8d00c2a77a592f334e9eb5693e454f4c
COLLECT_GCC_OPTIONS='-v' '-O2' '-fPIC' '-mtune=generic' '-march=x86-64'
 /usr/gnu/bin/as -v -V -Qy -s --64 -o /var/tmp//cciW7WLb.o /var/tmp//ccFCnhKb.s
GNU assembler version 2.30 (x86_64-pc-solaris2.11) using BFD version (GNU Binutils) 2.30
COMPILER_PATH=/usr/gcc/9-gnuld/lib/gcc/x86_64-pc-solaris2.11/9.1.0/:/usr/gcc/9-gnuld/lib/gcc/x86_64-pc-solaris2.11/9.1.0/:/usr/gcc/9-gnuld/lib/gcc/x86_64-pc-solaris2.11/:/usr/gcc/9-gnuld/lib/gcc/x86_64-pc-solaris2.11/9.1.0/:/usr/gcc/9-gnuld/lib/gcc/x86_64-pc-solaris2.11/:/usr/ccs/bin/
LIBRARY_PATH=/usr/gcc/9-gnuld/lib/gcc/x86_64-pc-solaris2.11/9.1.0/:/usr/gcc/9-gnuld/lib/gcc/x86_64-pc-solaris2.11/9.1.0/../../../amd64/:/lib/amd64/:/usr/lib/amd64/:/usr/gcc/9-gnuld/lib/gcc/x86_64-pc-solaris2.11/9.1.0/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-O2' '-fPIC' '-mtune=generic' '-march=x86-64'
 /usr/gcc/9-gnuld/lib/gcc/x86_64-pc-solaris2.11/9.1.0/collect2 -plugin /usr/gcc/9-gnuld/lib/gcc/x86_64-pc-solaris2.11/9.1.0/liblto_plugin.so -plugin-opt=/usr/gcc/9-gnuld/lib/gcc/x86_64-pc-solaris2.11/9.1.0/lto-wrapper -plugin-opt=-fresolution=/var/tmp//cce7p_6a.res -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_eh -plugin-opt=-pass-through=-lc -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_eh --eh-frame-hdr -V -m elf_x86_64_sol2 -Y P,/lib/amd64:/usr/lib/amd64 -Qy /usr/lib/amd64/crt1.o /usr/gcc/9-gnuld/lib/gcc/x86_64-pc-solaris2.11/9.1.0/crtp.o /usr/lib/amd64/crti.o /usr/lib/amd64/values-Xa.o /usr/lib/amd64/values-xpg6.o /usr/gcc/9-gnuld/lib/gcc/x86_64-pc-solaris2.11/9.1.0/crtbegin.o -L/usr/gcc/9-gnuld/lib/gcc/x86_64-pc-solaris2.11/9.1.0 -L/usr/gcc/9-gnuld/lib/gcc/x86_64-pc-solaris2.11/9.1.0/../../../amd64 -L/lib/amd64 -L/usr/lib/amd64 -L/usr/gcc/9-gnuld/lib/gcc/x86_64-pc-solaris2.11/9.1.0/../../.. /var/tmp//cciW7WLb.o -lgcc -lgcc_eh -lc -lgcc -lgcc_eh /usr/gcc/9-gnuld/lib/gcc/x86_64-pc-solaris2.11/9.1.0/crtend.o /usr/lib/amd64/crtn.o
GNU ld (GNU Binutils) 2.30
  Supported emulations:
   elf_x86_64_sol2
   elf_x86_64
   elf_i386_sol2
   elf_i386_ldso
   elf_i386
   elf_iamcu
   elf_l1om
   elf_k1om
COLLECT_GCC_OPTIONS='-v' '-O2' '-fPIC' '-mtune=generic' '-march=x86-64'
```
Comment 4 Andrew Pinski 2019-06-18 23:44:56 UTC
Well GCC is producing correct code is GNU LD works with it.  Report this bug to Sun^wOracle instead.
Comment 5 Ondřej Surý 2019-06-21 16:23:05 UTC
(In reply to Andrew Pinski from comment #4)
> Well GCC is producing correct code is GNU LD works with it.

No, not really.

> Report this bug to Sun^wOracle instead.

That said, we did this and it has been already fixed in GCC via PR84010.
Comment 6 Andrew Pinski 2019-06-21 17:31:27 UTC
(In reply to Ondřej Surý from comment #5)
> 
> That said, we did this and it has been already fixed in GCC via PR84010.

How so?  That PR is for sparc, the patches are against sparc too.