Re:Show HN: Tiny 1k Rust binary on Windows
by slezyr on Tuesday December 01, 2020 @08:13AM
(#25264798)
You're comparing C+StdLib vs. Rust without StdLib. Disable stdlib for C version and use the following program:
#include <windows.h>
int _start() {
const char str[] = "Hello world!\n";
HANDLE stdout = GetStdHandle(STD_OUTPUT_HANDLE);
DWORD written;
WriteFile(stdout, str, sizeof(str), &written, NULL);
ExitProcess(12);
return 0;
}https://stackoverflow.com/a/42536990
Which is similar to what the Rust program does. The result should be significantly less than 1k
Re:Show HN: Tiny 1k Rust binary on Windows
by ChrisSD on Tuesday December 01, 2020 @11:18AM
(#25266511)
I tried your program with MSVC using the following command:
cl small2.c /MD /link /entry:_start /subsystem:CONSOLE kernel32.lib
It's 5kb.
Re:Show HN: Tiny 1k Rust binary on Windows
by slezyr on Tuesday December 01, 2020 @02:45PM
(#25269021)
If I remember correctly you need a /nodefaultlib flag to disable stdlibs
https://docs.microsoft.com/en-us/cpp/build/reference/nodefau...
Re:Show HN: Tiny 1k Rust binary on Windows
by ChrisSD on Tuesday December 01, 2020 @03:31PM
(#25269504)
Ok, I've just tried that but I also needed to add `/force` otherwise it complained about missing symbols. So the full compile line is:
cl small2.c /link /force /nodefaultlib /entry:_start /subsystem:CONSOLE kernel32.lib
This gets it down to 3kb.
Re:Show HN: Tiny 1k Rust binary on Windows
by zamadatix on Tuesday December 01, 2020 @10:09AM
(#25265805)
I think the thing being measured by GP is "easiest way to get to the 1kb mentioned in the title" not "most complicated way to get as small as possible".
Re:Show HN: Tiny 1k Rust binary on Windows
by ChrisSD on Tuesday December 01, 2020 @11:03AM
(#25266338)
Have you actually tried this on MSVC? This is my program. I created a file called `small.c`
#include <stdio.h>
int main(void) {
puts("Hello World!");
return 0;
}
I then compiled it using this command: cl small.c /MD
The result? 9kb! That's 9 times the size.
Re:Show HN: Tiny 1k Rust binary on Windows
by m1el on Tuesday December 01, 2020 @08:13AM
(#25264802)
Can you, though?
Compiling that source on Windows using visual studio produces a 98K executable.
Re:Show HN: Tiny 1k Rust binary on Windows
by userbinator on Tuesday December 01, 2020 @09:36AM
(#25265491)
You just need to add some compiler options; /MD should get the size down quite a bit already, then decrease the file alignment and merge sections together:
/MD /link /align:4096 /filealign:512 /merge:.rdata=.text /merge:.data=.text
That produced a 1.5K binary, which with a custom stub should reach 1K.
Re:Show HN: Tiny 1k Rust binary on Windows
by simlevesque on Tuesday December 01, 2020 @09:43AM
(#25265561)
So the point is moot, you can't have a simple 1k C example.
Re:Show HN: Tiny 1k Rust binary on Windows
by ChrisSD on Tuesday December 01, 2020 @11:10AM
(#25266416)
Have you actually tried this? Merging .data into .text doesn't work. The binary doesn't run and there's this link warning:
> LINK : warning LNK4254: section '.data' (C0000040) merged into '.text' (60000020) with different attributes
Re:Show HN: Tiny 1k Rust binary on Windows
by mcountryman on Tuesday December 01, 2020 @10:57AM
(#25266250)
In order to get that to 1Kb you would probably have to drop the `.idata` section and resolve syscalls from the PEB like I did.
Re:Show HN: Tiny 1k Rust binary on Windows
by mythz on Tuesday December 01, 2020 @09:38AM
(#25265505)
The smallest executable gcc (on Ubuntu) compiles that to is 8296 bytes (tried every -O optimization).
Re:Show HN: Tiny 1k Rust binary on Windows
by nitrogen on Tuesday December 01, 2020 @04:29PM
(#25270177)
Is that stripped or unstripped?
Re:Show HN: Tiny 1k Rust binary on Windows
by pantalaimon on Tuesday December 01, 2020 @11:50AM
(#25266890)
Hm I only looked at the output of size which gives
text data bss dec hex filename
1566 600 8 2174 87e hello
(no optimisations)
Re:Show HN: Tiny 1k Rust binary on Windows
by renewiltord on Tuesday December 01, 2020 @12:31PM
(#25267431)
Actually, the equivalent Rust code to this is just the following:
println!("Hello world!");
Exactly that text. Compile it and it comes in at exactly 13.6% of the C program you have that compiles to 1k.
[0] https://github.com/mcountryman/min-sized-rust-windows/blob/m...