x64 calling convention (stack) and varargs
I've read Microsoft's documentation, but the scheme is so awkward, I thought I'd double-check to make sure I'm understanding it correctly...
My understanding is the generic method by which parameters are passed is this:
--- bottom of stack --- (return address) [shadow space for arg 1] [shadow space for arg 2] [shadow space for arg 3] [shadow space for arg 4] arg N arg N - 1 arg N - 2 ... arg 6 arg 5 ---- top of stack -----
It seems so awkward when implementing va_arg and such... is this actually correct?
The correct diagram is
--- Bottom of stack --- RSP + size (higher addresses) arg N arg N - 1 arg N - 2 ... arg 6 arg 5 [shadow space for arg 4] [shadow space for arg 3] [shadow space for arg 2] [shadow space for arg 1] (return address) ---- Top of stack ----- RSP (lower addresses) [grows downward]
The return address is at the top of the stack (most recently pushed), followed by shadow space for the first four parameters, followed by parameters 5 and onward.
The parameters are pushed right to left: The last parameter (N) is pushed first, so it is closest to the bottom of the stack.