C: Utility to analyze .obj files, to measure size of some of the functions in exact bytes?

3.5k views Asked by At

I needed to look-up the exact byte size of few C functions. Any recommendation of any utility which is able to analyze .obj files generated by the gcc compiler?

1

There are 1 answers

5
DevSolar On BEST ANSWER

I only have a GCC v4 here for testing, but I don't think this particular feature of binutils has changed recently.

test.c:

int foo()
{
    return 42;
}

Compiled:

$ gcc -c test.c -o test.o

Disassembled:

$ objdump -D test.o
...
0000000000000000 <foo>:
   0:   55                      push   %rbp
   1:   48 89 e5                mov    %rsp,%rbp
   4:   b8 2a 00 00 00          mov    $0x2a,%eax
   9:   c9                      leaveq
   a:   c3                      retq
...

(We see foo is located at address 0, the size being 0x000b bytes.)

Proof is in the pudding:

$ nm -S test.o
0000000000000000 000000000000000b T foo

At address 0, size 0x000b. There you are. ;-)

However, be careful with what you want to do with these numbers. This is the size of the function allright, but there might be more (e.g. global data objects) required to make the function run correctly.