Macho Elves

Most of my time working on neutrino these days is spent generating object files. I had originally planned to generate executables directly and had a limited version of that working. However, I quickly ran into trouble with dynamic linking, which is required as soon as you want your program to interact with the surrounding system in any way.

Eventually I learned that you’re much better off interacting with the system through calls to the standard C library because libc is more stable than the underlying system call API. And you’re much better off letting the system linker turn your object files into executables than generating those executables yourself. It’s not that it’s impossible to do it yourself but it’s painful and I’m fine with letting the linker developers deal with that pain.

So now I’m working on generating native object files which means that I’ve spent a lot of time poring over the Mach-O file format reference and, lately, the ELF specification. They’re nice straightforward formats actually but it still takes a lot of time to shape the program output into the required form. The tools for working with object files are really good, otool and nm on mac and readelf on linux, and make this work a lot easier.

The current status is that you can build .o files on mac and link them into executables that can be run to successfully. As in

$> scons neuneu
[scons output]

$> ./bin/obj/neuneu.ia386.mach-o
int32 comparison
int32 arith
raw malloc
struct malloc
static data

What’s running there is neutrino code, compiled by neutrino code.

What I’m working on now is being able to do the same on linux and then the next step, the ability to store nontrivial data structures in object files. I will also need to eventually generated DLLs but since mac and linux are the only platforms I use regularly they’re the ones I’ll start with.

Leave a Reply

Your email address will not be published. Required fields are marked *