I am trying to load a kernel module for android.
First, I've download the kernel source code from the target device which MotoG5 from here: https://github.com/MotorolaMobilityLLC/kernel-msm. Also, I've added these files which contain f2fs for compilng the kernel (available here https://github.com/MotorolaMobilityLLC/motorola-kernel) . I was able to compile the kernel using the following commands :
export CROSS_COMPILE=/media/hero/HDD1/android-ndk-r15c/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/arm-linux-androideabi-
export ARCH=arm
make clean && make mrproper
make msm8937-perf_defconfig
make -j4
make modules // to generate Module.symvers which can be used later to compile a kernel module.
Then, I wrote small kernel code as follow :
#include "linux/module.h"
#include "linux/kernel.h"
//replace the "" with angular brackets
int init_module(void)
{
printk(KERN_INFO "Hello android kernel...\n");
return 0;
}
void cleanup_module(void)
{
printk(KERN_INFO "Goodbye android kernel...\n");
}
android_module.c
obj-m += android_module.o
KDIR := /media/hero/HDD1/k3/kmsm/
PWD := $(shell pwd)
ARCH=arm
CROSS_COMPILE=/media/hero/HDD1/android-ndk-r15c/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/arm-linux-androideabi-
all:
make -C $(KDIR) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) M=$(PWD) modules
clean:
$(MAKE) -C $(KDIR) M=$(PWD) clean
Makefile
After that, I was able to compile the kernel successfully with no warning at all. here are some info about the vermagic and file command output for the generated kernel module.
root@root:/hello3# modinfo android_module.ko
filename: /media/hero/HDD1/hello3/android_module.ko
depends:
vermagic: 3.18.31-perf SMP preempt mod_unload modversions ARMv7 p2v8
root@root:/hello3# file android_module.ko
android_module.ko: ELF 32-bit LSB relocatable, ARM, EABI5 version 1 (SYSV), BuildID[sha1]=a9bcb514badd8f5799151e0ee4993fd73302175c, with debug_info, not stripped
I compare these info to an existing kernel module in the phone and they match exactly. this is a sample for existing loadable kernel.
modinfo *******.ko
filename: *******.ko
license: GPL
description: Input driver event debug module
author: Vojtech Pavlik <vojtech@ucw.cz>
alias: input:b*v*p*e*-e*k*r*a*m*l*s*f*w*
depends:
intree: Y
vermagic: 3.18.31-perf SMP preempt mod_unload modversions ARMv7 p2v8
file *******.ko
evbug.ko: ELF 32-bit LSB relocatable, ARM, EABI5 version 1 (SYSV), BuildID[sha1]=b14c49bf595842f41a31b9a5d0e66bc8523703a8, with debug_info, not stripped
Now. The problem is whenever I load the module I got this message
insmod: failed to load android_module.ko: Exec format error
Also, dmesg show the following :
android_module: no symbol version for module_layout
Please note that some answers online said that I should generate Module.symvers before compiling a kernel module so I did that and I did not encounter any error regarding missing Module.symvers while compiling the kernel module.
Any idea, why this module cannot be load ? please let me know.
Regards,