Bug 13178 - Unnecessary GOT relocation created for IFUNC
Summary: Unnecessary GOT relocation created for IFUNC
Status: RESOLVED FIXED
Alias: None
Product: binutils
Classification: Unclassified
Component: ld (show other bugs)
Version: 2.22
: P2 normal
Target Milestone: ---
Assignee: unassigned
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-09-11 14:26 UTC by Ulrich Drepper
Modified: 2011-09-12 18:19 UTC (History)
2 users (show)

See Also:
Host: x86_64-linux
Target:
Build:
Last reconfirmed:


Attachments
Test program to show unnecessary IFUNC relocation (150 bytes, text/plain)
2011-09-11 14:26 UTC, Ulrich Drepper
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Ulrich Drepper 2011-09-11 14:26:13 UTC
Created attachment 5926 [details]
Test program to show unnecessary IFUNC relocation

When compiling the attached code using

  gcc -shared -o u.so -fpic u.so

on an x86-64 system you will see the following relocations:

Relocation section [ 7] '.rela.dyn' for section [ 0] '' at offset 0x3e8 contains 5 entries:
  Offset              Type            Value               Addend Name
  0x0000000000200760  X86_64_RELATIVE 000000000000000000  +2099040 
  0x00000000002008e8  X86_64_GLOB_DAT 000000000000000000      +0 __gmon_start__
  0x00000000002008f0  X86_64_GLOB_DAT 000000000000000000      +0 _Jv_RegisterClasses
  0x00000000002008f8  X86_64_GLOB_DAT 000000000000000000      +0 __cxa_finalize
  0x0000000000200900  X86_64_GLOB_DAT 0x00000000000005d8      +0 f

Relocation section [ 8] '.rela.plt' for section [10] '.plt' at offset 0x460 contains 3 entries:
  Offset              Type            Value               Addend Name
  0x0000000000200920  X86_64_JUMP_SLOT 000000000000000000      +0 __cxa_finalize
  0x0000000000200928  X86_64_JUMP_SLOT 0x00000000000005d8      +0 f
  0x0000000000200930  X86_64_JUMP_SLOT 0x00000000000005e5      +0 g


There are two relocations against f.  Since there is no reference to f other than through a call there is no need for a GOT relocation.  The PLT relocation is all that's needed.  It works fine for normal symbols (there is only one relocation for g) and IFUNCs should be handled just like that.
Comment 1 H.J. Lu 2011-09-11 16:21:48 UTC
X86_64_GLOB_DAT was used to preempt f.  Will preempt work without
X86_64_GLOB_DAT?
Comment 2 Ulrich Drepper 2011-09-11 17:13:58 UTC
(In reply to comment #1)
> X86_64_GLOB_DAT was used to preempt f.  Will preempt work without
> X86_64_GLOB_DAT?

You mean preloading such an object?  Why should this matter?  Any relocation will only look at the symbol record in the DSO with the definition.  This will prevoke the indirect function be called, even in the case of dlsym().

What do I miss?  Please describe the situation I think needs this relocation.
Comment 3 H.J. Lu 2011-09-11 18:29:15 UTC
(In reply to comment #2)
> (In reply to comment #1)
> > X86_64_GLOB_DAT was used to preempt f.  Will preempt work without
> > X86_64_GLOB_DAT?
> 
> You mean preloading such an object?  Why should this matter?  Any relocation
> will only look at the symbol record in the DSO with the definition.  This will
> prevoke the indirect function be called, even in the case of dlsym().
> 
> What do I miss?  Please describe the situation I think needs this relocation.

There may be another 'f' definition in executable or a DSO before
u.so. We need to make sure that f in u.so isn't used in this case
by updating its GOT entry.
Comment 4 Alan Modra 2011-09-12 00:01:06 UTC
HJ, where is the .got entry used in the .so?  I can't see any use, but maybe I'm missing something.  If it isn't used, then your last comment makes no sense.
Comment 5 Sourceware Commits 2011-09-12 18:17:40 UTC
CVSROOT:	/cvs/src
Module name:	src
Changes by:	hjl@sourceware.org	2011-09-12 18:17:36

Modified files:
	bfd            : ChangeLog elf-ifunc.c 
	ld/testsuite   : ChangeLog 
	ld/testsuite/ld-ifunc: ifunc-13-i386.d ifunc-13-x86-64.d 
	                       ifunc-3a-x86.d 
	ld/testsuite/ld-x86-64: pr13082-5a.d 
Added files:
	ld/testsuite/ld-ifunc: ifunc-15-i386.d ifunc-15-i386.s 
	                       ifunc-15-x86-64.d ifunc-15-x86-64.s 

Log message:
	Use .got.plt for IFUNC symbols if there are no GOT relocations.
	
	bfd/
	
	2011-09-12  H.J. Lu  <hongjiu.lu@intel.com>
	
	PR ld/13178
	* elf-ifunc.c (_bfd_elf_allocate_ifunc_dyn_relocs): Use .got.plt
	if there are no GOT relocations.
	
	ld/testsuite/
	
	2011-09-12  H.J. Lu  <hongjiu.lu@intel.com>
	
	PR ld/13178
	* ld-ifunc/ifunc-13-i386.d: Updated.
	* ld-ifunc/ifunc-13-x86-64.d: Likewise.
	* ld-ifunc/ifunc-3a-x86.d: Likewise.
	* ld-x86-64/pr13082-5a.d: Likewise.
	
	* ld-ifunc/ifunc-15-i386.d: New.
	* ld-ifunc/ifunc-15-i386.s: Likewise.
	* ld-ifunc/ifunc-15-x86-64.d: Likewise.
	* ld-ifunc/ifunc-15-x86-64.s: Likewise.

Patches:
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/ChangeLog.diff?cvsroot=src&r1=1.5468&r2=1.5469
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elf-ifunc.c.diff?cvsroot=src&r1=1.6&r2=1.7
http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/ChangeLog.diff?cvsroot=src&r1=1.1456&r2=1.1457
http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/ld-ifunc/ifunc-15-i386.d.diff?cvsroot=src&r1=NONE&r2=1.1
http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/ld-ifunc/ifunc-15-i386.s.diff?cvsroot=src&r1=NONE&r2=1.1
http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/ld-ifunc/ifunc-15-x86-64.d.diff?cvsroot=src&r1=NONE&r2=1.1
http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/ld-ifunc/ifunc-15-x86-64.s.diff?cvsroot=src&r1=NONE&r2=1.1
http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/ld-ifunc/ifunc-13-i386.d.diff?cvsroot=src&r1=1.1&r2=1.2
http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/ld-ifunc/ifunc-13-x86-64.d.diff?cvsroot=src&r1=1.1&r2=1.2
http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/ld-ifunc/ifunc-3a-x86.d.diff?cvsroot=src&r1=1.5&r2=1.6
http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/ld-x86-64/pr13082-5a.d.diff?cvsroot=src&r1=1.1&r2=1.2
Comment 6 H.J. Lu 2011-09-12 18:19:37 UTC
Fixed.