简体   繁体   中英

How static init and exit functions in a module are called outside the file by the kernel?

I am reading LDD3 and had a doubt regarding the usage of static storage class in the __init and __exit function calls. http://static.lwn.net/images/pdf/LDD3/ch02.pdf

"Initialization functions should be declared static, since they are not meant to be visible outside the specific file; there is no hard rule about this, though, as no function is exported to the rest of the kernel unless explicitly requested"

But then the kernel is able to use init and exit function using insmod and rmmod system calls. If static functions are functions that are only visible to other functions in the same file, then how is the kernel able to use the __init and __exit functions defined static in our module?

Static function in one translation unit can be exported through back-doors like function pointers. The dynamic linking uses the same approach.

In your driver code you must also be writing

module_init(__init_function_name__) and module_exit( exit_function_name ).

You may see how these macros are implemented.

Technically in C, by exporting function pointer of static function in one file and calling them from another file using extern function pointer is possible. And many such framework like Linux kernel are doing the same.


module_inint(your_func);

module_init is macro which expands such that your your_func 's function pointer is getting added in one array. Which getting called at boot up time or module loading time.

A great explannation about module_init() is at https://stackoverflow.com/a/18606561/775964

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