Monday, August 26, 2013

Building/Running Android Goldfish kernel

Among the tasks for build an Android kernel development, building the Android Goldfish kernel is the easiest :-) (I mean, no compiling errors...)

First, you need to have access to the cross-compiling tools. The Android source code provides them in folder /prebuilt/linux-x86/toolchain/. 

Steps


1. Set the path to include the pre-build toolchain
   # export PATH=/prebuilt/linux-x86/toolchain/arm-eabi-4.4.3/bin/:$PATH
2. Set the Kernel config file. In this case, we are compiling for the ARM emulator (armv7).
   # make ARCH=arm goldfish_armv7_defconfig
3. Run make
   # CROSS_COMPILE=/prebuilt/linux-x86/toolchain/arm-eabi-4.4.3/bin/arm-eabi- ARCH=arm make
4. Change var ANDROID_PRODUCT_OUT
   # export ANDROID_PRODUCT_OUT=/out/target/product/generic/
5. Run the emulator
   # /out/host/linux-x86/bin/emulator-arm -system /out/target/product/generic/system.img -kernel /goldfish/arch/arm/boot/zImage -data /out/target/product/generic/userdata.img -ramdisk /out/target/product/generic/ramdisk.img -skindir /sdk/emulator/skins/ -skin HVGA -verbose -show-kernel

Issues


  • The option CONFIG_MODULES is NOT enabled by default. If you want to insert modules into the kernel, make sure to set CONFIG_MODULES;
  • Android emulator does NOT work with Kernels > 2.6.X So, make sure to use the proper Kernel. For devices, any support Kernel will work.


Building Android ICS


To compile ICS (and probably any new version of Android platform >= 4.0), you
might have some problems with the compilation tools.

  • Follow the instructions described on http://source.android.com/source/building.html
    • It also provides information about Android Kernel building.
  • Check which compiler best fits into the version of Android platform you are using.
    • Usually, due to new features (for example, C++11 support on GCC/++ 4.7), it is a bit more complicated to build it with new compilers... you will see several compiling errors. For example, for the ICS, GCC/++ 4.6 works better than GCC/++ 4.7.
  • You may need some changes in order to compile it properly (mostly on Makefile). Again, depending on your compiler, you may apply more or less changes on your code.
  • For ICS, GCC/++ 4.6 works better.
This link provides a good discussion about it (and solutions!). In my case, for compiling ICS, I made only a few changes (see below) and I also used GCC/++ 4.6.

=================================================================

project build/
diff --git a/core/combo/HOST_linux-x86.mk b/core/combo/HOST_linux-x86.mk
index 5ae4972..7df2893 100644
--- a/core/combo/HOST_linux-x86.mk
+++ b/core/combo/HOST_linux-x86.mk
@@ -53,6 +53,6 @@ HOST_GLOBAL_CFLAGS += \
  -include $(call select-android-config-h,linux-x86)
 # Disable new longjmp in glibc 2.11 and later. See bug 2967937.
-HOST_GLOBAL_CFLAGS += -D_FORTIFY_SOURCE=0
+HOST_GLOBAL_CFLAGS += -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=0
 HOST_NO_UNDEFINED_LDFLAGS := -Wl,--no-undefined

project dalvik/
diff --git a/vm/native/dalvik_system_Zygote.cpp b/vm/native/dalvik_system_Zygote.cpp
index 31fecfd..b3b745a 100644
--- a/vm/native/dalvik_system_Zygote.cpp
+++ b/vm/native/dalvik_system_Zygote.cpp
@@ -19,6 +19,7 @@
  */
 #include "Dalvik.h"
 #include "native/InternalNativePriv.h"
+#include "sys/resource.h"
 #include
 #include

project development/
diff --git a/tools/emulator/opengl/host/renderer/Android.mk b/tools/emulator/opengl/host/renderer/Android.mk
index 55fcb80..613179c 100644
--- a/tools/emulator/opengl/host/renderer/Android.mk
+++ b/tools/emulator/opengl/host/renderer/Android.mk
@@ -6,6 +6,8 @@ $(call emugl-import,libOpenglRender)
 LOCAL_SRC_FILES := main.cpp
 LOCAL_CFLAGS    += -O0 -g
+LOCAL_LDLIBS += -lX11
+
 #ifeq ($(HOST_OS),windows)
 #LOCAL_LDLIBS += -lws2_32
 #endif

project external/gtest/
diff --git a/include/gtest/internal/gtest-param-util.h b/include/gtest/internal/gtest-param-util.h
index 5559ab4..0af13b0 100644
--- a/include/gtest/internal/gtest-param-util.h
+++ b/include/gtest/internal/gtest-param-util.h
@@ -39,6 +39,7 @@
 #include
 #include
+#include
 #if GTEST_HAS_PARAM_TEST
diff --git a/src/Android.mk b/src/Android.mk
index 2465e16..1d93ac8 100644
--- a/src/Android.mk
+++ b/src/Android.mk
@@ -49,7 +49,7 @@ LOCAL_SRC_FILES := gtest-all.cc
 LOCAL_C_INCLUDES := $(libgtest_host_includes)
-LOCAL_CFLAGS += -O0
+LOCAL_CFLAGS += -O0 -fpermissive
 LOCAL_MODULE := libgtest_host
 LOCAL_MODULE_TAGS := eng
@@ -67,7 +67,7 @@ LOCAL_SRC_FILES := gtest_main.cc
 LOCAL_C_INCLUDES := $(libgtest_host_includes)
-LOCAL_CFLAGS += -O0
+LOCAL_CFLAGS += -O0 -fpermissive
 LOCAL_STATIC_LIBRARIES := libgtest

project external/llvm/
diff --git a/llvm-host-build.mk b/llvm-host-build.mk
index 5219efd..cc91ee0 100644
--- a/llvm-host-build.mk
+++ b/llvm-host-build.mk
@@ -44,6 +44,8 @@ LOCAL_C_INCLUDES := \
 LOCAL_IS_HOST_MODULE := true
+LOCAL_LDLIBS := -lpthread -ldl
+
 ###########################################################
 ## Commands for running tblgen to compile a td file
 ###########################################################

project external/oprofile/
diff --git a/libpp/format_output.h b/libpp/format_output.h
index b6c4592..8e527d5 100644
--- a/libpp/format_output.h
+++ b/libpp/format_output.h
@@ -91,7 +91,7 @@ protected:
  symbol_entry const & symbol;
  sample_entry const & sample;
  size_t pclass;
- mutable counts_t & counts;
+ counts_t & counts;
  extra_images const & extra;
  double diff;
  };

project frameworks/base/
diff --git a/libs/utils/Android.mk b/libs/utils/Android.mk
index 638f72f..127cad9 100644
--- a/libs/utils/Android.mk
+++ b/libs/utils/Android.mk
@@ -61,7 +61,7 @@ LOCAL_SRC_FILES:= $(commonSources)
 LOCAL_MODULE:= libutils
-LOCAL_CFLAGS += -DLIBUTILS_NATIVE=1 $(TOOL_CFLAGS)
+LOCAL_CFLAGS += -DLIBUTILS_NATIVE=1 $(TOOL_CFLAGS) -fpermissive
 LOCAL_C_INCLUDES += external/zlib
 ifeq ($(HOST_OS),windows)
diff --git a/tools/aapt/Android.mk b/tools/aapt/Android.mk
index a3e5d9a..4c415d6 100644
--- a/tools/aapt/Android.mk
+++ b/tools/aapt/Android.mk
@@ -28,7 +28,7 @@ LOCAL_SRC_FILES := \
     ZipFile.cpp
-LOCAL_CFLAGS += -Wno-format-y2k
+LOCAL_CFLAGS += -Wno-format-y2k -fpermissive
 LOCAL_C_INCLUDES += external/expat/lib
 LOCAL_C_INCLUDES += external/libpng

project frameworks/compile/slang/
diff --git a/Android.mk b/Android.mk
index fce3637..8ffe68f 100644
--- a/Android.mk
+++ b/Android.mk
@@ -19,7 +19,7 @@ ifeq ($(TARGET_BUILD_APPS),)
 LOCAL_PATH := $(call my-dir)
-local_cflags_for_slang := -Wno-sign-promo -Wall -Wno-unused-parameter -Werror
+local_cflags_for_slang := -Wno-sign-promo -Wall -Wno-unused-parameter
 ifneq ($(TARGET_BUILD_VARIANT),eng)
 local_cflags_for_slang += -D__DISABLE_ASSERTS
 endif

Android (Goldfish) kernel development

Finally, back to this :-)

Lately, I have being developing a new project which basically needs some kernel changes on Android Goldfish kernel. Initially, I thought that this would be something like "traditional" kernel development, but NOT. Android kernel is, of course, a Linux kernel but it has some peculiarities that impact on the development phase.

[NOTE] I'd rather provide a link to the source that I used than replicating the same information.

So, I split my experience into the following topics:
  1. Building Android ICS
  2. Building/Running Android Goldfish kernel 
  3. Creating a Goldfish kernel module
  4. Adding new syscalls into the Goldfish kernel

Thursday, January 24, 2013

Moving...

My life has changed upside down recently: I have been accepted to Saarland University Graduate School and I will move to Germany soon. I will try to keep this updated as possible.