Handle STT_GNU_IFUNC symols when building shared library.
authorH.J. Lu <hjl.tools@gmail.com>
Thu, 6 Jan 2011 18:45:05 +0000 (18:45 +0000)
committerH.J. Lu <hjl.tools@gmail.com>
Thu, 6 Jan 2011 18:45:05 +0000 (18:45 +0000)
bfd/

2012-01-06  H.J. Lu  <hongjiu.lu@intel.com>

PR ld/12366
PR ld/12371
* elf-ifunc.c (_bfd_elf_allocate_ifunc_dyn_relocs): Properly
handle symbols marked with regular reference, but not non-GOT
reference when building shared library.

* elf32-i386.c (elf_i386_gc_sweep_hook): Properly handle
local and global STT_GNU_IFUNC symols when building shared
library.
* elf64-x86-64.c (elf_x86_64_gc_sweep_hook): Likewise.

ld/testsuite/

2012-01-06  H.J. Lu  <hongjiu.lu@intel.com>

PR ld/12366
PR ld/12371
* ld-ifunc/ifunc-10-i386.s: Add more tests.
* ld-ifunc/ifunc-10-x86-64.s: Likewise.
* ld-ifunc/ifunc-11-i386.s: Likewise.
* ld-ifunc/ifunc-11-x86-64.s: Likewise.

* ld-ifunc/ifunc-12-i386.d: New.
* ld-ifunc/ifunc-12-i386.s: Likewise.
* ld-ifunc/ifunc-12-x86-64.d: Likewise.
* ld-ifunc/ifunc-12-x86-64.s: Likewise.
* ld-ifunc/ifunc-13-i386.d: Likewise.
* ld-ifunc/ifunc-13-x86-64.d: Likewise.
* ld-ifunc/ifunc-13a-i386.s: Likewise.
* ld-ifunc/ifunc-13a-x86-64.s: Likewise.
* ld-ifunc/ifunc-13b-i386.s: Likewise.
* ld-ifunc/ifunc-13b-x86-64.s: Likewise.

19 files changed:
bfd/ChangeLog
bfd/elf-ifunc.c
bfd/elf32-i386.c
bfd/elf64-x86-64.c
ld/testsuite/ChangeLog
ld/testsuite/ld-ifunc/ifunc-10-i386.s
ld/testsuite/ld-ifunc/ifunc-10-x86-64.s
ld/testsuite/ld-ifunc/ifunc-11-i386.s
ld/testsuite/ld-ifunc/ifunc-11-x86-64.s
ld/testsuite/ld-ifunc/ifunc-12-i386.d [new file with mode: 0644]
ld/testsuite/ld-ifunc/ifunc-12-i386.s [new file with mode: 0644]
ld/testsuite/ld-ifunc/ifunc-12-x86-64.d [new file with mode: 0644]
ld/testsuite/ld-ifunc/ifunc-12-x86-64.s [new file with mode: 0644]
ld/testsuite/ld-ifunc/ifunc-13-i386.d [new file with mode: 0644]
ld/testsuite/ld-ifunc/ifunc-13-x86-64.d [new file with mode: 0644]
ld/testsuite/ld-ifunc/ifunc-13a-i386.s [new file with mode: 0644]
ld/testsuite/ld-ifunc/ifunc-13a-x86-64.s [new file with mode: 0644]
ld/testsuite/ld-ifunc/ifunc-13b-i386.s [new file with mode: 0644]
ld/testsuite/ld-ifunc/ifunc-13b-x86-64.s [new file with mode: 0644]

index eb99588945571031255b12a81b95f7ab0d8332b2..e11d9876e8a0129070b3a800245a9c73f3882680 100644 (file)
@@ -1,3 +1,16 @@
+2012-01-06  H.J. Lu  <hongjiu.lu@intel.com>
+
+       PR ld/12366
+       PR ld/12371
+       * elf-ifunc.c (_bfd_elf_allocate_ifunc_dyn_relocs): Properly
+       handle symbols marked with regular reference, but not non-GOT
+       reference when building shared library.
+
+       * elf32-i386.c (elf_i386_gc_sweep_hook): Properly handle
+       local and global STT_GNU_IFUNC symols when building shared
+       library.
+       * elf64-x86-64.c (elf_x86_64_gc_sweep_hook): Likewise.
+
 2011-01-05  DJ Delorie  <dj@redhat.com>
 
        * reloc.c: Add BFD_RELOC_RX_OP_NEG.
index 760fc26c1a158b7cb7aebc67a02db933ccaedd22..17b23c2ffc0e7688d1cbde0bd705c276f7cb08e6 100644 (file)
@@ -190,10 +190,29 @@ _bfd_elf_allocate_ifunc_dyn_relocs (struct bfd_link_info *info,
   /* Support garbage collection against STT_GNU_IFUNC symbols.  */
   if (h->plt.refcount <= 0 && h->got.refcount <= 0)
     {
-      h->got = htab->init_got_offset;
-      h->plt = htab->init_plt_offset;
-      *head = NULL;
-      return TRUE;
+      /* When building shared library, we need to handle the case
+         where it is marked with regular reference, but not non-GOT
+        reference.  It may happen if we didn't see STT_GNU_IFUNC
+        symbol at the time when checking relocations.  */
+      bfd_size_type count = 0;
+
+      if (info->shared
+         && !h->non_got_ref
+         && h->ref_regular)
+       {
+         for (p = *head; p != NULL; p = p->next)
+           count += p->count;
+         if (count != 0)
+           h->non_got_ref = 1;
+       }
+
+      if (count == 0)
+       {
+         h->got = htab->init_got_offset;
+         h->plt = htab->init_plt_offset;
+         *head = NULL;
+         return TRUE;
+       }
     }
 
   /* Return and discard space for dynamic relocations against it if
index ae749c6b4e87cfdb89a6480b33cb2ffd41a7ee7e..e9b3933cbd6caaad61ca0369169a77b45290e31f 100644 (file)
@@ -1807,23 +1807,10 @@ elf_i386_gc_sweep_hook (bfd *abfd,
       r_symndx = ELF32_R_SYM (rel->r_info);
       if (r_symndx >= symtab_hdr->sh_info)
        {
-         struct elf_i386_link_hash_entry *eh;
-         struct elf_dyn_relocs **pp;
-         struct elf_dyn_relocs *p;
-
          h = sym_hashes[r_symndx - symtab_hdr->sh_info];
          while (h->root.type == bfd_link_hash_indirect
                 || h->root.type == bfd_link_hash_warning)
            h = (struct elf_link_hash_entry *) h->root.u.i.link;
-         eh = (struct elf_i386_link_hash_entry *) h;
-
-         for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
-           if (p->sec == sec)
-             {
-               /* Everything must go for SEC.  */
-               *pp = p->next;
-               break;
-             }
        }
       else
        {
@@ -1843,6 +1830,22 @@ elf_i386_gc_sweep_hook (bfd *abfd,
            }
        }
 
+      if (h)
+       {
+         struct elf_i386_link_hash_entry *eh;
+         struct elf_dyn_relocs **pp;
+         struct elf_dyn_relocs *p;
+
+         eh = (struct elf_i386_link_hash_entry *) h;
+         for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
+           if (p->sec == sec)
+             {
+               /* Everything must go for SEC.  */
+               *pp = p->next;
+               break;
+             }
+       }
+
       r_type = ELF32_R_TYPE (rel->r_info);
       if (! elf_i386_tls_transition (info, abfd, sec, NULL,
                                     symtab_hdr, sym_hashes,
@@ -1883,7 +1886,8 @@ elf_i386_gc_sweep_hook (bfd *abfd,
 
        case R_386_32:
        case R_386_PC32:
-         if (info->shared)
+         if (info->shared
+             && (h == NULL || h->type != STT_GNU_IFUNC))
            break;
          /* Fall through */
 
index 3dd16ba123a90c7184d3fd1729953efa880f5c67..ceb1a0a19a3949827396ea00a62ce83e610328f1 100644 (file)
@@ -1684,23 +1684,10 @@ elf_x86_64_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
       r_symndx = htab->r_sym (rel->r_info);
       if (r_symndx >= symtab_hdr->sh_info)
        {
-         struct elf_x86_64_link_hash_entry *eh;
-         struct elf_dyn_relocs **pp;
-         struct elf_dyn_relocs *p;
-
          h = sym_hashes[r_symndx - symtab_hdr->sh_info];
          while (h->root.type == bfd_link_hash_indirect
                 || h->root.type == bfd_link_hash_warning)
            h = (struct elf_link_hash_entry *) h->root.u.i.link;
-         eh = (struct elf_x86_64_link_hash_entry *) h;
-
-         for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
-           if (p->sec == sec)
-             {
-               /* Everything must go for SEC.  */
-               *pp = p->next;
-               break;
-             }
        }
       else
        {
@@ -1720,6 +1707,23 @@ elf_x86_64_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
            }
        }
 
+      if (h)
+       {
+         struct elf_x86_64_link_hash_entry *eh;
+         struct elf_dyn_relocs **pp;
+         struct elf_dyn_relocs *p;
+
+         eh = (struct elf_x86_64_link_hash_entry *) h;
+
+         for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
+           if (p->sec == sec)
+             {
+               /* Everything must go for SEC.  */
+               *pp = p->next;
+               break;
+             }
+       }
+
       r_type = ELF32_R_TYPE (rel->r_info);
       if (! elf_x86_64_tls_transition (info, abfd, sec, NULL,
                                       symtab_hdr, sym_hashes,
@@ -1771,7 +1775,8 @@ elf_x86_64_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
        case R_X86_64_PC16:
        case R_X86_64_PC32:
        case R_X86_64_PC64:
-         if (info->shared)
+         if (info->shared
+             && (h == NULL || h->type != STT_GNU_IFUNC))
            break;
          /* Fall thru */
 
index 0b2308280dce13b08cd611d2a93c452a455721a3..c379f99d94bd9f0eb6fedcc40501d45f000af72d 100644 (file)
@@ -1,3 +1,23 @@
+2012-01-06  H.J. Lu  <hongjiu.lu@intel.com>
+
+       PR ld/12366
+       PR ld/12371
+       * ld-ifunc/ifunc-10-i386.s: Add more tests.
+       * ld-ifunc/ifunc-10-x86-64.s: Likewise.
+       * ld-ifunc/ifunc-11-i386.s: Likewise.
+       * ld-ifunc/ifunc-11-x86-64.s: Likewise.
+
+       * ld-ifunc/ifunc-12-i386.d: New.
+       * ld-ifunc/ifunc-12-i386.s: Likewise.
+       * ld-ifunc/ifunc-12-x86-64.d: Likewise.
+       * ld-ifunc/ifunc-12-x86-64.s: Likewise.
+       * ld-ifunc/ifunc-13-i386.d: Likewise.
+       * ld-ifunc/ifunc-13-x86-64.d: Likewise.
+       * ld-ifunc/ifunc-13a-i386.s: Likewise.
+       * ld-ifunc/ifunc-13a-x86-64.s: Likewise.
+       * ld-ifunc/ifunc-13b-i386.s: Likewise.
+       * ld-ifunc/ifunc-13b-x86-64.s: Likewise.
+
 2011-01-03  H.J. Lu  <hongjiu.lu@intel.com>
 
        * ld-x86-64/dummy.s: New.
index 8411e815eb60967cce00ba644b5a50d69ab7b9f8..76c5bef9bda8601a98f930b37ed8fe4faf60daee 100644 (file)
@@ -6,6 +6,8 @@ foo:
         movl ifunc@GOTOFF(%ecx), %eax
        call ifunc@PLT
        call ifunc
+        movl xxx@GOT(%ecx), %eax
+        movl xxx, %eax
         ret
 
         .section .text.bar,"ax",@progbits
@@ -18,3 +20,7 @@ bar:
         .type ifunc, @gnu_indirect_function
 ifunc:
         ret
+
+        .section .data.foo,"aw",@progbits
+xxx:
+       .long ifunc 
index ea6f8c2d16e4fc1cdc58116c874e9d3abd92ee58..11e29b52b489d14645e6bc8bf31f163c3ad822c2 100644 (file)
@@ -6,6 +6,7 @@ foo:
         movl ifunc(%rip), %eax
        call ifunc@PLT
        call ifunc
+        movl xxx(%rip), %eax
         ret
 
         .section .text.bar,"ax",@progbits
@@ -18,3 +19,7 @@ bar:
         .type ifunc, @gnu_indirect_function
 ifunc:
         ret
+
+        .section .data.foo,"aw",@progbits
+xxx:
+       .quad ifunc 
index 06f592400e89815c87ee636804b555bab8b6dc28..e0e474079546077ea9e4e9caf8e9f9c3fdc18a17 100644 (file)
@@ -3,9 +3,11 @@
 foo:
         .global foo
         movl ifunc@GOT(%ecx), %eax
-       movl ifunc@GOTOFF(%ecx), %eax
+        movl ifunc@GOTOFF(%ecx), %eax
        call ifunc@PLT
        call ifunc
+        movl xxx@GOT(%ecx), %eax
+        movl xxx, %eax
         ret
 
         .section .text.bar,"ax",@progbits
@@ -16,6 +18,10 @@ bar:
 
         .section .text.ifunc,"ax",@progbits
         .type ifunc, @gnu_indirect_function
-        .global ifunc
+       .global ifunc
 ifunc:
         ret
+
+        .section .data.foo,"aw",@progbits
+xxx:
+       .long ifunc 
index 70d4fbfdb16bceae38ba149f98df445bf2bb3588..8525419b5296b3109d3fdbe8a528a8b8cf341ef0 100644 (file)
@@ -6,6 +6,7 @@ foo:
         movl ifunc(%rip), %eax
        call ifunc@PLT
        call ifunc
+        movl xxx(%rip), %eax
         ret
 
         .section .text.bar,"ax",@progbits
@@ -19,3 +20,7 @@ bar:
         .global ifunc
 ifunc:
         ret
+
+        .section .data.foo,"aw",@progbits
+xxx:
+       .quad ifunc 
diff --git a/ld/testsuite/ld-ifunc/ifunc-12-i386.d b/ld/testsuite/ld-ifunc/ifunc-12-i386.d
new file mode 100644 (file)
index 0000000..de14181
--- /dev/null
@@ -0,0 +1,6 @@
+#ld: -shared -m elf_i386 -e bar --gc-sections
+#as: --32
+#readelf: -r --wide
+#target: x86_64-*-* i?86-*-*
+
+There are no relocations in this file.
diff --git a/ld/testsuite/ld-ifunc/ifunc-12-i386.s b/ld/testsuite/ld-ifunc/ifunc-12-i386.s
new file mode 100644 (file)
index 0000000..840302f
--- /dev/null
@@ -0,0 +1,25 @@
+        .section .text.foo,"ax",@progbits
+        .type foo, @function
+foo:
+        movl ifunc@GOT(%ecx), %eax
+        movl ifunc@GOTOFF(%ecx), %eax
+       call ifunc@PLT
+       call ifunc
+        movl xxx@GOT(%ecx), %eax
+        movl xxx, %eax
+        ret
+
+        .section .text.bar,"ax",@progbits
+        .type bar, @function
+bar:
+        .global bar
+        ret
+
+        .section .text.ifunc,"ax",@progbits
+        .type ifunc, @gnu_indirect_function
+ifunc:
+        ret
+
+        .section .data.foo,"aw",@progbits
+xxx:
+       .long ifunc 
diff --git a/ld/testsuite/ld-ifunc/ifunc-12-x86-64.d b/ld/testsuite/ld-ifunc/ifunc-12-x86-64.d
new file mode 100644 (file)
index 0000000..40f07c3
--- /dev/null
@@ -0,0 +1,6 @@
+#ld: -shared -m elf_x86_64 -e bar --gc-sections
+#as: --64
+#readelf: -r --wide
+#target: x86_64-*-*
+
+There are no relocations in this file.
diff --git a/ld/testsuite/ld-ifunc/ifunc-12-x86-64.s b/ld/testsuite/ld-ifunc/ifunc-12-x86-64.s
new file mode 100644 (file)
index 0000000..491684b
--- /dev/null
@@ -0,0 +1,24 @@
+        .section .text.foo,"ax",@progbits
+        .type foo, @function
+foo:
+        movl ifunc@GOTPCREL(%rip), %eax
+        movl ifunc(%rip), %eax
+       call ifunc@PLT
+       call ifunc
+        movl xxx(%rip), %eax
+        ret
+
+        .section .text.bar,"ax",@progbits
+        .type bar, @function
+bar:
+        .global bar
+        ret
+
+        .section .text.ifunc,"ax",@progbits
+        .type ifunc, @gnu_indirect_function
+ifunc:
+        ret
+
+        .section .data.foo,"aw",@progbits
+xxx:
+       .quad ifunc 
diff --git a/ld/testsuite/ld-ifunc/ifunc-13-i386.d b/ld/testsuite/ld-ifunc/ifunc-13-i386.d
new file mode 100644 (file)
index 0000000..162c3e4
--- /dev/null
@@ -0,0 +1,19 @@
+#source: ifunc-13a-i386.s
+#source: ifunc-13b-i386.s
+#ld: -shared -m elf_i386 -z nocombreloc
+#as: --32
+#readelf: -r --wide
+#target: x86_64-*-* i?86-*-*
+
+Relocation section '.rel.got' at .*
+[ ]+Offset[ ]+Info[ ]+Type[ ]+.*
+#...
+[0-9a-f]+[ ]+[0-9a-f]+[ ]+R_386_GLOB_DAT[ ]+ifunc\(\)[ ]+ifunc
+#...
+Relocation section '.rel.ifunc' at .*
+[ ]+Offset[ ]+Info[ ]+Type[ ]+.*
+[0-9a-f]+[ ]+[0-9a-f]+[ ]+R_386_32[ ]+ifunc\(\)[ ]+ifunc
+#...
+Relocation section '.rel.plt' at .*
+[ ]+Offset[ ]+Info[ ]+Type[ ]+.*
+[0-9a-f]+[ ]+[0-9a-f]+[ ]+R_386_JUMP_SLOT[ ]+ifunc\(\)[ ]+ifunc
diff --git a/ld/testsuite/ld-ifunc/ifunc-13-x86-64.d b/ld/testsuite/ld-ifunc/ifunc-13-x86-64.d
new file mode 100644 (file)
index 0000000..d0c0647
--- /dev/null
@@ -0,0 +1,18 @@
+#source: ifunc-13a-x86-64.s
+#source: ifunc-13b-x86-64.s
+#ld: -shared -m elf_x86_64 -z nocombreloc
+#as: --64
+#readelf: -r --wide
+#target: x86_64-*-*
+
+Relocation section '.rela.got' at .*
+[ ]+Offset[ ]+Info[ ]+Type[ ]+.*
+[0-9a-f]+[ ]+[0-9a-f]+[ ]+R_X86_64_GLOB_DAT[ ]+ifunc\(\)[ ]+ifunc \+ 0
+#...
+Relocation section '.rela.ifunc' at .*
+[ ]+Offset[ ]+Info[ ]+Type[ ]+.*
+[0-9a-f]+[ ]+[0-9a-f]+[ ]+R_X86_64_64[ ]+ifunc\(\)[ ]+ifunc \+ 0
+#...
+Relocation section '.rela.plt' at .*
+[ ]+Offset[ ]+Info[ ]+Type[ ]+.*
+[0-9a-f]+[ ]+[0-9a-f]+[ ]+R_X86_64_JUMP_SLOT[ ]+ifunc\(\)[ ]+ifunc \+ 0
diff --git a/ld/testsuite/ld-ifunc/ifunc-13a-i386.s b/ld/testsuite/ld-ifunc/ifunc-13a-i386.s
new file mode 100644 (file)
index 0000000..eb893af
--- /dev/null
@@ -0,0 +1,10 @@
+       .text
+        .type foo, @function
+       .global
+foo:
+       movl xxx@GOT(%ebx), %eax
+        ret
+
+       .data
+xxx:
+       .long ifunc 
diff --git a/ld/testsuite/ld-ifunc/ifunc-13a-x86-64.s b/ld/testsuite/ld-ifunc/ifunc-13a-x86-64.s
new file mode 100644 (file)
index 0000000..5bfc9e0
--- /dev/null
@@ -0,0 +1,10 @@
+       .text
+        .type foo, @function
+       .global
+foo:
+        movl xxx(%rip), %eax
+        ret
+
+       .data
+xxx:
+       .quad ifunc 
diff --git a/ld/testsuite/ld-ifunc/ifunc-13b-i386.s b/ld/testsuite/ld-ifunc/ifunc-13b-i386.s
new file mode 100644 (file)
index 0000000..3560394
--- /dev/null
@@ -0,0 +1,5 @@
+       .text
+        .type ifunc, @gnu_indirect_function
+       .globl ifunc
+ifunc:
+        ret
diff --git a/ld/testsuite/ld-ifunc/ifunc-13b-x86-64.s b/ld/testsuite/ld-ifunc/ifunc-13b-x86-64.s
new file mode 100644 (file)
index 0000000..3560394
--- /dev/null
@@ -0,0 +1,5 @@
+       .text
+        .type ifunc, @gnu_indirect_function
+       .globl ifunc
+ifunc:
+        ret