int main()
{
foo();
// static
// creates two variables, one const and one mutable,
// then prints the section of memory they end up in,
// followed by printing the section name itself.
poo();
// automatic
// does the same thing with a normal local variable
// and prints its section name.
// we also import a disassembler so we can inspect
// how this actually affects the generated code.
// inspector function bytes function address deref jump count
inspectfunction(GrabFunctionBytes(foo), reinterpretcast<uintptrt>(&foo), 1);
inspectfunction(GrabFunctionBytes(poo), reinterpretcast<uintptrt>(&poo), 1);
}
foo output: (statics)
const: .rdata (0x7ff63ff03188)
mutable: .data (0x7ff63ff1c008)
snippet #1
0x7FF63FE7CBB0 | 48 8D 05 51 F4 09 | lea rax, rip+0x9F451 abs: 0x7FF63FF1C008 <--
0x7FF63FE7CBB7 | 48 89 45 08 | mov rbp+0x08, rax
0x7FF63FE7CBBB | 48 8D 05 C6 65 08 | lea rax, rip+0x865C6 abs: 0x7FF63FF03188 <--
0x7FF63FE7CBC2 | 48 89 45 28 | mov rbp+0x28, rax
0x7FF63FE7CBC6 | 8B 05 BC 65 08 00 | mov eax, rip+0x865BC abs: 0x7FF63FF03188 <--
poo output: (automatic)
integer: stack / heap (0xbc06ff454)
snippet #2
0x7FF63FE8B98C | E8 4E 79 FC FF | call -0x386AD
0x7FF63FE8B991 | 90 | nop
0x7FF63FE8B992 | C7 45 04 33 03 00 | mov dword ptr rbp+0x04, 0x333 <--
0x7FF63FE8B999 | 48 8D 45 04 | lea rax, rbp+0x04 <--
0x7FF63FE8B99D | 48 89 45 28 | mov rbp+0x28, rax
What I'm trying to achieve is getting the compiler to prefer stack-based dereferencing over RIP-relative dereferencing not counting branching/jumps and calls ofc.
I know I can control this to some extent by just avoiding
static, but it starts getting annoying with things like std::cout and other CRT objects since they naturally end up in sections like .data, .rdata, and .bss.I've tried a few different methods already. Some worked partly, some didn't really solve the issue. I'm mainly just wondering if there's a cleaner way to push things toward runtime (stack/heap) storage and avoid these RIP-relative accesses, without having to rely on weird hacks or patching the compiler itself.
https://redd.it/1tw3d10
@r_cpp