Date: Wed, 25 Sep 2002 07:20:03 -0700 (PDT) From: Bruce Evans <bde@zeta.org.au> To: freebsd-bugs@FreeBSD.org Subject: Re: i386/41528: better stack alignment patch for lib/csu/i386-elf/ Message-ID: <200209251420.g8PEK35G074297@freefall.freebsd.org>
next in thread | raw e-mail | index | archive | help
The following reply was made to PR i386/41528; it has been noted by GNATS.
From: Bruce Evans <bde@zeta.org.au>
To: NIIMI Satoshi <sa2c@sa2c.net>
Cc: FreeBSD-gnats-submit@FreeBSD.ORG
Subject: Re: i386/41528: better stack alignment patch for lib/csu/i386-elf/
Date: Thu, 26 Sep 2002 00:28:06 +1000 (EST)
On 13 Aug 2002, NIIMI Satoshi wrote:
> Bruce Evans <bde@zeta.org.au> writes:
> > ...
> > I would only use this fix or one like it in RELENG_4. Maybe my kernel
> > hack is better since it "fixes" most applications without a recompile.
> > It is simpler because it doesn't use any assembly code or have to recover
> > from the kernel pushing the args in a misaligned place.
>
> Thanks. But is it possible? I attached a patch for -current so that
> it can be commited into -current then MFC'ed to -stable.
I just got around to preparing this for commit (hopefully just before 4.7),
and found a small problem. There seems to be an off-by-8 error.
Original patch:
% Index: stable/lib/csu/i386-elf/crt1.c
% ===================================================================
% RCS file: /home/ncvs/src/lib/csu/i386-elf/crt1.c,v
% retrieving revision 1.4.2.1
% diff -u -r1.4.2.1 crt1.c
% --- stable/lib/csu/i386-elf/crt1.c 30 Oct 2000 20:32:24 -0000 1.4.2.1
% +++ stable/lib/csu/i386-elf/crt1.c 10 Aug 2002 19:40:54 -0000
% @@ -93,7 +93,33 @@
% monstartup(&eprol, &etext);
% #endif
% _init();
% +#if 0
% exit( main(argc, argv, env) );
% +#else
% + /*
% + * GCC expects stack frame to be aligned like following figure.
% + *
% + * +--------------+
% + * |%ebp (if any) |
% + * +--------------+
% + * |return address|
% + * +--------------+ --- aligned by PREFERRED_STACK_BOUNDARY
% + * | arguments |
% + * | : |
% + * | : |
% + */
This is where gcc-3 wants the stack aligned, but gcc-2 apparently wants
it defined 8 bytes lower (higher in the diagram), after pushing %ebp.
I am now testing the following patch:
%%%
Index: crt1.c
===================================================================
RCS file: /home/ncvs/src/lib/csu/i386-elf/crt1.c,v
retrieving revision 1.9
diff -u -2 -r1.9 crt1.c
--- crt1.c 16 Jul 2002 12:28:49 -0000 1.9
+++ crt1.c 25 Sep 2002 14:23:24 -0000
@@ -101,5 +101,34 @@
#endif
_init();
+#ifndef __GNUC__
exit( main(argc, argv, env) );
+#else
+ /*
+ * gcc-2 expects the stack frame to be aligned as follows after it
+ * is set up in main():
+ *
+ * +--------------+ <--- aligned by PREFERRED_STACK_BOUNDARY
+ * +%ebp (if any) +
+ * +--------------+
+ * |return address|
+ * +--------------+
+ * | arguments |
+ * | : |
+ * | : |
+ * +--------------+
+ *
+ * The call must be written in assembler to implement this.
+ */
+ __asm__("
+ andl $~0xf, %%esp # align stack to 16-byte boundary
+ subl $12+12, %%esp # space for args and padding
+ movl %0, 0(%%esp)
+ movl %1, 4(%%esp)
+ movl %2, 8(%%esp)
+ call main
+ movl %%eax, 0(%%esp)
+ call exit
+ " : : "r" (argc), "r" (argv), "r" (env) : "ax", "cx", "dx", "memory");
+#endif
}
%%%
Bruce
To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-bugs" in the body of the message
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200209251420.g8PEK35G074297>
