Linux – Why do ‘word count’ and ‘size’ return different sizes for a compiled file under Linux


How do you account for the difference between the size in bytes of a compiled ELF file as reported by wc (relatively large) and size (sum total of sections in file – relatively small) under Linux?

Edit: For example, compile a very simple C++ program using g++ and run 'wc myexe' and 'size myexe' and wc may return, for example 500B, whilst size may return a total of 100 bytes for all sections.

Edit II: I understand the the two commands do different things, sorry I should have said that I'm not looking for the answer 'because they're different'. I want to know what exactly accounts for the difference. Why should the wc bytes be so much larger than the total sum of the size of the sections, which after all, comprises the executable part of the code.

Best Solution

wc just counts the number of bytes in a file (and words and lines). It works on any normal files, not just object and executable files.

size parses the headers of an object or executable file and shows information about the segments that the author of size thought would be useful, back when size was written (hint - long before Linux was born!).

readelf and some of the other binutils programs read and parse the more modern ELF format files and show more info, including segments that size doesn't know about.

If you really want to understand what is going on under the hood, you can write your own readelf-like program, starting with /usr/include/elf.h, and parse the files for yourself. :)

Related Question