Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can the android NDK compile kernel module source?

I want make a dynamic loaded kernel module for android. I don't want to install a linux, I just have cygwin and android NDK.

like image 552
lovespring Avatar asked Oct 13 '22 17:10

lovespring


1 Answers

Yes, it is possible to build kernel modules with the NDK. Note, this works best with a Linux system (I'm told Linux x86_64 is the supported environment) because it's harder to cross-compile kernel code on case sensitive filesystems (such as those that come by default on Windows and Mac systems), and because building kernel modules requires building ELF-manipulation binaries (modpost) which require ELF headers typically only present on Linux. That said...

First you need to get the source code to the same exact kernel on your device, and make sure that the configuration is the same as your device. (otherwise there's a chance you will confuse the build system)

Second, you need to determine where in your Android NDK the cross-compiler toolchain is. Here's how I found mine:

$ cd $NDK_HOME
$ find . | grep '\-gcc$'
./toolchains/arm-eabi-4.4.0/prebuilt/linux-x86/bin/arm-eabi-gcc
./toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/bin/arm-linux-androideabi-gcc

(note, $NDK_HOME is where I installed the Android NDK)

Third, you need to add the $NDK_HOME/toolchains/arm-eabi-4.4.0/prebuilt/linux-x86/bin directory (or wherever it is on your system) to your PATH environment variable.

Then you need to export two environment variables:

export ARCH=arm 
export CROSS_COMPILE=arm-eabi-

(note, the arm-eabi- prefix is the same as what we saw in the find command. When the kernel is built, commands such as gcc and ld will be prefixed with this. Note, if you were building for an x86 platform I expect you would have to adjust these. I have only built modules for ARM.)

Next, you should compile the kernel. (to do this, I pulled down /proc/config.gz from my Android device, ran zcat config.gz > .config within the kernel source directory, then ran make menuconfig && make.) Kernel build gurus may know some shortcuts here, but I wasn't able to set up the kernel source directory correctly for building a module without doing an actual build. (If the kernel in your build tree matches your device, you don't have to actually update the kernel, you can just insert the modules.)

Last, I used the normal process to build the kernel modules from source. Typically kernel modules will have a parameterized build which will read in the kernel source tree directory somehow, then invoke the build. At that point, as long as the kernel source tree is set up correctly and ARCH and CROSS_COMPILE are set up, your module should build!

Good luck with this. I'm sure there is some per-device variance.

like image 94
mpontillo Avatar answered Oct 18 '22 02:10

mpontillo