x86: Properly check invalid relocation against protected symbol
authorH.J. Lu <hjl.tools@gmail.com>
Mon, 18 Jul 2022 18:44:32 +0000 (11:44 -0700)
committerH.J. Lu <hjl.tools@gmail.com>
Tue, 19 Jul 2022 01:15:39 +0000 (18:15 -0700)
Only check invalid relocation against protected symbol defined in shared
object.

bfd/

PR ld/29377
* elf32-i386.c (elf_i386_scan_relocs): Only check invalid
relocation against protected symbol defined in shared object.
* elf64-x86-64.c (elf_x86_64_scan_relocs): Likewise.

ld/

PR ld/29377
* testsuite/ld-elf/linux-x86.exp: Run PR ld/29377 tests.
* testsuite/ld-elf/pr29377a.c: New file.
* testsuite/ld-elf/pr29377b.c: Likewise.

bfd/elf32-i386.c
bfd/elf64-x86-64.c
ld/testsuite/ld-elf/linux-x86.exp
ld/testsuite/ld-elf/pr29377a.c [new file with mode: 0644]
ld/testsuite/ld-elf/pr29377b.c [new file with mode: 0644]

index e4106d9fd3b89fed18c9b9d1544c7db0af8f379d..3d39dc07fe0e1bb21e52764c1b597440022662b2 100644 (file)
@@ -1811,6 +1811,8 @@ elf_i386_scan_relocs (bfd *abfd,
                  if (h->pointer_equality_needed
                      && h->type == STT_FUNC
                      && eh->def_protected
+                     && !SYMBOL_DEFINED_NON_SHARED_P (h)
+                     && h->def_dynamic
                      && elf_has_indirect_extern_access (h->root.u.def.section->owner))
                    {
                      /* Disallow non-canonical reference to canonical
index 6154a70bdd7073e5ad8124bbdb9f813064d86ec8..48ca6309d1b0bf377675104c9728f50d2864e937 100644 (file)
@@ -2254,6 +2254,8 @@ elf_x86_64_scan_relocs (bfd *abfd, struct bfd_link_info *info,
                  if (h->pointer_equality_needed
                      && h->type == STT_FUNC
                      && eh->def_protected
+                     && !SYMBOL_DEFINED_NON_SHARED_P (h)
+                     && h->def_dynamic
                      && elf_has_indirect_extern_access (h->root.u.def.section->owner))
                    {
                      /* Disallow non-canonical reference to canonical
index 6035ecd68293bf58573cb2c1eff7476d9601337c..0f0a6f01ec083388fa0ff35c4674b86fe54c8ad0 100644 (file)
@@ -134,6 +134,22 @@ run_cc_link_tests [list \
        {{readelf {--dyn-syms -W} pr28158.rd} {nm -D pr28158.nd}} \
        "pr28158" \
     ] \
+    [list \
+       "Build pr29377.o" \
+       "-r -Wl,-z,noexecstack" \
+       "" \
+       {pr29377a.c indirect-extern-access.S} \
+       {} \
+       "pr29377.o" \
+    ] \
+    [list \
+       "Build pr29377" \
+       "$NOPIE_LDFLAGS tmpdir/pr29377.o" \
+       "-fPIC" \
+       {pr29377b.c} \
+       {} \
+       "pr29377" \
+    ] \
 ]
 
 run_ld_link_exec_tests [list \
diff --git a/ld/testsuite/ld-elf/pr29377a.c b/ld/testsuite/ld-elf/pr29377a.c
new file mode 100644 (file)
index 0000000..41d9545
--- /dev/null
@@ -0,0 +1,5 @@
+__attribute__ ((visibility ("protected")))
+void
+foo (void)
+{
+}
diff --git a/ld/testsuite/ld-elf/pr29377b.c b/ld/testsuite/ld-elf/pr29377b.c
new file mode 100644 (file)
index 0000000..3cf163b
--- /dev/null
@@ -0,0 +1,10 @@
+extern void foo (void);
+
+void (*foo_p) (void);
+
+int
+main ()
+{
+  foo_p = foo;
+  return 0;
+}