[PATCH 3/5] LoongArch: Make protected function symbols local for -shared

Xi Ruoyao xry111@xry111.site
Tue Jun 25 10:44:02 GMT 2024


On Tue, 2024-06-25 at 14:38 +0800, mengqinggang wrote:
> > +   $ cat t.c
> > +   #include <assert.h>
> > +   void check(void *p) {assert(p == check);}
> > +   $ cat main.c
> > +   extern void check(void *);
> > +   int main(void) { check(check); }
> > +   $ cc t.c -fPIC -shared -o t.so
> > +   $ cc main.c -mdirect-extern-access t.so -Wl,-rpath=.
> > +   $ ./a.out
> > +   a.out: t.c:2: check: Assertion `p == check' failed.
> > +   Aborted
> 
> 
> I have some questions about this example.
> 
> This example fail in PIE and success in no-PIE whether has this patch or 
> not.

The example just shows the difference between LoongArch and
"traditional" platforms (with copy relocation).  It's not a test case
that we should try to "fix."

With copy relocation, those traditional platforms use PCREL instead of
GOT to refer external symbols from a main executable, thus the example
is supposed to work on these platforms.

But on LoongArch there's no copy relocation, and a dynamically-linked
main executable should always use GOT to refer an external symbol
(unless it's going to call an external function).  Thus the example is
not supposed to work, at all.

So while this change is controversial for other ports (it's just
"controversial", not "impossible": ld.gold is already treating
STV_PROTECTED functions non-preemptible anyway, and GCC has stopped to
use PCREL for referring external functions on at least x86 and AArch64),
on LoongArch it should be perfectly fine: it'll only break already
broken code.

Note that on LoongArch -mdirect-extern-access is not the default and
it's clearly documented as "don't use unless you know there's no dynamic
linking."  In this case it's used with dynamic linking, thus the
produced assembly is just broken code.

-- 
Xi Ruoyao <xry111@xry111.site>
School of Aerospace Science and Technology, Xidian University


More information about the Binutils mailing list