cstack
cstack

Reputation: 2232

How to create an ELF executable on Mac OS X?

I'm following this tutorial: https://littleosbook.github.io/#linking-the-kernel

and I'm on a Mac. I'm at the point where I have an object file created from nasm, and I want to turn it into an ELF executable using ld. The ld on a mac doesn't look like it supports the ELF format, but I don't want to run a virtual machine with Ubuntu just to do this link step.

Is it possible to install GNU ld on mac and run it alongside Mac's ld ?

Upvotes: 7

Views: 7113

Answers (1)

nneonneo
nneonneo

Reputation: 179392

Yes, you can. ld is part of GNU binutils. You can build and install it as follows:

wget -nc https://ftp.gnu.org/gnu/binutils/binutils-2.27.tar.gz
tar xzf binutils-2.27.tar.gz
cd binutils-2.27
mkdir build && cd build
../configure --prefix=${HOME}/.local/binutils --target=i386-unknown-linux-gnu
make -j7
make install

That installs ld to ~/.local/binutils/bin/ld. If you want a 64-bit binutils, use --target=x86_64-unknown-linux-gnu.

Upvotes: 9

Related Questions