Find size of a function in C

后端 未结 9 1762
别跟我提以往
别跟我提以往 2020-12-10 16:37

I am learning function pointers,I understand that we can point to functions using function pointers.Then I assume that they stay in memory.Do they stay in stack or heap?Can

9条回答
  •  执念已碎
    2020-12-10 17:01

    There's an interesting way to discover the size of the function.

    #define RETN_empty 0xc3
    #define RETN_var   0xc2  
    typedef unsigned char BYTE;
    size_t FunctionSize(void* Func_addr) {
        BYTE* Addr = (BYTE*)Func_addr;
        size_t function_sz = 0;
        size_t instructions_qt = 0;
            while(*Addr != (BYTE)RETN_empty && *Addr != (BYTE)RETN_var) {
                size_t inst_sz = InstructionLength((BYTE*)Addr);
                function_sz += inst_sz;
                Addr += inst_sz;
                ++instructions_qt;
            }
        return function_sz + 1;
    }
    

    But you need a function that returns the size of the instruction. You can find a function that finds the Instruction Length here: Get size of assembly instructions. This function basically keeps checking the instructions of the function until it finds the instruction to return (RETN)[ 0xc3, 0xc2], and returns the size of the function.

提交回复
热议问题