438-byte "Hello, world" Win32 EXE in D

Vladimir Panteleev via Digitalmars-d-announce digitalmars-d-announce at puremagic.com
Sun Sep 7 14:03:15 PDT 2014


I've picked up an older project for using D on barebones Win32 as 
a "better C".

Thanks to recent advances in DMD (-betterC and -m32mscoff), I 
could get a "Hello, world" program on Win32 down to just 438 
bytes when compiled. This is without assembly, linker scripts, 
custom Phobos/Druntime, or manual post-build tweaks.

-betterC allows stripping things like ModuleInfo, and as of 
recently it also strips file/module names used for asserts/range 
check errors (which were emitted even in -release mode). A better 
alternative would be a DMD equivalent to -fdata-sections, though, 
so unneeded variables, constants, and data generated by the 
compiler could be garbage-collected by the linker.

-m32mscoff allows using more linkers. Specifically, the Microsoft 
Linker and Crinkler, which only understand COFF, can both 
generate executables which are much smaller than those created by 
OPTLINK.

The 438-byte "Hello, world" program is achieved using Crinkler, 
which is a COFF linker with aggressive compression and header 
optimization. It was created for compressing 4K demos.

Without compression on header optimization, you can reach 630 
bytes using Unilink, a freeware linker which is compatible with 
both COFF and OMF.

The source code is not much to look at, most of the "magic" is in 
the makefiles:

https://github.com/CyberShadow/SlimD

See samples/01-msgbox for more commentary. The figures above are 
for samples/02-console.

The motivation for this project is mostly hack value and 
aesthetical (a 500 KB EXE with 5 KB worth of actual functionality 
is not pretty).


More information about the Digitalmars-d-announce mailing list