Tag Archives: goldfish

Compiling the Android emulator kernel – the easy way

I have recently posted a series on how to build your Android kernel emulator for arm, x86 and mips. You can do this manually to have full control or you can use a script Google provides in external/qemu/distrib/build-kernel.sh.

First you need to download the goldfish kernel and check out the 2.6.29 branch:

$ git clone https://android.googlesource.com/kernel/goldfish.git
$ cd goldfish
$ git checkout -b 2.6.29 origin/android-goldfish-2.6.29

The Android source tree has a script to compile the kernel in external/qemu/distrib/build-kernel.sh. You need to be in the root of the kernel directory and set the architecture you want to build for.

For arm:

$ ${ANDROID_BUILD_TOP}/external/qemu/distrib/build-kernel.sh --arch=arm

For x86:

$ ${ANDROID_BUILD_TOP}/external/qemu/distrib/build-kernel.sh --arch=x86

For mips:

$ ${ANDROID_BUILD_TOP}/external/qemu/distrib/build-kernel.sh --arch=mips

This will build the kernel in the kernel directory. You can access the zImage/bzImage and vmlinux images as usual from the kernel directory or from the /tmp/kernel-qemu directory. You can start the emulator using one of these images.

For more details you can check the posts on how to build the emulator for arm, x86 or mips.

How to compile the kernel for the mips emulator

If using the Android precompiled kernel for mips is not enough for you, you can compile your own kernel from source.

First you need to download the Android source tree to get the mips emulator.

Next you need to download the emulator kernel source code (the 2.6.29 branch of the goldfish kernel):

$ git clone https://android.googlesource.com/kernel/goldfish.git
$ cd goldfish
$ git checkout -b 2.6.29 origin/android-goldfish-2.6.29

There are instructions on how to build the kernel for the arm emulator on Google’s site. This is what you have to do for mips:

$ export CROSS_COMPILE=${ANDROID_BUILD_TOP}/prebuilts/gcc/linux-x86/mips/mipsel-linux-android-4.6/bin/mipsel-linux-android-
$ export ARCH=mips
$ export SUBARCH=mips
$ make goldfish_defconfig
$ make

When you’re done building the kernel, you can start the emulator with the newly compiled mips image. When building the kernel for mips you do not get a bzImage or zImage, so we’ll have to use vmlinux.

$ cd ${ANDROID_BUILD_TOP}
$ emulator -kernel ~/workspace/android/goldfish/vmlinux -wipe-data &

To check that you really have your own compiled kernel version, connect with adb to the emulator and check /proc/version:

adb shell cat /proc/version
Linux version 2.6.29-gf1ef1c8 (yaap@yaap-desk) (gcc version 4.6 20120106 (prerelease) (GCC) ) #3 Sun Sep 30 19:14:25 EEST 2012

You can see your username and hostname in the description for the running Linux Kernel.

How to compile the kernel for the arm emulator

If you want to compile the kernel for the arm emulator, you first need to read Google’s instructions on how to compile the kernel. Except from following these instructions you will have to fix one issue to run the arm emulator with the new kernel.

Google’s instructions

First you need to download the Android emulator kernel sources. The stable branch is 2.6.39 so this is what we are going to use.

$ git clone https://android.googlesource.com/kernel/goldfish.git
$ cd goldfish
$ git checkout -b 2.6.29 origin/android-goldfish-2.6.29

Google’s site gives detailed instructions on how to compile the kernel for arm:

$ export CROSS_COMPILE=${ANDROID_BUILD_TOP}/prebuilts/gcc/linux-x86/arm/arm-linux-androideabi-4.6/bin/arm-linux-androideabi-
$ export ARCH=arm
$ export SUBARCH=arm
$ make goldfish_defconfig
$ make

${ANDROID_BUILD_TOP} should point to the top of your Google Android tree. It should be set automatically if you have the Google environment set. In order to do that you need to run source build/envsetup.sh and lunch full-eng.

You just need to start the arm emulator with the newly compiled kernel:

$ cd ${ANDROID_BUILD_TOP}
$ emulator -kernel ~/workspace/android/goldfish/arch/arm/boot/zImage -wipe-data &

First (and only) problem: arm emulator does not boot

After the steps mentioned above, the emulator will not boot. Seems that the goldfish_defconfig is not working. The solution is to use goldfish_armv7_defconfig instead:

$ make goldfish_armv7_defconfig

How to compile the kernel for the x86 emulator

If you want to compile the x86 kernel for the emulator, you first need to read Google’s instructions on how to compile the kernel. Just by following these instructions you will not be able to successfully build the kernel because you will run into a few issues.

Google’s instructions

First you need to download the Android emulator kernel sources. The stable branch is 2.6.39 so this is what we are going to use.

$ git clone https://android.googlesource.com/kernel/goldfish.git
$ cd goldfish
$ git checkout -b 2.6.29 origin/android-goldfish-2.6.29

Google’s site only has instructions for arm, but you can easily adapt them to x86:

$ export CROSS_COMPILE=${ANDROID_BUILD_TOP}/prebuilts/gcc/linux-x86/x86/i686-linux-android-4.6/bin/i686-linux-android-
$ export ARCH=x86
$ export SUBARCH=x86
$ make goldfish_defconfig
$ make

${ANDROID_BUILD_TOP} should point to the top of your Google Android tree. It should be set automatically if you have the Google environment set. In order to do that you need to run source build/envsetup.sh and lunch full_x86-eng.

First problem: you need to set your cross-compiler properly

If you will follow the steps mentioned above, you get the following error:

warning: SSE instruction set disabled, using 387 arithmetics [enabled by default]

The solution is to set the cross compiler as mentioned below:

$ export CROSS_COMPILE=${ANDROID_BUILD_TOP}/external/qemu/distrib/kernel-toolchain/android-kernel-toolchain-
$ export REAL_CROSS_COMPILE=${ANDROID_BUILD_TOP}/prebuilts/gcc/linux-x86/x86/i686-linux-android-4.6/bin/i686-linux-android-

As an alternative, you can use the build-kernel.sh script to compile the kernel:

$ ${ANDROID_BUILD_TOP}/external/qemu/distrib/build-kernel.sh --arch=x86

Second problem: you need a workaround for the GCC 4.6 toolchain

After solving the first problem, you will still get a build error:

i686-linux-android-gcc: error: elf_i386: No such file or directory

This is a bug in the GCC 4.6 toolchain.
A fast workaround is to change arch/x86/vdso/Makefile by replacing -m elf_i386 with -m32 and -m elf_x86_64 with -m64.

$ sed -i "s/-m elf_i386/-m32/g" arch/x86/vdso/Makefile
$ sed -i "s/-m elf_x86_64/-m64/g" arch/x86/vdso/Makefile