Dinky is a tiny web server.
How tiny?
1808 bytes tiny.
That's 8.3 times smaller than the favicon.ico for the sample index.html page [1]
We get there partially by skimping hard on features. Only a single file is served (path passed on the command line),
and there's no https support.
Dinky is written in Zig specifically for Linux, and depends on nothing but a few system calls.
To minimize the binary size, Dinky targets a freestanding environment. This means the standard library isn't useful for us (nothing involving syscalls or allocation will work unless we spend bytes wiring that up).
Additionally, unnecessary ELF sections are nuked.
Building
You can build Dinky on a x86_64 Linux machine with make install. You need a recent Zig 0.12+ compiler to build this.
Running
To serve index.html on the default port 8088, run make serve
Which simply runs ./dinky 8088 index.html text/html
Dinky is obviously not intended for production use (no https, single-file serving), as it's just a little elf-golfing experiment.
The _start of the journey
First order of business is to provide the entry-point function, whose symbol name we can find like this:
$ ld -verbose | grep ENTRY
ENTRY(_start)
Linux will call this function after loading the ELF image, passing argc and argv on the stack:
var argc_argv_ptr: [*]usize = undefined;
export fn _start() callconv(.Naked) noreturn {
asm volatile (
\\ xorl %%ebp, %%ebp
\\ movq %%rsp, %[argc_argv_ptr]
: [argc_argv_ptr] "=m" (argc_argv_ptr),
: [startServer] "X" (&startServer),
);
}
fn startServer() callconv(.C) noreturn {
const argc = argc_argv_ptr[0];
const argv = @as([*][*:0]const u8, @ptrCast(argc_argv_ptr + 1));
...
}
First we zero our ebp, which most libc implementation do. This serves as a
callstack marker for debuggers.
Next, we extract argc and argv from the stack. This is essentially what Zig's startup
code does when targetting x86_64 architecture.
After that, implementing the web server is a matter of calling the appropriate syscalls.
For this, we create a generic syscall function that uses inline assembly to pass
arguments according to the System V ABI. For each syscall we need, such as bind and
listen, we create a corresponding Zig function wrapper. Some of these are marked
as inline as that (sometimes) produce a smaller binary.
What's in the ELF?
Not much:
$ objdump -trh dinky
dinky: file format elf64-x86-64
Sections:
Idx Name Size VMA LMA File off Algn
0 .text 000003d4 0000000001001158 0000000001001158 00000158 2**2
CONTENTS, ALLOC, LOAD, READONLY, CODE
1 .data 00000087 000000000100252c 000000000100252c 0000052c 2**0
CONTENTS, ALLOC, LOAD, DATA
2 .bss 00000008 00000000010025b8 00000000010025b8 000005b3 2**3
ALLOC
SYMBOL TABLE:
no symbols
where the .data section is just the string literals:
$ objdump -s -j .data dinky
dinky: file format elf64-x86-64
Contents of section .data:
100252c 48545450 2f312e31 20323030 204f4b0d HTTP/1.1 200 OK.
100253c 0a436f6e 6e656374 696f6e3a 20636c6f .Connection: clo
100254c 73650d0a 436f6e74 656e742d 6c656e67 se..Content-leng
100255c 74683a20 000d0a43 6f6e7465 6e742d74 th: ...Content-t
100256c 7970653a 20000d0a 0d0a0053 65727665 ype: ......Serve
100257c 72206572 726f720a 00557361 67653a0a r error..Usage:.
100258c 20202020 2e2f6469 6e6b7920 38303838 ./dinky 8088
100259c 20696e64 65782e68 746d6c20 74657874 index.html text
10025ac 2f68746d 6c0a00 /html..
Next steps
If you wanna play around with this, here's a few ideas for next steps:
- Add support for reading requests, headers and serving multiple files
- Use the sendfile syscall for zero-copy file serving
- Optimize the code for size even further
[1] The favicon is embedded into the sample webpage, as Dinky is a single-file server. The original favicon file was generated by an online tool and clocked in at 15406 bytes