Is it possible to create a pointer to a function, and then get its size (the
actual size the function takes in machine code), such that you can copy the
function to another memory location. You could then modify it (I know it
would be modifing the machine code) and then call the modified function via
a function pointer?
funccat() is not implemented on any known system. It takes a pointer
to a function f(a) and another pointer to function g(b), where "a"
and the return type of g have the same type, and returns a pointer
to a function h(b) such that h(b) = f(g(b)).
How much do you know about assembly language? If you have some
experience with it, you should be familiar with the idea of
"relocation": the bytes in a function (especially those representing
address fields within instructions) are different depending on where
it is loaded. Some of this can be avoided by the use of pc-relative
addressing on machines which have it. Some (for example, reference
to static data areas or other functions) cannot. Relocation
information is likely present in shared libraries, and may not be
in executables. Also, you may have the problem that the copied
function needs a static data area *different* from the static data
area of the original function.
There is no guarantee that the code for a function is contiguous
and non-overlapping with the code for a different function. Optimizers
can cause code sharing between functions. In one compiler, where
the linkage conventions required a bit of code, generally had a
common function return sequence in the whole program and most of
the functions used it. This could be an unpleasant surprise if you
set a breakpoint there with a debugger, expecting to see returns
from one function.