Created
July 11, 2021 15:45
-
-
Save Silva97/14ea59af384b55ac44b1f463b03f8c18 to your computer and use it in GitHub Desktop.
Just an example of how to get the size of a function in bytes using the GCC compiler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// See how it's works using: gcc -S get-function-size.c -o get-function-size.s | |
// To manually check function size: objdump -d get-function-size | |
#include <stdio.h> | |
#define DECLARE_FUNCSIZE(funcname) \ | |
extern unsigned int funcname##_funcsize; \ | |
asm(#funcname "_funcsize: .long . - " #funcname "\n\t") | |
#define FUNCSIZE(funcname) \ | |
funcname##_funcsize | |
int add(int x, int y) | |
{ | |
return x + y; | |
} | |
DECLARE_FUNCSIZE(add); // Need to be declared immediately after the function declaration! | |
int main(void) | |
{ | |
printf("Size: %d\n", FUNCSIZE(add)); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment