Re: [PATCH v1 1/1] tools/nolibc: i386: Fix a stack misalign bug on _start

From: Ammar Faizi
Date: Sat Aug 26 2023 - 11:56:16 EST


On Sat, Aug 26, 2023 at 11:07:04PM +0800, Zhangjin Wu wrote:
> > @@ -167,7 +167,8 @@ void __attribute__((weak, noreturn, optimize("Os", "omit-frame-pointer"))) __no_
> > __asm__ volatile (
> > "xor %ebp, %ebp\n" /* zero the stack frame */
> > "mov %esp, %eax\n" /* save stack pointer to %eax, as arg1 of _start_c */
> > - "and $-16, %esp\n" /* last pushed argument must be 16-byte aligned */
> > + "and $-16, %esp\n" /* align stack to 16 bytes */
> > + "sub $12, %esp\n" /* last pushed argument must be 16-byte aligned */
>
> Ammar, the new call of _start_c() only requires a single push, it pushes
> the argument and minus %esp by 4, so, the alignment of %esp requires to
> minus 12 to reserve 16-byte alignment, is this description right?

Yes, that's correct.

> If so, What about further?
>
> "and $-16, %esp\n" /* align stack to 16 bytes */
> "sub ($16 - $4), %esp" /* the 'push %eax' breaks stack alignment, fix up it */

The sub part should have been:

"sub $(16 - 4), %esp\n"

It's fine to me writing it that way too.

--
Ammar Faizi