简体   繁体   中英

Can a linux kernel source use a function from driver?

I have a loadable module(driver) function that linux kernel source need to use.

The function has already opened by EXPORT_SYMBOL_GPL() .

The linux kernel source is one of c file in linux-3.16.1/mm .

However, I add extern function in c file and recompile entire Linux kernel source.

The error message print 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.

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).

Consider a sample GPIO driver(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

Structure is defined here
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.

Fill that structure from LKM and use it on 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. 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 ). 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 ).

Side note:
I agree with unwind and Ian Abbott - search for a better solution, kernel provides API. For example look at other modules.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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