M3 Operating System Development

A chronicle of the development of the M3 operating system

Tutorial – Building an i386-elf cross-compiler and binutils on OS X

with 2 comments

As I mentioned in my previous post, I managed to build a cross-compiler and binutils toolchain targeting i386-elf executables on OS X. While the process isn’t too difficult, there are a couple of gotchas that you should be aware of.

Before I start, I’d like to reference this tutorial for building an ARM cross-compiler on OS X. It provided a pretty good roadmap for me as I set out on my task.

OK – the first thing that you need to do is get the source code for both gcc and binutils. To do this, we’ll use a handy tool called wget. Unfortunately, OS X does not come with wget as one of its pre-installed apps, so we’ll have to install it. You can grab a copy of wget compiled for OS X here. Follow the instructions in the README to install it into the proper directories and add the proper folders to your PATH.

If you don’t want to install wget, OS X comes with a similar utility called curl. If you are comfortable using that tool, then adjust the wget commands below accordingly.

Now, drop to a command prompt and issue the following commands to download and extract the source code for binutils and gcc:

$ mkdir ~/crossgcc && cd ~/crossgcc
$ wget ftp://sourceware.org/pub/binutils/snapshots/binutils-2.18.50.tar.bz2
$ tar jxf binutils-2.18.50.tar.bz2
$ wget http://ftp.gnu.org/pub/gnu/gcc/gcc-4.2.3/gcc-4.2.3.tar.bz2
$ tar jxf gcc-4.2.3.tar.bz2

I've chosen a target directory of  /usr/local/i386elfgcc

We’ll have to create that directory and make it world writable for now:

$ sudo mkdir /usr/local/i386elfgcc
$ sudo chmod 777 /usr/local/i386elfgcc

First we build the binutils:

$ mkdir build-binutils && cd build-binutils
$ ../binutils-2.18.50/configure --target=i386-elf 
--prefix=/usr/local/i386elfgcc/ 2>&1 | tee configure.log
$ make all install 2>&1 | tee make.log

The compiler will do its thing for a few minutes. When it’s done, your cross-compiled binutils will be installed in the bin folder of the target folder.

Now we'll build the gcc compiler with C support. This is someplace that we need to specialize the configuration for OS X. Be sure to include the parameter --disable-libssp when you are running configure for gcc. Otherwise, your build will blow up with errors about libssp.

$ cd ../gcc-4.2.3
$ cd ..
$ mkdir build-gcc && cd build-gcc
$ ../gcc-4.2.3/configure --target=i386-elf 
--prefix=/usr/local/i386elfgcc/   
--with-gnu-as --with-gnu-ld --disable-libssp --enable-languages=c 2>&1 | tee configure.log
$ make all install 2>&1 | tee make.log

The compiler will again do its thing for a few minutes. When it is done, your cross-compiled gcc will be installed in the bin folder of the target folder.

All the cross-compiled tools in our dev toolchain are now installed into

/usr/local/i386elfgcc/bin

Because they are cross-compiled versions, they have different names from the standard tools, to avoid naming conflicts. For example, our cross-compiled gcc is called i386-elf-gcc. Basically, all the tools have the prefix i386-gcc.

If you’d like these tools to be generally accessible, you can add the target path to your PATH, in .bash_profile or a similar shell configuration file.

You’ve now got a complete cross-compiled development toolchain for targeting the IA-32 platform on OS X. Pat yourself on the back, and if you’re feeling especially amazed and encouraged by the many excellent free development tools available with source, show your support by donating to the Free Software Foundation.

About these ads

Written by m3os

March 29, 2009 at 1:57 pm

2 Responses

Subscribe to comments with RSS.

  1. Hello,

    I’ve made a beta package for an ELF cross-compiler on OS X, that provides binutils, gcc, and g++ for i386, ppc, arm, and sparc targets. I made it for AWOS (the operating system project I maintain), but it should be useable by any kernel (I’ve tested it with another one, Snowflake, too). You can find it on the AWOS downloads page.

    Thanks for this tutorial, it helped me figure out how to get binutils to compile.

    Andrew

    Andrew Wilcox

    May 11, 2009 at 3:07 pm

  2. […] searched the net and found this interesting page by M3 Operating System Development where it was described how to build an i386 cross-compiler. I thought that this could be a way to […]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: