Source:
<include/linux/moudule.h>
.......
#ifndef Module_symbol_prefix
#define MODULE_SYMBOL_PREFIX ""
#endif
.......
struct Kernel_symbol//kernel symbol structure
{
Unsignedlong value; The address of the symbol in the memory address
Constchar *name; The name of the symbol
};
......
#define __EXPORT_SYMBOL (SYM,SEC) \
Externtypeof (sym) sym; \
__crc_symbol (sym,sec) \
Staticconst char __kstrtab_# #sym [] \
__attribute__ (Section ("__ksymtab_strings"), aligned (1)) \
=module_symbol_prefix#sym; \
staticconst struct Kernel_symbol __ksymtab_# #sym \
__used \
__attribute__ ((Section ("__ksymatab" sec), unused)) \
={(Unsignedlong) &sym,_kstrab_#sym}
#define EXPORT_SYMBOL (SYM) \
__expotr_symbol (Sym, "")
#define EXPORT_SYMBOL_GPL (SYM) \
__expotr_symbol (Sym, "_GPL")
#define EXPORT_SYMBOL (SYM) \
__expotr_symbol (Sym, "_gpl_future")
1. Preliminary knowledge:
Before analyzing, you should know the following knowledge:
(1) #运算符, # #运算符
You typically use # in a macro definition to create a string #abc就表示字符串 "abc", and so on.
# #运算符称为预处理器的粘合剂, used to replace the glue two different symbols,
such as: #definexName (n) x# #n
Then XName (4) becomes x4
(2) __attribute__ Properties of GCC:
The role of __attribute__ ((section ("Section_name")) is to place the specified function or variable into a segment named "Section_name".
The __attribute__ property can be added directly to the definition statement when the function or variable is defined.
such as: int myvar__attribute__ ((section ("MyData")) = 0;
Indicates that the shaping variable myvar=0 is defined, and that the variable is stored in a section named "MyData"
About Gcc_attribute detailed can refer to: http://blog.sina.com.cn/s/blog_661314940100qujt.html
2. Code Analysis:
Example: to export kernel symbols (kernel functions) MYFC,
such as calling Export_symbol (MYFC)
Expand to __export_symbol (MyFC, "")
Expand to
static const Char __kstrtab_myfc[] __attribute__ (Section ("__ksymtab_strings"), aligned (1))
=module_symbol_prefix MyFC;
static const struct Kernel_symbol __KSYMTAB_MYFC
__used
__attribute__ ((Section ("__ksymatab"), unsed))
={(unsigned long) &SYM,_KSTRAB_MYFC}
As previously known, __ATTRIBUTE__ is a property in GCC (__used is also the GCC attribute), which is used to add related properties to the specified function or variable, so that the __attribute__ property is masked before the definition of the variable is obscured, and the above definition becomes:
static const char __kstrtab_myfc[] = "MYFC";
static const struct kernel_symbol__ksymtab_myfc={(unsigned long) &myfc,_kstrab_myfc};
Defines a character array __kstrtab_myfc[] to hold the exported symbol name MYFC
Defines a kernel symbolic structure __KSYMTAB_MYFC used to hold the MYFC address and name in memory for the outgoing symbol.
When the __attribute__ property is added, it means:
Place the character array __kstrtab_myfc[] in a section named "__ksymtab_strings".
Place the kernel symbol structure __KSYMTAB_MYFC in a section called "__ksymatab".
If EXPORT_SYMBOL_GPL (MYFC) is called, the corresponding kernel symbol structure is placed in the section named "__KSYMATAB_GPL".
3. Summary: In kernel symbol export, Export_symbol (SYM) is called, the following actions are done:
(1) Define a character array to hold the name of the kernel export symbol and place it in the section "__ksymtab_strings".
(2) Define a kernel symbol structure to hold the memory address and name of the exported symbol and place it in "__ksymatab".
That is, through Export_symbol (SYM) tells the world outside the kernel about this symbol two point information: the name of the kernel symbol and its memory address.