Re: [RFC PATCH v2 3/8] tools/nolibc: i386: Implement syscall with 6 arguments

From: Willy Tarreau
Date: Tue Mar 22 2022 - 07:24:36 EST


On Tue, Mar 22, 2022 at 10:57:01AM +0000, David Laight wrote:
> From: Ammar Faizi
> > Sent: 22 March 2022 10:21
> >
> > On i386, the 6th argument of syscall goes in %ebp. However, both Clang
> > and GCC cannot use %ebp in the clobber list and in the "r" constraint
> > without using -fomit-frame-pointer. To make it always available for
> > any kind of compilation, the below workaround is implemented.
> >
> > For clang (the Assembly statement can't clobber %ebp):
> > 1) Push the 6-th argument.
> > 2) Push %ebp.
> > 3) Load the 6-th argument from 4(%esp) to %ebp.
> > 4) Do the syscall (int $0x80).
> > 5) Pop %ebp (restore the old value of %ebp).
> > 6) Add %esp by 4 (undo the stack pointer).
> >
> > For GCC, fortunately it has a #pragma that can force a specific function
> > to be compiled with -fomit-frame-pointer, so it can use "r"(var) where
> > var is a variable bound to %ebp.
>
> You need to use the 'clang' pattern for gcc.
> #pragma optimise is fundamentally broken.
> What actually happens here is the 'inline' gets lost
> (because of the implied -O0) and you get far worse code
> than you might expect.
>
> Since you need the 'clang' version, use it all the time.

I clearly prefer it as well, it looks much cleaner!

Willy