[1/3] Replace BZERO_P/PIC with USE_AS_BZERO/SHARED

Message ID 20150826134331.GA19484@gmail.com
State Committed
Headers

Commit Message

H.J. Lu Aug. 26, 2015, 1:43 p.m. UTC
  Replace BZERO_P with USE_AS_BZERO in i586/i686 memset.S to support i386
multi-arch memset.  Also we should check SHARED not PIC for libc.so
since libc.a may be compiled with PIC.

OK for master?

H.J.
---
	* sysdeps/i386/i586/bzero.S (USE_AS_BZERO): New.
	* sysdeps/i386/i686/bzero.S (USE_AS_BZERO): Likewise.
	* sysdeps/i386/i586/memset.S (BZERO_P): Removed.
	Check USE_AS_BZERO/SHARED instead of BZERO_P/PIC.
	(__memset_zero_constant_len_parameter): New.
	* sysdeps/i386/i686/memset.S (BZERO_P): Removed.
	Check USE_AS_BZERO/SHARED instead of BZERO_P/PIC.
	(__memset_zero_constant_len_parameter): Don't define if
	__memset_chk or USE_AS_BZERO are defined.
---
 sysdeps/i386/i586/bzero.S  |  1 +
 sysdeps/i386/i586/memset.S | 22 +++++++++++-----------
 sysdeps/i386/i686/bzero.S  |  1 +
 sysdeps/i386/i686/memset.S | 20 +++++++-------------
 4 files changed, 20 insertions(+), 24 deletions(-)
  

Comments

Ondrej Bilka Aug. 26, 2015, 1:59 p.m. UTC | #1
On Wed, Aug 26, 2015 at 06:43:31AM -0700, H.J. Lu wrote:
> Replace BZERO_P with USE_AS_BZERO in i586/i686 memset.S to support i386
> multi-arch memset.  Also we should check SHARED not PIC for libc.so
> since libc.a may be compiled with PIC.
> 
> OK for master?
> 
This is ok as patch.

However as I noted in x64 memset this is ineffective as it doubles code
size for mostly identical code. So as followup one needs to create bzero
stub that jumps into memset. Should I do it?
  
H.J. Lu Aug. 26, 2015, 2:38 p.m. UTC | #2
On Wed, Aug 26, 2015 at 6:59 AM, Ondřej Bílka <neleai@seznam.cz> wrote:
> On Wed, Aug 26, 2015 at 06:43:31AM -0700, H.J. Lu wrote:
>> Replace BZERO_P with USE_AS_BZERO in i586/i686 memset.S to support i386
>> multi-arch memset.  Also we should check SHARED not PIC for libc.so
>> since libc.a may be compiled with PIC.
>>
>> OK for master?
>>
> This is ok as patch.
>
> However as I noted in x64 memset this is ineffective as it doubles code
> size for mostly identical code. So as followup one needs to create bzero
> stub that jumps into memset. Should I do it?

IA32 doesn't pass argument in registers.   Can we have a second
entry point in memset for bzero?
  
Ondrej Bilka Aug. 26, 2015, 2:52 p.m. UTC | #3
On Wed, Aug 26, 2015 at 07:38:53AM -0700, H.J. Lu wrote:
> On Wed, Aug 26, 2015 at 6:59 AM, Ondřej Bílka <neleai@seznam.cz> wrote:
> > On Wed, Aug 26, 2015 at 06:43:31AM -0700, H.J. Lu wrote:
> >> Replace BZERO_P with USE_AS_BZERO in i586/i686 memset.S to support i386
> >> multi-arch memset.  Also we should check SHARED not PIC for libc.so
> >> since libc.a may be compiled with PIC.
> >>
> >> OK for master?
> >>
> > This is ok as patch.
> >
> > However as I noted in x64 memset this is ineffective as it doubles code
> > size for mostly identical code. So as followup one needs to create bzero
> > stub that jumps into memset. Should I do it?
> 
> IA32 doesn't pass argument in registers.   Can we have a second
> entry point in memset for bzero?
> 
Yes, that is what I wanted.
  

Patch

diff --git a/sysdeps/i386/i586/bzero.S b/sysdeps/i386/i586/bzero.S
index 84d2f70..2a10671 100644
--- a/sysdeps/i386/i586/bzero.S
+++ b/sysdeps/i386/i586/bzero.S
@@ -1,3 +1,4 @@ 
+#define USE_AS_BZERO
 #define memset __bzero
 #include <sysdeps/i386/i586/memset.S>
 weak_alias (__bzero, bzero)
diff --git a/sysdeps/i386/i586/memset.S b/sysdeps/i386/i586/memset.S
index bc26501..82f7878 100644
--- a/sysdeps/i386/i586/memset.S
+++ b/sysdeps/i386/i586/memset.S
@@ -21,13 +21,10 @@ 
 #include <sysdep.h>
 #include "asm-syntax.h"
 
-/* BEWARE: `#ifdef memset' means that memset is redefined as `bzero' */
-#define BZERO_P (defined memset)
-
 #define PARMS	4+4	/* space for 1 saved reg */
 #define RTN	PARMS
 #define DEST	RTN
-#if BZERO_P
+#ifdef USE_AS_BZERO
 # define LEN	DEST+4
 #else
 # define CHR	DEST+4
@@ -35,7 +32,7 @@ 
 #endif
 
         .text
-#if defined PIC && IS_IN (libc) && !BZERO_P
+#if defined SHARED && IS_IN (libc) && !defined USE_AS_BZERO
 ENTRY (__memset_chk)
 	movl	12(%esp), %eax
 	cmpl	%eax, 16(%esp)
@@ -50,7 +47,7 @@  ENTRY (memset)
 	movl	DEST(%esp), %edi
 	cfi_rel_offset (edi, 0)
 	movl	LEN(%esp), %edx
-#if BZERO_P
+#ifdef USE_AS_BZERO
 	xorl	%eax, %eax	/* we fill with 0 */
 #else
 	movb	CHR(%esp), %al
@@ -104,7 +101,7 @@  L(2):	shrl	$2, %ecx	/* convert byte count to longword count */
 	rep
 	stosb
 
-#if !BZERO_P
+#ifndef USE_AS_BZERO
 	/* Load result (only if used as memset).  */
 	movl DEST(%esp), %eax	/* start address of destination is result */
 #endif
@@ -112,10 +109,13 @@  L(2):	shrl	$2, %ecx	/* convert byte count to longword count */
 	cfi_adjust_cfa_offset (-4)
 	cfi_restore (edi)
 
-#if BZERO_P
-	ret
-#else
 	ret
-#endif
 END (memset)
 libc_hidden_builtin_def (memset)
+
+#if defined SHARED && IS_IN (libc) && !defined __memset_chk \
+    && !defined USE_AS_BZERO
+strong_alias (__memset_chk, __memset_zero_constant_len_parameter)
+	.section .gnu.warning.__memset_zero_constant_len_parameter
+	.string "memset used with constant zero length parameter; this could be due to transposed parameters"
+#endif
diff --git a/sysdeps/i386/i686/bzero.S b/sysdeps/i386/i686/bzero.S
index 34b0faa..c7898f1 100644
--- a/sysdeps/i386/i686/bzero.S
+++ b/sysdeps/i386/i686/bzero.S
@@ -1,3 +1,4 @@ 
+#define USE_AS_BZERO
 #define memset __bzero
 #include <sysdeps/i386/i686/memset.S>
 weak_alias (__bzero, bzero)
diff --git a/sysdeps/i386/i686/memset.S b/sysdeps/i386/i686/memset.S
index b6dbf2a..86c3010 100644
--- a/sysdeps/i386/i686/memset.S
+++ b/sysdeps/i386/i686/memset.S
@@ -21,11 +21,8 @@ 
 #include <sysdep.h>
 #include "asm-syntax.h"
 
-/* BEWARE: `#ifdef memset' means that memset is redefined as `bzero' */
-#define BZERO_P (defined memset)
-
 #define PARMS	4+4	/* space for 1 saved reg */
-#if BZERO_P
+#ifdef USE_AS_BZERO
 # define DEST	PARMS
 # define LEN	DEST+4
 #else
@@ -36,7 +33,7 @@ 
 #endif
 
         .text
-#if defined PIC && IS_IN (libc) && !BZERO_P
+#if defined SHARED && IS_IN (libc) && !defined USE_AS_BZERO
 ENTRY_CHK (__memset_chk)
 	movl	12(%esp), %eax
 	cmpl	%eax, 16(%esp)
@@ -50,7 +47,7 @@  ENTRY (memset)
 	cfi_adjust_cfa_offset (4)
 	movl	DEST(%esp), %edx
 	movl	LEN(%esp), %ecx
-#if BZERO_P
+#ifdef USE_AS_BZERO
 	xorl	%eax, %eax	/* fill with 0 */
 #else
 	movzbl	CHR(%esp), %eax
@@ -74,7 +71,7 @@  ENTRY (memset)
 2:	movl	%ecx, %edx
 	shrl	$2, %ecx
 	andl	$3, %edx
-#if !BZERO_P
+#ifndef USE_AS_BZERO
 	imul	$0x01010101, %eax
 #endif
 	rep
@@ -84,22 +81,19 @@  ENTRY (memset)
 	stosb
 
 1:
-#if !BZERO_P
+#ifndef USE_AS_BZERO
 	movl DEST(%esp), %eax	/* start address of destination is result */
 #endif
 	popl	%edi
 	cfi_adjust_cfa_offset (-4)
 	cfi_restore (edi)
 
-#if BZERO_P
-	ret
-#else
 	ret
-#endif
 END (memset)
 libc_hidden_builtin_def (memset)
 
-#if defined PIC && IS_IN (libc) && !BZERO_P
+#if defined SHARED && IS_IN (libc) && !defined __memset_chk \
+    && !defined USE_AS_BZERO
 strong_alias (__memset_chk, __memset_zero_constant_len_parameter)
 	.section .gnu.warning.__memset_zero_constant_len_parameter
 	.string "memset used with constant zero length parameter; this could be due to transposed parameters"