Patchwork PATCH: Replace @PLT with @GOTPCREL(%rip) in call

login
register
mail settings
Submitter H.J. Lu
Date March 4, 2016, 11:40 p.m.
Message ID <CAMe9rOo+EHGLOUotjk++cUdUbeLNp86dXy-diH-8w9D=7g=Ehw@mail.gmail.com>
Download mbox | patch
Permalink /patch/11204/
State New
Headers show

Comments

H.J. Lu - March 4, 2016, 11:40 p.m.
On Thu, Mar 3, 2016 at 4:03 PM, Roland McGrath <roland@hack.frob.com> wrote:
>> --- a/sysdeps/x86_64/crti.S
>> +++ b/sysdeps/x86_64/crti.S
>> @@ -66,7 +66,7 @@ _init:
>>       movq PREINIT_FUNCTION@GOTPCREL(%rip), %rax
>>       testq %rax, %rax
>>       je .Lno_weak_fn
>> -     call PREINIT_FUNCTION@PLT
>> +     call *PREINIT_FUNCTION@GOTPCREL(%rip)
>
> Can't this just be "call *%rax"?

Good idea.

>> --- a/sysdeps/x86_64/start.S
>> +++ b/sysdeps/x86_64/start.S
>> @@ -105,7 +105,7 @@ ENTRY (_start)
>>
>>       /* Call the user's main function, and exit with its value.
>>          But let the libc call main.    */
>> -     call __libc_start_main@PLT
>> +     call *__libc_start_main@GOTPCREL(%rip)
>>  #else
>>       /* Pass address of our own entry points to .fini and .init.  */
>>       mov $__libc_csu_fini, %R8_LP
>
> In the general case, foo@PLT is preferable to foo@GOTPCREL(%rip) because it
> allows for lazy resolution.  That's not worthwhile here because _start is
> presumably always run and run very early.  But it merits a comment saying
> why it's not following the normal pattern for PIC calls.
>

Here are updated patches.
Roland McGrath - March 4, 2016, 11:46 p.m.
Both look fine to me.

Patch

From 9ff9ae1b792e3d986f68a127e0b3f8679e6bea17 Mon Sep 17 00:00:00 2001
From: "H.J. Lu" <hjl.tools@gmail.com>
Date: Fri, 4 Mar 2016 15:36:18 -0800
Subject: [PATCH 2/2] Replace PREINIT_FUNCTION@PLT with *%rax in call

Since we have loaded address of PREINIT_FUNCTION into %rax, we can
avoid extra branch to PLT slot.

	[BZ #19745]
	* sysdeps/x86_64/crti.S (_init): Replace PREINIT_FUNCTION@PLT
	with *%rax in call.
---
 ChangeLog             | 6 ++++++
 sysdeps/x86_64/crti.S | 2 +-
 2 files changed, 7 insertions(+), 1 deletion(-)

diff --git a/ChangeLog b/ChangeLog
index af55529..f5b8d1e 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,6 +1,12 @@ 
 2016-03-04  H.J. Lu  <hongjiu.lu@intel.com>
 
 	[BZ #19745]
+	* sysdeps/x86_64/crti.S (_init): Replace PREINIT_FUNCTION@PLT
+	with *%rax in call.
+
+2016-03-04  H.J. Lu  <hongjiu.lu@intel.com>
+
+	[BZ #19745]
 	* sysdeps/x86_64/start.S (_start): Replace __libc_start_main@PLT
 	with *__libc_start_main@GOTPCREL(%rip) in call.
 
diff --git a/sysdeps/x86_64/crti.S b/sysdeps/x86_64/crti.S
index a345259..e9d86ed 100644
--- a/sysdeps/x86_64/crti.S
+++ b/sysdeps/x86_64/crti.S
@@ -66,7 +66,7 @@  _init:
 	movq PREINIT_FUNCTION@GOTPCREL(%rip), %rax
 	testq %rax, %rax
 	je .Lno_weak_fn
-	call PREINIT_FUNCTION@PLT
+	call *%rax
 .Lno_weak_fn:
 #else
 	call PREINIT_FUNCTION
-- 
2.5.0