I have recently discovered unclear fact about memory reservation for local variables in generated by g++ code: the variables are placed at the addresses below the stack pointer.
For example, when this program (main.cpp) is compiled:
void __attribute__ ((noinline)) foo() {
volatile int v0, v1, v2, v3;
v0 = 0;
v1 = 1;
v2 = 2;
v3 = 3;
}
int main() {
foo();
}
by command:
g++ -Ofast -S main.cpp
with compiler
g++ (Ubuntu 4.8.5-4ubuntu2) 4.8.5 on x86_64
then this assembly is created (main.s):
.file "main.cpp"
.text
.p2align 4,,15
.globl _Z3foov
.type _Z3foov, @function
_Z3foov:
.LFB0:
.cfi_startproc
movl $0, -16(%rsp)
movl $1, -12(%rsp)
movl $2, -8(%rsp)
movl $3, -4(%rsp)
ret
.cfi_endproc
.LFE0:
.size _Z3foov, .-_Z3foov
.section .text.startup,"ax",@progbits
.p2align 4,,15
.globl main
.type main, @function
main:
.LFB1:
.cfi_startproc
call _Z3foov
xorl %eax, %eax
ret
.cfi_endproc
.LFE1:
.size main, .-main
.ident "GCC: (Ubuntu 4.8.5-4ubuntu2) 4.8.5"
.section .note.GNU-stack,"",@progbits
As you can see, local variables of function "foo" are placed at "-16(%rsp)" ... "-4(%rsp)" addresses.
But, as far as I know, these addresses (below the stack pointer register) are reserved for flow interrupting events, for example, for signal handling. So, the below-stack-pointer memory area can be unpredictably changed in any moment. So, it can't be used for local variables storage.
Can somebody, please, clarify this issue?