]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
Treat common symbol in executable as definition
authorH.J. Lu <hjl.tools@gmail.com>
Wed, 9 Mar 2016 13:59:20 +0000 (05:59 -0800)
committerH.J. Lu <hjl.tools@gmail.com>
Wed, 9 Mar 2016 13:59:31 +0000 (05:59 -0800)
Common symbol in executable is a definition, which overrides definition
from shared objects.  When linker sees a new definition from a shared
object, the new dynamic definition should be overridden by the previous
common symbol in executable.

Backport from master

bfd/

PR ld/19579
* elflink.c (_bfd_elf_merge_symbol): Group common symbol checking
together.

* elflink.c (_bfd_elf_merge_symbol): Treat common symbol in
executable as definition if the new definition comes from a
shared library.

ld/

PR ld/19579
* testsuite/ld-elf/pr19579a.c: New file.
* testsuite/ld-elf/pr19579b.c: Likewise.
* testsuite/ld-elf/shared.exp: Run PR ld/19579 test.

bfd/ChangeLog
bfd/elflink.c
ld/ChangeLog
ld/testsuite/ld-elf/pr19579a.c [new file with mode: 0644]
ld/testsuite/ld-elf/pr19579b.c [new file with mode: 0644]
ld/testsuite/ld-elf/shared.exp

index 0a08b5563840fb4555a90f07537f94f9b4e732ac..a8beeccbdbacc347283e03b9e2118ea2ab82e052 100644 (file)
@@ -1,3 +1,18 @@
+2016-03-09  H.J. Lu  <hongjiu.lu@intel.com>
+
+       PR ld/19579
+       Backport from master
+       2016-03-08  H.J. Lu  <hongjiu.lu@intel.com>
+
+       * elflink.c (_bfd_elf_merge_symbol): Group common symbol checking
+       together.
+
+       2016-03-04  H.J. Lu  <hongjiu.lu@intel.com>
+
+       * elflink.c (_bfd_elf_merge_symbol): Treat common symbol in
+       executable as definition if the new definition comes from a
+       shared library.
+
 2016-03-09  Nick Clifton  <nickc@redhat.com>
            Alan Modra  <amodra@gmail.com>
 
index 8fcaaddfd44ac9bf4fdf77b4d3b51855809fbbbe..842e85ba741703885e1be03a5dcf334887a58046 100644 (file)
@@ -1485,13 +1485,16 @@ _bfd_elf_merge_symbol (bfd *abfd,
      represent variables; this can cause confusion in principle, but
      any such confusion would seem to indicate an erroneous program or
      shared library.  We also permit a common symbol in a regular
-     object to override a weak symbol in a shared object.  */
+     object to override a weak symbol in a shared object.  A common
+     symbol in executable also overrides a symbol in a shared object.  */
 
   if (newdyn
       && newdef
       && (olddef
          || (h->root.type == bfd_link_hash_common
-             && (newweak || newfunc))))
+             && (newweak
+                 || newfunc
+                 || (!olddyn && bfd_link_executable (info))))))
     {
       *override = TRUE;
       newdef = FALSE;
index 1ee2dfa392d19c5f11d188c6d0ce77c1bb1e2239..2fa9ad37636d077f197a2a4173e86fdfbeb9a71d 100644 (file)
@@ -1,3 +1,13 @@
+2016-03-09  H.J. Lu  <hongjiu.lu@intel.com>
+
+       Backport from master
+       2016-03-04  H.J. Lu  <hongjiu.lu@intel.com>
+
+       PR ld/19579
+       * testsuite/ld-elf/pr19579a.c: New file.
+       * testsuite/ld-elf/pr19579b.c: Likewise.
+       * testsuite/ld-elf/shared.exp: Run PR ld/19579 test.
+
 2016-03-04  H.J. Lu  <hongjiu.lu@intel.com>
 
        Backport from master
diff --git a/ld/testsuite/ld-elf/pr19579a.c b/ld/testsuite/ld-elf/pr19579a.c
new file mode 100644 (file)
index 0000000..e4a6eb1
--- /dev/null
@@ -0,0 +1,15 @@
+#include <stdio.h>
+
+int foo[1];
+int bar[2];
+
+extern int *foo_p (void);
+extern int *bar_p (void);
+
+int
+main ()
+{
+  if (foo[0] == 0 && foo == foo_p () && bar[0] == 0 && bar == bar_p ())
+    printf ("PASS\n");
+  return 0;
+}
diff --git a/ld/testsuite/ld-elf/pr19579b.c b/ld/testsuite/ld-elf/pr19579b.c
new file mode 100644 (file)
index 0000000..d906545
--- /dev/null
@@ -0,0 +1,14 @@
+int foo[2];
+int bar[2] = { -1, -1 };
+
+int *
+foo_p (void)
+{
+  return foo;
+}
+
+int *
+bar_p (void)
+{
+  return bar;
+}
index 731eef30911156cf9c1f98354571d5e7422d5140..b8c12cb1f365dc23f0898941baefe110044ad5be 100644 (file)
@@ -524,6 +524,21 @@ if { [istarget *-*-linux*]
            {} \
            "libpr2404b.a" \
        ] \
+       [list \
+           "Build pr19579a.o" \
+           "" "-fPIE" \
+           {pr19579a.c} \
+           {} \
+           "libpr19579a.a" \
+       ] \
+       [list \
+           "Build libpr19579.so" \
+           "-shared" \
+           "-fPIC" \
+           {pr19579b.c} \
+           {} \
+           "libpr19579.so" \
+       ] \
     ]
     run_ld_link_exec_tests [] [list \
        [list \
@@ -580,5 +595,14 @@ if { [istarget *-*-linux*]
            "pass.out" \
            "-O2 -fPIC -I../bfd" \
        ] \
+       [list \
+           "Run pr19579" \
+           "-pie -z text tmpdir/pr19579a.o tmpdir/libpr19579.so" \
+           "" \
+           {dummy.c} \
+           "pr19579" \
+           "pass.out" \
+           "-fPIE" \
+       ] \
     ]
 }