【问题标题】:How to port a linux driver , which is compiled in 2.6 kernel ,without compiling in other new version of kernel如何移植2.6内核编译的linux驱动,而不用其他新版本内核编译
【发布时间】:2013-07-24 18:37:43
【问题描述】:

感谢每一个人,

这是我在一次采访中提出的问题。

我有一个在 Linux 内核版本 2.6 中编译的 Linux 设备驱动程序。我想将相同的驱动程序移植到具有内核 3.X 的 Linux PC 中,而无需在新版本中编译。

有可能吗?如果可能,请让我知道如何。如果不可能,请告诉我为什么不呢?

感谢和问候 湿婆

【问题讨论】:

  • 移植您的驱动程序通常会涉及代码更改。如果您更改了代码,但不允许重新编译,您将如何获得内核模块?

标签: linux linux-device-driver


【解决方案1】:

不,您不能将针对一个版本编译的模块移植到另一个版本。

原因如下

Modules are strongly tied to the data structures and function prototypes defined in a particular kernel version; the interface seen by a module can change significantly from one kernel version to the next. This is especially true of development kernels, of course

The kernel does not just assume that a given module has been built against the proper kernel version. One of the steps in the build process is to link your module against a file (called vermagic.o) from the current kernel tree; this object contains a fair amount of information about the kernel the module was built for, including the target kernel version, compiler version, and the settings of a number of important configuration variables. When an attempt is made to load a module, this information can be tested for compatibility with the running kernel. If things don’t match,

模块未加载;相反,您会看到如下内容:

# insmod hello.ko

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

查看系统日志文件(/var/log/messages 或您的系统配置的任何内容 to use) 将揭示导致模块无法加载的具体问题。

内核接口经常在不同版本之间发生变化。如果您正在编写一个模块 旨在与多个版本的内核一起工作(特别是如果它必须工作 跨主要版本),您可能必须使用宏和#ifdef 结构 使您的代码正确构建。

【讨论】:

  • 假设我像 #insmod -f hello.ko 那样强制会发生什么
  • 如果你试图强制你只会看到上面的消息。插入'./hello.ko'时出错:-1 无效的模块格式。另外,如果您看到dmesg,您会看到“不同意符号 module_layout 的版本
【解决方案2】:

现在不可能了:

  • 通常,“驱动程序”是二进制内核模块

  • 移植将涉及内核模块的代码更改。如果更改代码,则需要对其进行编译,以获得二进制文件。

  • 由于内核模块在内核空间中运行,因此它们的健壮性至关重要。由于内核 API 的某些部分不时更改,因此尝试将针对内核 X 编译的模块与另一个内核 Y 一起使用,可能会因为缺少符号(如果幸运的话)而无法加载,或者会导致内核恐慌,因为语义发生了变化。

  • 顺便说一句,所有这些都与2.6.x3.y 没有真正的关系,但适用于任何内核版本

但是:当然理论上可以在您最喜欢的十六进制编辑器中将内核模块“编写”为二进制代码,而无需求助于编译器等。这将允许您将驱动程序从一个内核“移植”到另一个内核,而无需重新编译。我想这不适合人类……

【讨论】:

    猜你喜欢
    • 2015-07-07
    • 1970-01-01
    • 1970-01-01
    • 2012-06-26
    • 2017-03-09
    • 1970-01-01
    • 2019-11-19
    • 2014-09-26
    相关资源
    最近更新 更多