This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: detect empty functions
- From: vijay nag <vijunag at gmail dot com>
- To: Prathamesh Kulkarni <bilbotheelffriend at gmail dot com>
- Cc: "gcc-help at gcc dot gnu dot org" <gcc-help at gcc dot gnu dot org>
- Date: Wed, 5 Feb 2014 19:33:03 +0530
- Subject: Re: detect empty functions
- Authentication-results: sourceware.org; auth=none
- References: <CAJXstsC1Bj4sxY_OiC-k3HcEjMm0qyu901ftmdyn4CMxCAWk_g at mail dot gmail dot com>
Hello,
You can use the below program to find the length of the function
compiled for X86 target.
Note that assembly instruction for "function()" is immediately
followed by assembly for "functionEnd"
Key things to note here
1) Compiler still emits epilogue/prologue for a function with empty
body. However you could disable this using -fomit-frame-ptr in which
case function
body will only have "ret" instruction and in which case difference
would be 1.
0x80483a4 <function> ret
â
â0x80483a5 <functionEnd> ret
So size of the function would be 0x80483a5 - 0x80483a4 = 1
2)In case if you need frame-ptr, there will be a minimum of 4
instructions and in this case difference would be 5.
0x80483a4 <function> push %ebp
â
â0x80483a5 <function+1> mov %esp,%ebp
â
â0x80483a7 <function+3> pop %ebp
â
â0x80483a8 <function+4> ret
â
â0x80483a9 <functionEnd> push %ebp
â
â0x80483aa <functionEnd+1> mov %esp,%ebp
â
â0x80483ac <functionEnd+3> pop %ebp
â
â0x80483ad <functionEnd+4> ret
So size of the function would be 0x80483a9 - 0x80483a4 = 5
cat fsize.c
#include <stdio.h>
int function(){
}
int functionEnd(){
}
int main()
{
printf("%x\n", &functionEnd - &function);
}
vijayNag
On Wed, Feb 5, 2014 at 7:00 PM, Prathamesh Kulkarni
<bilbotheelffriend@gmail.com> wrote:
> Hi, is there a way to find if a function has empty body ?
>
> Thanks and Regards,
> Prathamesh