Linux 尝试插入访问导出符号的模块时出现“无效参数”错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16360689/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 22:51:11  来源:igfitidea点击:

"Invalid parameters" error when trying to insert module that accesses exported symbol

linuxlinux-kernellinux-device-driver

提问by Vilhelm Gray

I'm trying to share a global variable between two modules in order to understand how to use the EXPORT_SYMBOLmacro correctly, but I keep getting an Invalid paramaterserror when I try to insert the second module.

我试图在两个模块之间共享一个全局变量,以便了解如何EXPORT_SYMBOL正确使用宏,但是Invalid paramaters当我尝试插入第二个模块时,我不断收到错误消息。

In the first module foo.c:

在第一个模块中foo.c

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

extern unsigned myvar;
unsigned myvar = 42;
EXPORT_SYMBOL(myvar);

static int __init foo_init(void){
        printk(KERN_INFO "HELLO FROM MODULE 1");

        return 0;
}

static void __exit foo_exit(void){
        printk(KERN_INFO "BYE FROM MODULE 1");
}

module_init(foo_init);
module_exit(foo_exit);

MODULE_LICENSE("GPL");

In the second module bar.c:

在第二个模块中bar.c

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

extern unsigned myvar;

static int __init bar_init(void){
        printk(KERN_INFO "HELLO FROM MODULE 2");

        printk(KERN_INFO "myvar: %u", myvar);

        return 0;
}

static void __exit bar_exit(void){
        printk(KERN_INFO "BYE FROM MODULE 2");
}

module_init(bar_init);
module_exit(bar_exit);

MODULE_LICENSE("GPL");


I compile each module in separate directories with separate Makefiles. Then I insert each module manually:

我使用单独的 Makefile 在单独的目录中编译每个模块。然后我手动插入每个模块:

# insmod foo.ko
# insmod bar.ko

Error: could not insert module bar.ko: Invalid parameters   

If I attempt to insert bar.kofirst, I receive the expected unknown symbol error:

如果我尝试先插入bar.ko,则会收到预期的未知符号错误:

# insmod bar.ko
Error: could not insert module bar.ko: Unknown symbol in module


Here's the symbol dump:

这是符号转储:

# nm foo.ko | grep myvar

00000000ec933bae A __crc_myvar
0000000000001118 r __kcrctab_myvar
000000000000057c r __kstrtab_myvar
0000000000000b20 r __ksymtab_myvar
0000000000000180 D myvar

I'm running a Debiansystem (kernel v3.2.21) with a Xenomaipatch applied:

我正在运行一个应用了Xenomai补丁的Debian系统(内核v3.2.21):

# uname -r
3.2.21-xenomai-2.6.2.1-ipipe

Unfortunately, I don't think CONFIG_KALLSYMS_ALLis enabled, so I can't look in /proc/kallsyms/to verify that myvaris actually exported.

不幸的是,我认为CONFIG_KALLSYMS_ALL没有启用,因此我无法查看/proc/kallsyms/以验证是否myvar已实际导出。

采纳答案by Vilhelm Gray

I decided to poke around the system to try to find the cause of the error:

我决定在系统中四处看看,试图找出错误的原因:

# dmesg | tail
[11169.107152] HELLO FROM MODULE 1
[11226.101245] bar: no symbol version for myvar
[11226.101254] bar: Unknown symbol myvar (err -22)

Looks like the problem wasn't with exporting the symbol, but rather accounting for the symbol version.

看起来问题不在于导出符号,而是考虑符号版本

The solution is detailed in Documentation/kbuild/modules.txtand is quite simple: make sure the symbol has an entry in the Module.symversfile.

解决方案在Documentation/kbuild/modules.txt 中有详细说明,非常简单:确保该符号在Module.symvers文件中有一个条目。



For example, in my case the two modules where located in /home/vilhelm/foo/and /home/vilhelm/bar/respectively. Since I compile each module separately, each directory has its own respective Makefile. First I executed makein the foodirectory to generate the Module.symversfile in that directory for the foomodule.

例如,在我的例子中,两个模块分别位于/home/vilhelm/foo//home/vilhelm/bar/ 中。由于我分别编译每个模块,因此每个目录都有其各自的Makefile. 首先我makefoo目录中执行以Module.symvers在该目录中为foo模块生成文件。

# make

Next I modified the Makefilefor the barmodule by inserting the following line at the top of the Makefile:

接着我修改了Makefile用于bar通过在的顶部插入以下线路模块Makefile

KBUILD_EXTRA_SYMBOLS := /home/vilhelm/foo/Module.symvers

Note that this is an absolute path!

注意这是绝对路径!

Finally I execute makein the bardirectory and insert the module manually:

最后我makebar目录下执行,手动插入模块:

# make

...

# insmod /home/vilhelm/foo/foo.ko
# insmod /home/vilhelm/bar/bar.ko

No errors, so that's a good sign.

没有错误,所以这是一个好兆头。

And now the moment of truth:

现在是关键时刻:

# dmesg | tail
[12675.200451] HELLO FROM MODULE 1
[12715.743320] HELLO FROM MODULE 2
[12715.743328] myvar: 42

Success! :-)

成功!:-)