[Tutorial] How to compile a kernel module outside the kernel

Search This thread

viulian

Inactive Recognized Developer
Apr 6, 2011
419
411
hex.ro
I've decided to make a short tutorial and present the way I compile kernel modules (outside the kernel sources).

I've built few kernel modules (governors - ineractive and smartass, cifs, nls, etc) and I started receiving private messages asking how I did it.

For kernel modules that come with the kernel itself - cifs / tun for example - they just work if you compile the kernel and activate correct config parameters.
Some other modules (such as the smartass governor that doesn't come with the kernel) you compile outside the kernel source. However they require changes since kernel does not export the symbols the module needs to use - so you have to know what k_all_syms are needed, grab them from the phone and update the kernel module.

So there will be changes there. However, the main steps are:

a) follow tutorials to get the kernel / android ndk to compile. People seem able to do this.
b) then take the module you want (For example cpufreq_smartass.c from here: http://pastebin.com/rR4QUCrk ) and copy it in a new folder on the disk.
c) create a Makefile like the one below, but with your paths of course:

Code:
KERNEL_DIR=/home/viulian/android_platform/kernel-2.1.A.0.435/kernel

obj-m := cpufreq_smartass.o
PWD := $(shell pwd)
default:
        $(MAKE) ARCH=arm CROSS_COMPILE=/home/viulian/android_platform/prebuilt/linux-x86/toolchain/arm-eabi-4.4.3/bin/arm-eabi- -C $(KERNEL_DIR) SUBDIRS=$(PWD) modules
clean:
        $(MAKE) -C $(KERNEL_DIR) SUBDIRS=$(PWD) clean

d) execute make

Of course, the module source needs to be adjusted as you need to put in the frequencies, and also update the k_all_syms pointers .. But you can retrieve them from /proc/kallsyms on the device itself - just look for the method name, and use the address you see in the log.

If you still can't get it to compile, try to compile a very basic hello_world kernel module. I used the code below when testing:

Code:
#include <linux/module.h>  /* Needed by all modules */
#include <linux/kernel.h>  /* Needed for KERN_ALERT */

MODULE_LICENSE("GPL");
MODULE_AUTHOR("viulian, 2011");
MODULE_DESCRIPTION("Demo module for X10i");

int init_module(void)
{
   printk("<1>Hello world\n");

   // A non 0 return means init_module failed; module can't be loaded.
   return 0;
}


void cleanup_module(void)
{
  printk(KERN_ALERT "Goodbye world 1.\n");
}

It is not perfect, but if you manage to insmod-it and check dmesg, you will see "Hello world" written there.

One more thing, linux kernel is fussy about the module versions. Even if nothing is changed between two kernel versions related to what a module needs, is enough a small difference in module's modinfo value to make the kernel to refuse the module.

For this, you need to trick your local kernel and adjust EXTRAVERSION value in kernel's main Makefile to have the exact version of the one on the device:

In X10 stock kernel (GB 2.3.3 release), the kernel version is 2.6.29-00054-g5f01537 visible in phone settings.

This means that the kernel on the phone will only accept modules that are compiled for that exact version. But the kernel version is just a string in the module .ko, so is a string comparison - the module might work perfectly, but is not loaded.
There is luck though, the string value comes from a define in kernel's Makefile, which you can change before you compile!

The Makefile in the kernel you are going to use to build the module will have to include these lines at the top:

Code:
VERSION = 2
PATCHLEVEL = 6
SUBLEVEL = 29
EXTRAVERSION = -00054-g5f01537

Other than that, it should work .. Expect phone reboots and difficulty to debug if stuff goes wrong. Android kernel doesn't come with syslog functionality, kernel prints are found in /proc/kmsg. Dmesg works, but you can't execute if if phone reboots.
I usually had to keep another adb shell opening with 'cat /proc/kmsg' which showed as much as possible from the module's outputs.

Happy compiling :) on your risk!
 
Last edited:

fabricioemmerick

Senior Member
Feb 25, 2011
75
10
NY
Guide to Compiling Custom Kernel Modules in Android
I've spent the better part of today trying to figure out how to compile and load a custom kernel modules in android to aid me in my research. It has been in entirely frustrating experience, as there is almost no documentation on the topic that I can find. Below you will find my attempt at a guide. Hopefully this will help save someone else the hassle.

PREREQUISITES

Disclaimer: This list may be incomplete, since I've not tried it on a fresh install. Please let me know if I've missed anything.
Install the general android prereqs found here .
Download and un(zip|tar) the android NDK found here .
http://developer.android.com/sdk/ndk/index.html
Download and un(zip|tar) the android SDK found here .
http://developer.android.com/sdk/index.html
Download and untar the kernel source for your device. This can usually be found on the website of your device manufacturer or by a quick Google search.
Root your phone. In order to run custom kernel modules, you must have a rooted phone.
Plug your phone into your computer.

PREPARING YOUR KERNEL SOURCE

First we must retrieve and copy the kernel config from our device.

Code:
$ cd /path/to/android-sdk/tools
$ ./adk pull /proc/config.gz
$ gunzip ./config.gz
$ cp config /path/to/kernel/.config

Next we have to prepare our kernel source for our module.

Code:
$ cd /path/to/kernel
$ make ARCH=arm CROSS_COMPILE=/path/to/android-ndk/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/bin/arm-linux-androideabi- modules_prepare

PREPARING YOUR MODULE FOR COMPILATION

We need to create a Makefile to cross-compile our kernel module. The contents of your Makefile should be similar to the following:

Code:
obj-m := modulename.o
KDIR := /path/to/kernel
PWD := $(shell pwd)
CCPATH := /path/to/android-ndk/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/bin
default:
 $(MAKE) ARCH=arm CROSS_COMPILE=$(CCPATH)/arm-linux-androideabi- -C $(KDIR) M=$(PWD) modules

COMPILING AND INSTALLING YOUR MODULE

Code:
$ cd /path/to/module/src
$ make
$ cd /path/to/android-sdk/tools/
$ ./adb push /path/to/module/src/modulename.ko /sdcard/modulename.ko

RUNNING YOUR MODULE

Code:
$ cd /path/to/android-sdk/
$ ./adb shell
$ su
# insmod /sdcard/modulename.ko

:D:D:D:D

---------- Post added at 07:40 PM ---------- Previous post was at 07:37 PM ----------

IMPORNTANT TOO

Preparing a build environment

To build an Android kernel, you need a cross-compiling toolchain. Theoretically, any will do, provided it targets ARM. I just used the one coming in the Android NDK:

$ wget http://dl.google.com/android/ndk/android-ndk-r6b-linux-x86.tar.bz2
$ tar -jxf android-ndk-r6b-linux-x86.tar.bz2
$ export ARCH=arm
$ export CROSS_COMPILE=$(pwd)/android-ndk-r6b/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/bin/arm-linux-androideabi-

For the latter, you need to use a directory path containing prefixed versions (such as arm-eabi-gcc orarm-linux-androideabi-gcc), and include the prefix, but not “gcc”.

You will also need the adb tool coming from the Android SDK. You can install it this way:

$ wget http://dl.google.com/android/android-sdk_r12-linux_x86.tgz
$ tar -zxf android-sdk_r12-linux_x86.tgz
$ android-sdk-linux_x86/tools/android update sdk -u -t platform-tool
$ export PATH=$PATH:$(pwd)/android-sdk-linux_x86/platform-tools
 

fabricioemmerick

Senior Member
Feb 25, 2011
75
10
NY
Any takers to do a video status? Come on people it would be good for newbies like me and many that tme around. When teaching the community grows.
 

hamsterksu

Member
Nov 12, 2011
10
3
hi
i'm traing to compile module for acer a500.
but i have got an error: Nothing to be done for `default'.

my makefile:
Code:
obj-m += hello.o
KDIR := /home/hamster/android
PWD := $(shell pwd)
CCPATH := /home/hamster/android-ndk/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/bin
default:
 $(MAKE) ARCH=arm CROSS_COMPILE=$(CCPATH)/arm-linux-androideabi- -C $(KDIR) M=$(PWD) modules

acer kernel code is located in /home/hamster/android

could you help me?

thanks
 

DiasFlac

New member
Jun 28, 2012
3
2
Columbus
Makefile including tabs

hi
i'm traing to compile module for acer a500.
but i have got an error: Nothing to be done for `default'.

my makefile:
Code:
obj-m += hello.o
KDIR := /home/hamster/android
PWD := $(shell pwd)
CCPATH := /home/hamster/android-ndk/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/bin
default:
 $(MAKE) ARCH=arm CROSS_COMPILE=$(CCPATH)/arm-linux-androideabi- -C $(KDIR) M=$(PWD) modules

acer kernel code is located in /home/hamster/android

could you help me?

thanks

This is probably because you need to add a tab in front of your $(MAKE). Without the tab it will not recognize the command.
 
  • Like
Reactions: blackmambazzz

DKSL

Member
Feb 15, 2012
27
0
Help with compiling module

I am trying to compile a module for Galaxy S. I am getting this error.

# insmod hello_world.ko
insmod: init_module 'hello_world.ko' failed (Exec format error)

These are the module related options that I have enabled in the .config

CONFIG_MODULES=y
CONFIG_MODULE_FORCE_LOAD=y
CONFIG_MODULE_UNLOAD=y
CONFIG_MODULE_FORCE_UNLOAD=y
# CONFIG_MODVERSIONS is not set
# CONFIG_MODULE_SRCVERSION_ALL is not set

Further this is the cat /proc/kmsg out put

<3>[53597.457275] hello_world: version magic '2.6.35.7-I900XXJVP-CL264642 preempt mod_unload ARMv7 ' should be '2.6.35.7-I9000XXJVP-CL264642 preempt mod_unload ARMv7 '

Why am I getting this error??

These are the steps I followed,

1. Downloaded the GT-I9000_OpenSource_GB.zip from samsung open source.
2. Change the EXTRAVERSION to EXTRAVERSION = .7-I900XXJVP-CL264642 (kernel version shown on phone is 2.6.35.7-I9000XXJVP-CL264642se.infra@SEP-40 #2)
I tried with EXTRAVERSION = .7-I900XXJVP-CL264642se.infra@SEP-40 as well.
3. Added this line to the main make file -
core-y := usr/ TestModule/
5. Place the TestModule/ with the module code on the root directory.
6. Created the TestModule/Makefile and added this entry
obj-m := hello_world.o
4. On the read me of the kernel source it says to install Sourcery G++ Lite 2009q3-68 toolchain for ARM EABI, which I did.
5. Execute 'make aries_eur_defconfig'.
6. Execute make (again this is how the readme in the source says)

I have compiled this module for the emulator and it works fine, What am I doing wrong here?

Thanks in advance.
 
Last edited:

Samezrp

Member
Sep 24, 2008
34
3
hi
but i have got an error: Nothing to be done for `default'.

Code:
default:
 $(MAKE) ARCH=arm CROSS_COMPILE=$(CCPATH)/arm-linux-androideabi- -C $(KDIR) M=$(PWD) modules

be sure to have {tab} not space or other symbol before: $(MAKE) in:
Code:
default:
 $(MAKE) ARCH=arm CROSS_COMPILE=$(CCPATH)/arm-linux-androideabi- -C $(KDIR) M=$(PWD) modules
 
  • Like
Reactions: blackmambazzz

m00gle

Member
Mar 7, 2013
13
0
Hello,

I'm trying to load a module in my GS3 but I encounter problems about version. Maybe it's just a mismatch between the kernel i use to compile and the one on my phone.

I have a 3.0.31-742798 kernel version on my GS3, so I put this info in the makefile like viulian said but it didn't work.

I manage to compile and put the module on the phone, but when I want to insmod it, I've got
Code:
insmod: init_module 'hello_world.ko' failed (Exec format error)

and the /proc/kmsg say
Code:
... disagrees about version of symbol module_layout


And there no config.gz on the /proc/ dir like fabricioemmerick suggest to use



EDIT: I try to modify the symbol by copying the one of module from the phone. Have another error.
btw , with modinfo I found that the compilation always add -gc33f1bc-dirty after the subversion. Maybe something in the compilation goes wrong. Still use the stock kernel and the toolchain from the ndk sourcecode
 
Last edited:

Noob_XDA

Member
Oct 10, 2013
39
8
Hello,

I'm trying to load a module in my GS3 but I encounter problems about version. Maybe it's just a mismatch between the kernel i use to compile and the one on my phone.

I have a 3.0.31-742798 kernel version on my GS3, so I put this info in the makefile like viulian said but it didn't work.

I manage to compile and put the module on the phone, but when I want to insmod it, I've got
Code:
insmod: init_module 'hello_world.ko' failed (Exec format error)

and the /proc/kmsg say
Code:
... disagrees about version of symbol module_layout


And there no config.gz on the /proc/ dir like fabricioemmerick suggest to use



EDIT: I try to modify the symbol by copying the one of module from the phone. Have another error.
btw , with modinfo I found that the compilation always add -gc33f1bc-dirty after the subversion. Maybe something in the compilation goes wrong. Still use the stock kernel and the toolchain from the ndk sourcecode

Maybe your kernel source code version and your phone kernel version is different. If both are 3.0.31 but just the subversion is different, you can
try "insmod -f" to load. The -f option will ignore the version.
 

ravike14

Senior Member
Feb 9, 2013
1,307
1,077
Colombo
I'm not sure I understand the question ... you can just dmesg and grep by the module name ? Or some string that the module outputs ? You have full control :)

That's the part I don't understand, the grep part.. When I enter grep with my commamd I get as it's not a recognized command.. I'm using Windows is that the reason?

I'm using 'adb shell dmesg > dmesg.txt' how do I add the grep part for it and and the module name.. I did alot research but all are Linux kernel specific debugging guides.. What would be the exact command to grep a specific module.ko logs? :)

Sent from my HTC_Amaze_4G using XDA Premium 4 mobile app
 

viulian

Inactive Recognized Developer
Apr 6, 2011
419
411
hex.ro
I'm using 'adb shell dmesg > dmesg.txt' how do I add the grep part for it and and the module name.. I did alot research but all are Linux kernel specific debugging guides.. What would be the exact command to grep a specific module.ko logs? :)

I think I understand now :)

The order is this:
a) DroidSSHd from here: http://code.google.com/p/droidsshd/downloads/list
b) Busybox installer from the market.
c) Putty on your windows to connect to the phone

Now you can dmesg + grep once you are connected to the phone (don't forget to su before and allow DroidSSHd root).

You need to be connected to the phone since it is much more easier. Use the phone as your remote Linux machine.
 
  • Like
Reactions: ravike14

Top Liked Posts

  • There are no posts matching your filters.
  • 28
    I've decided to make a short tutorial and present the way I compile kernel modules (outside the kernel sources).

    I've built few kernel modules (governors - ineractive and smartass, cifs, nls, etc) and I started receiving private messages asking how I did it.

    For kernel modules that come with the kernel itself - cifs / tun for example - they just work if you compile the kernel and activate correct config parameters.
    Some other modules (such as the smartass governor that doesn't come with the kernel) you compile outside the kernel source. However they require changes since kernel does not export the symbols the module needs to use - so you have to know what k_all_syms are needed, grab them from the phone and update the kernel module.

    So there will be changes there. However, the main steps are:

    a) follow tutorials to get the kernel / android ndk to compile. People seem able to do this.
    b) then take the module you want (For example cpufreq_smartass.c from here: http://pastebin.com/rR4QUCrk ) and copy it in a new folder on the disk.
    c) create a Makefile like the one below, but with your paths of course:

    Code:
    KERNEL_DIR=/home/viulian/android_platform/kernel-2.1.A.0.435/kernel
    
    obj-m := cpufreq_smartass.o
    PWD := $(shell pwd)
    default:
            $(MAKE) ARCH=arm CROSS_COMPILE=/home/viulian/android_platform/prebuilt/linux-x86/toolchain/arm-eabi-4.4.3/bin/arm-eabi- -C $(KERNEL_DIR) SUBDIRS=$(PWD) modules
    clean:
            $(MAKE) -C $(KERNEL_DIR) SUBDIRS=$(PWD) clean

    d) execute make

    Of course, the module source needs to be adjusted as you need to put in the frequencies, and also update the k_all_syms pointers .. But you can retrieve them from /proc/kallsyms on the device itself - just look for the method name, and use the address you see in the log.

    If you still can't get it to compile, try to compile a very basic hello_world kernel module. I used the code below when testing:

    Code:
    #include <linux/module.h>  /* Needed by all modules */
    #include <linux/kernel.h>  /* Needed for KERN_ALERT */
    
    MODULE_LICENSE("GPL");
    MODULE_AUTHOR("viulian, 2011");
    MODULE_DESCRIPTION("Demo module for X10i");
    
    int init_module(void)
    {
       printk("<1>Hello world\n");
    
       // A non 0 return means init_module failed; module can't be loaded.
       return 0;
    }
    
    
    void cleanup_module(void)
    {
      printk(KERN_ALERT "Goodbye world 1.\n");
    }

    It is not perfect, but if you manage to insmod-it and check dmesg, you will see "Hello world" written there.

    One more thing, linux kernel is fussy about the module versions. Even if nothing is changed between two kernel versions related to what a module needs, is enough a small difference in module's modinfo value to make the kernel to refuse the module.

    For this, you need to trick your local kernel and adjust EXTRAVERSION value in kernel's main Makefile to have the exact version of the one on the device:

    In X10 stock kernel (GB 2.3.3 release), the kernel version is 2.6.29-00054-g5f01537 visible in phone settings.

    This means that the kernel on the phone will only accept modules that are compiled for that exact version. But the kernel version is just a string in the module .ko, so is a string comparison - the module might work perfectly, but is not loaded.
    There is luck though, the string value comes from a define in kernel's Makefile, which you can change before you compile!

    The Makefile in the kernel you are going to use to build the module will have to include these lines at the top:

    Code:
    VERSION = 2
    PATCHLEVEL = 6
    SUBLEVEL = 29
    EXTRAVERSION = -00054-g5f01537

    Other than that, it should work .. Expect phone reboots and difficulty to debug if stuff goes wrong. Android kernel doesn't come with syslog functionality, kernel prints are found in /proc/kmsg. Dmesg works, but you can't execute if if phone reboots.
    I usually had to keep another adb shell opening with 'cat /proc/kmsg' which showed as much as possible from the module's outputs.

    Happy compiling :) on your risk!
    10
    Guide to Compiling Custom Kernel Modules in Android
    I've spent the better part of today trying to figure out how to compile and load a custom kernel modules in android to aid me in my research. It has been in entirely frustrating experience, as there is almost no documentation on the topic that I can find. Below you will find my attempt at a guide. Hopefully this will help save someone else the hassle.

    PREREQUISITES

    Disclaimer: This list may be incomplete, since I've not tried it on a fresh install. Please let me know if I've missed anything.
    Install the general android prereqs found here .
    Download and un(zip|tar) the android NDK found here .
    http://developer.android.com/sdk/ndk/index.html
    Download and un(zip|tar) the android SDK found here .
    http://developer.android.com/sdk/index.html
    Download and untar the kernel source for your device. This can usually be found on the website of your device manufacturer or by a quick Google search.
    Root your phone. In order to run custom kernel modules, you must have a rooted phone.
    Plug your phone into your computer.

    PREPARING YOUR KERNEL SOURCE

    First we must retrieve and copy the kernel config from our device.

    Code:
    $ cd /path/to/android-sdk/tools
    $ ./adk pull /proc/config.gz
    $ gunzip ./config.gz
    $ cp config /path/to/kernel/.config

    Next we have to prepare our kernel source for our module.

    Code:
    $ cd /path/to/kernel
    $ make ARCH=arm CROSS_COMPILE=/path/to/android-ndk/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/bin/arm-linux-androideabi- modules_prepare

    PREPARING YOUR MODULE FOR COMPILATION

    We need to create a Makefile to cross-compile our kernel module. The contents of your Makefile should be similar to the following:

    Code:
    obj-m := modulename.o
    KDIR := /path/to/kernel
    PWD := $(shell pwd)
    CCPATH := /path/to/android-ndk/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/bin
    default:
     $(MAKE) ARCH=arm CROSS_COMPILE=$(CCPATH)/arm-linux-androideabi- -C $(KDIR) M=$(PWD) modules

    COMPILING AND INSTALLING YOUR MODULE

    Code:
    $ cd /path/to/module/src
    $ make
    $ cd /path/to/android-sdk/tools/
    $ ./adb push /path/to/module/src/modulename.ko /sdcard/modulename.ko

    RUNNING YOUR MODULE

    Code:
    $ cd /path/to/android-sdk/
    $ ./adb shell
    $ su
    # insmod /sdcard/modulename.ko

    :D:D:D:D

    ---------- Post added at 07:40 PM ---------- Previous post was at 07:37 PM ----------

    IMPORNTANT TOO

    Preparing a build environment

    To build an Android kernel, you need a cross-compiling toolchain. Theoretically, any will do, provided it targets ARM. I just used the one coming in the Android NDK:

    $ wget http://dl.google.com/android/ndk/android-ndk-r6b-linux-x86.tar.bz2
    $ tar -jxf android-ndk-r6b-linux-x86.tar.bz2
    $ export ARCH=arm
    $ export CROSS_COMPILE=$(pwd)/android-ndk-r6b/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/bin/arm-linux-androideabi-

    For the latter, you need to use a directory path containing prefixed versions (such as arm-eabi-gcc orarm-linux-androideabi-gcc), and include the prefix, but not “gcc”.

    You will also need the adb tool coming from the Android SDK. You can install it this way:

    $ wget http://dl.google.com/android/android-sdk_r12-linux_x86.tgz
    $ tar -zxf android-sdk_r12-linux_x86.tgz
    $ android-sdk-linux_x86/tools/android update sdk -u -t platform-tool
    $ export PATH=$PATH:$(pwd)/android-sdk-linux_x86/platform-tools
    1
    Makefile including tabs

    hi
    i'm traing to compile module for acer a500.
    but i have got an error: Nothing to be done for `default'.

    my makefile:
    Code:
    obj-m += hello.o
    KDIR := /home/hamster/android
    PWD := $(shell pwd)
    CCPATH := /home/hamster/android-ndk/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/bin
    default:
     $(MAKE) ARCH=arm CROSS_COMPILE=$(CCPATH)/arm-linux-androideabi- -C $(KDIR) M=$(PWD) modules

    acer kernel code is located in /home/hamster/android

    could you help me?

    thanks

    This is probably because you need to add a tab in front of your $(MAKE). Without the tab it will not recognize the command.
    1
    hi
    but i have got an error: Nothing to be done for `default'.

    Code:
    default:
     $(MAKE) ARCH=arm CROSS_COMPILE=$(CCPATH)/arm-linux-androideabi- -C $(KDIR) M=$(PWD) modules

    be sure to have {tab} not space or other symbol before: $(MAKE) in:
    Code:
    default:
     $(MAKE) ARCH=arm CROSS_COMPILE=$(CCPATH)/arm-linux-androideabi- -C $(KDIR) M=$(PWD) modules
    1
    How can I get a dmesg of a specific kernel module using adb shell or any other way? :)