简体   繁体   English

加载 kernel 模块时出现未知符号

[英]Unknown symbol when loading a kernel module

I tried to redo the code of the kernel module found in this topic How can I obtain battery level inside a Linux kernel module?我试图重做本主题中找到的 kernel 模块的代码如何获取 Linux kernel 模块内的电池电量? . . But when I try to use the functions contained in the power_supply.h header file, the loading of the module fails because it does not recognize the power_supply_get_by_name function.但是当我尝试使用 power_supply.h header 文件中包含的函数时,模块加载失败,因为它无法识别 power_supply_get_by_name function。

Here is the code that I am using on Ubuntu 18.04 with kernel version 4.15.0-101-generic:这是我在 Ubuntu 18.04 和 kernel 版本 4.15.0-101-generic 上使用的代码:

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

static int __init test_init (void)
{
        struct power_supply *psy;
        char name[] = "BAT1";

        psy = power_supply_get_by_name(name);

        printk(KERN_DEBUG "Test module inserted");

        return 0;
}

static void __exit test_exit (void)
{
        printk(KERN_DEBUG "Test module removed");
}

module_init (test_init);
module_exit (test_exit);

I get no error at compiling except a warning concerning the module license which is I think not related to my problem but I get the following errors:我在编译时没有收到错误,除了关于模块许可证的警告,我认为这与我的问题无关,但我收到以下错误:

  1. when running insmod in the terminal: "insmod: ERROR: could not insert module test.ko: Unknown symbol in module"在终端中运行 insmod 时:“insmod:错误:无法插入模块 test.ko:模块中的未知符号”
  2. in the /var/log/kern.log file: "test: Unknown symbol power_supply_get_by_name (err 0)"在 /var/log/kern.log 文件中:“测试:未知符号 power_supply_get_by_name (err 0)”

I checked the kallsyms proc file and the function is indicated as usable in other kernel modules if I understood well this topic What is the difference between T and t in /proc/kallsyms .我检查了 kallsyms proc 文件,如果我很好地理解了这个主题,则 function 被指示在其他 kernel 模块中可用。 /proc/kallsyms 中的 T 和 t 有什么区别 Here is the output from reading the kallsyms file:这是读取 kallsyms 文件的 output:

ffffffff8e9bd270 T power_supply_get_by_name

Does anyone knows why this is not working while I can use other linux headers functions without any problem and, if so, how I can fix my problem?有谁知道为什么这不起作用,而我可以毫无问题地使用其他 linux 标头功能,如果是这样,我该如何解决我的问题?

Thanks in advance提前致谢

This may actually be related to the module license, If you look at the kernel source code, the function power_supply_get_by_name is exported here .这实际上可能与模块许可证有关,如果您查看 kernel 源代码,则 function power_supply_get_by_name is export here You can see it's using EXPORT_SYMBOL_GPL .您可以看到它正在使用EXPORT_SYMBOL_GPL As this answer explains:正如这个答案所解释的:

EXPORT_SYMBOL_GPL will show the symbol only in GPL-licensed modules EXPORT_SYMBOL_GPL 将仅在 GPL 许可的模块中显示符号

The use of this macro is controversial, but that's the way the project operates... To gain access to the symbols you need, you'll need to license your module as GPL:这个宏的使用是有争议的,但这是项目的运作方式......要访问您需要的符号,您需要将您的模块许可为 GPL:

MODULE_LICENSE("GPL");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM