tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: Trivial program size inflation



On Fri, 30 Jun 2023, Mouse wrote:

Based on something at work, I was looking at executable sizes.  I
eventually tried a program stripped about as far down as I could:

int main(void);
int main(void)
{
return(0);
}

and built it -static.  size on the resulting binary:
[...]
amd64, 9.0_STABLE (ftp.n.o):

  text    data     bss     dec     hex filename
562318   29064 2176416 2767798  2a3bb6 main

12K to do nothing is bad enough (I'm going to be looking at why it's
that big).  149K is even more disturbing (I'll be looking at that too).
But over half a meg of text and two megs of BSS?  To do nothing?
Surely something is wrong somewhere.


You need to look at the CRT stuff in /usr/src/lib/csu/ to figure this
out. A lot of initialization and set-up has to be done to get to the
point of calling main(): Calling the libc init routine--which pulls in
malloc, which brings in pthreads (stub or real code). Now you have all
the stuff needed to bring in malloc and stdio coming in: system calls
for those (write, mmap), and the functions these all rely on: strlen(),
strcmp(), ...

Not to forget the code for C++ support. And, of course even static
binaries may call dlopen() and friends. So that dl*() and the ELF bits
right there.

If you want to slim your static binary down, use a different libc first :)

-RVP



Home | Main Index | Thread Index | Old Index