Hello world kernel module for android & unknown relocation: 27 when insmod

匿名 (未验证) 提交于 2019-12-03 02:29:01

问题:

I am trying to create a simple kernel module. I am trying to print messages to dmesg but i keep getting

insmod: init_module 'hello.ko' failed (Exec format error) in android

after : dmesg: unknown relocation: 27

#include <linux/module.h> #include <linux/kdb.h> int init_module(void) {     printk(KERN_ALERT "Hello world!\n");     return 1; }  void cleanup_module(void) {     printk(KERN_INFO "Goodbye world 1.\n"); } MODULE_AUTHOR("Robert P. J. Day"); MODULE_LICENSE("Dual BSD/GPL"); MODULE_VERSION("2:1.0") ; MODULE_DESCRIPTION("You have to start somewhere."); 

The make file

    obj-m +=hello.o   KERNELDIR ?= ~/android/kernel/common #KERNELDIR ?= /lib/modules/$(shell uname -r)/build PWD := $(shell pwd) CROSS_COMPILE=~/android-ndk-r8/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/bin/arm-linux-androideabi-  ARCH=arm default: $(MAKE) -C $(KERNELDIR) M=$(PWD) ARCH=arm CROSS_COMPILE=$(CROSS_COMPILE) modules  clean: $(MAKE) -C $(KERNELDIR) M=$(PWD) clean rm *.symvers 

does anyone know why? And how to get it working?

I found after doing a readelf that when it is compiled the relocation section is pointing to the wrong directions.

Offset     Info    Type            Sym.Value  Sym. Name 00000008  0000171b R_ARM_PLT32       00000000   printk 

When in fact it should be:

Offset     Info    Type            Sym.Value  Sym. Name 00000008  0000171c R_ARM_CALL       00000000   printk 

Can someone guess/know how this might be? Thanks @Chris Stratton for helping me this far.


I found after doing a readelf that when it is compiled the relocation section is pointing to the wrong directions.

Offset     Info    Type            Sym.Value  Sym. Name 00000008  0000171b R_ARM_PLT32       00000000   printk 

When in fact it should be:

Offset     Info    Type            Sym.Value  Sym. Name 00000008  0000171c R_ARM_CALL       00000000   printk 

Can someone guess/know how this might be? Thanks @Chris Stratton for helping me this far.

回答1:

It turns out I had to use

make CFLAGS_MODULE=-fno-pic 


回答2:

Thanks, -fno-pic got my dlkm working also. On to "real" work...

BTW, you can export ARCH, SUBARCH, CROSS_COMPILE, KERNELDIR in the shell so the Makefile becomes very simple. This also allows one to more easily build the dlkm for the native or target platform by setting the KERNELDIR properly (native does not need the ARCH, SUBARCH or CROSS_COMPILE vars.)

obj-m += hello.o

all: make -C $(KERNELDIR) M=$(PWD) modules



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!