Sisir
Sisir

Reputation: 5418

Create a C program of size 100 bytes with Visual Studio

I want to write a C application program which upon building will create an executable of size 100 bytes or less.

Even if I create a simple C program with just an empty main(), my output file becomes 11KB on Visual Studio 2015. Is there a way to tell VS not to include any default libs which will reduce my executable size. Or is there any other way to reduce the executable file size?

Upvotes: 1

Views: 1747

Answers (2)

You can create an executable with the text section (i.e. the section that has the executable code in it) of less than 100 bytes for a hello world console output, but the .EXE file size will be larger because it needs to have a valid PE format structure, and that format requires quite a bit of padding. /NODEFAULT will of course give you errors. You'll then have to reimplement whatever's missing, usually making things no-op, and you'll need to use link-time code generation so that all the calls to the empty functions get removed (as well as the functions themselves). You'll also need to find compiler flags that disable all "cool" features. E.g. to make the compiler stop emitting buffer security checks (__security_check_cookie & al.), provide the /GS- option.

You'll probably need to use a custom tool to completely strip the .EXE file from unnecessary PE cruft that VS linker emits. And your executable will still be runtime-linked with at least KERNEL32.DLL, since without that you won't be able to do anything useful. If you're brave, you could use NTDLL.DLL (i.e. the native API) directly, but that will probably need more than 100 bytes of code to.

Your executable will also need to be targeting the 32 bit architecture; 64-bit one will be about 25% larger (at such small section sizes to start with).

It's a nice challenge.

Upvotes: 3

The Techel
The Techel

Reputation: 873

A sensible Win32 executable cannot be less than some hundred bytes in size: What is the smallest possible Windows (PE) executable? You can however write a plain old COM executable, which can only be run on x86-Windows. You would need appropriate toolchains: Looking for 16-bit c compiler for x86

Upvotes: 3

Related Questions