Is there a tool that analyzes the size of compiled functions and their dependencies in a binary executable?
In my case, the program is written in C++, but most of the dependencies I am interested in analyzing come from C libraries. The platform is Linux.
My goal is to reduce the size of a statically linked version of the program. I am considering removing calls to library functions (and implementing my own replacements), but before I start doing that work, I would like to know which library functions contribute the most "bloat".
For example, consider the following program test.c
:
int foo (int n) { return n+1; }
int main(int argc, char** argv) { return foo(argc)+1; }
This is, of course, a trivial example without any libraries, but I use it for illustrative purposes.
I compile this with gcc test.c
.
The output of objdump -d a.out
includes the following:
0000000000001129 <foo>:
1129: f3 0f 1e fa endbr64
112d: 55 push %rbp
112e: 48 89 e5 mov %rsp,%rbp
1131: 89 7d fc mov %edi,-0x4(%rbp)
1134: 8b 45 fc mov -0x4(%rbp),%eax
1137: 83 c0 01 add $0x1,%eax
113a: 5d pop %rbp
113b: c3 retq
000000000000113c <main>:
113c: f3 0f 1e fa endbr64
1140: 55 push %rbp
1141: 48 89 e5 mov %rsp,%rbp
1144: 48 83 ec 10 sub $0x10,%rsp
1148: 89 7d fc mov %edi,-0x4(%rbp)
114b: 48 89 75 f0 mov %rsi,-0x10(%rbp)
114f: 8b 45 fc mov -0x4(%rbp),%eax
1152: 89 c7 mov %eax,%edi
1154: e8 d0 ff ff ff callq 1129 <foo>
1159: 83 c0 01 add $0x1,%eax
115c: c9 leaveq
115d: c3 retq
115e: 66 90 xchg %ax,%ax
We can see at address 1154
that main
calls foo
.
So I'm looking for a tool that will tell me the following:
- The size of
main
is 36 bytes. - The size of
foo
is 19 bytes. - The size of
main
and its dependencies is 55 bytes. (This is the tricky number to calculate, because the tool needs to perform a recursive descent search to find all the functions upon whichmain
depends.) main
depends on the following functions:foo
It would be an added bonus if the tool also told me about the size of any static data structures that the functions use.
I've found the following related Stack Overflow questions, but it appears none of the answers mention a tool that does the dependency analysis I want.
Measure static memory usage for C++ ported to embedded platform
Analyzing an ELF binary to minimize its size
Tool to analyze size of ELF sections and symbol
puncover looks nice, but appears to lack the dependency analysis I want.