Hello world kernel module for android & unknown re

2019-02-21 09:21发布

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.

2条回答
劫难
2楼-- · 2019-02-21 09:39

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

查看更多
劳资没心,怎么记你
3楼-- · 2019-02-21 09:52

It turns out I had to use

make CFLAGS_MODULE=-fno-pic
查看更多
登录 后发表回答