Demistifying linking: let's implement a static ELF linker together
This workshop is all about linkers, on Linux, in Zig. In 4h you will write a working basic ELF linker in Zig for static x86_64 ELF binaries. We will explore the fundamental building blocks of any linker:
- What is an (relocatable) object file and how do we parse it? Actually, what do we need to parse in the first instance?
- What is an input symbol and how do we resolve undefined symbols across multiple input object files?
- What is the dreaded relocation you have been hearing about all this time?
- How do we allocate extracted symbols, and fix up those dreaded relocations? What happens if we don’t? Experimentation and segfaults are your best friend when writing a linker, and we will learn how we can use them to quickly narrow down where the mislinking happened.
- If we have the time, we will attempt linking against static musl libc.
- If we are doing incredibly well, we will learn how to deal with thread-local storage too which is a prerequisite for handing panics in Zig.
Since this workshop is all about linking ELF binaries targeting Linux, it would be easiest done on an x86_64 Linux laptop. However, writing a linker doesn’t require a Linux host, only a guest to verify that the linked programs actually start up, so a server with access to a Linux instance will be provided.
During this workshop, we will only get to write the more interesting parts of the linker - the boring parts will be prepared in advance. To make the workshop extra useful and interesting, we will purposely experiment with incorrect output a lot to demonstrate how to reason about the failures and mislinking, and use it to actually identify and solve the underlying problem.