Copy a function in memory and execute it

后端 未结 5 883
走了就别回头了
走了就别回头了 2020-12-10 07:41

I would like to know how in C in can copy the content of a function into memory and the execute it?

I\'m trying to do something like this:

typedef vo         


        
5条回答
  •  没有蜡笔的小新
    2020-12-10 08:04

    You seem to have figured out the part about protection flags. If you know the size of the function, now you can just do memcpy() and pass the address of f1 as the source address.

    One big caveat is that, on many platforms, you will not be able to call any other functions from the one you're copying (f1), because relative addresses are hardcoded into the binary code of the function, and moving it into a different location it the memory can make those relative addresses turn bad.

提交回复
热议问题