无法加载模块:不同意符号 module_layout 的版本

use*_*508 17 linux compiling kernel-modules

按照本教程编写我的第一个驱动程序。

生成文件是:

# Makefile – makefile of our first driver

# if KERNELRELEASE is defined, we've been invoked from the
# kernel build system and can use its language.
ifneq (${KERNELRELEASE},)
    obj-m := ofd.o
# Otherwise we were called directly from the command line.
# Invoke the kernel build system.
else
    KERNEL_SOURCE := /usr/src/linux 3.8
    PWD := $(shell pwd)
default:
    ${MAKE} -C ${KERNEL_SOURCE} SUBDIRS=${PWD} modules

clean:
    ${MAKE} -C ${KERNEL_SOURCE} SUBDIRS=${PWD} clean
endif
Run Code Online (Sandbox Code Playgroud)

驱动程序代码是:

* ofd.c – Our First Driver code */
#include <linux/module.h>
#include <linux/version.h>
#include <linux/kernel.h>

static int __init ofd_init(void) /* Constructor */
{
    printk(KERN_INFO "Namaskar: ofd registered");
    return 0;
}

static void __exit ofd_exit(void) /* Destructor */
{
    printk(KERN_INFO "Alvida: ofd unregistered");
}

module_init(ofd_init);
module_exit(ofd_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Anil Kumar Pugalia <email_at_sarika-pugs_dot_com>");
MODULE_DESCRIPTION("Our First Driver");
Run Code Online (Sandbox Code Playgroud)

制作过程中没有错误。但是当我使用时,insmod ofd.ko我无法加载它。在dmesg这样说的:

不同意符号 module_layout 的版本

  • uname -r 返回“3.8.0-38-generic”,内核源代码也是 3.8。
  • modprobe -f ofd.ko 也失败

还:

#56~precise1-Ubuntu SMP Thu Mar 13 16:23:47 UTC 2014
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 12.04.4 LTS
Release:    12.04
Codename:   precise
Run Code Online (Sandbox Code Playgroud)

怎么了?

Gil*_*il' 16

Linux 内核包含数据结构,其布局不仅因版本而异,而且还取决于编译选项。因此,当您编译内核模块时,不仅需要来自内核源代码的头文件,还需要一些内核编译过程中生成的头文件。仅仅解压内核源代码是不够的。

对于使用 构建的内核CONFIG_MODVERSIONS,版本号可以不同,但​​数据结构的布局必须相同。这个选项在 Ubuntu 内核中被激活。使用此选项,除了头文件之外,还需要针对正确的Module.symvers文件编译模块。与大多数发行版一样,Ubuntu 将此文件包含在与编译产生的内核头文件相同的包中。Ubuntu 内核头文件包被称为,例如。linux-headers-VERSION-VARIANTlinux-headers-3.8.0-38-generic

如果内核没有构建CONFIG_MODVERSIONS(如果您编译自己的内核,情况可能如此),加载模块时的检查是一个简单的版本检查。您可以获取解压后的内核源代码,复制.config在编译运行内核期间使用的内核源代码,然后运行make modules_prepare. 您有责任验证您对内核所做的任何补丁都不会影响二进制兼容性。