[PATCH] Allow direct access relocations referencing a protected function symbol

Alan Modra amodra@gmail.com
Thu Jun 17 02:59:02 GMT 2021


On Wed, Jun 16, 2021 at 01:11:43AM -0700, Fangrui Song wrote:
> While (I think Cary/Michael) and I prefer that STV_PROTECTED STT_FUNC symbols don't have unneeded dynamic relocations in -shared mode,

I strongly prefer that too.  The only thing I object to is incorrect
fixes..

> I don't mind if GNU ld keeps producing them.
> Anyhow the important thing is the following spurious error:
> 
>   % gcc -fpic -shared -fuse-ld=bfd a.s
>   /usr/bin/ld.bfd: /tmp/ccWPJCLw.o: relocation R_X86_64_PC32 against protected symbol `foo' can not be used when making a shared object
>   __attribute__((visibility("protected"))) void *foo () {
>     return (void *)foo;
>   }

Agreed, that is an x86 linker bug.

Here is the result of your testcase on powerpc64le, power10 to get
pc-relative cpu support, and linked without startup files so just the
relocs in this code are seen.

powerpc64le-linux-gcc -c -O2 -fPIC -mcpu=power10 prot2.c
powerpc64le-linux-ld -shared -o prot2 prot2.o
readelf -r prot2
There are no relocations in this file.

> 
> If you decide to take over, fixing the error will be really great and will be an important step fixing protected symbols.

It would be inappropriate for me to take over.  This is a target
issue, caused by a deliberate choice on the part of HJ to optimise in
the executable at the expense of shared library optimisation (*).  The
fix needs to be in the x86 target code, and it's a very long time
since I was an x86 binutils maintainer.

*) I'll note that at the time, when non-pic executables were more
common, the trick HJ used made quite a lot of sense from an
optimisation viewpoint, but frankly I'm amazed that he managed to get
it all working.  In fact, the optimisation makes even more sense on
powerpc prior to power10 but I was unwilling to go against the ELF
gABI (when narrowly considering the resulting shared libraries in
isolation) just to shave off cycles in micro-benchmarks.

-- 
Alan Modra
Australia Development Lab, IBM


More information about the Binutils mailing list