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
"Invalid parameters" error when trying to insert module that accesses exported symbol
提问by Vilhelm Gray
I'm trying to share a global variable between two modules in order to understand how to use the EXPORT_SYMBOL
macro correctly, but I keep getting an Invalid paramaters
error 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.ko
first, 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_ALL
is enabled, so I can't look in /proc/kallsyms/
to verify that myvar
is 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.symvers
file.
解决方案在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 make
in the foodirectory to generate the Module.symvers
file in that directory for the foo
module.
例如,在我的例子中,两个模块分别位于/home/vilhelm/foo/和/home/vilhelm/bar/ 中。由于我分别编译每个模块,因此每个目录都有其各自的Makefile
. 首先我make
在foo目录中执行以Module.symvers
在该目录中为foo
模块生成文件。
# make
Next I modified the Makefile
for the bar
module 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 make
in the bardirectory and insert the module manually:
最后我make
在bar目录下执行,手动插入模块:
# 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! :-)
成功!:-)