x86: Make protected symbols local for -shared

Call _bfd_elf_symbol_refs_local_p with local_protected==true.  This has
2 noticeable effects for -shared:

* GOT-generating relocations referencing a protected data symbol no
  longer lead to a GLOB_DAT (similar to a hidden symbol).
* Direct access relocations (e.g. R_X86_64_PC32) no longer has the
  confusing diagnostic below.

    __attribute__((visibility("protected"))) void *foo() {
      return (void *)foo;
    }

    // gcc -fpic -shared -fuse-ld=bfd
    relocation R_X86_64_PC32 against protected symbol `foo' can not be used when making a shared object

The new behavior matches arm, aarch64 (commit
83c325007c5599fa9b60b8d5f7b84842160e1d1b), and powerpc ports, and other
linkers: gold and ld.lld.

Note: if some code tries to use direct access relocations to take the
address of foo, the pointer equality will break, but the error should be
reported on the executable link, not on the innocent shared object link.
glibc 2.36 will give a warning at relocation resolving time.

With this change, `#define elf_backend_extern_protected_data 1` is no
longer effective.  Just remove it.

Remove the test "Run protected-func-1 without PIE" since -fno-pic
address taken operation in the executable doesn't work with protected
symbol in a shared object by default.  Similarly, remove
protected-data-1a and protected-data-1b.  protected-data-1b can be made
working by removing HAVE_LD_PIE_COPYRELOC from GCC
(https://sourceware.org/pipermail/gcc-patches/2022-June/596678.html).
This commit is contained in:
Fangrui Song 2022-06-25 10:44:26 -07:00 committed by H.J. Lu
parent e9061058b4
commit d19a265487
13 changed files with 24 additions and 39 deletions

View File

@ -4426,7 +4426,6 @@ elf_i386_link_setup_gnu_properties (struct bfd_link_info *info)
#define elf_backend_got_header_size 12
#define elf_backend_plt_alignment 4
#define elf_backend_dtrel_excludes_plt 1
#define elf_backend_extern_protected_data 1
#define elf_backend_caches_rawsize 1
#define elf_backend_want_dynrelro 1

View File

@ -5277,7 +5277,6 @@ elf_x86_64_special_sections[]=
#define elf_backend_got_header_size (GOT_ENTRY_SIZE*3)
#define elf_backend_rela_normal 1
#define elf_backend_plt_alignment 4
#define elf_backend_extern_protected_data 1
#define elf_backend_caches_rawsize 1
#define elf_backend_dtrel_excludes_plt 1
#define elf_backend_want_dynrelro 1

View File

@ -3094,7 +3094,7 @@ _bfd_x86_elf_link_symbol_references_local (struct bfd_link_info *info,
2. When building executable, there is no dynamic linker. Or
3. or "-z nodynamic-undefined-weak" is used.
*/
if (SYMBOL_REFERENCES_LOCAL (info, h)
if (_bfd_elf_symbol_refs_local_p (h, info, 1)
|| (h->root.type == bfd_link_hash_undefweak
&& (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
|| (bfd_link_executable (info)

View File

@ -1,3 +1,5 @@
#as: --32
#ld: -shared -melf_i386
#error: .*relocation R_386_GOTOFF against protected function `foo' can not be used when making a shared object
#readelf: -rW
#...
There are no relocations in this file.

View File

@ -8,7 +8,7 @@
Disassembly of section .text:
0+[a-f0-9]+ <bar>:
[ ]*[a-f0-9]+: 8b 81 [a-f0-9][a-f0-9] [a-f0-9][a-f0-9] ff ff mov -0x[a-f0-9]+\(%ecx\),%eax
[ ]*[a-f0-9]+: 8d 81 00 00 00 00 lea 0x0\(%ecx\),%eax
[ ]*[a-f0-9]+: 8b 00 mov \(%eax\),%eax
[ ]*[a-f0-9]+: c3 ret
#pass

View File

@ -1,4 +1,6 @@
#source: protected6.s
#as: --32
#ld: -shared -melf_i386
#error: .*relocation R_386_GOTOFF against protected data `foo' can not be used when making a shared object
#readelf: -rW
#...
There are no relocations in this file.

View File

@ -1,4 +1,6 @@
#source: pr24151a.s
#as: --x32
#ld: -shared -melf32_x86_64
#error: .*relocation R_X86_64_PC32 against protected symbol `foo' can not be used when making a shared object
#readelf: -rW
#...
There are no relocations in this file.

View File

@ -1,3 +1,5 @@
#as: --64
#ld: -shared -melf_x86_64
#error: .*relocation R_X86_64_PC32 against protected symbol `foo' can not be used when making a shared object
#readelf: -rW
#...
There are no relocations in this file.

View File

@ -1,3 +1,5 @@
#as: --64
#ld: -shared -melf_x86_64
#error: .*relocation R_X86_64_PC32 against protected symbol `foo' can not be used when making a shared object
#readelf: -rW
#...
There are no relocations in this file.

View File

@ -8,7 +8,7 @@
Disassembly of section .text:
0+[a-f0-9]+ <bar>:
[ ]*[a-f0-9]+: 48 8b 05 ([0-9a-f]{2} ){4} * mov 0x[a-f0-9]+\(%rip\),%rax # [a-f0-9]+ <.*>
[ ]*[a-f0-9]+: 48 8d 05 ([0-9a-f]{2} ){4} * lea 0x[a-f0-9]+\(%rip\),%rax # [a-f0-9]+ <.*>
[ ]*[a-f0-9]+: 8b 00 mov \(%rax\),%eax
[ ]*[a-f0-9]+: c3 ret
#pass

View File

@ -1,4 +1,6 @@
#source: protected6.s
#as: --64
#ld: -shared -melf_x86_64
#error: .*relocation R_X86_64_GOTOFF64 against protected data `foo' can not be used when making a shared object
#readelf: -rW
#...
There are no relocations in this file.

View File

@ -1,4 +1,6 @@
#source: protected7.s
#as: --64
#ld: -shared -melf_x86_64
#error: .*relocation R_X86_64_GOTOFF64 against protected function `foo' can not be used when making a shared object
#readelf: -rW
#...
There are no relocations in this file.

View File

@ -1832,15 +1832,6 @@ if { [isnative] && [check_compiler_available] } {
"pr23997" \
"pass.out" \
] \
[list \
"Run protected-func-1 without PIE" \
"$NOPIE_LDFLAGS -Wl,--no-as-needed tmpdir/libprotected-func-1.so" \
"-Wa,-mx86-used-note=yes" \
{ protected-func-1b.c } \
"protected-func-1a" \
"pass.out" \
"$NOPIE_CFLAGS" \
] \
[list \
"Run protected-func-1 with PIE" \
"-Wl,--no-as-needed -pie tmpdir/libprotected-func-1.so" \
@ -1904,24 +1895,6 @@ if { [isnative] && [check_compiler_available] } {
"pass.out" \
"-fPIE" \
] \
[list \
"Run protected-data-1a without PIE" \
"$NOPIE_LDFLAGS -Wl,--no-as-needed tmpdir/libprotected-data-1a.so" \
"-Wa,-mx86-used-note=yes" \
{ protected-data-1b.c } \
"protected-data-1a" \
"pass.out" \
"$NOPIE_CFLAGS" \
] \
[list \
"Run protected-data-1b with PIE" \
"-Wl,--no-as-needed -pie tmpdir/libprotected-data-1a.so" \
"-Wa,-mx86-used-note=yes" \
{ protected-data-1b.c } \
"protected-data-1b" \
"pass.out" \
"-fPIE" \
] \
[list \
"Run protected-data-2a without PIE" \
"$NOPIE_LDFLAGS -Wl,--no-as-needed tmpdir/libprotected-data-2a.so" \