Stack Alignment Bug

David Ronis ronis@ronispc.chem.mcgill.ca
Wed Nov 8 09:27:00 GMT 2000


I've managed to come up with a trivial example of the stack alignment
problem Steve Johnson pointed out some time ago.  

With the following code:

______________________________________________
int main(void)
{
  double s;
  if((((unsigned long) &s) & 0x7L)) {
          printf("main (%u)\n",((unsigned long)&s)&0x7L);
  }
  return 0;
}
-------------------------------------------------

I compile on an i686-linux-gnu(2.1.3) box using this weeks snapshot
(gcc-2.97 20001106) using 

-O0 -mpreferred-stack-boundary=X with X=2,3,4

I get 

main (4)

In all cases.  Perhaps more interesting is the fact that when I add
-save-temps, the resulting assembly code is independant of which
alignment I request (note the .align 16 )

-----------------------------------------------------------
	.file	"stack4.i"
	.version	"01.01"
gcc2_compiled.:
	.section	.rodata
.LC0:
	.string	"main (%u)\n"
	.text
	.align 16
.globl main
	.type	main,@function
main:
	pushl	%ebp
	movl	%esp, %ebp
	subl	$8, %esp
	leal	-8(%ebp), %eax
	andl	$7, %eax
	testl	%eax, %eax
	je	.L3
	leal	-8(%ebp), %eax
	andl	$7, %eax
	pushl	%eax
	pushl	$.LC0
	call	printf
	addl	$8, %esp
.L3:
	movl	$0, %eax
	movl	%ebp, %esp
	popl	%ebp
	ret
.Lfe1:
	.size	main,.Lfe1-main
	.ident	"GCC: (GNU) 2.97 20001106 (experimental)"

--------------------------------------------------------------

Could someone explain what's going on?

David


More information about the Gcc-bugs mailing list