Support garbage collection against STT_GNU_IFUNC symbols.
authorH.J. Lu <hjl.tools@gmail.com>
Tue, 13 Jul 2010 16:59:14 +0000 (16:59 +0000)
committerH.J. Lu <hjl.tools@gmail.com>
Tue, 13 Jul 2010 16:59:14 +0000 (16:59 +0000)
bfd/

2010-07-13  H.J. Lu  <hongjiu.lu@intel.com>

PR ld/11791
* elf-ifunc.c (_bfd_elf_allocate_ifunc_dyn_relocs): Support
garbage collection against STT_GNU_IFUNC symbols.

* elf32-i386.c (elf_i386_get_local_sym_hash): Don't set
elf.plt.offset/elf.got.offset to -1.
(elf_i386_tls_transition): Skip TLS transition for functions.
(elf_i386_gc_sweep_hook): Support STT_GNU_IFUNC symbols.

* elf64-x86-64.c (elf64_x86_64_get_local_sym_hash): Don't set
elf.plt.offset/elf.got.offset to -1.
(elf64_x86_64_tls_transition): Skip TLS transition for functions.
(elf64_x86_64_gc_sweep_hook): Support STT_GNU_IFUNC symbols.

ld/testsuite/

2010-07-13  H.J. Lu  <hongjiu.lu@intel.com>

PR ld/11791
* ld-ifunc/ifunc-10-i386.d: New.
* ld-ifunc/ifunc-10-i386.s: Likewise.
* ld-ifunc/ifunc-10-x86-64.d: Likewise.
* ld-ifunc/ifunc-10-x86-64.s: Likewise.
* ld-ifunc/ifunc-11-i386.d: Likewise.
* ld-ifunc/ifunc-11-i386.s: Likewise.
* ld-ifunc/ifunc-11-x86-64.d: Likewise.
* ld-ifunc/ifunc-11-x86-64.s: Likewise.

13 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.d [new file with mode: 0644]
ld/testsuite/ld-ifunc/ifunc-10-i386.s [new file with mode: 0644]
ld/testsuite/ld-ifunc/ifunc-10-x86-64.d [new file with mode: 0644]
ld/testsuite/ld-ifunc/ifunc-10-x86-64.s [new file with mode: 0644]
ld/testsuite/ld-ifunc/ifunc-11-i386.d [new file with mode: 0644]
ld/testsuite/ld-ifunc/ifunc-11-i386.s [new file with mode: 0644]
ld/testsuite/ld-ifunc/ifunc-11-x86-64.d [new file with mode: 0644]
ld/testsuite/ld-ifunc/ifunc-11-x86-64.s [new file with mode: 0644]

index 32c44a9..e12771a 100644 (file)
@@ -1,3 +1,19 @@
+2010-07-13  H.J. Lu  <hongjiu.lu@intel.com>
+
+       PR ld/11791
+       * elf-ifunc.c (_bfd_elf_allocate_ifunc_dyn_relocs): Support
+       garbage collection against STT_GNU_IFUNC symbols.
+
+       * elf32-i386.c (elf_i386_get_local_sym_hash): Don't set
+       elf.plt.offset/elf.got.offset to -1.
+       (elf_i386_tls_transition): Skip TLS transition for functions.
+       (elf_i386_gc_sweep_hook): Support STT_GNU_IFUNC symbols.
+
+       * elf64-x86-64.c (elf64_x86_64_get_local_sym_hash): Don't set
+       elf.plt.offset/elf.got.offset to -1.
+       (elf64_x86_64_tls_transition): Skip TLS transition for functions.
+       (elf64_x86_64_gc_sweep_hook): Support STT_GNU_IFUNC symbols.
+
 2010-07-12  H.J. Lu  <hongjiu.lu@intel.com>
 
        * elf32-i386.c (elf_i386_check_relocs): Re-indent.
index 0de236f..760fc26 100644 (file)
@@ -187,6 +187,15 @@ _bfd_elf_allocate_ifunc_dyn_relocs (struct bfd_link_info *info,
 
   htab = elf_hash_table (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;
+    }
+
   /* Return and discard space for dynamic relocations against it if
      it is never referenced in a non-shared object.  */
   if (!h->ref_regular)
index e85a7e5..ac9bdb6 100644 (file)
@@ -789,8 +789,6 @@ elf_i386_get_local_sym_hash (struct elf_i386_link_hash_table *htab,
       ret->elf.indx = sec->id;
       ret->elf.dynstr_index = ELF32_R_SYM (rel->r_info);
       ret->elf.dynindx = -1;
-      ret->elf.plt.offset = (bfd_vma) -1;
-      ret->elf.got.offset = (bfd_vma) -1;
       *slot = ret;
     }
   return &ret->elf;
@@ -1162,6 +1160,12 @@ elf_i386_tls_transition (struct bfd_link_info *info, bfd *abfd,
   unsigned int to_type = from_type;
   bfd_boolean check = TRUE;
 
+  /* Skip TLS transition for functions.  */
+  if (h != NULL
+      && (h->type == STT_FUNC
+         || h->type == STT_GNU_IFUNC))
+    return TRUE;
+
   switch (from_type)
     {
     case R_386_TLS_GD:
@@ -1819,6 +1823,23 @@ elf_i386_gc_sweep_hook (bfd *abfd,
                break;
              }
        }
+      else
+       {
+         /* A local symbol.  */
+         Elf_Internal_Sym *isym;
+
+         isym = bfd_sym_from_r_symndx (&htab->sym_cache,
+                                       abfd, r_symndx);
+
+         /* Check relocation against local STT_GNU_IFUNC symbol.  */
+         if (isym != NULL
+             && ELF32_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
+           {
+             h = elf_i386_get_local_sym_hash (htab, abfd, rel, FALSE);
+             if (h == NULL)
+               abort ();
+           }
+       }
 
       r_type = ELF32_R_TYPE (rel->r_info);
       if (! elf_i386_tls_transition (info, abfd, sec, NULL,
@@ -1845,6 +1866,11 @@ elf_i386_gc_sweep_hook (bfd *abfd,
            {
              if (h->got.refcount > 0)
                h->got.refcount -= 1;
+             if (h->type == STT_GNU_IFUNC)
+               {
+                 if (h->plt.refcount > 0)
+                   h->plt.refcount -= 1;
+               }
            }
          else if (local_got_refcounts != NULL)
            {
@@ -1867,6 +1893,16 @@ elf_i386_gc_sweep_hook (bfd *abfd,
            }
          break;
 
+       case R_386_GOTOFF:
+         if (h != NULL && h->type == STT_GNU_IFUNC)
+           {
+             if (h->got.refcount > 0)
+               h->got.refcount -= 1;
+             if (h->plt.refcount > 0)
+               h->plt.refcount -= 1;
+           }
+         break;
+
        default:
          break;
        }
index 21524fa..108f257 100644 (file)
@@ -603,8 +603,6 @@ elf64_x86_64_get_local_sym_hash (struct elf64_x86_64_link_hash_table *htab,
       ret->elf.indx = sec->id;
       ret->elf.dynstr_index = ELF64_R_SYM (rel->r_info);
       ret->elf.dynindx = -1;
-      ret->elf.plt.offset = (bfd_vma) -1;
-      ret->elf.got.offset = (bfd_vma) -1;
       *slot = ret;
     }
   return &ret->elf;
@@ -951,6 +949,12 @@ elf64_x86_64_tls_transition (struct bfd_link_info *info, bfd *abfd,
   unsigned int to_type = from_type;
   bfd_boolean check = TRUE;
 
+  /* Skip TLS transition for functions.  */
+  if (h != NULL
+      && (h->type == STT_FUNC
+         || h->type == STT_GNU_IFUNC))
+    return TRUE;
+
   switch (from_type)
     {
     case R_X86_64_TLSGD:
@@ -1657,6 +1661,24 @@ elf64_x86_64_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
                break;
              }
        }
+      else
+       {
+         /* A local symbol.  */
+         Elf_Internal_Sym *isym;
+
+         isym = bfd_sym_from_r_symndx (&htab->sym_cache,
+                                       abfd, r_symndx);
+
+         /* Check relocation against local STT_GNU_IFUNC symbol.  */
+         if (isym != NULL
+             && ELF64_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
+           {
+             h = elf64_x86_64_get_local_sym_hash (htab, abfd, rel,
+                                                  FALSE);
+             if (h == NULL)
+               abort ();
+           }
+       }
 
       r_type = ELF64_R_TYPE (rel->r_info);
       if (! elf64_x86_64_tls_transition (info, abfd, sec, NULL,
@@ -1687,6 +1709,11 @@ elf64_x86_64_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
                h->plt.refcount -= 1;
              if (h->got.refcount > 0)
                h->got.refcount -= 1;
+             if (h->type == STT_GNU_IFUNC)
+               {
+                 if (h->plt.refcount > 0)
+                   h->plt.refcount -= 1;
+               }
            }
          else if (local_got_refcounts != NULL)
            {
index 463a14c..6755523 100644 (file)
@@ -1,3 +1,15 @@
+2010-07-13  H.J. Lu  <hongjiu.lu@intel.com>
+
+       PR ld/11791
+       * ld-ifunc/ifunc-10-i386.d: New.
+       * ld-ifunc/ifunc-10-i386.s: Likewise.
+       * ld-ifunc/ifunc-10-x86-64.d: Likewise.
+       * ld-ifunc/ifunc-10-x86-64.s: Likewise.
+       * ld-ifunc/ifunc-11-i386.d: Likewise.
+       * ld-ifunc/ifunc-11-i386.s: Likewise.
+       * ld-ifunc/ifunc-11-x86-64.d: Likewise.
+       * ld-ifunc/ifunc-11-x86-64.s: Likewise.
+
 2010-07-06  Alan Modra  <amodra@gmail.com>
 
        * ld-powerpc/relax.s: Add branch back to _start.
diff --git a/ld/testsuite/ld-ifunc/ifunc-10-i386.d b/ld/testsuite/ld-ifunc/ifunc-10-i386.d
new file mode 100644 (file)
index 0000000..5f56b24
--- /dev/null
@@ -0,0 +1,6 @@
+#ld: -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-10-i386.s b/ld/testsuite/ld-ifunc/ifunc-10-i386.s
new file mode 100644 (file)
index 0000000..8411e81
--- /dev/null
@@ -0,0 +1,20 @@
+        .section .text.foo,"ax",@progbits
+        .type foo, @function
+foo:
+        .global foo
+        movl ifunc@GOT(%ecx), %eax
+        movl ifunc@GOTOFF(%ecx), %eax
+       call ifunc@PLT
+       call ifunc
+        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
diff --git a/ld/testsuite/ld-ifunc/ifunc-10-x86-64.d b/ld/testsuite/ld-ifunc/ifunc-10-x86-64.d
new file mode 100644 (file)
index 0000000..8ece379
--- /dev/null
@@ -0,0 +1,6 @@
+#ld: -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-10-x86-64.s b/ld/testsuite/ld-ifunc/ifunc-10-x86-64.s
new file mode 100644 (file)
index 0000000..ea6f8c2
--- /dev/null
@@ -0,0 +1,20 @@
+        .section .text.foo,"ax",@progbits
+        .type foo, @function
+foo:
+        .global foo
+        movl ifunc@GOTPCREL(%rip), %eax
+        movl ifunc(%rip), %eax
+       call ifunc@PLT
+       call ifunc
+        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
diff --git a/ld/testsuite/ld-ifunc/ifunc-11-i386.d b/ld/testsuite/ld-ifunc/ifunc-11-i386.d
new file mode 100644 (file)
index 0000000..5f56b24
--- /dev/null
@@ -0,0 +1,6 @@
+#ld: -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-11-i386.s b/ld/testsuite/ld-ifunc/ifunc-11-i386.s
new file mode 100644 (file)
index 0000000..06f5924
--- /dev/null
@@ -0,0 +1,21 @@
+        .section .text.foo,"ax",@progbits
+        .type foo, @function
+foo:
+        .global foo
+        movl ifunc@GOT(%ecx), %eax
+       movl ifunc@GOTOFF(%ecx), %eax
+       call ifunc@PLT
+       call ifunc
+        ret
+
+        .section .text.bar,"ax",@progbits
+        .type bar, @function
+bar:
+        .global bar
+        ret
+
+        .section .text.ifunc,"ax",@progbits
+        .type ifunc, @gnu_indirect_function
+        .global ifunc
+ifunc:
+        ret
diff --git a/ld/testsuite/ld-ifunc/ifunc-11-x86-64.d b/ld/testsuite/ld-ifunc/ifunc-11-x86-64.d
new file mode 100644 (file)
index 0000000..8ece379
--- /dev/null
@@ -0,0 +1,6 @@
+#ld: -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-11-x86-64.s b/ld/testsuite/ld-ifunc/ifunc-11-x86-64.s
new file mode 100644 (file)
index 0000000..70d4fbf
--- /dev/null
@@ -0,0 +1,21 @@
+        .section .text.foo,"ax",@progbits
+        .type foo, @function
+foo:
+        .global foo
+        movl ifunc@GOTPCREL(%rip), %eax
+        movl ifunc(%rip), %eax
+       call ifunc@PLT
+       call ifunc
+        ret
+
+        .section .text.bar,"ax",@progbits
+        .type bar, @function
+bar:
+        .global bar
+        ret
+
+        .section .text.ifunc,"ax",@progbits
+        .type ifunc, @gnu_indirect_function
+        .global ifunc
+ifunc:
+        ret