bug-binutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Bug ld/26815] Unnecessary error on linking STV_PROTECTED library: reloc


From: thiago at kde dot org
Subject: [Bug ld/26815] Unnecessary error on linking STV_PROTECTED library: relocation R_X86_64_PC32 against protected symbol `f' can not be used when making a shared object
Date: Fri, 30 Oct 2020 03:43:16 +0000

https://sourceware.org/bugzilla/show_bug.cgi?id=26815

--- Comment #6 from Thiago Macieira <thiago at kde dot org> ---
A bit more testing shows that we'd need a compiler update too, to tell the
compiler that a variable should be accessed via the GOT regardless (equiv. to
Windows __declspec(dllimport)). We can accomplish that by always compiling
everything with -fPIC, though.

lib.c:
long variable = 0;
void *addr()
{
    ++variable;
    return &variable;
}

main.c:
#include <stdio.h>
void *addr(void);

extern long variable;
__auto_type ptr2 = &variable;
int main()
{
    variable++;
    printf("addr() = %p, &variable = %p\n", addr(), ptr2);
}

$ gcc -O2 -shared -fPIC  -fvisibility=protected -fuse-ld=gold -o lib.so lib.c

Now the error comes from Gold:

$ gcc -O2 -pie -fPIE -fuse-ld=gold main.c ./lib.so 
/usr/bin/ld.gold: error: /tmp/ccUGJmAG.o: cannot make copy relocation for
protected symbol 'variable', defined in ./lib.so
collect2: error: ld returned 1 exit status

ld.bfd can link it:
$ gcc -O2 -pie -fPIE main.c ./lib.so               

When run, this particular example appears to work but the variable was actually
copy-relocated:
$ ./a.out 
addr() = 0x555555558050, &variable = 0x555555558050

When linking the library with ld.bfd instead:
# gcc -O2 -shared -fPIC  -fvisibility=protected
-Wl,--dynamic-list,empty.dynlist -o lib.so lib.c

Then we get a mismatch:
$ ./a.out                                                                       
addr() = 0x7ffa8cabc028, &variable = 0x556094d36050

All these problems go away when the main application is compiled with -fPIC.

-- 
You are receiving this mail because:
You are on the CC list for the bug.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]