Transferred from: http://blog.chinaunix.net/uid-23381466-id-3837650.html
Kernel version: 2.6.38-11-generic
The kernel itself is a large number of kernel symbol table export function, then should be exported, LDD3 said only need to export_symbol a class of macro export can, the results have been a long time, the final view of the document, is understood a little.
For the export symbol table, the kernel document gives three solutions, see trailer, now ignored.
Now there are two modules, a module export function myprint,b module Use this function, imagine if a module Export_symbol (myprint), actually B module know, it is clear that the B module is not very clear to the matter (so it is not very accurate), To call the Myprint function of the A module, you need to know the location of the Myprint function in memory, first in the kernel symbol table is not described, so ...
When we compile the A module, look at some of the files, do not have a module.symvers file, open to see what the situation?
0x705034f7 Myprint/home/darren/desktop/darren/print/myprint Export_symbol
Well, this line is sufficient for the B module, specifying the memory location, symbol name, and module path. The simplest way is to copy this file to the directory where the B module is located, and then compile it without annoying errors, and you can insmod the module normally. This approach is one of the methods mentioned in the kernel documentation.
But every time the function is called to copy once, even worse is a module every compile, to replicate again, why the kernel itself exported functions we can directly use it? Now it's settled:
Compile the kernel will also generate a module.symvers file, the kernel exported all the symbols are inside, we compile the module will actually call the kernel of the top layer makefile, that is, the kernel module.symvers to our module is visible, and our own MODULE.S Ymvers file, OK, the module a module.symvers file merge into the kernel module.symvers file, this time the Myprint function becomes the real export function, the other modules only need to life a bit can be used.
The code is as follows
a module code:
- #include <linux/module.h>
- #include <linux/init.h>
- #include <linux/kernel.h>
- Module_license ("GPL");
- int myprint (void)
- {
- PRINTK ("C");
- return 0;
- }
- static int darren_init (void)
- {
- return 0;
- }
- static void Darren_exit (void)
- {
- }
- Module_init (Darren_init);
- Module_exit (Darren_exit);
- Export_symbol (Myprint);
B Module Code :
- #include <linux/seq_file.h>
- #include <linux/cdev.h>
- #include <asm/system.h>
- Module_license ("GPL");
- extern int print (void);
- static int darren_init (void)
- {
- int i=0;
- PRINTK ("b module init\n");
- for (; i<10;i++) print ();
- return 0;
- }
- static void Darren_exit (void)
- {
- }
- Module_init (Darren_init);
- Module_exit (Darren_exit);
The makefile of a module is as follows:
- Name:=a
- Sym:=/usr/src/linux-headers-2.6.38-8-generic/module.symvers
- dir:=/lib/modules/$ (shell uname-r)/build/
- pwd:=$ (shell pwd)
- Obj-m = $ (NAME). O
- Build
- Make-c $ (DIR) m=$ (PWD)
- sudo chmod 777 $ (SYM)
- sudo sed-i '/myprint/d ' $ (SYM)
- sudo cat module.symvers>>$ (SYM)
- sudo chmod 644 $ (SYM)
B-Module Makefile :
- Name:=b
- dir:=/lib/modules/$ (shell uname-r)/build/
- pwd:=$ (shell pwd)
- Obj-m = $ (NAME). O
- Build
- Make-c $ (DIR) m=$ (PWD)
Note: The path/usr/src/linux-headers-2.6.38-8-generic/module.symvers may not be correct if it does not change to/usr/src/linux-headers-' uname-r '- Generic/module.symvers
Kernel Documentation :
- Sometimes, an external module uses exported symbols from
- Another external module. Kbuild needs to has full knowledge of
- All symbols to avoid spitting out warnings about undefined
- Symbols. Three solutions exist for this situation.
- Note:the method with a top-level Kbuild file is recommended
- But May is impractical in certain situations.
- Use a top-level kbuild file
- If you have both modules, Foo.ko and Bar.ko, where
- Foo.ko needs symbols from Bar.ko, can use a
- Common top-level Kbuild file so both modules is
- Compiled in the same build. Consider the following
- Directory layout:
- ./foo/<= contains Foo.ko
- ./bar/<= contains Bar.ko
- The top-level kbuild file would then look like:
- #./kbuild (or./makefile):
- Obj-y: = foo/bar/
- and executing
- $ make-c $KDIR m= $PWD
- Expected and compile both modules with
- Full knowledge of symbols from either module.
- Use of an extra module.symvers file
- When the external module is built, a module.symvers file
- is generated containing all exported symbols which are
- Not defined in the kernel. To get access to symbols
- From Bar.ko, copy the Module.symvers file from the
- Compilation of Bar.ko to the directory where Foo.ko is
- Built. During the module build, Kbuild would read the
- Module.symvers file in the directory of the external
- module, and when the build is finished, a new
- Module.symvers file is created containing the sum of
- All symbols defined and is part of the kernel.
- Use ' Make ' variable kbuild_extra_symbols
- If It is impractical to copy module.symvers from
- Another module, you can assign a space separated list
- of files to Kbuild_extra_symbols in your build file.
- These files would be loaded by modpost during the
- Initialization of its symbol tables.
driver:linux2.6 Kernel module Export function instance (export_symbol) "Go"