On 11 Feb 2025, at 23:25, Brian Inglis <brian.ing...@systematicsw.ab.ca> wrote:
> 
> On 2025-02-11 13:58, Dimitry Andric wrote:
>>> On 11 Feb 2025, at 21:45, Brian Inglis via Cygwin wrote:
>>> On 2025-02-11 12:41, Dimitry Andric via Cygwin wrote:
>>>>> On 11 Feb 2025, at 20:26, Ken Brown via Cygwin wrote:
>>>>> Does Cygwin support __builtin_thread_pointer?  I'm guessing not, because
>>>>> I'm getting a link error (undefined reference to
>>>>> `__builtin_thread_pointer') when I try to build some software that uses
>>>>> it.  Or is there something that would have to be added to the link command
>>>>> line?  The full command line and error message are quoted below, in case
>>>>> anyone spots something obvious.
>>> 
>>>> It's a gcc builtin function, not something implemented in an external 
>>>> library. Therefore, there is no linker option that can fix this.
>>>> Also, as far as I can see from gcc's documentation,
>>>> __builtin_thread_pointer() is only supported for the RISCV and SH
>>>> architectures. Then again, gcc's implementation seems to hinge this on
>>>> whether the platform supports TLS or not.
>>>> In any case, it is probably better to not use this function if your gcc 
>>>> does
>>>> not support it.
>>> Does `info gcc tls` help and do we know if that is supported by our gcc(s)?
> 
> > It does not look like it's supported by Cygwin's gcc:
> > $ gcc -v
> > Using built-in specs.
> > COLLECT_GCC=gcc
> > COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-cygwin/12/lto-wrapper.exe
> > Target: x86_64-pc-cygwin
> > Configured with: /mnt/share/cygpkgs/gcc/gcc.x86_64/src/gcc-12.4.0/configure 
> > --srcdir=/mnt/share/cygpkgs/gcc/gcc.x86_64/src/gcc-12.4.0 --prefix=/usr 
> > --exec-prefix=/usr --localstatedir=/var --sysconfdir=/etc 
> > --docdir=/usr/share/doc/gcc --htmldir=/usr/share/doc/gcc/html -C 
> > --build=x86_64-pc-cygwin --host=x86_64-pc-cygwin --target=x86_64-pc-cygwin 
> > --without-libiconv-prefix --without-libintl-prefix --libexecdir=/usr/lib 
> > --with-gcc-major-version-only --enable-shared --enable-shared-libgcc 
> > --enable-static --enable-version-specific-runtime-libs --enable-bootstrap 
> > --enable-__cxa_atexit --enable-clocale=newlib --with-dwarf2 
> > --with-tune=generic 
> > --enable-languages=ada,c,c++,fortran,lto,objc,obj-c++,jit --enable-graphite 
> > --enable-threads=posix --enable-libatomic --enable-libgomp 
> > --enable-libquadmath --enable-libquadmath-support --disable-libssp 
> > --enable-libada --disable-symvers --disable-multilib --with-gnu-ld 
> > --with-gnu-as --with-cloog-include=/usr/include/cloog-isl 
> > --without-libiconv-prefix
  --without-libintl-prefix --with-system-zlib --enable-linker-build-id 
--with-default-libstdcxx-abi=gcc4-compatible --enable-libstdcxx-filesystem-ts
> > Thread model: posix
> > Supported LTO compression algorithms: zlib zstd
> > gcc version 12.4.0 (GCC)
> 
> > On Ubuntu 24.04 this program compiles without warnings.
> 
> On which platform - RISCV or SH?

Sorry, I forgot to mention that: just plain old x86_64. I think the Ubuntu 
package build will have detected TLS support during its configure phase.

> 
> > It looks like the gcc configure script checks for TLS here: 
> > https://gcc.gnu.org/git/?p=gcc.git;a=blob;f=gcc/configure.ac;h=8fab93c9365496062583c5bd8ace402c8a797817;hb=HEAD#l3846,
> >  by running some code through the assembler. I would guess that doesn't 
> > work on Cygwin.
> 
> It should as gas supports storage class 'T' for thread, and GCC supports and 
> uses posix threads.

I tried to find Cygwin gcc build logs on 
https://cygwin.com/cgi-bin2/jobs.cgi?srcpkg=gcc&status=&user=, but that comes 
up with only "not built" builds, or failed builds. If somebody knows a URL 
where the configure output can be found, it might shed some more light.


> This compiles just fine under Cygwin:
> 
> /* test-thread-storage.c */
> int f(int *np, char *cp)
> {
>  static __thread int  n;
>  static __thread char *p;
>  int i;
> 
>  n = *np;
>  p = cp;
> 
>  for (i = 0; i < n; ++i)
>      *cp++ = *p++;
> 
>  return i;
> }
> 
> Cygwin and GCC support POSIX threads, and they are used in many Cygwin 
> packages, this may just work!
> 
> Cygwin could define its own builtin_thread_pointer() to access its own cygtls 
> areas from GCC, and if they are defined using storage class 'T', it should 
> work.
> It is not clear if this is just intended as an optimization for a few 
> embedded targets that have a dedicated pointer register for tls.

Yes, that is the RISCV or SH specific method. On x86_64, you access it via the 
fs register, i.e. gcc (on Linux at least :) compiles:

  void *p = __builtin_thread_pointer();

to something like:

        movq    %fs:0, %rax
        movq    %rax, -8(%rbp)

If somebody knows how to build the Cygwin gcc package from scratch, they could 
inspect the config.log file, to see what that says about TLS detection.

-Dimitry


-- 
Problem reports:      https://cygwin.com/problems.html
FAQ:                  https://cygwin.com/faq/
Documentation:        https://cygwin.com/docs.html
Unsubscribe info:     https://cygwin.com/ml/#unsubscribe-simple

Reply via email to