Initialization of local variables with movabs
Yes this instruction should move absolute hardcoded data embedded into instruction itself into register,
or data from absolute address. But I got into this instruction only recently when I wanted to know where constant data for initialization of local variables are because I didnt find them in .rodata ELF section.
So basically GCC uses this instruction to initialize automatic variables on stack!
Lets dive into code to find out how this instruction is used.
As we see in this code example we have local variable test initialized:
We know that local variables are put in stack and I was interested to know
where and how local variables get initialized.
When we dump .text section with objdump we see that this constant 0x41414141414141 is realy embedded in the code itself:
116c: f3 0f 1e fa endbr64
1170: 55 push %rbp
1171: 48 89 e5 mov %rsp,%rbp
1174: 48 83 ec 10 sub $0x10,%rsp
1178: 64 48 8b 04 25 28 00 mov %fs:0x28,%rax
117f: 00 00
1181: 48 89 45 f8 mov %rax,-0x8(%rbp)
1185: 31 c0 xor %eax,%eax
1187: 48 b8 41 41 41 41 41 movabs $0x41414141414141,%rax
118e: 41 41 00
As we see disassembled function and register content from GDB:
So when we look at code we see that this string test, in HEX notation 0x0x41414141414141 is hardcoded in movabs instruction, and is put in rax register, and then content of rax register is put on address in rbp minus offset 0x10:
0x000055555555517d <+27>: movabs $0x41414141414141,%rax
0x0000555555555187 <+37>: mov %rax,-0x10(%rbp)
So this local string will be placed on address 0x7fffffffdde0 - 0x10
We see that this local string is placed in stack frame of function
on address 0x7fffffffddd0, and this we see when we dump the stack:
(gdb) x/16gx $sp
0x7fffffffddd0: 0x0041414141414141 0x6a8574f1d6812b00
0x7fffffffdde0: 0x00007fffffffddf0 0x000055555555515b
0x7fffffffddf0: 0x0000000000000001 0x00007ffff7daad90
So we ended with initialized local string on a stack just as we expected.