Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does an EXE file that does *nothing* contain so many dummy zero bytes?

I've compiled a C file that does absolutely nothing (just a main that returns... not even a "Hello, world" gets printed), and I've compiled it with various compilers (MinGW GCC, Visual C++, Windows DDK, etc.). All of them link with the C runtime, which is standard.

But what I don't get is: When I open up the file in a hex editor (or a disassembler), why do I see that almost half of the 16 KB is just huge sections of either 0x00 bytes or 0xCC bytes? It seems rather ridiculous to me... is there any way to prevent these from occurring? And why are they there in the first place?

Thank you!

like image 598
user541686 Avatar asked Jan 05 '11 09:01

user541686


People also ask

How to fix 0 byte file in Windows 10?

Follow these steps to fix and recover a 0 byte file. Open the Run dialog box by pressing Win and R keys together. At the Command Prompt, type "chkdsk /f e:", where e is the name of the storage device or partition of the hard drive that holds the 0 byte file.

What causes a 0 byte file to appear?

A zero bytes file can be caused by many different reasons, which can fall into 2 main situtaions. Hard drives run the risk of corruption and can lose data for a number of different reasons. This can cause your important files to become 0 byte files.

What are some POSIX dummy executable files that do nothing?

Most notably :, a POSIX required null utility. But you also require that it is an external file. So: the POSIX standard specified dummy executable files that do nothing could be: printf '' # requires a format. The format may be empty. test 1 # requires a non-empty string (or number).

What is a zero-byte file?

A zero-byte file means that there is no data being stored in the file and the length of the file also becomes zero. In other words, the file contains no content that can be written and read.


2 Answers

Executables in general contain a code segment and at least one data segment. I guess each of these has a standard minimum size, which may be 8K. And unused space is filled up with zeros. Note also that an EXE written in a higher level (than assembly) language contains some extra stuff on top of the direct translation of your own code and data:

  • startup and termination code (in C and its successors, this handles the input arguments, calls main(), then cleans up after exiting from main())
  • stub code and data (e.g. Windows executables contain a small DOS program stub whose only purpose is to display the message "This program is not executable under DOS").

Still, since executables are usually supposed to do something (i.e. their code and data segment(s) do contain useful stuff), and storage is cheap, by default noone optimizes for your case :-)

However, I believe most of the compilers have command line parameters with which you can force them to optimize for space - you may want to check the results with that setting.

Here is more details on the EXE file formats.

like image 126
Péter Török Avatar answered Nov 15 '22 07:11

Péter Török


As it turns out, I should've been able to guess this beforehand... the answer was the debug symbols and code; those were taking up most of the space. Not compiling with /DEBUG and /PDB (which I always do by default) reduced the 13 K down to 3 K.

like image 29
user541686 Avatar answered Nov 15 '22 05:11

user541686