Writing a Linux ELF Binary by Hand
We write a 164 bytes long x86-64 ELF binary, a proper "Hello World" executable.
Code: https://github.com/simonracz/elf-binary
Video: https://youtu.be/JM9jX2aqkog
I added a WIP port of the mold linker to openbsd-wip for those interested in trying it out.
Read the TODO file before using!
https://github.com/jasperla/openbsd-wip/commit/474de666318b2b3516fc011f6bdca2c7c118473e
Apple has released a new linker, and it's pretty fast, https://twitter.com/davidecci/status/1665835119331135488 and https://twitter.com/rui314/status/1665929739155177472.
They claim it's 5x faster than ld64.
Image showing a benchmark from Rui (mold's author).
It's a good time to continue my work on `weld`, https://fosstodon.org/@hywan/109783158363196851 and https://github.com/Hywan/weld.
My biggest difficulty with linkers is… understanding their errors! I know I'm not alone.
That's why `weld` must be exemplary on errors, period.
Please welcome `weld-errors`, https://github.com/Hywan/weld/compare/b7fde2dbf9fb523dccb041063bfd030967f5e814...4a41bd666c6dc8b76a59bf7c127247312e84127e.
What's new?
* Any error contains a code, a (formatted) message, and a help message,
* `weld` pretty prints those errors,
* `weld --explain <error_code>` gives detailed diagnostics,
* Automatic awesome documentation.
See the screenshots.
`weld` now takes a `--target <triple>` argument. `weld` is designed to be cross-platform entirely, hence it's legit to be able to specify a target triple, https://github.com/Hywan/weld/commit/b7fde2dbf9fb523dccb041063bfd030967f5e814.
Based on the provided target triple, `weld` will use a particular linker strategy (e.g Elf, MachO, Coff, Wasm etc.).
$ weld --target x86-64-unknown-linux <input> -o <output> # = Elf
$ weld --target aarch64-apple-darwin <input> -o <output> # = MachO
Cool huh?
Yesterday, I’ve also added support for big- and little-endian. All parser combinators can now handle endianness based on a generic type + trait, https://github.com/Hywan/weld/commit/5a1ff9f9643fe6b82e7b789e4c2cca7ee6615024.
It’s magic. Rust is cool.
So far, I’m writing the Elf64 parser. The goal is to get zero copy, period.
Yesterday I’ve added section’s data and name, still with zero copy, https://github.com/Hywan/weld/blob/bfb9fd55c5b2f9114e8f8ab21c5f49d48f9c3b98/crates/parser/src/elf64/mod.rs#L720.
It relies heavily on Rust lifetimes, and bstr to get bytes-based string-ish. The parser is written with nom, and is manipulating bytes slices only.
bstr: https://blog.burntsushi.net/bstr/
nom: https://github.com/rust-bakery/nom
I’m starting a new project to learn about linkers. It’s called ˋweld`, and it lives here https://github.com/Hywan/weld.
I do this on my free time; understand very sporadically.
This toot is a thread to show progress or to ask help.
Basic elf64 parser
Enable ThinLTO for rustc on `x86_64-apple-darwin`, https://github.com/rust-lang/rust/pull/103647.