Symbol refcounting for STT_GNU_IFUNC symbols

H.J. Lu hjl.tools@gmail.com
Mon Jul 12 23:30:00 GMT 2010


On Mon, Jul 5, 2010 at 9:05 AM, Stephen Clarke <stephen.clarke@st.com> wrote:
> H.J. Lu wrote:
>>
>> On Fri, Jul 2, 2010 at 7:47 AM, Stephen Clarke <stephen.clarke@st.com>
>> wrote:
>>>
>>> In bfd/elf32-386.c, elf_i386_check_relocs(), for R_386_GOT32 and
>>> R_386_GOTOFF relocations with a symbol with STT_GNU_IFUNC type, the
>>> h->got.refcount is incremented:
>>>
>>>               case R_386_GOT32:
>>>               case R_386_GOTOFF:
>>>                 h->got.refcount += 1;
>>>
>>> I guess this is because the mechanism for IFUNC symbols requires a GOT
>>> entry (even for a GOTOFF relocation which does not normally require a
>>> GOT entry).
>>>
>>> However, there is no corresponding decrement in the
>>> elf_i386_gc_sweep_hook() if the relocation is removed.
>>> Is this just a minor (safe) inaccuracy, or is there some reason why the
>>> refcount cannot be decremented in elf_i386_gc_sweep_hook()?
>>>
>>
>> It could be an oversight.  Do you have an testcase?
>
> I don't have a real testcase ... the question arose from a discussion
> with colleagues about the refcounting in elf32-sh.c ... and since that is
> based on the refcounting in elf32-i386.c, we reviewed that.
> (I know ifunc is not supported yet in elf32-sh.c: our original
> discussion was unrelated to ifunc symbols.)
>
> However, here's a synthetic testcase:
> $ cat test.s
>        .section .text.foo,"ax",@progbits
>        .type foo, @function
> foo:
>        .global foo
>        movl ifunc@GOT(%ecx), %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
>
> $ as -o test.o test.s
> $ ld -e bar --gc-sections test.o
>
> If I use a recent version of binutils:
> $ ld -v
> GNU ld (GNU Binutils) 2.20.51.20100702
>
> then for the ld command I get segv, i.e.
> $ ld -e bar --gc-sections test.o
> Segmentation fault
>
> but if I hack out the cause of that, then I get an executable in
> which garbage collection has removed all the original sections that
> refer to the ifunc symbol, but there is still a plt entry, got entry
> and  dynamic relocation for the ifunc symbol.
>
> $ objdump -h -d a.out
>
> a.out:     file format elf32-i386
>
> Sections:
> Idx Name          Size      VMA       LMA       File off  Algn
>  0 .rel.plt      00000008  08048074  08048074  00000074  2**2
>                  CONTENTS, ALLOC, LOAD, READONLY, DATA
>  1 .plt          00000010  0804807c  0804807c  0000007c  2**2
>                  CONTENTS, ALLOC, LOAD, READONLY, CODE
>  2 .text         00000001  0804808c  0804808c  0000008c  2**0
>                  CONTENTS, ALLOC, LOAD, READONLY, CODE
>  3 .got.plt      00000010  08049090  08049090  00000090  2**2
>                  CONTENTS, ALLOC, LOAD, DATA
>
> Disassembly of section .plt:
>
> 0804807c <.plt>:
>  804807c:       ff 25 9c 90 04 08       jmp    *0x804909c
>  8048082:       68 00 00 00 00          push   $0x0
>  8048087:       e9 00 00 00 00          jmp    804808c <bar>
>
> Disassembly of section .text:
>
> 0804808c <bar>:
>  804808c:       c3                      ret
>
>
> Steve.
>
>

I am testing this patch. Totally removing .got.plt section will
be tricky since it is created before garbage collection.
Please open another bug if it is really needed.

Thanks.

-- 
H.J.
---
bfd/

2010-07-12  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-12  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.
-------------- next part --------------
bfd/

2010-07-12  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-12  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.

diff --git a/bfd/elf-ifunc.c b/bfd/elf-ifunc.c
index 0de236f..742e575 100644
--- a/bfd/elf-ifunc.c
+++ b/bfd/elf-ifunc.c
@@ -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)
diff --git a/bfd/elf32-i386.c b/bfd/elf32-i386.c
index e85a7e5..ac9bdb6 100644
--- a/bfd/elf32-i386.c
+++ b/bfd/elf32-i386.c
@@ -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;
 	}
diff --git a/bfd/elf64-x86-64.c b/bfd/elf64-x86-64.c
index 21524fa..108f257 100644
--- a/bfd/elf64-x86-64.c
+++ b/bfd/elf64-x86-64.c
@@ -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)
 	    {
--- /dev/null	2010-07-09 11:29:32.805426765 -0700
+++ binutils/ld/testsuite/ld-ifunc/ifunc-10-i386.d	2010-07-12 16:07:54.899638448 -0700
@@ -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.
--- /dev/null	2010-07-09 11:29:32.805426765 -0700
+++ binutils/ld/testsuite/ld-ifunc/ifunc-10-i386.s	2010-07-12 16:07:02.761538046 -0700
@@ -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
--- /dev/null	2010-07-09 11:29:32.805426765 -0700
+++ binutils/ld/testsuite/ld-ifunc/ifunc-10-x86-64.d	2010-07-12 16:09:27.086846682 -0700
@@ -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.
--- /dev/null	2010-07-09 11:29:32.805426765 -0700
+++ binutils/ld/testsuite/ld-ifunc/ifunc-10-x86-64.s	2010-07-12 16:09:40.831469890 -0700
@@ -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
--- /dev/null	2010-07-09 11:29:32.805426765 -0700
+++ binutils/ld/testsuite/ld-ifunc/ifunc-11-i386.d	2010-07-12 16:08:07.264477641 -0700
@@ -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.
--- /dev/null	2010-07-09 11:29:32.805426765 -0700
+++ binutils/ld/testsuite/ld-ifunc/ifunc-11-i386.s	2010-07-12 16:11:04.141413023 -0700
@@ -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
--- /dev/null	2010-07-09 11:29:32.805426765 -0700
+++ binutils/ld/testsuite/ld-ifunc/ifunc-11-x86-64.d	2010-07-12 16:09:52.784598599 -0700
@@ -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.
--- /dev/null	2010-07-09 11:29:32.805426765 -0700
+++ binutils/ld/testsuite/ld-ifunc/ifunc-11-x86-64.s	2010-07-12 16:10:06.159537495 -0700
@@ -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


More information about the Binutils mailing list