Skip to content

Instantly share code, notes, and snippets.

@Silva97
Created July 11, 2021 15:45
Show Gist options
  • Select an option

  • Save Silva97/14ea59af384b55ac44b1f463b03f8c18 to your computer and use it in GitHub Desktop.

Select an option

Save Silva97/14ea59af384b55ac44b1f463b03f8c18 to your computer and use it in GitHub Desktop.

Revisions

  1. Silva97 created this gist Jul 11, 2021.
    22 changes: 22 additions & 0 deletions get-function-size.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    // 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;
    }