Mark all weak aliases for copy relocations
authorAlan Modra <amodra@gmail.com>
Sun, 26 Jan 2020 23:57:42 +0000 (10:27 +1030)
committerAlan Modra <amodra@gmail.com>
Mon, 27 Jan 2020 00:23:00 +0000 (10:53 +1030)
bfd/
PR ld/25458
* elflink.c (_bfd_elf_gc_mark_rsec): Mark all weak aliases.
ld/
PR ld/25458
* testsuite/ld-elf/pr25458.map: New file.
* testsuite/ld-elf/pr25458.rd: Likewise.
* testsuite/ld-elf/pr25458a.s: Likewise.
* testsuite/ld-elf/pr25458b.s: Likewise.
* testsuite/ld-elf/shared.exp: Run PR ld/25458 test.

bfd/ChangeLog
bfd/elflink.c
ld/ChangeLog
ld/testsuite/ld-elf/pr25458.map [new file with mode: 0644]
ld/testsuite/ld-elf/pr25458.rd [new file with mode: 0644]
ld/testsuite/ld-elf/pr25458a.s [new file with mode: 0644]
ld/testsuite/ld-elf/pr25458b.s [new file with mode: 0644]
ld/testsuite/ld-elf/shared.exp

index 558e11eda344d86dcfa12c46bc9e893b4c30033c..3deba430f13ebaf966376f52ddb299080478f56a 100644 (file)
@@ -1,3 +1,9 @@
+2020-01-27  H.J. Lu  <hongjiu.lu@intel.com>
+           Alan Modra  <amodra@gmail.com>
+
+       PR ld/25458
+       * elflink.c (_bfd_elf_gc_mark_rsec): Mark all weak aliases.
+
 2020-01-24  Jim Wilson  <jimw@sifive.com>
 
        * elfxx-riscv.c (riscv_get_prefix_class): Format s case like others.
index 300be3f7437fdd8dc84709521789edb109d759af..5217528a79b2c61a33fecc0d32e07375f33a359a 100644 (file)
@@ -13123,7 +13123,7 @@ _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
                       bfd_boolean *start_stop)
 {
   unsigned long r_symndx;
-  struct elf_link_hash_entry *h;
+  struct elf_link_hash_entry *h, *hw;
 
   r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
   if (r_symndx == STN_UNDEF)
@@ -13143,12 +13143,16 @@ _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
             || h->root.type == bfd_link_hash_warning)
        h = (struct elf_link_hash_entry *) h->root.u.i.link;
       h->mark = 1;
-      /* If this symbol is weak and there is a non-weak definition, we
-        keep the non-weak definition because many backends put
-        dynamic reloc info on the non-weak definition for code
-        handling copy relocs.  */
-      if (h->is_weakalias)
-       weakdef (h)->mark = 1;
+      /* Keep all aliases of the symbol too.  If an object symbol
+        needs to be copied into .dynbss then all of its aliases
+        should be present as dynamic symbols, not just the one used
+        on the copy relocation.  */
+      hw = h;
+      while (hw->is_weakalias)
+       {
+         hw = hw->u.alias;
+         hw->mark = 1;
+       }
 
       if (start_stop != NULL)
        {
index f71c13564d9e58fd7079b64007d50445fb378b1b..b39c8c09b212e048112055d04346e35111e04117 100644 (file)
@@ -1,3 +1,13 @@
+2020-01-27  H.J. Lu  <hongjiu.lu@intel.com>
+           Alan Modra  <amodra@gmail.com>
+
+       PR ld/25458
+       * testsuite/ld-elf/pr25458.map: New file.
+       * testsuite/ld-elf/pr25458.rd: Likewise.
+       * testsuite/ld-elf/pr25458a.s: Likewise.
+       * testsuite/ld-elf/pr25458b.s: Likewise.
+       * testsuite/ld-elf/shared.exp: Run PR ld/25458 test.
+
 2020-01-22  H.J. Lu  <hongjiu.lu@intel.com>
 
        PR ld/25416
diff --git a/ld/testsuite/ld-elf/pr25458.map b/ld/testsuite/ld-elf/pr25458.map
new file mode 100644 (file)
index 0000000..5578d1f
--- /dev/null
@@ -0,0 +1,4 @@
+FOO {
+global:
+  __environ; _environ; environ;
+};
diff --git a/ld/testsuite/ld-elf/pr25458.rd b/ld/testsuite/ld-elf/pr25458.rd
new file mode 100644 (file)
index 0000000..d0fc6b9
--- /dev/null
@@ -0,0 +1,10 @@
+#...
+Symbol table '\.dynsym' contains [0-9]+ entries:
+ +Num: +Value +Size Type +Bind +Vis +Ndx Name
+#...
+ +[0-9]+: [0-9a-f]+ +(4|8)+ OBJECT +(WEAK|GLOBAL) +DEFAULT +[0-9]+ _*environ@FOO \(2\)
+#...
+ +[0-9]+: [0-9a-f]+ +(4|8)+ OBJECT +(WEAK|GLOBAL) +DEFAULT +[0-9]+ _*environ@FOO \(2\)
+#...
+ +[0-9]+: [0-9a-f]+ +(4|8)+ OBJECT +(WEAK|GLOBAL) +DEFAULT +[0-9]+ _*environ@FOO \(2\)
+#pass
diff --git a/ld/testsuite/ld-elf/pr25458a.s b/ld/testsuite/ld-elf/pr25458a.s
new file mode 100644 (file)
index 0000000..59e6af2
--- /dev/null
@@ -0,0 +1,6 @@
+       .text
+       .globl  _start
+       .type   _start, %function
+_start:
+       .dc.a   environ
+       .size   _start, .-_start
diff --git a/ld/testsuite/ld-elf/pr25458b.s b/ld/testsuite/ld-elf/pr25458b.s
new file mode 100644 (file)
index 0000000..ff64cc7
--- /dev/null
@@ -0,0 +1,11 @@
+       .data
+       .globl __environ
+       .type __environ,%object
+__environ:
+       .dc.a   0
+       .size   __environ, .-__environ
+       .weak _environ
+       .globl _environ
+       .set _environ, __environ
+       .weak environ
+       .set environ, __environ
index e03906a142d3153336e03949ae291c4bc8c5155c..b1762aff9be16b94503676e4bdc5bc2a20199ce4 100644 (file)
@@ -337,6 +337,38 @@ if { [check_gc_sections_available] } {
            "pr22150" \
        ] \
     ]
+
+    switch -glob $target_triplet {
+       # exclude targets that don't support copy relocs
+       bfin-*-* { }
+       frv-*-* { }
+       lm32-*-* { }
+       mips*-*-* { }
+       tic6x-*-* { }
+       xtensa-*-* { }
+       default {
+           run_ld_link_tests [list \
+               [list \
+                    "Build pr25458.so" \
+                    "$LFLAGS -shared --version-script=pr25458.map" \
+                    "" \
+                    "$AFLAGS_PIC" \
+                    {pr25458b.s} \
+                    {} \
+                    "pr25458.so" \
+               ] \
+               [list \
+                    "Build pr25458" \
+                    "$LFLAGS -e _start --gc-sections" \
+                    "tmpdir/pr25458.so" \
+                    "$AFLAGS_PIC" \
+                    {pr25458a.s} \
+                    {{readelf {--dyn-sym --wide} pr25458.rd}} \
+                    "pr25458" \
+               ] \
+           ]
+       }
+    }
 }
 
 set ASFLAGS $old_ASFLAGS