Skip to content

Instantly share code, notes, and snippets.

@Silva97
Created July 11, 2021 15:45
Show Gist options
  • Save Silva97/14ea59af384b55ac44b1f463b03f8c18 to your computer and use it in GitHub Desktop.
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
// 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