Building kernel module for Android - android

I need to add the FTDI USB module to the Android kernel (Android 2.3.1, Linux 2.6.32), so I got the 2.6.32 kernel and tried to build the module:
make modules ARCH=arm CROSS_COMPILE=arm-none-eabi-
But when I tried to do 'insmod ftdi_sio.ko', the error message was
insmod: init_module 'ftdi_sio.ko' failed (Exec format error)
In dmesg's output I found this
ftdi_sio: version magic '2.6.32.27 preempt mod_unload ARMv5 ' should be '2.6.32.27 preempt mod_unload ARMv7 '
I tried to add 'armv7-a' flag (as I read in Android NDK docs)
make modules ARCH=arm CROSS_COMPILE=arm-linux-androideabi- CFLAGS='-march=armv7-a -mfloat-abi=softfp'
Also, I tried following cross-compilers: arm-none-eabi-, arm-eabi-, arm-linux-android-eabi-
Every time result was the same - ARMv5 in the version magic.
How can I build the module for ARMv7?

Try setting up CONFIG_CPU_V7=y in your kernel .config file

Related

Error in loading android kernel module (No symbol version for module_layout)

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,

Android source code compile error: "Try increasing heap size with java option '-Xmx<size>'"

Error happens when I try to compile Android source code(Sourcecode-version: 6.0.1; RAM: 6G; host system: ubuntu 14.04),log is below:
including ./system/netd/Android.mk ...
including ./system/security/keystore-engine/Android.mk ...
including ./system/security/keystore/Android.mk ...
including ./system/security/softkeymaster/Android.mk ...
including ./system/tools/aidl/Android.mk ...
including ./system/update_engine/Android.mk ...
including ./system/vold/Android.mk ...
including ./system/weaved/Android.mk ...
including ./system/webservd/Android.mk ...
including ./tools/external/fat32lib/Android.mk ...
Starting build with ninja
ninja: Entering directory `.'
[ 0% 1/21275] Ensure Jack server is installed and started
Jack server already installed in "/home/eddy/.jack-server"
Launching Jack server java -Djava.io.tmpdir=/tmp -Dfile.encoding=UTF-8 -XX:+TieredCompilation -cp /home/eddy/.jack-server/launcher.jar com.android.jack.launcher.ServerLauncher
[ 0% 17/21275] host Java: conscrypt-host (out/host/common/obj/JAVA_LIBRARIES/conscrypt-host_intermediates/classes)
warning: [options] bootstrap class path not set in conjunction with -source 1.7
external/conscrypt/src/openjdk/java/org/conscrypt/Platform.java:39: warning: AlgorithmId is internal proprietary API and may be removed in a future release
import sun.security.x509.AlgorithmId;
^
external/conscrypt/src/openjdk/java/org/conscrypt/Platform.java:243: warning: AlgorithmId is internal proprietary API and may be removed in a future release
return AlgorithmId.get(oid).getName();
^
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
3 warnings
[ 0% 18/21275] host Java: signapk (out/host/common/obj/JAVA_LIBRARIES/signapk_intermediates/classes)
warning: [options] bootstrap class path not set in conjunction with -source 1.7
1 warning
[ 0% 73/21275] Building with Jack: out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/with-local/classes.dex
FAILED: /bin/bash out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/with-local/classes.dex.rsp
GC overhead limit exceeded
Try increasing heap size with java option '-Xmx<size>'
Warning: This may have produced partial or corrupted output.
ninja: build stopped: subcommand failed.
make: *** [ninja_wrapper] Error 1
#### make failed to build some targets (14:09 (mm:ss)) ####
eddy#eddy-OptiPlex-390:~/WORKING_DIRECTORY$
Anyone can tell me why the compiling failed?
I had the same problem. So I've tried to set JACK_SERVER_VM_ARGUMENTS to include -Xmx=4g, but when building again the log output showed that this was not included in the startup. I don't know why, it seems like the env vars do not get passed to the build script correctly.
Solution
Before starting a clean android build set the JACK_SERVER_VM_ARGUMENTS to include -Xmx=4g, then stop and start the jack server manually. Given you're in the main source tree of AOSP run the following:
export JACK_SERVER_VM_ARGUMENTS="-Dfile.encoding=UTF-8 -XX:+TieredCompilation -Xmx4g"
./prebuilts/sdk/tools/jack-admin kill-server
./prebuilts/sdk/tools/jack-admin start-server
for cm you can use
export JACK_SERVER_VM_ARGUMENTS="-Dfile.encoding=UTF-8 -XX:+TieredCompilation -Xmx4g"
jack-admin kill-server && jack-admin start-server
This resolved the issue for me.
The current way to set Xmx for jack is:
export JACK_SERVER_VM_ARGUMENTS="-Dfile.encoding=UTF-8 -XX:+TieredCompilation -Xmx4096m"
out/host/linux-x86/bin/jack-admin kill-server
out/host/linux-x86/bin/jack-admin start-server
I also had this problem after updating to 6.0 My computer is an i7 laptop with 8GB of ram. It worked fine with v5.x and below.
The reason is not enough memory as the error message states. In the v6.x build, more Jack is used. In my case, reducing the number of Jacks to 1 resolved the issue.
I can now continue compiling with 8GB of ram.
$HOME/.jack-server/config.properties
jack.server.max-service=1
[100% 19740/19740]
#### make completed successfully (11:55:27 (hh:mm:ss)) ####
Android build (tested with 7.1.2) uses its own, private version of Jack (there could be a Jack toolchain installed elsewhere on the system), so you have to use Android-specific variables.
From https://android.googlesource.com/platform/prebuilts/sdk/+/master/tools/README-jack-server.md:
(search for "If you experience Jack compilations failing on Out of memory error.:")
To summarize:
$ # Stop the Jack server
$ jack-admin stop-server
$ # apply the new setting
$ export ANDROID_JACK_VM_ARGS="-Xmx2g -Dfile.encoding=UTF-8 -XX:+TieredCompilation"
$ Finally, restart the build (do not manually start the Jack server!)
$ m # or any other build command
The build system should pick up the change and output the line:
Environment variable ANDROID_JACK_VM_ARGS was set, regenerating...
or (after a subsequent change):
Environment variable ANDROID_JACK_VM_ARGS was modified (-Xmx2g -Dfile.encoding=UTF-8 -XX:+TieredCompilation => -Xmx4g -Dfile.encoding=UTF-8 -XX:+TieredCompilation), regenerating...
I found this article:
Building AOSP 7.x Nougat with only 8 GiB RAM
on an 8 GiB machine, Max Heap size is less than 2 GiB:
$ java -XshowSettings 2>&1 | grep Heap
Max. Heap Size (Estimated): 1.71G
The solution
Once I understood the problem, it was just a question of changing the default heap size when jack-server is launched. To fix it, you need to edit
~/.jack-settings, and add this line
JACK_SERVER_VM_ARGUMENTS="-Dfile.encoding=UTF-8 -XX:+TieredCompilation -Xmx4096m"
Then, restart jack-server – otherwise if it is running in the background it will continue to use the old Xmx value:
$ prebuilts/sdk/tools/jack-admin kill-server
$ prebuilts/sdk/tools/jack-admin start-server
Launching Jack server java -XX:MaxJavaStackTraceDepth=-1 -Djava.io.tmpdir=/tmp -Dfile.encoding=UTF-8 -XX:+TieredCompilation -Xmx4096m
-cp /home/chris/.jack-server/launcher.jar com.android.jack.launcher.ServerLauncher
This was also my problem: the java default Xmx setting was already close to 4GB (precisely: 4011MB) so the issue was rather the number of concurrent jack servers running. My machine had 8 CPUs but only 16GB of RAM, but would have required 32GB (8*4=32).
My (slightly more dynamic) solution:
MAX=$(($(free -g | awk '/^Mem:/{print $NF}') / 4))
if [ $MAX -le 0 ]; then
MAX=1
fi
echo "SERVER_NB_COMPILE=$MAX" >> ~/.jack
mkdir -p ~/.jack-server
echo "jack.server.max-service=$MAX" >> ~/.jack-server/config.properties
It adds the MAX number of jack server to both the old-location and new-location based on the android official doc:
Jack Troubleshooting: If your computer becomes unresponsive during compilation or if you experience Jack compilations failing on “Out of memory error”, you can improve the situation by reducing the number of Jack simultaneous compilations by editing your $HOME/.jack and changing SERVER_NB_COMPILE to a lower value. https://source.android.com/source/jack.html

How to test android Goldfish Kernel 3.10 on Versatilepb Suported QEMU?

I'm trying to port Android on VersatilePB using [Android Goldfish 3.10 kernel](https://android.googlesource.com/kernel/goldfish/+/android-goldfish-3.10).
First, I configured kernel for versatile_defconfig and I successfully built it. I got .config file, renamed it as versatile_config and added Android base configuration to that using the following command
ARCH=arm scripts/kconfig/merge_config.sh versatile_config android/configs/android-base.cfg
Again, I built the kernel successfully. After that, I built the Android Google source code and I have taken ramdisk.img from google_src_v4.4/out/target/product/generic. After that, I tested using following command,
qemu-system-arm -M versatilepb -kernel ./arch/arm/boot/zImage -initrd ramdisk.img -append "console=ttyAMA0 root=/dev/ram0 rw" -serial stdio
but I'm getting the following error. Kernel panic - not syncing: Attempted to kill init! exitcode=0x00000004
CPU: 0 PID: 1 Comm: init Not tainted 3.10.0 #15
[<c001271c>] (unwind_backtrace+0x0/0xe8) from [<c001129c>] (show_stack+0x10/0x14)
[<c001129c>] (show_stack+0x10/0x14) from [<c0368304>] (panic+0x8c/0x1d4)
[<c0368304>] (panic+0x8c/0x1d4) from [<c001fe94>] (do_exit+0x7f8/0x8d8)
[<c001fe94>] (do_exit+0x7f8/0x8d8) from [<c001ffe0>] (do_group_exit+0x40/0xd4)
[<c001ffe0>] (do_group_exit+0x40/0xd4) from [<c002a75c>] (get_signal_to_deliver+0x178/0x5c4)
[<c002a75c>] (get_signal_to_deliver+0x178/0x5c4) from [<c00109a4>] (do_signal+0xd0/0x434)
[<c00109a4>] (do_signal+0xd0/0x434) from [<c0010ea4>] (do_work_pending+0xa8/0xb8)
[] (do_work_pending+0xa8/0xb8) from [] (work_pending+0xc/0x20)
Somehow related to the "init()" function, where kernel is trying to create the very first process (in userspace) for the first time - this is also where LInux and Android differs. So did you use the QEMU from Android?:
https://github.com/android/platform_external_qemu
And see the discussion here:
https://balau82.wordpress.com/2010/03/22/compiling-linux-kernel-for-qemu-arm-emulator/
there are some hints here:
Run Android app in qemu-arm?
And here has commandline for emulation:
How to compile android goldfish 3.4 kernel and run on emulator

Android goldfish armv7 kernel module compiling

I have compiled goldfish armv7 kernel for android.
everything works well, my compiled kernel works fine on the emulator.
my problem is, I want to insert a kernel module(.ko) into my goldfish kernel.
but cross compiling the 'hello world' kernel module gives me some error.
so I googled it and found out that I need to recompile my goldfish kernel with
'CONFIG_MODULES=y'
option enabled.
as I read on the Internet, I added the following lines to my .config script.
CONFIG_MODULES=y
CONFIG_MODULES_FORCE_LOAD=y
CONFIG_MODULES_UNLOAD=y
CONFIG_MODULES_FORCE_UNLOAD=y
But if I recompile my goldfish kernel with these options, I get the following error.
LD net/built-in.o
LD vmlinux.o
MODPOST vmlinux.o
GEN .version
CHK include/generated/compile.h
UPD include/generated/compile.h
CC init/version.o
LD init/built-in.o
LD .tmp_vmlinux1
net/built-in.o: In function `tcp_nuke_addr':
activity_stats.c:(.text+0x4e4b4): undefined reference to `rt6_lookup'
activity_stats.c:(.text+0x4e5c4): undefined reference to `in6addr_any'
make: *** [.tmp_vmlinux1] Error 1
root#ubuntu:/disk2/android/kernel/goldfish#
some of the documentation says that I should remove the CONFIG_NETFILTER=y from the script.
I did it but the result is same.
I am stuck right now... can someone help me??
thank you in advance.
There is a suggested list of Linux configuration options at https://source.android.com/devices/tech/kernel.html including CONFIG_NETFILTER.
i removed all the configurations to do with IPV6, INET6
Remember to disable only IPV6 related modules and not the IPv4.
# CONFIG_IPV6 is not set
After that I was able to build it.
Also works
CONFIG_IPV6=y
It seems that build try to configure IPv6 as a module, but there are functions used by kernel that not are available if IPv6 is not built-in in the kernel

Custom Android kernel - unable to load kernel modules

I'm having issues loading kernel modules on Android, the kernel has been crosscompiled from a 64bit linux box. Kernel boots fine, it just wont load any modules (even bcm4329.ko for wifi)
The kernel and modules are flashed onto the device (HTC Desire) using CWM "install from zip" feature in recovery.
root#android:/data # uname -a
Linux localhost 2.6.38.8-dtbaker2-ics+ #3 PREEMPT Sun May 5 18:50:10 EST 2013 armv7l GNU/Linux
trying to load module manually:
root#android:/system/lib/modules/dtbaker2-ics # ls -l
-rw-rw-rw- root root 287308 2013-05-05 08:50 bcm4329.ko
root#android:/system/lib/modules/dtbaker2-ics # depmod -a
root#android:/system/lib/modules/dtbaker2-ics # modprobe bcm4329
modprobe: 'dtbaker2-ics/bcm4329.ko': unknown symbol in module or invalid parameter
root#android:/system/lib/modules/dtbaker2-ics # insmod bcm4329.ko
insmod: init_module 'bcm4329.ko' failed (No such file or directory)
root#android:/system/lib/modules/dtbaker2-ics # modinfo bcm4329.ko
filename: bcm4329.ko
license: GPL v2
alias: sdio:c*v02D0d4319*
alias: sdio:c*v02D0d4329*
alias: sdio:c*v02D0d0493*
alias: sdio:c*v02D0d0492*
alias: sdio:c*v02D0d0000*
depends:
vermagic: 2.6.38.8-dtbaker2-ics+ preempt mod_unload ARMv7
parm: dhd_oob_gpio_num:DHD oob gpio number
parm: clockoverride:SDIO card clock override
dmesg output after modprobe or insmod:
[ 1525.047424] bcm4329: Unknown symbol _GLOBAL_OFFSET_TABLE_ (err 0)
The /proc/config.gz output from my custom built kernel is identical to the output from a fresh install of this ICS ROM (bar the date).
A couple of forum posts mentioned SLAB/SLUB and module loading issues, so I even tried to rebuild the initial SLAB kernel (dtbaker-ics) with the SLUB option (dtbaker2-ics) and the identical problem exists.
Could this be a 64bit cross compilation issue? Is it weird that the kernel works fine but modules are not loaded?
Ah! "Unknown symbol _GLOBAL_OFFSET_TABLE_" error: https://groups.google.com/forum/?fromgroups=#!topic/android-kernel/dzEIOVuxtEo
I remembered early on I swapped to using prebuilt toolchain arm-linux-androideabi-4.4.3 instead of arm-eabi-4.4.0 because arm-eabi-4.4.0 did not work for me on 64bit ubuntu ( shrug )
As per the above google groups post, modified my kernel Makefile from this:
MODFLAGS        = -DMODULE -march=armv7-a -mfpu=vfpv3 -ftree-vectorize
to this:
MODFLAGS        = -DMODULE -march=armv7-a -mfpu=vfpv3 -ftree-vectorize -fno-pic
Then re-built the kernel modules:
make clean
make modules
and copied my new module back over to android, away she goes!

Categories

Resources