Stack alignment issues

Discussion in 'Windows 64bit' started by jacob navia, Dec 18, 2005.

  1. jacob navia

    jacob navia Guest

    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, Dec 18, 2005
    #1
    1. Advertising

  2. Did you search the Microsoft knowledge base?
    You might start here:
    http://msdn.microsoft.com/library/d..._7179a08c-8baa-44f5-a33d-8660a324a57d.xml.asp

    --
    Colin Barnhorst [MVP Windows - Virtual Machine]
    (Reply to the group only unless otherwise requested)
    "jacob navia" <> wrote in message
    news:...
    > 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, Dec 18, 2005
    #2
    1. Advertising

  3. jacob navia

    jacob navia Guest

    Colin Barnhorst a écrit :
    > Did you search the Microsoft knowledge base?
    > You might start here:
    > http://msdn.microsoft.com/library/d..._7179a08c-8baa-44f5-a33d-8660a324a57d.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
    jacob navia, Dec 19, 2005
    #3
  4. 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" <> wrote in message
    news:%...
    > Colin Barnhorst a écrit :
    >> Did you search the Microsoft knowledge base?
    >> You might start here:
    >> http://msdn.microsoft.com/library/d..._7179a08c-8baa-44f5-a33d-8660a324a57d.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], Dec 19, 2005
    #4
  5. jacob navia

    jorgon Guest

    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
    jorgon, Dec 31, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?U3RlcGhlbiBJbmtwZW4=?=

    Connectivity issues with Bluetooth Stack on PC

    =?Utf-8?B?U3RlcGhlbiBJbmtwZW4=?=, Jul 6, 2005, in forum: Wireless Networking
    Replies:
    0
    Views:
    515
    =?Utf-8?B?U3RlcGhlbiBJbmtwZW4=?=
    Jul 6, 2005
  2. Melv

    epson print head alignment

    Melv, Aug 19, 2003, in forum: Computer Support
    Replies:
    1
    Views:
    7,110
    Patrick
    Aug 19, 2003
  3. rte
    Replies:
    0
    Views:
    1,319
  4. JC
    Replies:
    8
    Views:
    1,205
  5. =?Utf-8?B?SmVyZW15IEdvcmRvbg==?=

    16-byte stack alignment - is it really necessary?

    =?Utf-8?B?SmVyZW15IEdvcmRvbg==?=, Aug 1, 2005, in forum: Windows 64bit
    Replies:
    5
    Views:
    1,904
    =?Utf-8?B?SmVyZW15IEdvcmRvbg==?=
    Aug 20, 2005
Loading...

Share This Page