[patch] libc: Do not export .cerror

Jilles Tjoelker jilles at stack.nl
Fri Aug 31 22:48:56 UTC 2012


On Tue, Aug 28, 2012 at 02:03:22PM +0300, Konstantin Belousov wrote:
> On Sat, Aug 25, 2012 at 12:16:55AM +0200, Jilles Tjoelker wrote:
> > Not exporting .cerror causes it to be jumped to directly instead of via
> > the PLT.

> > The below patch is for i386 only and also takes advantage of .cerror's
> > new status by not saving and loading %ebx before jumping to it.
> > (Therefore, .cerror now saves and loads %ebx itself.) Where there was a
> > conditional jump to a jump to .cerror, the conditional jump has been
> > changed to jump to .cerror directly (many modern CPUs don't do static
> > prediction and in any case it is not much of a benefit anyway).

> Why do you need to save/restore the %ebx at all ? %ebx ==
> &__GLOBAL_OFFSET_TABLE__ is only needed when you access GOT, but .cerror
> only works with PLT, which is addressed using the instruction capable of
> relative addressing. The old .cerror does not need it as well, but it is
> just engraved in the function ABI.

On i386, a shared object's PLT entry needs %ebx set up to work properly.
This is because such a PLT entry needs to access the GOT to find the
address to jump to (the first instruction is jmp *d32(%ebx)).

An executable's PLT entry accesses the GOT via absolute addressing and
therefore does not need %ebx.

> > The patch decreases the size of libc.so.7 by a few kilobytes.

> > Similar changes could be made to other architectures, and there may be
> > more symbols that are exported but need not be.
> Sure, would you handle at least amd64 too ?

The below patch handles amd64.

I'm a bit annoyed that most of the syscall stubs are 17 bytes long now
and have the maximum 15 bytes of padding. This means that the patch
provides virtually no gain in code size.

Index: lib/libc/amd64/Symbol.map
===================================================================
--- lib/libc/amd64/Symbol.map	(revision 239865)
+++ lib/libc/amd64/Symbol.map	(working copy)
@@ -66,7 +66,6 @@
 	.curbrk;
 	.minbrk;
 	_brk;
-	.cerror;
 	_end;
 	__sys_vfork;
 	_vfork;
Index: lib/libc/amd64/SYS.h
===================================================================
--- lib/libc/amd64/SYS.h	(revision 239865)
+++ lib/libc/amd64/SYS.h	(working copy)
@@ -36,38 +36,20 @@
 #include <sys/syscall.h>
 #include <machine/asm.h>
 
-#ifdef PIC
 #define	RSYSCALL(x)	ENTRY(__CONCAT(__sys_,x));			\
 			.weak CNAME(x);					\
 			.set CNAME(x),CNAME(__CONCAT(__sys_,x));	\
 			.weak CNAME(__CONCAT(_,x));			\
 			.set CNAME(__CONCAT(_,x)),CNAME(__CONCAT(__sys_,x)); \
-			mov __CONCAT($SYS_,x),%rax; KERNCALL; jb 2f; ret; \
-			2: movq PIC_GOT(HIDENAME(cerror)),%rcx; jmp *%rcx; \
+			mov __CONCAT($SYS_,x),%eax; KERNCALL;		\
+			jb HIDENAME(cerror); ret;			\
 			END(__CONCAT(__sys_,x))
 
 #define	PSEUDO(x)	ENTRY(__CONCAT(__sys_,x));			\
 			.weak CNAME(__CONCAT(_,x));			\
 			.set CNAME(__CONCAT(_,x)),CNAME(__CONCAT(__sys_,x)); \
-			mov __CONCAT($SYS_,x),%rax; KERNCALL; jb 2f; ret ; \
-			2: movq PIC_GOT(HIDENAME(cerror)),%rcx; jmp *%rcx; \
+			mov __CONCAT($SYS_,x),%eax; KERNCALL;		\
+			jb HIDENAME(cerror); ret;			\
 			END(__CONCAT(__sys_,x))
-#else
-#define	RSYSCALL(x)	ENTRY(__CONCAT(__sys_,x));			\
-			.weak CNAME(x);					\
-			.set CNAME(x),CNAME(__CONCAT(__sys_,x));	\
-			.weak CNAME(__CONCAT(_,x));			\
-			.set CNAME(__CONCAT(_,x)),CNAME(__CONCAT(__sys_,x)); \
-			mov __CONCAT($SYS_,x),%rax; KERNCALL; jb 2f; ret; \
-			2: jmp HIDENAME(cerror);			\
-			END(__CONCAT(__sys_,x))
 
-#define	PSEUDO(x)	ENTRY(__CONCAT(__sys_,x));			\
-			.weak CNAME(__CONCAT(_,x));			\
-			.set CNAME(__CONCAT(_,x)),CNAME(__CONCAT(__sys_,x)); \
-			mov __CONCAT($SYS_,x),%rax; KERNCALL; jb 2f; ret; \
-			2: jmp HIDENAME(cerror);			\
-			END(__CONCAT(__sys_,x))
-#endif
-
 #define KERNCALL	movq %rcx, %r10; syscall
Index: lib/libc/amd64/gen/rfork_thread.S
===================================================================
--- lib/libc/amd64/gen/rfork_thread.S	(revision 239865)
+++ lib/libc/amd64/gen/rfork_thread.S	(working copy)
@@ -93,12 +93,7 @@
 2:
 	popq	%r12
 	popq	%rbx
-#ifdef PIC
-	movq	PIC_GOT(HIDENAME(cerror)), %rdx
-	jmp	*%rdx
-#else
 	jmp	HIDENAME(cerror)
-#endif
 END(rfork_thread)
 
 	.section .note.GNU-stack,"",%progbits
Index: lib/libc/amd64/sys/brk.S
===================================================================
--- lib/libc/amd64/sys/brk.S	(revision 239865)
+++ lib/libc/amd64/sys/brk.S	(working copy)
@@ -76,12 +76,7 @@
 	ret
 err:
 	addq	$8, %rsp
-#ifdef PIC
-	movq	PIC_GOT(HIDENAME(cerror)),%rdx
-	jmp	*%rdx
-#else
 	jmp	HIDENAME(cerror)
-#endif
 END(brk)
 
 	.section .note.GNU-stack,"",%progbits
Index: lib/libc/amd64/sys/getcontext.S
===================================================================
--- lib/libc/amd64/sys/getcontext.S	(revision 239865)
+++ lib/libc/amd64/sys/getcontext.S	(working copy)
@@ -42,16 +42,9 @@
 	movq	(%rsp),%rsi	/* save getcontext return address */
 	mov	$SYS_getcontext,%rax
 	KERNCALL
-	jb	1f
+	jb	HIDENAME(cerror)
 	addq	$8,%rsp		/* remove stale (setcontext) return address */
 	jmp	*%rsi		/* restore return address */
-1:
-#ifdef PIC
-	movq	PIC_GOT(HIDENAME(cerror)),%rdx
-	jmp	*%rdx
-#else
-	jmp	HIDENAME(cerror)
-#endif
 END(__sys_getcontext)
 
 	.section .note.GNU-stack,"",%progbits
Index: lib/libc/amd64/sys/setlogin.S
===================================================================
--- lib/libc/amd64/sys/setlogin.S	(revision 239865)
+++ lib/libc/amd64/sys/setlogin.S	(working copy)
@@ -47,7 +47,7 @@
 ENTRY(__sys_setlogin)
 	mov	$SYS_setlogin,%rax
 	KERNCALL
-	jb	1f
+	jb	HIDENAME(cerror)
 #ifdef PIC
 	movq	PIC_GOT(CNAME(_logname_valid)),%rdx
 	movl	$0,(%rdx)
@@ -55,13 +55,6 @@
 	movl	$0,CNAME(_logname_valid)(%rip)
 #endif
 	ret				/* setlogin(name) */
-1:
-#ifdef PIC
-	movq	PIC_GOT(HIDENAME(cerror)),%rdx
-	jmp	*%rdx
-#else
-	jmp	HIDENAME(cerror)
-#endif
 END(__sys_setlogin)
 
 	.section .note.GNU-stack,"",%progbits
Index: lib/libc/amd64/sys/sbrk.S
===================================================================
--- lib/libc/amd64/sys/sbrk.S	(revision 239865)
+++ lib/libc/amd64/sys/sbrk.S	(working copy)
@@ -79,12 +79,7 @@
 	ret
 err:
 	addq	$8, %rsp
-#ifdef PIC
-	movq	PIC_GOT(HIDENAME(cerror)),%rdx
-	jmp	*%rdx
-#else
 	jmp	HIDENAME(cerror)
-#endif
 END(sbrk)
 
 	.section .note.GNU-stack,"",%progbits
Index: lib/libc/amd64/sys/ptrace.S
===================================================================
--- lib/libc/amd64/sys/ptrace.S	(revision 239865)
+++ lib/libc/amd64/sys/ptrace.S	(working copy)
@@ -48,15 +48,8 @@
 #endif
 	mov	$SYS_ptrace,%eax
 	KERNCALL
-	jb	err
+	jb	HIDENAME(cerror)
 	ret
-err:
-#ifdef PIC
-	movq	PIC_GOT(HIDENAME(cerror)),%rdx
-	jmp	*%rdx
-#else
-	jmp	HIDENAME(cerror)
-#endif
 END(ptrace)
 
 	.section .note.GNU-stack,"",%progbits
Index: lib/libc/amd64/sys/vfork.S
===================================================================
--- lib/libc/amd64/sys/vfork.S	(revision 239865)
+++ lib/libc/amd64/sys/vfork.S	(working copy)
@@ -50,12 +50,7 @@
 	jmp	*%rsi
 1:
 	pushq	%rsi
-#ifdef PIC
-	movq	PIC_GOT(HIDENAME(cerror)),%rdx
-	jmp	*%rdx
-#else
 	jmp	HIDENAME(cerror)
-#endif
 END(__sys_vfork)
 
 	.section .note.GNU-stack,"",%progbits
Index: lib/libc/amd64/sys/reboot.S
===================================================================
--- lib/libc/amd64/sys/reboot.S	(revision 239865)
+++ lib/libc/amd64/sys/reboot.S	(working copy)
@@ -45,15 +45,8 @@
 ENTRY(__sys_reboot)
 	mov	$SYS_reboot,%rax
 	KERNCALL
-	jb	1f
+	jb	HIDENAME(cerror)
 	iretq
-1:
-#ifdef PIC
-	movq	PIC_GOT(HIDENAME(cerror)),%rdx
-	jmp	*%rdx
-#else
-	jmp	HIDENAME(cerror)
-#endif
 END(__sys_reboot)
 
 	.section .note.GNU-stack,"",%progbits
Index: lib/libc/amd64/sys/pipe.S
===================================================================
--- lib/libc/amd64/sys/pipe.S	(revision 239865)
+++ lib/libc/amd64/sys/pipe.S	(working copy)
@@ -45,18 +45,11 @@
 ENTRY(__sys_pipe)
 	mov	$SYS_pipe,%rax
 	KERNCALL
-	jb	1f
+	jb	HIDENAME(cerror)
 	movl	%eax,(%rdi)	/* %rdi is preserved by syscall */
 	movl	%edx,4(%rdi)
 	movq	$0,%rax
 	ret
-1:
-#ifdef PIC
-	movq	PIC_GOT(HIDENAME(cerror)),%rdx
-	jmp	*%rdx
-#else
-	jmp	HIDENAME(cerror)
-#endif
 END(__sys_pipe)
 
 	.section .note.GNU-stack,"",%progbits
Index: lib/libc/amd64/sys/exect.S
===================================================================
--- lib/libc/amd64/sys/exect.S	(revision 239865)
+++ lib/libc/amd64/sys/exect.S	(working copy)
@@ -47,12 +47,7 @@
 	pushq	%r8
 	popfq
 	KERNCALL
-#ifdef PIC
-	movq	PIC_GOT(HIDENAME(cerror)),%rdx
-	jmp	*%rdx
-#else
 	jmp	HIDENAME(cerror)
-#endif
 END(exect)
 
 	.section .note.GNU-stack,"",%progbits

-- 
Jilles Tjoelker


More information about the freebsd-hackers mailing list