insmod error: inserting './hello.ko': -1 Invalid module format"

insmod error: inserting './hello.ko': -1 Invalid module format"

我刚刚制作了我的第一个驱动模块,继LDD3之后的hello world模块。然而不幸遇到了这个错误:

insmod: error inserting './hello.ko': -1 Invalid module format.

我在 Ubuntu 11.04 上执行此操作,我的环境:

$ uname -r
2.6.38-8-generic

我得到这样的内核源代码:

sudo apt-cache search linux-source
linux-source - Linux kernel source with Ubuntu patches
linux-source-2.6.38 - Linux kernel source for version 2.6.38 with Ubuntu patches
$sudo apt-get install linux-source-2.6.38

我的/usr/src:

$ls /usr/src/
linux-headers-2.6.38-8          linux-source-2.6.38          vboxguest-5.0.10
linux-headers-2.6.38-8-generic  linux-source-2.6.38.tar.bz2

然后我编译内核

$sudo cp /boot/config-2.6.38-8-generic ./.config
$sudo make menuconfig -- load the .config file
$make
$make modules

然后我编译我的内核模块

$make -C /usr/src/linux-source-2.6.38/linux-source-2.6.38 M=`pwd` modules

使用生成文件:

obj-m := hello.o

最后当我插入模块时:

$sudo insmod hello_world.ko
insmod: error inserting 'hello_world.ko': -1 Invalid module format

我在 dmesg 中发现了什么:

hello: disagrees about version of symbol module_layout

那么问题是什么?

我也注意到 linux-header is -2.26.38-generic 和源代码版本是-2.26.38,这是问题吗?但我真的没有在网上找到 linux-source-2.26.38-generic 包。

状态更新: 我发现文件 /lib/moduels/$(name -r)/build/Makefile 表明我的 运行 内核版本:

VERSION = 2
PATCHLEVEL = 6
SUBLEVEL = 38
EXTRAVERSION = .2

所以我下载了linux-2.6.38.2并编译,还是一样的错误

我还发现 /boot/config-$(uname -r):

中有一行
CONFIG_VERSION_SIGNATURE="Ubuntu 2.6.38-8.42-generic 2.6.38.2"

有谁知道这是什么意思吗?我在构建的内核的配置文件中没有看到它。

构建内核模块的内核和要插入模块的内核应该是同一版本。如果你不想处理这件事,你可以使用下面的 Makefile。

obj−m += hello−world.o

all:
 make −C /lib/modules/$(shell uname −r)/build M=$(PWD) modules
clean:
 make −C /lib/modules/$(shell uname −r)/build M=$(PWD) clean

现在您可以构建并尝试插入模块了。

我建议你尽可能在这行之前成为 root

$sudo cp /boot/config-2.6.38-8-generic ./.config

$su
#cp /boot/config-2.6.38-8-generic ./.config
#insmod hello_world.ko

或者你也可以使用下面的 make 文件

TARGET  := hello-world
WARN    := -W -Wall -Wstrict-prototypes -Wmissing-prototypes
INCLUDE := -isystem /lib/modules/`uname -r`/build/include
CFLAGS  := -O2 -DMODULE -D__KERNEL__ ${WARN} ${INCLUDE}
CC      := gcc-3.0

${TARGET}.o: ${TARGET}.c

.PHONY: clean

clean:
    rm -rf ${TARGET}.o

您做的一切都正确,但没有使用您编译的内核引导系统,所以第一步是您应该使用它引导。 如果您使用的是 Ubuntu,您可以在启动时按住 shift 键,您将从那里获得系统中已编译内核的列表 select linux- source-2.6.38 然后尝试构建你的模块并按照你的方式安装它,你不会发现任何问题。 祝你好运。

尝试使用交叉编译。请查看下面的 make 文件代码。请注意缩进,否则您可能会遇到诸如 missing separator. Stop

之类的错误

obj-m += hello_.o #此名称应该是您的 .c 文件的名称。例如,我只是使用 hello

我建议最好的方法是通过交叉编译

创建一个变量来保存linux内核目录所在的目录名称在我的示例中,将值"PATH_TO_LINUX_KERNEL_DIRECTORY"更改为真实路径值 示例 ~/linux 您确实需要这样做,以便 make 文件知道在哪里可以找到 arm-linux-gnueabi- 如果没有这个,您可能 运行 进入问题 arm-linux-gnueabi-

KDIR := PATH_TO_LINUX_KERNEL_DIRECTORY

all:
    make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- -C $(KDIR) M=$(shell pwd) modules

clean:
    make -C $(KDIR) M=$(shell pwd) clean