How to compile C to raw binary in OSX?

1k views Asked by At

Im following a tutorial on how to create an OS from scratch. I'm at the point where I need to start writing code in C but I need to compile it, in free standing mode, to raw binary. The commands given are:

gcc -ffreestanding -c kernel.c -o kernel.o
ld -o kernel.bin -Ttext 0x1000 kernel.o --oformat binary

The first command works, the second doesn't. OSX's linker doesn't know anything about -Ttext. Anyone know a way around this without using a virtual linux system? There must be a way.

objcopy doesn't work either.

1

There are 1 answers

0
Chuck Onwuzuruike On

I downloaded the GNU utils using homebrew and that came with gobjcopy which I used. I got the download information from here: https://www.topbug.net/blog/2013/04/14/install-and-use-gnu-command-line-tools-in-mac-os-x/

I just pasted all the installatoin commands into a .sh file and ran it.

The commands I ended up using after installing were:

gcc -ffreestanding -c kernel.c -o kernel.o
gobjcopy -O binary kernel.o kernel.bin