C language > Expert questions

How to get the size of function body?

<< < (3/3)

Vortex:
Here is an attempt for the 64-bit version :


--- Code: ---#include <stdio.h>

int FindFuncTerm();

int testfunc(int x,int y,int *EndOfFunc)
{
    int result;

    result=x;
    result+=y;

*EndOfFunc=FindFuncTerm();

return result;
}

int __cdecl main(void)
{
    int EndFunc;

    testfunc(10,20,&EndFunc);

    printf("Size of the function testfunc = %d bytes\n",EndFunc-(int)testfunc);

    return 0;

}
--- End code ---

FindFuncTerm.asm :


--- Code: ---.code

FindFuncTerm PROC

    mov     rax,QWORD PTR [rsp]
    dec     rax
@@:
    inc     rax
    cmp     BYTE PTR [rax],195 ; =0xC3
    jne     @b
    inc     rax
    ret

FindFuncTerm ENDP

--- End code ---

Grincheux:
0xC3 is the code for "RET". In C there is a no return case. That case, the "RET" ins the best way to measure the function size, I thought that it was possible to use the NOP, but there are too many other bytes used for alignment.

Navigation

[0] Message Index

[*] Previous page

Go to full version