Machop
Over the past couple weeks I've been building a linker for Mach-O executables called machop. It's still a long way from having any semblance of being ready but I thought I'd do a bit of a write up about it and why I decided to build it just so that I can look back on it.
Why?
Basically, I know nothing about linkers. I've been writing a lot of
Rust lately, for personal projects and work, and compile times is
something that is a problem. There are lots of things we can do to
improve that and one of the things that commonly comes up is using a
different linker like mold or
lld. I haven't
spent much time digging into lld
but I've been using mold
as a
recipe to understand what linkers do - the code is reasonably
accessible but as someone totally unfamiliar with C++ I have been
having a tough time with it. Anyway, using a different linker is a
common piece of advice and I wanted to know more about what exactly it
does and how these can speed it up. It's also just an interesting
thing to do. I've wanted to do some more systems programming related
projects and writing a linker is a common project in OS classes at
unis. I never got to do this so why not do it now?
Why Mach-O?
I think I'd probably have an easier time creating an ELF
linker. mold
is predominately an ELF linker. There is also gold
to
look at. However, my day-to-day system is a Mac so writing one for
Mach-O is just easier because it is what I've got access to (without
having to run in Docker or a VM). Being a more closed format there
seems to be less material on Mach-O than ELF, especially material that
is accessible to someone with minimal exposure to linking and object
file concepts. There are still quite a few blog posts and Apple has
documentation on the format. Coupled with reading existing linkers
things are coming along okay.
What do I hope to get out of it?
- Learn about how linkers work - a lot of the high level stuff should be transferable to linking any type of object file.
- See if there are gains that can be made from using more performent linkers in my Rust projects.
- Break into the fun world of systems programming, in particular how debuggers and other things that have to deal with object files.
Useful resources
Here are some useful resources, mostly related to Mach-O in particular, that I've been using:
mold
's Mach-O linking implementation: https://github.com/rui314/mold/tree/bd584ac05eb58c518cbf0551cfae6ae9c4cd9565/macho- Ian Lance Taylor's linker series: https://lwn.net/Articles/276782/
- Aidan Steele's Mach-O file format reference: https://github.com/aidansteele/osx-abi-macho-file-format-reference
- m4b's Mach-O Binaries article: http://www.m4b.io/reverse/engineering/mach/binaries/2015/03/29/mach-binaries.html
m4b is also the person who wrote and maintains the goblin which I've been using to parse and navigate Mach-O files.
Edit (2023-06-08): The Mach-O linker in mold has been moved to sold