From owner-freebsd-hackers@FreeBSD.ORG Fri Aug 31 22:48:56 2012 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 71321106566C for ; Fri, 31 Aug 2012 22:48:56 +0000 (UTC) (envelope-from jilles@stack.nl) Received: from mx1.stack.nl (unknown [IPv6:2001:610:1108:5012::107]) by mx1.freebsd.org (Postfix) with ESMTP id BA4F68FC0A for ; Fri, 31 Aug 2012 22:48:55 +0000 (UTC) Received: from snail.stack.nl (snail.stack.nl [IPv6:2001:610:1108:5010::131]) by mx1.stack.nl (Postfix) with ESMTP id 0C03D1203B1; Sat, 1 Sep 2012 00:48:51 +0200 (CEST) Received: by snail.stack.nl (Postfix, from userid 1677) id E5EE62847B; Sat, 1 Sep 2012 00:48:50 +0200 (CEST) Date: Sat, 1 Sep 2012 00:48:50 +0200 From: Jilles Tjoelker To: Konstantin Belousov Message-ID: <20120831224850.GA12423@stack.nl> References: <20120824221655.GA76607@stack.nl> <20120828110322.GJ33100@deviant.kiev.zoral.com.ua> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20120828110322.GJ33100@deviant.kiev.zoral.com.ua> User-Agent: Mutt/1.5.21 (2010-09-15) Cc: freebsd-hackers@freebsd.org Subject: Re: [patch] libc: Do not export .cerror X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 31 Aug 2012 22:48:56 -0000 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 #include -#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