On Tue, Sep 9, 2025 at 9:04 AM Alexander Monakov <amona...@ispras.ru> wrote:
>
>
> On Tue, 9 Sep 2025, H.J. Lu wrote:
>
> > > > to restore assert if not compiling for shared library.  When compiling
> > > > for shared library, the recomputed model may be weaker.
> > >
> > > I don't understand why (for shared libraries), can you explain?
> >
> > Another shared library or executable may override any global
> > symbols with the default visibility in a shared library.
>
> I know that symbols with default visibility can be interposed, but that 
> doesn't
> answer the question. Do you have a testcase that demonstrates the issue?

TLS tests were submitted at

https://patchwork.sourceware.org/project/gcc/patch/20250908221002.3223418-1-hjl.to...@gmail.com/

The key point is that compiler doesn't know if PIC code will be in
shared library or executable.   If there is a programmer error, linker will
issue an error:

[hjl@gnu-cfl-3 gcc]$ cat /tmp/x.c
__attribute__ ((tls_model ("local-exec"))) __thread int i;

int *
foo (void)
{
  return &i;
}
[hjl@gnu-cfl-3 gcc]$  ./xgcc -B./ -fPIC -O2 /tmp/x.c -c
[hjl@gnu-cfl-3 gcc]$ ld -shared x.o
ld: x.o: relocation R_X86_64_TPOFF32 against symbol `i' can not be
used when making a shared object; recompile with -fPIC
ld: failed to set dynamic section sizes: bad value
[hjl@gnu-cfl-3 gcc]$

In this case, linker suggestion isn't very clear.  The issue is the wrong TLS
model, not PIC.

> Otherwise, let's say we are talking about
>
> extern __thread int tls_undef;
> static __thread int tls_local;
> /****/ __thread int tls_global;
>
> With -fpic GCC initially selects local-dynamic for tls_local, global-dynamic
> for tls_undef and tls_global. Why would recomputed model for tls_local be 
> weaker?
>
> > __thread int x;
> >
> > __attribute__((common))
> > __thread int x;
> >
> > int *f()
> > {
> >     return &x;
> > }
> > [hjl@gnu-zen4-1 gcc]$ ./xgcc -B./ -O2 -std=c11 -fno-common -S /tmp/x.c
> >  -fdump-ipa-whole-program
> > [hjl@gnu-zen4-1 gcc]$ cat x.c.084i.whole-program
> >
> > Marking local functions:
> >
> >
> > Marking externally visible functions: f/3
> >
> >
> > Marking externally visible variables: x/1
> >
> > Clearing variable flags:
> >
> > Reclaiming functions:
> > Reclaiming variables:
> > Clearing address taken flags:
> > Symbol table:
> >
> > f/3 (f)
> >   Type: function definition analyzed
> >   Visibility: externally_visible semantic_interposition public
> >   References: x/1 (addr)
> >   Referring:
> >   Availability: available
> >   Function flags: count:1073741824 (estimated locally) body
> >   Called by:
> >   Calls:
> > x/1 (x)
> >   Type: variable definition analyzed
> >   Visibility: externally_visible semantic_interposition public common
> >   References:
> >   Referring: f/3 (addr)
> >   Availability: overwritable
> >   Varpool flags: tls-local-exec
>
> Thanks. Unrelated to this particular patch, but this is questionable: we have
> local-exec for an interposable (Availability: overwritable) symbol. Perhaps

Definition in executable isn't interposable.

> it's better to error out on the redeclaration, when the previous declaration
> set a stronger TLS model?
>
> Alexander



-- 
H.J.

Reply via email to