简体   繁体   English

Linux内核源代码可以使用驱动程序中的功能吗?

[英]Can a linux kernel source use a function from driver?

I have a loadable module(driver) function that linux kernel source need to use. 我有一个可加载的module(driver)函数,Linux内核源需要使用它。

The function has already opened by EXPORT_SYMBOL_GPL() . 该函数已经由EXPORT_SYMBOL_GPL()打开。

The linux kernel source is one of c file in linux-3.16.1/mm . linux内核源代码是linux-3.16.1/mm的c文件之一。

However, I add extern function in c file and recompile entire Linux kernel source. 但是,我在c文件中添加了extern function ,然后重新编译了整个Linux内核源代码。

The error message print undefined reference to fun . 错误消息打印undefined reference to fun

I think the error is occured at link time . 我认为该错误是在链接时发生的。

It could not find the function reference from the driver. 无法从驱动程序中找到功能参考。

How can I solve this problem? 我怎么解决这个问题?

I have a loadable module(driver) function that linux kernel source need to use. 我有一个可加载的module(driver)函数,Linux内核源需要使用它。

Yes. 是。 This is possible. 这个有可能。 But Not in straight forward way you looking for. 但是,您并非以直截了当的方式寻找。 Most of the drivers works in the same way being as a loadable kernel module(LKM). 大多数驱动程序与可加载内核模块(LKM)的工作方式相同。

Consider a sample GPIO driver(CONFIG_ATH79). 考虑一个示例GPIO驱动程序(CONFIG_ATH79)。

In the following link, we can see the functions are assigned to a structure of function pointers. 在以下链接中,我们可以看到将函数分配给函数指针的结构。
http://lxr.free-electrons.com/source/drivers/gpio/gpio-ath79.c#L124 http://lxr.free-electrons.com/source/drivers/gpio/gpio-ath79.c#L124

Structure is defined here 结构在这里定义
http://lxr.free-electrons.com/source/include/linux/gpio/driver.h#L90 http://lxr.free-electrons.com/source/include/linux/gpio/driver.h#L90

Similar way, it can be done. 同样的方法,也可以做到。 Declare a structure in corresponding header file which should be included in LKM. 在相应的头文件中声明一个结构,该结构应包含在LKM中。

Fill that structure from LKM and use it on linux-3.16.1/mm/fileX.c 从LKM填充该结构,并在linux-3.16.1 / mm / fileX.c上使用它

Problem in nutshell: consider this example 简而言之的问题:请考虑以下示例

extern int test_module_function();

int teste_function() {
    return test_module_function();
}

When you compile this source - compiler doesn't need body of test_module_function() , declaration is enough. 当您编译此源代码时-编译器不需要test_module_function()主体,声明就足够了。 But when we are linking - we need definition of function. 但是,当我们进行链接时,我们需要定义功能。 For more information please read this and this . 欲了解更多信息,请阅读这个这个

In your case you provided only declaration, because EXPORT_SYMBOL_GPL() just makes your function callable from modules ( provides extern declaration of function ). 在您的情况下,您仅提供了声明,因为EXPORT_SYMBOL_GPL()仅使您的函数可从模块调用(提供函数的extern声明)。 It doesn't copy body of function in kernel sources ( see this ). 它不会在内核源代码中复制功能体(请参阅此内容 )。 And body of function is in your module and that module ( I presume ) wasn't builtin into kernel. 函数主体在您的模块中,并且该模块(我认为)没有内置在内核中。 So linker can't find it. 因此,链接器找不到它。

Possible solution - make module builtin into kernel ( as mentioned by dragosht ). 可能的解决方案-将模块内置到内核中(如dragosht所述 )。

Side note: 边注:
I agree with unwind and Ian Abbott - search for a better solution, kernel provides API. 我同意unwindIan Abbott的看法-寻找更好的解决方案,内核提供了API。 For example look at other modules. 例如,看看其他模块。

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

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