My current set is:
Apple clang version 14.0.3 (clang-1403.0.22.14.1)
Target: arm64-apple-darwin23.0.0
I want to insert some data inside the function body.
Here is the code:
#include <string>
#include <iostream>
static bool gs_false = false;
uint64_t to_start, to_mid;
int main(int argc, char *argv[])
{
if(!gs_false)
{
gs_false = !gs_false;
goto lend;
lstart:
asm volatile(".byte 0x01");
asm volatile(".byte 0x02");
asm volatile(".byte 0x03");
asm volatile(".byte 0x04");
lmid:
asm volatile(".byte 0x05");
asm volatile(".byte 0x06");
asm volatile(".byte 0x07");
asm volatile(".byte 0x08");
lend:
to_start = reinterpret_cast<uint64_t>(&&lstart);
if(!gs_false) {
goto lstart;
}
}
const char *p = (const char*)to_start;
for(int n = 0; n < 8; ++n, ++p)
{
char c = *p;
std::cout << std::to_string(c) << std::endl;
}
return 0;
}
I use the gs_false variable (and all code around it) to cheat the optimization. Without that, my data is thrown out with optimization even in debug mode.
But even in debug mode if run the code as is it will print 1,2,3,4, and four “random” numbers.
If I remove lmid: label it will print: 1,2,3,4,5,6,7,8.
I checked the program binary image and found that in the debug image my data is present but is not present in the release image.
The optimizer removes all of it.
Is there exist way to keep my data inside the function body?