PATCH: PR ld/10270: IFUNC local symbol

H.J. Lu hjl.tools@gmail.com
Sun Jun 14 22:41:00 GMT 2009


On Fri, Jun 12, 2009 at 11:01 PM, Ulrich Drepper<drepper@redhat.com> wrote:
>
>> The other issue can be seen with:
>> gcc -O2 -fpic -shared -o ifunc3lib1.{so,c}
>> gcc -O2 -o ifunc3 ./ifunc3lib1.so
>> ./ifunc3lib1
>> Here, &lib1t3 in the binary resolves to a .plt slot in the binary, while
>> &lib1t3 in the shared library resolves to the actual address the ifunc
>> returned.
>> Not sure what exactly we want to do here, but the function pointers should
>> be the same.
>
> It's tricky alright.  There really isn't a good answer for this.  The
> existing behavior is the only sensible solution.  One could try to make
> things more complicated by changing the linker to associate the symbol
> with the PLT slot and somehow allow ld.so to recognize such symbols, but
> I think it's not worth it.
>
> This all comes about only because IFUNCs are used in situations where
> they really were not meant to be used.  IFUNC are supposed to be
> definitions in DSOs which can be used in place of FUNC symbols.  This
> will work without changes to any semantics.
>
> In your test case you're creating and IFUNC symbol in the executable.
> While I have no problem with supporting it this does mean the program
> author take responsibility for doing this.  I think we can in this case
> very well live with the difference in function addresses.
>
> Therefore I suggest to leave this case and just document it.
>

Hi,

I am checking in this patch to disallow dynamic IFUNC pointer in
non-shared object.


-- 
H.J.
---
bfd/

2009-06-14  H.J. Lu  <hongjiu.lu@intel.com>

	PR ld/10270
	* elf32-i386.c (elf_i386_allocate_dynrelocs): Disallow
	dynamic IFUNC pointer in non-shared object.  Use .got.plt
	for IFUNC definition in PIE.
	(elf_i386_allocate_dynrelocs): Resolve IFUNC definition in
	PIE locally.

	* elf64-x86-64.c (elf64_x86_64_allocate_dynrelocs): Disallow
	dynamic IFUNC pointer in non-shared object.  Use .got.plt
	for IFUNC definition in PIE.
	(elf64_x86_64_relocate_section): Resolve IFUNC definition in
	PIE locally.

ld/testsuite/

2009-06-14  H.J. Lu  <hongjiu.lu@intel.com>

	PR ld/10270
	* ld-ifunc/ifunc-9-x86.d: New.
	* ld-ifunc/ifunc-9-x86.s: Likewise.
-------------- next part --------------
bfd/

2009-06-14  H.J. Lu  <hongjiu.lu@intel.com>

	PR ld/10270
	* elf32-i386.c (elf_i386_allocate_dynrelocs): Disallow
	dynamic IFUNC pointer in non-shared object.  Use .got.plt
	for IFUNC definition in PIE.
	(elf_i386_allocate_dynrelocs): Resolve IFUNC definition in
	PIE locally.

	* elf64-x86-64.c (elf64_x86_64_allocate_dynrelocs): Disallow
	dynamic IFUNC pointer in non-shared object.  Use .got.plt
	for IFUNC definition in PIE.
	(elf64_x86_64_relocate_section): Resolve IFUNC definition in
	PIE locally.

ld/testsuite/

2009-06-14  H.J. Lu  <hongjiu.lu@intel.com>

	PR ld/10270
	* ld-ifunc/ifunc-9-x86.d: New.
	* ld-ifunc/ifunc-9-x86.s: Likewise.

Index: ld/testsuite/ld-ifunc/ifunc-9-x86.d
===================================================================
--- ld/testsuite/ld-ifunc/ifunc-9-x86.d	(revision 0)
+++ ld/testsuite/ld-ifunc/ifunc-9-x86.d	(revision 0)
@@ -0,0 +1,3 @@
+#ld: --export-dynamic
+#error: .*dynamic STT_GNU_IFUNC symbool `foo' with pointer equality in `.*.o' can not be used when making an executable; recompile with -fPIE and relink with -pie
+#target: x86_64-*-* i?86-*-*
Index: ld/testsuite/ld-ifunc/ifunc-9-x86.s
===================================================================
--- ld/testsuite/ld-ifunc/ifunc-9-x86.s	(revision 0)
+++ ld/testsuite/ld-ifunc/ifunc-9-x86.s	(revision 0)
@@ -0,0 +1,18 @@
+	.text
+	.type foo, %gnu_indirect_function
+.globl foo
+	.type	foo, @function
+foo:
+	ret
+	.size	foo, .-foo
+	.type start,"function"
+	.global start
+start:
+	.type _start,"function"
+	.global _start
+_start:
+	.type __start,"function"
+	.global __start
+__start:
+	.type __start,"function"
+	movl    foo, %eax
Index: bfd/elf64-x86-64.c
===================================================================
--- bfd/elf64-x86-64.c	(revision 6121)
+++ bfd/elf64-x86-64.c	(working copy)
@@ -1982,6 +1982,27 @@ elf64_x86_64_allocate_dynrelocs (struct 
     {
       asection *plt, *gotplt, *relplt;
 
+      /* When a shared library references a STT_GNU_IFUNC symbol
+	 defined in executable. the .got.plt slot in the shared library
+	 will contain address of the .plt slot in the binary and only
+	 its .got.plt will contain the resolved function that should be
+	 called.  Pointer equality won't work correctly.  PIE should
+	 be used if pointer equality is required here.  */
+      if (!info->shared
+	  && (h->dynindx != -1
+	      || info->export_dynamic)
+	  && h->pointer_equality_needed)
+	{
+	  info->callbacks->einfo 
+	    (_("%F%P: dynamic STT_GNU_IFUNC symbool `%s' with pointer "
+	       "equality in `%B' can not be used when making an "
+	       "executable; recompile with -fPIE and relink with -pie\n"),
+	     h->root.root.string,
+	     h->root.u.def.section->owner);
+	  bfd_set_error (bfd_error_bad_value);
+	  return FALSE;
+	}
+
       /* Return and discard space for dynamic relocations against it if
 	 it is never referenced in a non-shared object.  */
       if (!h->ref_regular)
@@ -2049,8 +2070,9 @@ elf64_x86_64_allocate_dynrelocs (struct 
 	 not dynamic.
 	 2. Use .got.plt in a non-shared object if pointer equality 
 	 isn't needed.
-	 3. Use .got.plt if .got isn't used.
-	 4. Otherwise use .got so that it can be shared among different
+	 3. Use .got.plt in PIE.
+	 4. Use .got.plt if .got isn't used.
+	 5. Otherwise use .got so that it can be shared among different
 	 objects at run-time.
 	 We only need to relocate .got entry in shared object.  */
       if ((info->shared
@@ -2058,6 +2080,7 @@ elf64_x86_64_allocate_dynrelocs (struct 
 	       || h->forced_local))
 	  || (!info->shared
 	      && !h->pointer_equality_needed)
+	  || (info->executable && info->shared)
 	  || htab->sgot == NULL)
 	{
 	  /* Use .got.plt.  */
@@ -2914,7 +2937,8 @@ elf64_x86_64_relocate_section (bfd *outp
 				      + input_section->output_offset);
 
 		  if (h->dynindx == -1
-		      || h->forced_local)
+		      || h->forced_local
+		      || info->executable)
 		    {
 		      /* This symbol is resolved locally.  */
 		      outrel.r_info = ELF64_R_INFO (0, R_X86_64_IRELATIVE);
Index: bfd/elf32-i386.c
===================================================================
--- bfd/elf32-i386.c	(revision 6121)
+++ bfd/elf32-i386.c	(working copy)
@@ -2143,6 +2143,27 @@ elf_i386_allocate_dynrelocs (struct elf_
     {
       asection *plt, *gotplt, *relplt;
 
+      /* When a shared library references a STT_GNU_IFUNC symbol
+	 defined in executable. the .got.plt slot in the shared library
+	 will contain address of the .plt slot in the binary and only
+	 its .got.plt will contain the resolved function that should be
+	 called.  Pointer equality won't work correctly.  PIE should
+	 be used if pointer equality is required here.  */
+      if (!info->shared
+	  && (h->dynindx != -1
+	      || info->export_dynamic)
+	  && h->pointer_equality_needed)
+	{
+	  info->callbacks->einfo 
+	    (_("%F%P: dynamic STT_GNU_IFUNC symbool `%s' with pointer "
+	       "equality in `%B' can not be used when making an "
+	       "executable; recompile with -fPIE and relink with -pie\n"),
+	     h->root.root.string,
+	     h->root.u.def.section->owner);
+	  bfd_set_error (bfd_error_bad_value);
+	  return FALSE;
+	}
+
       /* Return and discard space for dynamic relocations against it if
 	 it is never referenced in a non-shared object.  */
       if (!h->ref_regular)
@@ -2210,8 +2231,9 @@ elf_i386_allocate_dynrelocs (struct elf_
 	 not dynamic.
 	 2. Use .got.plt in a non-shared object if pointer equality 
 	 isn't needed.
-	 3. Use .got.plt if .got isn't used.
-	 4. Otherwise use .got so that it can be shared among different
+	 3. Use .got.plt in PIE.
+	 4. Use .got.plt if .got isn't used.
+	 5. Otherwise use .got so that it can be shared among different
 	 objects at run-time.
 	 We only need to relocate .got entry in shared object.  */
       if ((info->shared
@@ -2219,6 +2241,7 @@ elf_i386_allocate_dynrelocs (struct elf_
 	       || h->forced_local))
 	  || (!info->shared
 	      && !h->pointer_equality_needed)
+	  || (info->executable && info->shared)
 	  || htab->sgot == NULL)
 	{
 	  /* Use .got.plt.  */
@@ -3194,7 +3217,8 @@ elf_i386_relocate_section (bfd *output_b
 				     + offset);
 
 		  if (h->dynindx == -1
-		      || h->forced_local)
+		      || h->forced_local
+		      || info->executable)
 		    {
 		      /* This symbol is resolved locally.  */
 		      outrel.r_info = ELF32_R_INFO (0, R_386_IRELATIVE);


More information about the Libc-alpha mailing list