Preserve the maximum alignment/size for common symbols.
authorH.J. Lu <hjl.tools@gmail.com>
Sat, 8 Oct 2011 16:51:11 +0000 (16:51 +0000)
committerH.J. Lu <hjl.tools@gmail.com>
Sat, 8 Oct 2011 16:51:11 +0000 (16:51 +0000)
bfd/

2011-10-08  H.J. Lu  <hongjiu.lu@intel.com>

PR ld/13250
* elflink.c (elf_link_add_object_symbols): Preserve the maximum
alignment and size for common symbols.

ld/testsuite/

2011-10-08  H.J. Lu  <hongjiu.lu@intel.com>

PR ld/13250
* ld-elf/shared.exp (build_tests): Add tests for PR ld/13250.
(run_tests): Likewise.

* ld-elf/pr13250-1.c: New.
* ld-elf/pr13250-2.c: Likewise.
* ld-elf/pr13250-3.c: Likewise.

bfd/ChangeLog
bfd/elflink.c
ld/testsuite/ChangeLog
ld/testsuite/ld-elf/pr13250-1.c [new file with mode: 0644]
ld/testsuite/ld-elf/pr13250-2.c [new file with mode: 0644]
ld/testsuite/ld-elf/pr13250-3.c [new file with mode: 0644]
ld/testsuite/ld-elf/shared.exp

index 8f99755a4ca0eb9c85013bb9867954bfb5ab8d08..05372bf09b3c50d6eeee61cac4d2c084730df0b5 100644 (file)
@@ -1,3 +1,9 @@
+2011-10-08  H.J. Lu  <hongjiu.lu@intel.com>
+
+       PR ld/13250
+       * elflink.c (elf_link_add_object_symbols): Preserve the maximum
+       alignment and size for common symbols.
+
 2011-10-08  Alan Modra  <amodra@gmail.com>
 
        PR ld/13229
index 358ada8c28b06b4f2e589505f5ea112705a58c97..18aefdb569523e16c40b02bce69b92fbb86da77e 100644 (file)
@@ -4515,6 +4515,8 @@ error_free_dyn:
        {
          struct bfd_hash_entry *p;
          struct elf_link_hash_entry *h;
+         bfd_size_type size;
+         unsigned int alignment_power;
 
          for (p = htab->root.table.table[i]; p != NULL; p = p->next)
            {
@@ -4524,6 +4526,20 @@ error_free_dyn:
              if (h->dynindx >= old_dynsymcount)
                _bfd_elf_strtab_delref (htab->dynstr, h->dynstr_index);
 
+             /* Preserve the maximum alignment and size for common
+                symbols even if this dynamic lib isn't on DT_NEEDED
+                since it can still be loaded at the run-time by another
+                dynamic lib.  */
+             if (h->root.type == bfd_link_hash_common)
+               {
+                 size = h->root.u.c.size;
+                 alignment_power = h->root.u.c.p->alignment_power;
+               }
+             else
+               {
+                 size = 0;
+                 alignment_power = 0;
+               }
              memcpy (p, old_ent, htab->root.table.entsize);
              old_ent = (char *) old_ent + htab->root.table.entsize;
              h = (struct elf_link_hash_entry *) p;
@@ -4532,6 +4548,13 @@ error_free_dyn:
                  memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
                  old_ent = (char *) old_ent + htab->root.table.entsize;
                }
+             else if (h->root.type == bfd_link_hash_common)
+               {
+                 if (size > h->root.u.c.size)
+                   h->root.u.c.size = size;
+                 if (alignment_power > h->root.u.c.p->alignment_power)
+                   h->root.u.c.p->alignment_power = alignment_power;
+               }
            }
        }
 
index 592e7e3d432a042539176cb65f4c3bae74788624..29ae5aea533f8a5c1bdf88d86439869b41cd69b0 100644 (file)
@@ -1,3 +1,13 @@
+2011-10-08  H.J. Lu  <hongjiu.lu@intel.com>
+
+       PR ld/13250
+       * ld-elf/shared.exp (build_tests): Add tests for PR ld/13250.
+       (run_tests): Likewise.
+
+       * ld-elf/pr13250-1.c: New.
+       * ld-elf/pr13250-2.c: Likewise.
+       * ld-elf/pr13250-3.c: Likewise.
+
 2011-10-07  H.J. Lu  <hongjiu.lu@intel.com>
 
        * ld-elf/elf.exp: Run --gc-sections tests only if --gc-sections
diff --git a/ld/testsuite/ld-elf/pr13250-1.c b/ld/testsuite/ld-elf/pr13250-1.c
new file mode 100644 (file)
index 0000000..e43ebe0
--- /dev/null
@@ -0,0 +1,8 @@
+int common1[8];
+void
+foo ()
+{
+  int i;
+  for (i = 0; i < sizeof (common1)/ sizeof (common1[0]); i++)
+    common1[i] = -1;
+}
diff --git a/ld/testsuite/ld-elf/pr13250-2.c b/ld/testsuite/ld-elf/pr13250-2.c
new file mode 100644 (file)
index 0000000..af8268b
--- /dev/null
@@ -0,0 +1,10 @@
+extern int common1[8];
+
+extern void foo ();
+
+int
+bar ()
+{
+  foo ();
+  return common1[4];
+}
diff --git a/ld/testsuite/ld-elf/pr13250-3.c b/ld/testsuite/ld-elf/pr13250-3.c
new file mode 100644 (file)
index 0000000..a227670
--- /dev/null
@@ -0,0 +1,22 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+int common1[1];
+char common2[2];
+
+extern int bar ();
+
+int
+main ()
+{
+  int i;
+  if (bar () != -1)
+    abort ();
+  if (common1[0] != -1)
+    abort ();
+  for (i = 0; i < sizeof (common2)/ sizeof (common2[0]); i++)
+    if (common2[i] != 0)
+      abort ();
+  printf ("PASS\n");
+  return 0;
+}
index eea9c7e931061c12b78ca4b1cf7d1bd64e7259cc..9cd0221ccb9e10de47b982f8876d52457459b378 100644 (file)
@@ -157,6 +157,15 @@ set build_tests {
   {"Build libpr11138-2.o"
    "-r -nostdlib" ""
    {pr11138-2.c} {} "libpr11138-2.o"}
+  {"Build pr13250-1.so"
+   "-shared" "-fPIC"
+   {pr13250-1.c} {} "libpr13250-1.so"}
+  {"Build pr13250-2.so with libpr13250-1.so"
+   "-shared tmpdir/libpr13250-1.so" "-fPIC"
+   {pr13250-2.c} {} "libpr13250-2.so"}
+  {"Build libpr13250-3.o"
+   "-r -nostdlib" ""
+   {pr13250-3.c} {} "libpr13250-3.o"}
 }
 
 run_cc_link_tests $build_tests
@@ -283,6 +292,9 @@ set run_tests {
     {"Run with libpr11138-1.so pr11138-2.c"
      "--version-script=pr11138-2.map tmpdir/libpr11138-1.so tmpdir/pr11138-2.o" ""
      {dummy.c} "pr11138b" "pr11138.out"}
+    {"Run with pr13250-3.c, libpr13250-1.so and libpr13250-2.so"
+     "--as-needed tmpdir/pr13250-3.o tmpdir/libpr13250-1.so tmpdir/libpr13250-2.so" ""
+     {dummy.c} "pr13250" "pass.out"}
 }
 
 # NetBSD ELF systems do not currently support the .*_array sections.