Copy a function in memory and execute it

后端 未结 5 869
走了就别回头了
走了就别回头了 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:13

    I have tried this issue many times in C and came to the conclusion that it cannot be accomplished using only the C language. My main thorn was finding the length of the function to copy.

    The Standard C language does not provide any methods to obtain the length of a function. However, one can use assembly language and "sections" to find the length. Once the length is found, copying and executing is easy.

    The easiest solution is to create or define a linker segment that contains the function. Write an assembly language module to calculate and publicly declare the length of this segment. Use this constant for the size of the function.

    There are other methods that involve setting up the linker, such as predefined areas or fixed locations and copying those locations.

    In embedded systems land, most of the code that copies executable stuff into RAM is written in assembly.

提交回复
热议问题