首页 > 解决方案 > helloworld 内核模块中的 insmod 错误:模块格式无效

问题描述

我正在交叉编译一个 helloworld 内核模块

我的文件是:

你好-1.c

#include <linux/module.h>
#include <linux/kernel.h>

int init_module(void)
{
    printk(KERN_INFO "Hello world 1.\n");
    return 0;
}

void cleanup_module(void)
{
    printk(KERN_INFO "Goodbye world 1.\n");
}

KBuild

obj-m := hello-1-helper.o
hello-1-helper-y := hsllo-1.o

生成文件

export KERNDIR=/home/myname/NXP/SDK2_0/QorIQ-SDK-V2.0-20160527-yocto/build_t1042d4rdb/tmp/work/t1042d4rdb-fsl-linux/linux-qoriq/4.1-r0/git
export PATH:=/opt/fsl-qoriq/2.0/sysroots/x86_64-fslsdk-linux/usr/bin/powerpc-fsl-linux:${PATH}
export ARCH=powerpc
export ARCH_CFLAGS+= -fPIC

default:
    $(MAKE) -C $(KERNDIR) CROSS_COMPILE=powerpc-fsl-linux- M=`pwd` V=1 modules
clean:
    $(MAKE) -C $(KERNDIR) M=`pwd` clean

这是我的终端输出,在 insmod 命令中显示错误:

module_32: hello-1-helper: unknown ADD relocation: 18
insmod: can't insert 'hello-1-helper.ko': invalid module format

但是当我在makefile下面的行评论时,一切都很好。

export ARCH_CFLAGS+= -fPIC

对于像这样的简单代码,我可以删除这个标志。但在另一个我应该使用 -fPIC 标志的代码中,我有同样的错误

我该如何解决?

标签: clinuxlinux-kernelfpickbuild

解决方案


推荐阅读