Ignoring the fact that this is a blatant undefined behavior, if you are assigning a global variable, the generated code will most likely use relative addressing to refer to the variable on some architectures.
That is, the function expects that x itself will be at the given address, and if you move it, everything will break.
This is what my GCC generates for your test function:
x: .zero 4 .text .globl _Z4funcv .type _Z4funcv, @function _Z4funcv: .LFB2: .cfi_startproc pushq %rbp .cfi_def_cfa_offset 16 .cfi_offset 6, -16 movq %rsp, %rbp .cfi_def_cfa_register 6 movl $5, x(%rip) nop popq %rbp .cfi_def_cfa 7, 8 ret .cfi_endproc
Pay attention to movl $5, x(%rip) , which means that the code uses its own address (stored in% rip) to calculate the position of x and store 5 in it.
So, there is no easy way to do what you are trying to do, unless you are sure that your function has position-independent code . And even then, he only asks for trouble.
source share