skydoor
skydoor

Reputation: 25868

what's on the stack when a function is called?

I can only imagine 1) parameters; 2) local variables;

what else?

1) function return address? 2) function name?

Upvotes: 2

Views: 472

Answers (3)

Norman Ramsey
Norman Ramsey

Reputation: 202505

It depends on the calling convention; for Unix, you typically look up this information in the SYSV ABI (Application Binary Interface).

You may find:

  • Return address (if the machine is a popular Intel architecture). On more modern architectures, the return address is passed in a register.

  • Callee-saves registers—these are registers that "belong" to the caller which the callee has chosen to borrow and must therefore save and restore.

  • Any incoming parameters that could not be passed in registers. In IA-32, no parameters are passed in registers; they all go on the stack. In x86-64, up to six integer and six floating-point parameters can be passed in registers, so it is seldom necessary to use the stack for that purpose.

  • You may or may not find a saved stack pointer or frame pointer. Most modern calling conventions go without a frame pointer in order to save an extra registers. In this design, the size of each frame is known at compile time, so restoring the old stack pointer is just a matter of adding a constant. But it makes it harder to implement alloca().

    The older Intel calling conventions use both stack pointer and frame pointer, which burns an extra register, but it simplifies alloca() and also stack unwinding.

  • Local variables with storage class auto are allocated on the stack.

  • The stack may contain compiler temporaries that hold values which are "spilled" if the hardware does not provide enough registers to hold all the intermediate results of computations. (This happens if at any point the number of live intermediate results—the ones that will be needed later in the program—exceeds the number of registers available to the compiler for storing intermediate results.)

  • You may find variables allocated with alloca().

  • You may find metadata that says which PC ranges are in scope for which exception handlers, or other very platform-dependent exception stuff.

  • C and C++ do not support garbage collection, but in a language that does, you will often find metadata that identifies where in the stack frame you will find pointers.

  • Finally, the stack may contain "padding" used to ensure that the stack pointer is aligned on an 8-byte or 16-byte boundary.

Calling conventions are complex beasts, and stack-frame layout is not for the faint of heart!

Upvotes: 2

Seva Alekseyev
Seva Alekseyev

Reputation: 61351

It really depends on platform and architecture, but typically:

  • Function return address
  • Saved values of caller's CPU registers - most importantly, caller's stack frame pointer value
  • Variables allocated with alloca().
  • Sometimes - extra stuff for exception handling, this is VERY platform-dependent.
  • Sometimes - guard values to detect stack clobbering

Function name is never in the stack, to the best of my knowledge, unless your code places it there.

Upvotes: 5

Jon
Jon

Reputation: 437376

I think that a picture really is a thousand words.

Upvotes: 2

Related Questions