Preface
Export_symbol macros are used after Linux-2.6, in the Linux-2.4 kernel, the default non-static functions and variables are automatically imported into the kernel space and are not marked with Export_symbol ().
Then, after Linux-2.6, all symbols are not exported by default, so use Export_symbol () to mark them. the role of 1.export_symbol macros The functions or symbols defined within the Export_symbol tag are exposed to all kernel code, and can be invoked directly in kernel modules without modifying the kernel code, that is, using export_symbol to export a function as a symbol to other modules. The meaning of the symbol is the entry address of the function, or the symbols and corresponding addresses are saved, in the process of running the kernel, you can find the corresponding address of these symbols. Here's a comparison with System.map: System.map is the function address at the time of the connection. After the connection is complete, it is not known which symbol is at which address during the 2.6 kernel run. The symbol of Export_symbol is to save these symbols and corresponding addresses, and in the process of running the kernel, you can find the corresponding addresses of these symbols. In the module loading, its essence is dynamically linked to the kernel. If you refer to a kernel or other module symbol in a module, you must export_symbol these symbols to find the corresponding address connection. How to use 2export_symbol 1. Use Export_symbol (function name) after definition of module functions
2. Use extern to declare the symbol or function to use in the module that calls the function
3. First load the module that defines the function, then load the module that calls the function 2.1 export_symbol demo
For example, there are two driver modules: module A and Module B, where module B uses the function of export in module A, so it must be added in the makefile file of Module B:
Kbuild_extra_symbols + +/path/to/modulea/module.symvers
Export Kbuild_extra_symbols
In this way, when you compile module B, you will not see the warning, prompting that func1 this symbol can not be found, and resulting in a compilation of KO load will also error.
Module A (MOD_A.C)
#include <linux/init.h>
#include <linux/module.h>
#include <linux/ kernel.h>
static int func1 (void)
{
printk ("in Func:%s...\n", __func__);
return 0;
}
Export symbol func1
export_symbol (func1);
static int __init hello_init (void)
{
printk ("Module 1,say Hello world!\n");
return 0;
}
static void __exit hello_exit (void)
{
printk ("Module 1,exit!\n");
}
Module_init (hello_init);
Module_exit (Hello_exit);
Module B (MOD_B.C)
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/ module.h>
extern int functl (void);
static int Func2 (void)
{
func1 ();
PRINTK ("in Func:%s...\n", __func__);
return 0;
}
static int __init hello_init (void)
{
printk ("Module 2,is used Module 1 function!\n");
Func2 ();
return 0;
}
static void __exit hello_exit (void)
{
printk ("Module 2,exit!\n");
}
Module_init (hello_init);
Module_exit (Hello_exit);
When driving loads, be sure to load the module a modules that define function1, and then load the driver module of Module B that calls Function1. Follow these steps:
Insmod Module_a.ko
insmod Module_b.ko