Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Windows 64bit (http://www.velocityreviews.com/forums/f46-windows-64bit.html)
-   -   Stack alignment issues (http://www.velocityreviews.com/forums/t490394-stack-alignment-issues.html)

jacob navia 12-18-2005 09:04 PM

Stack alignment issues
 
Question:
I need to know the stack alignment requirements for
windows 64 bits.

Context:
I am writing a C compiler for windows 64 bits.
I have seen that the stack must be aligned in a 16 byte
boundary, but as it seems, there is an issue with
the return address.

1: Can I assume that when a function is called, the
stack is aligned in a 16 byte boundary? This is *before*
the function executes any instruction, i.e. RIGHT AFTER
the CALL instruction.

2: Or should I assume that the stack is NOT aligned in a 16 byte
boundary but doing a "push rbp" will align it?

I have been using solution (2) with some success. Normal callbacks
seem to function OK, but *some* functions just trap, for instance
SHAutoComplete. What is the *definitive* answer?

I would love to see a piece of documentation concerning this...
I discovered this alignment requirement by trial and error. I have
yet to see any documentation regarding this.

Second question:
When calling functions I leave systematically 32 bytes free for the
called function to store the arguments. Suppose however, that the
called function needs only 16 (it receives just two integers). Can
I just leave 16 bytes, or should I always leave 32???

For instance:
int fn(int a,int b);
fn(2,3)

I generate now:

movq 3,%rdx (put second arg in rdx)
movq 2,%rcx (put first argument in rcx)
subq $32,%rsp (allocate 32 bytes of stack for the called fn)
call fn
addq $32,%rsp (adjust the stack)

This is a simplified view, of course I do not subtract the 32 bytes at
each function call but that is an optimization that doesn't change
this general pattern.
The question is: Can I do a subq $16,%rsp instead of 32?

Thanks in advance for your time.

jacob

Colin Barnhorst 12-18-2005 10:18 PM

Re: Stack alignment issues
 
Did you search the Microsoft knowledge base?
You might start here:
http://msdn.microsoft.com/library/de...24a57d.xml.asp

--
Colin Barnhorst [MVP Windows - Virtual Machine]
(Reply to the group only unless otherwise requested)
"jacob navia" <jacob@jacob.remcomp.fr> wrote in message
news:OzeWyaBBGHA.4092@TK2MSFTNGP09.phx.gbl...
> Question:
> I need to know the stack alignment requirements for
> windows 64 bits.
>
> Context:
> I am writing a C compiler for windows 64 bits.
> I have seen that the stack must be aligned in a 16 byte
> boundary, but as it seems, there is an issue with
> the return address.
>
> 1: Can I assume that when a function is called, the
> stack is aligned in a 16 byte boundary? This is *before*
> the function executes any instruction, i.e. RIGHT AFTER
> the CALL instruction.
>
> 2: Or should I assume that the stack is NOT aligned in a 16 byte
> boundary but doing a "push rbp" will align it?
>
> I have been using solution (2) with some success. Normal callbacks
> seem to function OK, but *some* functions just trap, for instance
> SHAutoComplete. What is the *definitive* answer?
>
> I would love to see a piece of documentation concerning this...
> I discovered this alignment requirement by trial and error. I have
> yet to see any documentation regarding this.
>
> Second question:
> When calling functions I leave systematically 32 bytes free for the
> called function to store the arguments. Suppose however, that the
> called function needs only 16 (it receives just two integers). Can
> I just leave 16 bytes, or should I always leave 32???
>
> For instance:
> int fn(int a,int b);
> fn(2,3)
>
> I generate now:
>
> movq 3,%rdx (put second arg in rdx)
> movq 2,%rcx (put first argument in rcx)
> subq $32,%rsp (allocate 32 bytes of stack for the called fn)
> call fn
> addq $32,%rsp (adjust the stack)
>
> This is a simplified view, of course I do not subtract the 32 bytes at
> each function call but that is an optimization that doesn't change
> this general pattern.
> The question is: Can I do a subq $16,%rsp instead of 32?
>
> Thanks in advance for your time.
>
> jacob




jacob navia 12-19-2005 12:14 AM

Re: Stack alignment issues
 
Colin Barnhorst a écrit :
> Did you search the Microsoft knowledge base?
> You might start here:
> http://msdn.microsoft.com/library/de...24a57d.xml.asp
>

Yes, I know that of course. Nowhere in those pages is the stack alignment
requirement specified...

As I have found out, not all functions require 16 byte aligned stack.
Some will work, some others not, depending, probably, on whether the
function uses instructions that require 16 byte alignment.

The problem is to know if the stack should be aligned BEFORE
making the call instruction or afterward. Since the call instruction
pushed 8 bytes into the stack, this is quite important.

In any case thanks for your time.

Jacob

Andre Da Costa [Extended64] 12-19-2005 12:32 AM

Re: Stack alignment issues
 
This looks a question for an appropriate group:
microsoft.public.vc.language
--
Andre
Extended64 | http://www.extended64.com
Blog | http://www.extended64.com/blogs/andre
http://spaces.msn.com/members/adacosta
FAQ for MS AntiSpy http://www.geocities.com/marfer_mvp/FAQ_MSantispy.htm

"jacob navia" <jacob@jacob.remcomp.fr> wrote in message
news:%232mK9EDBGHA.628@TK2MSFTNGP10.phx.gbl...
> Colin Barnhorst a écrit :
>> Did you search the Microsoft knowledge base?
>> You might start here:
>> http://msdn.microsoft.com/library/de...24a57d.xml.asp
>>

> Yes, I know that of course. Nowhere in those pages is the stack alignment
> requirement specified...
>
> As I have found out, not all functions require 16 byte aligned stack.
> Some will work, some others not, depending, probably, on whether the
> function uses instructions that require 16 byte alignment.
>
> The problem is to know if the stack should be aligned BEFORE
> making the call instruction or afterward. Since the call instruction
> pushed 8 bytes into the stack, this is quite important.
>
> In any case thanks for your time.
>
> Jacob




jorgon 12-31-2005 07:40 PM

Re: Stack alignment issues
 

Jacob
> The problem is to know if the stack should be aligned BEFORE
> making the call instruction or afterward. Since the call instruction
> pushed 8 bytes into the stack, this is quite important.

I have found (in converting my assembler 'GoAsm'
(http://www.GoDevTool.com) to 64-bits) that the sequence should be:-

- Align the stack on a 16-byte boundary
- Push on the stack any parameters which need to be pushed (if more
than four)
- Add any other parameters to the registers as required
- Move the stack pointer to provide 32 bytes for the API to use as
placeholders
- Call the API
- Restore the stack to value it was before number 1 (this avoids
stack memory leakage)

I know the docs say that a "leaf" function shouldn't call anything, but
this is very restrictive. To avoid this, its probably necessary to
align the stack before each API call. The other potential problem I
have heard about is that wrong alignment might cause difficulties for
the exception handler. However, I believe there is a way round this,
if such a problem exists in the first place.


--
jorgon
Posted via http://ms-os.com Forum to Usenet gateway



All times are GMT. The time now is 07:17 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.