Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create an ELF executable on Mac OS X?

Tags:

macos

ld

elf

nasm

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 ?

like image 647
cstack Avatar asked Aug 20 '16 23:08

cstack


1 Answers

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.

like image 192
nneonneo Avatar answered Sep 17 '22 19:09

nneonneo