I have some code written in assembly which I need to copy at runtime (code that copies is written in C), hence I need to get the size of the function. I've written the function this way in assembly:
.globl function_foo
function_foo:
...
...
.globl function_foo_end
function_foo_end:
extern void function_foo(void);
extern void function_foo_end(void);
memcpy(dest, function_foo, (uintptr_t)function_foo_end - (uintptr_t)function_foo);
The code works as expected, but I'm concerned that if the symbols are re-ordered by the linking process, say if function_foo_end is moved before function_foo, or if function_foo and function_foo_end are not contiguous in memory, then memcpy may copy the wrong size.
Are these symbols guaranteed to be in order and contiguously placed in memory?
Linker works at granularity of sections so as long as your labels are within the same .text block linker will not split them apart.
There are certain post-link optimization tools (like BOLT) which can do more aggressive transformations of binary code, including moving labels around.
One alternative may be to compile your program two times:
readelf or like)-DFUNC_SIZE=...)If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With