Thursday, April 15, 2010

Cross compiling Linux on MAC OS X

This is a quick guide how to compile Linux kernels on Mac OSX.
First of all, get MacPorts (http://www.macports.org/) and install the following packages: git-core and libelf. These ports provide tools for download of Linux source code and libraries for handling of ELF files. (needed since Mac OSX uses Mach-O)
$ sudo port install libelf git-core

Then get Linux sources by cloning the linux source tree using git. Note: The volume used for Linux kernel compilation need to have a case sensitive file system. If this is not the case, create a new virtual volume with case sensitive fs on and mount it.
$ git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git linux-2.6

Change to the new directory linux-2.6 and update.
$ git pull

Install a cross compiler. In this post I use the cross arm-eabi- compiler created in the post below. MacPorts has an arm-elf- cross compiler that can be used if you do not want to compile one by yourself. Search for "arm-elf".

With the build infrastructure is in place it is time to configure a kernel. Start with coping a well known kernel configuration for your target to ”.config”. I use an ARM9 based development board that is close to KwikByte's KB9202 development board and thus I do the following:
$ cp arch/arm/configs/kb9202_defconfig .config

Try to build kernel. Don't forget to specify architecture and path to cross compiler.
$ make ARCH=arm CROSS_COMPILE=/opt/arm-none-eabi/bin/arm-none-eabi-

The build will fail due to missing "malloc.h" file. This header file is deprecated and not available on Mac. Replace all occurrences of this filename in all files in scripts/genksyms/ with "stdlib.h.".
$ sed -i .backup 's/malloc.h/stdlib.h/' *.c* 

If you try to build again it will still fail due to a missing elf.h file. A GNU alternative to this elf.h file can be found in /opt/local/include and is called "gelf.h". (installed by MacPorts libelf port.) Since the compiler has no search path set to the include directory of MacPorts per default it is required to alter ”HOSTCFLAGS” to include search path to MacPorts include dir. The missing file wasn't gelf.h but elf.h. Create a dummy ”elf.h” file that just includes ”gelf.h” + defines some missing ELF definitions. Like this:
/* ELF library in MAC ports is called gelf.h */
#include < gelf.h >

/* The following defines are needed to compensate for the following errors:
HOSTCC  scripts/mod/modpost.o
scripts/mod/modpost.c: In function ‘addend_386_rel’:
scripts/mod/modpost.c:1330: error: ‘R_386_32’ undeclared (first use in this function)
scripts/mod/modpost.c:1330: error: (Each undeclared identifier is reported only once
scripts/mod/modpost.c:1330: error: for each function it appears in.)
scripts/mod/modpost.c:1333: error: ‘R_386_PC32’ undeclared (first use in this function)
scripts/mod/modpost.c: In function ‘addend_arm_rel’:
scripts/mod/modpost.c:1348: error: ‘R_ARM_ABS32’ undeclared (first use in this function)
scripts/mod/modpost.c:1353: error: ‘R_ARM_PC24’ undeclared (first use in this function)
scripts/mod/modpost.c: In function ‘addend_mips_rel’:
scripts/mod/modpost.c:1371: error: ‘R_MIPS_HI16’ undeclared (first use in this function)
scripts/mod/modpost.c:1375: error: ‘R_MIPS_LO16’ undeclared (first use in this function)
scripts/mod/modpost.c:1378: error: ‘R_MIPS_26’ undeclared (first use in this function)
scripts/mod/modpost.c:1381: error: ‘R_MIPS_32’ undeclared (first use in this function)
make[2]: *** [scripts/mod/modpost.o] Error 1
make[1]: *** [scripts/mod] Error 2
*/

#define R_386_NONE        0
#define R_386_32          1
#define R_386_PC32        2
#define R_ARM_NONE        0
#define R_ARM_PC24        1
#define R_ARM_ABS32       2
#define R_MIPS_NONE       0
#define R_MIPS_16         1
#define R_MIPS_32         2
#define R_MIPS_REL32      3
#define R_MIPS_26         4
#define R_MIPS_HI16       5
#define R_MIPS_LO16       6

Add the path of this file to list of include directories. For this guide I put elf.h in the Linux top directory. The final build command now look like this:
$ make ARCH=arm CROSS_COMPILE=/opt/arm-none-eabi/bin/arm-none-eabi- HOSTCFLAGS="-I/opt/local/include/ -I."

4 comments:

Miguel Luis said...

Hi! What are this values on the defines? Where do you get those?

"#define R_386_NONE 0
#define R_386_32 1
#define R_386_PC32 2
#define R_ARM_NONE 0
#define R_ARM_PC24 1
#define R_ARM_ABS32 2
#define R_MIPS_NONE 0
#define R_MIPS_16 1
#define R_MIPS_32 2
#define R_MIPS_REL32 3
#define R_MIPS_26 4
#define R_MIPS_HI16 5
#define R_MIPS_LO16 6"

Thanks! :)

Henric Lindén said...

Hi Miguel,

When using MacPorts some GNU files that conflicts with the build-in Mac files are prefixed with a 'g'. E.g. GNU make will be installed as 'gmake' instead of 'make'. This goes also for include files. 'elf.h' is installed as 'gelf.h'.

But 'gelf.h' do not hold all values needed for creating cross compilers. The additional defines are derived from the Linux kernel. Eg. R_MIPS_16 is defined in arch/mips/include/asm/elf.h and the R_ARM_NONE in arch/arm/include/asm/elf.h.

Miguel Luis said...

I appreciate your fast response. Your post was an essential source for the work I've been doing all night and so I wanted to justify the origin of those defines. :)

Thank you Henric.

Unknown said...

Haha, look at this, years later, you helped yet another person. I am cross compiling from OSX to Android ARM and was stuck on the usual missing elf.h. This solution got me 90% of the way. If anyone is interested in fully compiling the android kernel on OSX take a look here: https://medium.com/@antonvattay/building-android-6-0-kernel-on-osx-the-missing-parts-cc2b2a935ded#.xhtdy28x7

Thank you!