C memcpy () function

Is there a way to calculate the size of a function? I have a pointer to a function, and I need to copy the entire function using memcpy. I have to malloc some space and know the 3rd memcpy-size parameter. I know that sizeof(function)does not work. Do you have any suggestions?

+5
source share
15 answers

Functions are not first-class objects in C. This means that they cannot be transferred to another function, they cannot be returned from a function, and they cannot be copied to another part of memory.

The function pointer, although it can satisfy all of this, is an object of the first class. A function pointer is just a memory address and usually has the same size as any other pointer on your computer.

+23
source

It does not directly answer your question, but you should not make callbacks from kernel code to user space .

Kernel space injection code is not a great working code .

/, . , , char. , . / , .so.

, , memcpy() . , . C, , , .

strlen(), strcmp() GCC.

, , , .

+7

sizeof(), , . , . . , .


, , , , , , /. , - .


, , ​​ /, , , , . ( ) + , , , . , , , .

, 32- , , .

, , , .

+5

? , C . , Harvard-Architecture, ( "" ) . . , ( ). , C .

, , :

  • (, / ).

  • C.

  • , .

  • , "", .

: , ?

+3

, , , -fPIC - , , , . , - , , 1 , .

, . x86:

:your_func_epilogue
mov esp, ebp
pop ebp
ret
:end_of_func

;expect a varying length run of NOPs here

:next_func_prologue
push ebp
mov ebp, esp

, , . , , , . , . , .

, , , , , -, . , , , , .

+3

, .

, -, , (.. , , , , , ).

+1

, , - .

, , kernel/userspace . , , , , . , , - , , .

, ​​ , ( , ). select/poll read - , , UNIX , , , ( ).

, , , read .

+1

- , . / ? , - binunils linux , , ?

, memcpy(), LD_PRELOAD.

0

, , , , .

0

, , , ( , ), . , , . , , , , .

0

: .

- , .

0

, - , ; , ++ --, C, .

, , , , , -, (.. , , , malloc, ). , . , , (, ). , . , , .

, , , , , , OP.

, , OP . , (, , , ..). , , (, )...

0

Nintendo GBA, - (16- ) (32- , ). , , size = (int) (NextFuncPtr - SourceFuncPtr). , , , ( Windows ).

0

, .

: func() a.c .

Try writing a perl script that compiles this file in object format (cc -o) to make sure that the instructions in front of the processor are not deleted. You will need them later to calculate the size from the object file.

Now find the two indicators and find the code size between them.

0
source

All Articles