driver:linux2.6 Kernel module Export function instance (export_symbol) "Go"

Source: Internet
Author: User

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:

    1. #include <linux/module.h>
    2. #include <linux/init.h>
    3. #include <linux/kernel.h>
    4. Module_license ("GPL");
    5. int myprint (void)
    6. {
    7. PRINTK ("C");
    8. return 0;
    9. }
    10. static int darren_init (void)
    11. {
    12. return 0;
    13. }
    14. static void Darren_exit (void)
    15. {
    16. }
    17. Module_init (Darren_init);
    18. Module_exit (Darren_exit);
    19. Export_symbol (Myprint);

B Module Code :

    1. #include <linux/seq_file.h>
    2. #include <linux/cdev.h>
    3. #include <asm/system.h>
    4. Module_license ("GPL");
    5. extern int print (void);
    6. static int darren_init (void)
    7. {
    8. int i=0;
    9. PRINTK ("b module init\n");
    10. for (; i<10;i++) print ();
    11. return 0;
    12. }
    13. static void Darren_exit (void)
    14. {
    15. }
    16. Module_init (Darren_init);
    17. Module_exit (Darren_exit);

The makefile of a module is as follows:

    1. Name:=a
    2. Sym:=/usr/src/linux-headers-2.6.38-8-generic/module.symvers
    3. dir:=/lib/modules/$ (shell uname-r)/build/
    4. pwd:=$ (shell pwd)
    5. Obj-m = $ (NAME). O
    6. Build
    7. Make-c $ (DIR) m=$ (PWD)
    8. sudo chmod 777 $ (SYM)
    9. sudo sed-i '/myprint/d ' $ (SYM)
    10. sudo cat module.symvers>>$ (SYM)
    11. sudo chmod 644 $ (SYM)


B-Module Makefile :

    1. Name:=b
    2. dir:=/lib/modules/$ (shell uname-r)/build/
    3. pwd:=$ (shell pwd)
    4. Obj-m = $ (NAME). O
    5. Build
    6. 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 :

  1. Sometimes, an external module uses exported symbols from
  2. Another external module. Kbuild needs to has full knowledge of
  3. All symbols to avoid spitting out warnings about undefined
  4. Symbols. Three solutions exist for this situation.
  5. Note:the method with a top-level Kbuild file is recommended
  6. But May is impractical in certain situations.
  7. Use a top-level kbuild file
  8. If you have both modules, Foo.ko and Bar.ko, where
  9. Foo.ko needs symbols from Bar.ko, can use a
  10. Common top-level Kbuild file so both modules is
  11. Compiled in the same build. Consider the following
  12. Directory layout:
  13. ./foo/<= contains Foo.ko
  14. ./bar/<= contains Bar.ko
  15. The top-level kbuild file would then look like:
  16. #./kbuild (or./makefile):
  17. Obj-y: = foo/bar/
  18. and executing
  19. $ make-c $KDIR m= $PWD
  20. Expected and compile both modules with
  21. Full knowledge of symbols from either module.
  22. Use of an extra module.symvers file
  23. When the external module is built, a module.symvers file
  24. is generated containing all exported symbols which are
  25. Not defined in the kernel. To get access to symbols
  26. From Bar.ko, copy the Module.symvers file from the
  27. Compilation of Bar.ko to the directory where Foo.ko is
  28. Built. During the module build, Kbuild would read the
  29. Module.symvers file in the directory of the external
  30. module, and when the build is finished, a new
  31. Module.symvers file is created containing the sum of
  32. All symbols defined and is part of the kernel.
  33. Use ' Make ' variable kbuild_extra_symbols
  34. If It is impractical to copy module.symvers from
  35. Another module, you can assign a space separated list
  36. of files to Kbuild_extra_symbols in your build file.
  37. These files would be loaded by modpost during the
  38. Initialization of its symbol tables.

driver:linux2.6 Kernel module Export function instance (export_symbol) "Go"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.