I have a C source file that I'm not allowed to change and it is defined as follows:
int main(int argc, char *argv[])
{
//doing something
return 0
}
void __magic()
{
__asm__("jmp %esp");
}
I do not use the fucntion __magic in my code, it is just declared after the main. I wish to find the address of the function __magic. How can I do that without having to declare the funtion before the main? I use gdb for debugging purposes.
P.S I'd also like to know whether this function is even saved in my process memory since there is no declaration/use of it. might the compiler just not add that function?
You'll need to clarify what exactly you mean by "find the address of the function __magic
". If you just want to see what the address is (i.e. not use it in code), then you can just use objdump
to show the symbol value.
If, however, you need the address at compile time, then there's no easy way to do it. There's no guarantee that the compiler will place the code for the functions in any particular order, and even if it did, there may be an unknown amount of padding between functions. Since you're using %esp
, I assume you're targeting 32-bit x86 - if you're targeting 64-bit however, then you'll also have to worry about ASLR.