1

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?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Sisir
  • 4,584
  • 4
  • 26
  • 37
  • Yes https://learn.microsoft.com/en-us/cpp/build/reference/nodefaultlib-ignore-libraries?view=vs-2019 – Richard Critten Oct 11 '19 at 13:02
  • 3
    It is likely not possible with standard C in Visual Studio 2015. See [this question](https://stackoverflow.com/questions/553029/what-is-the-smallest-possible-windows-pe-executable) and [this one](https://stackoverflow.com/questions/8688958/c-windows-compiler-for-smallest-executables). – Eric Postpischil Oct 11 '19 at 13:02
  • @RichardCritten With `/NODEFAULT` flag enabled the linker gives a huge list of errors on VS2015. Linking failing to `__security_check_cookie` `__mainCRTStartup` etc – Sisir Oct 11 '19 at 13:08
  • Is there any other way to do this; like can i create an exe using any scripting language like VB, perl or python and convert it to an exe? Will that make it smaller? – Sisir Oct 15 '19 at 12:43
  • What problem are you trying to solve? Even with good old DOS, there's not much you can do in a 100 byte .COM file. Never mind that there's no point to having such small files in modern file systems. There's lots of padding added to them when they get stored in the filesystem, so you gain nothing. Storing a 100 byte file in modern filesystems is often no different from storing a 256 byte file or 512 byte file. – Kuba hasn't forgotten Monica Oct 15 '19 at 14:14
  • `What problem are you trying to solve` Some people do these kind of things out of curiosity and to get a deeper understanding of the tech stack. – juwens Dec 08 '21 at 10:53
  • @Kubahasn'tforgottenMonica I see that I am a couple of years late :) But I remember writing and using a 2 (or 3?) byte `.COM` program that was a software interrupt for PrintScreen, and used it from other program as a printing feature. – Vlad Feinstein Jan 26 '22 at 19:19
  • @VladFeinstein care to share the code here – Sisir Jan 26 '22 at 19:51
  • 1
    @Sisir Would love to, but I don't remember exactly (was almost 40 years ago). It was a machine code for `interrupt`, `21h` (I think), and the value for `PrintScreen` (I want to say `5`, not sure). – Vlad Feinstein Jan 26 '22 at 21:33

2 Answers2

3

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

The Techel
  • 918
  • 8
  • 13
3

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.

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
  • Is there any other way to do this; like can i create an exe using any scripting language like VB, perl or python and convert it to an exe? Will that make it smaller? – Sisir Oct 15 '19 at 12:43
  • 1
    Those are limits inherent in the format of the binary executable file. It doesn't matter how you create it, what matters it that it's executable and has some code in it. You can write the code itself in assembly. It doesn't matter one bit, since the modern C++ compilers generate amazing assembly, as long as you use them correctly. Scripting languages make things much worse, since you have to distribute your EXE file along with the runtime of the scripting language. "Converting" perl or python to .EXE is done by bundling the runtime with bytecode in a single file. Those files are monstrous. – Kuba hasn't forgotten Monica Oct 15 '19 at 14:12