Frame Layout (GNU Compiler Collection (GCC) Internals)

#### 19.9.1 Basic Stack Layout ¶

Here is the basic stack layout.

Macro: STACK_GROWS_DOWNWARD ¶

  Define this macro to be true if pushing a word onto the stack moves the stack pointer to a smaller address, and false otherwise.

Macro: STACK_PUSH_CODE ¶

  This macro defines the operation used when something is pushed on the stack. In RTL, a push operation will be `(set (mem (STACK_PUSH_CODE (reg sp))) …)`

  The choices are `PRE_DEC`, `POST_DEC`, `PRE_INC`, and `POST_INC`. Which of these is correct depends on the stack direction and on whether the stack pointer points to the last item on the stack or whether it points to the space for the next item on the stack.

  The default is `PRE_DEC` when `STACK_GROWS_DOWNWARD` is true, which is almost always right, and `PRE_INC` otherwise, which is often wrong.

Macro: FRAME_GROWS_DOWNWARD ¶

  Define this macro to nonzero value if the addresses of local variable slots are at negative offsets from the frame pointer.

Macro: ARGS_GROW_DOWNWARD ¶

  Define this macro if successive arguments to a function occupy decreasing addresses on the stack.

Target Hook: `HOST_WIDE_INT` TARGET_STARTING_FRAME_OFFSET `(void)` ¶

  This hook returns the offset from the frame pointer to the first local variable slot to be allocated. If `FRAME_GROWS_DOWNWARD`, it is the offset to end of the first slot allocated, otherwise it is the offset to beginning of the first slot allocated. The default implementation returns 0.

Macro: STACK_ALIGNMENT_NEEDED ¶

  Define to zero to disable final alignment of the stack during reload. The nonzero default for this macro is suitable for most ports.

  On ports where `TARGET_STARTING_FRAME_OFFSET` is nonzero or where there is a register save block following the local block that doesn’t require alignment to `STACK_BOUNDARY`, it may be beneficial to disable stack alignment and do it in the backend.

Macro: STACK_POINTER_OFFSET ¶

  Offset from the stack pointer register to the first location at which outgoing arguments are placed. If not specified, the default value of zero is used. This is the proper value for most machines.

  If `ARGS_GROW_DOWNWARD`, this is the offset to the location above the first location at which outgoing arguments are placed.

Macro: FIRST_PARM_OFFSET (fundecl) ¶

  Offset from the argument pointer register to the first argument’s address. On some machines it may depend on the data type of the function.

  If `ARGS_GROW_DOWNWARD`, this is the offset to the location above the first argument’s address.

Macro: STACK_DYNAMIC_OFFSET (fundecl) ¶

  Offset from the stack pointer register to an item dynamically allocated on the stack, e.g., by `alloca`.

  The default value for this macro is `STACK_POINTER_OFFSET` plus the length of the outgoing arguments. The default is correct for most machines. See `function.cc` for details.

Macro: INITIAL_FRAME_ADDRESS_RTX ¶

  A C expression whose value is RTL representing the address of the initial stack frame. This address is passed to `RETURN_ADDR_RTX` and `DYNAMIC_CHAIN_ADDRESS`. If you don’t define this macro, a reasonable default value will be used. Define this macro in order to make frame pointer elimination work in the presence of `__builtin_frame_address (count)` and `__builtin_return_address (count)` for `count` not equal to zero.

Macro: DYNAMIC_CHAIN_ADDRESS (frameaddr) ¶

  A C expression whose value is RTL representing the address in a stack frame where the pointer to the caller’s frame is stored. Assume that frameaddr is an RTL expression for the address of the stack frame itself.

  If you don’t define this macro, the default is to return the value of frameaddr—that is, the stack frame address is also the address of the stack word that points to the previous frame.

Macro: SETUP_FRAME_ADDRESSES ¶

  A C expression that produces the machine-specific code to setup the stack so that arbitrary frames can be accessed. For example, on the SPARC, we must flush all of the register windows to the stack before we can access arbitrary stack frames. You will seldom need to define this macro. The default is to do nothing.

Target Hook: `rtx` TARGET_BUILTIN_SETJMP_FRAME_VALUE `(void)` ¶

  This target hook should return an rtx that is used to store the address of the current frame into the built in `setjmp` buffer. The default value, `virtual_stack_vars_rtx`, is correct for most machines. One reason you may need to define this target hook is if `hard_frame_pointer_rtx` is the appropriate value on your machine.

Macro: FRAME_ADDR_RTX (frameaddr) ¶

  A C expression whose value is RTL representing the value of the frame address for the current frame. frameaddr is the frame pointer of the current frame. This is used for __builtin_frame_address. You need only define this macro if the frame address is not the same as the frame pointer. Most machines do not need to define it.

Macro: RETURN_ADDR_RTX (count, frameaddr) ¶

  A C expression whose value is RTL representing the value of the return address for the frame count steps up from the current frame, after the prologue. frameaddr is the frame pointer of the count frame, or the frame pointer of the count − 1 frame if `RETURN_ADDR_IN_PREVIOUS_FRAME` is nonzero.

  The value of the expression must always be the correct address when count is zero, but may be `NULL_RTX` if there is no way to determine the return address of other frames.

Macro: RETURN_ADDR_IN_PREVIOUS_FRAME ¶

  Define this macro to nonzero value if the return address of a particular stack frame is accessed from the frame pointer of the previous stack frame. The zero default for this macro is suitable for most ports.

Macro: INCOMING_RETURN_ADDR_RTX ¶

  A C expression whose value is RTL representing the location of the incoming return address at the beginning of any function, before the prologue. This RTL is either a `REG`, indicating that the return value is saved in ‘`REG`’, or a `MEM` representing a location in the stack.

  You only need to define this macro if you want to support call frame debugging information like that provided by DWARF 2.

  If this RTL is a `REG`, you should also define `DWARF_FRAME_RETURN_COLUMN` to `DWARF_FRAME_REGNUM (REGNO)`.

Macro: DWARF_ALT_FRAME_RETURN_COLUMN ¶

  A C expression whose value is an integer giving a DWARF 2 column number that may be used as an alternative return column. The column must not correspond to any gcc hard register (that is, it must not be in the range of `DWARF_FRAME_REGNUM`).

  This macro can be useful if `DWARF_FRAME_RETURN_COLUMN` is set to a general register, but an alternative column needs to be used for signal frames. Some targets have also used different frame return columns over time.

Macro: DWARF_ZERO_REG ¶

  A C expression whose value is an integer giving a DWARF 2 register number that is considered to always have the value zero. This should only be defined if the target has an architected zero register, and someone decided it was a good idea to use that register number to terminate the stack backtrace. New ports should avoid this.

Macro: DWARF_VERSION_DEFAULT ¶

  A C expression whose value is the default dwarf standard version we’ll honor and advertise when generating dwarf debug information, in absence of an explicit `-gdwarf-version` option on the command line.

Target Hook: `void` TARGET_DWARF_HANDLE_FRAME_UNSPEC `(const char *label, rtx pattern, int index)` ¶

  This target hook allows the backend to emit frame-related insns that contain UNSPECs or UNSPEC_VOLATILEs. The DWARF 2 call frame debugging info engine will invoke it on insns of the form

      (set (reg) (unspec [...] UNSPEC_INDEX))

  and

      (set (reg) (unspec_volatile [...] UNSPECV_INDEX)).

  to let the backend emit the call frame instructions. label is the CFI label attached to the insn, pattern is the pattern of the insn and index is `UNSPEC_INDEX` or `UNSPECV_INDEX`.

Target Hook: `unsigned int` TARGET_DWARF_POLY_INDETERMINATE_VALUE `(unsigned int i, unsigned int *factor, int *offset)` ¶

  Express the value of `poly_int` indeterminate i as a DWARF expression, with i counting from 1. Return the number of a DWARF register R and set ‘`*factor`’ and ‘`*offset`’ such that the value of the indeterminate is:

      value_of(R) / factor - offset

  A target only needs to define this hook if it sets ‘`NUM_POLY_INT_COEFFS`’ to a value greater than 1.

Macro: INCOMING_FRAME_SP_OFFSET ¶

  A C expression whose value is an integer giving the offset, in bytes, from the value of the stack pointer register to the top of the stack frame at the beginning of any function, before the prologue. The top of the frame is defined to be the value of the stack pointer in the previous frame, just before the call instruction.

  You only need to define this macro if you want to support call frame debugging information like that provided by DWARF 2.

Macro: DEFAULT_INCOMING_FRAME_SP_OFFSET ¶

  Like `INCOMING_FRAME_SP_OFFSET`, but must be the same for all functions of the same ABI, and when using GAS `.cfi_*` directives must also agree with the default CFI GAS emits. Define this macro only if `INCOMING_FRAME_SP_OFFSET` can have different values between different functions of the same ABI or when `INCOMING_FRAME_SP_OFFSET` does not agree with GAS default CFI.

Macro: ARG_POINTER_CFA_OFFSET (fundecl) ¶

  A C expression whose value is an integer giving the offset, in bytes, from the argument pointer to the canonical frame address (cfa). The final value should coincide with that calculated by `INCOMING_FRAME_SP_OFFSET`. Which is unfortunately not usable during virtual register instantiation.

  The default value for this macro is `FIRST_PARM_OFFSET (fundecl) + crtl->args.pretend_args_size`, which is correct for most machines; in general, the arguments are found immediately before the stack frame. Note that this is not the case on some targets that save registers into the caller’s frame, such as SPARC and rs6000, and so such targets need to define this macro.

  You only need to define this macro if the default is incorrect, and you want to support call frame debugging information like that provided by DWARF 2.

Macro: FRAME_POINTER_CFA_OFFSET (fundecl) ¶

  If defined, a C expression whose value is an integer giving the offset in bytes from the frame pointer to the canonical frame address (cfa). The final value should coincide with that calculated by `INCOMING_FRAME_SP_OFFSET`.

  Normally the CFA is calculated as an offset from the argument pointer, via `ARG_POINTER_CFA_OFFSET`, but if the argument pointer is variable due to the ABI, this may not be possible. If this macro is defined, it implies that the virtual register instantiation should be based on the frame pointer instead of the argument pointer. Only one of `FRAME_POINTER_CFA_OFFSET` and `ARG_POINTER_CFA_OFFSET` should be defined.

Macro: CFA_FRAME_BASE_OFFSET (fundecl) ¶

  If defined, a C expression whose value is an integer giving the offset in bytes from the canonical frame address (cfa) to the frame base used in DWARF 2 debug information. The default is zero. A different value may reduce the size of debug information on some ports.

Target Hook: `bool` TARGET_HAVE_STRUB_SUPPORT_FOR `(tree)` ¶

  Returns true if the target supports stack scrubbing for the given function or type, otherwise return false. The default implementation always returns true.

Macro: STACK_ADDRESS_OFFSET ¶

  Offset from the stack pointer register to the boundary address between the stack area claimed by an active function, and stack ranges that could get clobbered if it called another function. It should NOT encompass any stack red zone, that is used in leaf functions.

  This value is added to the stack pointer register to compute the address returned by `__builtin_stack_address`, and this is its only use. If this macro is not defined, no offset is added. Defining it like `STACK_POINTER_OFFSET` may be appropriate for many machines, but not all.

  On SPARC, for example, the register save area is *not* considered active or used by the active function, but rather as akin to the area in which call-preserved registers are saved by callees, so the stack address is above that area, even though the (unbiased) stack pointer points below it. This enables `__strub_leave` to clear what would otherwise overlap with its own register save area.

  On PowerPC, `STACK_POINTER_OFFSET` also reserves space for a save area, but that area is used by the caller rather than the callee, so the boundary address is below it.

  If the address is computed too high or too low, parts of a stack range that should be scrubbed may be left unscrubbed, scrubbing may corrupt active portions of the stack frame, and stack ranges may be doubly-scrubbed by caller and callee.

Macro: TARGET_STRUB_USE_DYNAMIC_ARRAY ¶

  If defined to nonzero, `__strub_leave` will allocate a dynamic array covering the stack range that needs scrubbing before clearing it. Allocating the array tends to make scrubbing slower, but it enables the scrubbing to be safely implemented with a `memset` call, which could make up for the difference.

Macro: TARGET_STRUB_MAY_USE_MEMSET ¶

  If defined to nonzero, enable `__strub_leave` to be optimized so as to call `memset` for stack scrubbing. This is only enabled by default if `TARGET_STRUB_USE_DYNAMIC_ARRAY` is enabled; it’s not advisable to enable it otherwise, since `memset` would then likely overwrite its own stack frame, but it might work if the target ABI enables `memset` to not use the stack at all, not even for arguments or its return address, and its implementation is trivial enough that it doesn’t use a stack frame.