About the alignment of main

Kean Johnston jkj@sco.com
Sun Oct 16 03:43:00 GMT 2005


Ok, with the current patch I have to prevent sibcall optimization in 
main (the patch isn't final, but thats not important, the problem will 
be fixed somehow) I am now getting much better results from the 
testsuite. However, I am still running into problems when a function is 
inlined into main and it uses alignment sensitive functions.

The comment for 'FORCE_PREFERRED_STACK_BOUNDARY_IN_MAIN' clearly states 
that it will align the stack nicely for all calls from main, but does 
nothing for the alignment of main itself. I realize that I should 
probably fix this in the crt code such that main always enters aligned 
(as discussed previously here). However, I was wondering whether gcc 
couldn't help with even that, and actually align main itself (given some 
suitable target macro like TARGET_IS_LAME_AND_DOESNT_ALIGN_MAIN).

The way I would see this working is thus. gcc detects that it is dealing 
with the function "main".  If it does and T_I_L_A_D_A_M is set, it emits 
the code for aligning the stack as it currently does, then makes a call
to a local label, and does a normal return from main. So you go from
something like this:

main:
	pushl	%ebp
	movl	%esp, %ebp
	pushl	%edi
	push	%esi
	subl	$64, %esp
	andl	$-16, %esp	/ Stack now aligned
	.... normal main code here ....
	popl	%esi
	popl	%edi
	popl	%ebp
	ret

To something like this:
main:
	pushl	%ebp
	movl	%esp, %ebp
	pushl	%edi
	push	%esi
	subl	$64, %esp
	andl	$-16, %esp	/ stack now aligned
	movl	16(%ebp), %eax
	pushl	%eax		/ argc
	movl	12(%ebp), %eax
	pushl	%eax		/ argv
	movl	8(%ebp), %eax
	pushl	%eax		/ envp
	call	.temp_main_label
	addl	$12, %esp
	popl	%esi
	popl	%edi
	popl	%ebp
	ret

.temp_main_label:
	... rest of main code here ...

Intuitively, it seems like that would work. Comments? Suggestions?

Kean



More information about the Gcc mailing list