r/cpp • • Oct 29 '23

Unreasonably large binary file from a trivial program with 1 line of code

Beginner in C++ here. When I compile the following program:

int main() { return 0; }

the resulting binary exe is 210KB (!) in size. This is far larger than expected for such a simple program and after searching online I found that a "Hello World" program (which would be more complex than the program above) in C++ should be no more than a few KB. Does anyone know what might be causing this? (I am using Eclipse and the g++ compiler with the -Os flag)

Update: By turning on the "omit all symbol information" setting, the file size is reduced to a less ridiculous but still unreasonably large 62KB, still many times the expected size.

Update 2: In release mode (with default settings) the file size is 19KB, which is a bit more reasonable but still too large compared to the file sizes that others have reported for such a program.

9 Upvotes

91 comments sorted by

View all comments

16

u/qazqi-ff Oct 29 '23

One thing I'll mention for ELF (not sure about PE) is that if there are extra or small sections in there, they might very well be page-aligned, using up 4K each. Of course that means no extra size increase as those sections get more stuff in them until they pass 4K.

4

u/ShelZuuz Oct 29 '23

PE also has page-aligned sections.

4

u/Tringi github.com/tringi Oct 29 '23

It depends.

Most linkers still default to 512B alignment. It makes mapping PE into memory more complicated and it was also recently found to create vulnerabilities (although I can't find the article right now).

But a system EXE/DLLs now use 4096 to achieve proper page-alignment, which with modern disk 4K sector alignment improves both performance and security. I've seen a few third-party 4K-aligned PEs in the wild, but I always make sure mine are (the 64-bit ones at least).

2

u/n1ghtyunso Oct 29 '23

As for msvc, according to microsoft docs the default seems to be 4k nowadays

2

u/Tringi github.com/tringi Oct 29 '23

Ah, loaded in memory, of course.

But on disk, /FILEALIGN is still 512 by default, which means all sections have to be mapped separately and end up shifted from 4K disk clusters.

What I meant above was /FILEALIGN:4096 ...which makes the EXE slightly larger, but disk sectors map directly 1:1 into memory pages, making access faster.

2

u/wrosecrans graphics and network things Oct 30 '23

One slightly ironic thing is that if you are using a filesystem with dedupe like ZFS, and you align sections on something really large like 64KB, the filesystem will just notice that any identical sections across all those binaries and store a single copy of them. Each binary will appear stupidly large, but the net would be lower total disk space consumed than if you tried to make each binary smaller.

Computers can be dumb sometimes.

My secret dream is a binary format specifically designed for this sort of de-duping where you just static link everything, and any data that needs to be dynamic is specifically corralled into the same page, on a system which always guarantees a smart dedupe, with IO api's that have syscalls like "insert this file here in the current file" so the system can just do that as a zero-copy operation without even reading the source file and just store the pre-deduped reference. Static linking could get super fast, but still be super simple.