linux module export symbol EXPORT_SYMBOL_GPL EXPORT_SYMBOL

Turn from http://blog.csdn.net/lisan04/article/details/4076013 The'/ prob/kallsyms'file of Linux 2.6 corresponds to the symbol table of the ...

Turn from http://blog.csdn.net/lisan04/article/details/4076013

The'/ prob/kallsyms'file of Linux 2.6 corresponds to the symbol table of the kernel, recording the symbol and the memory address where the symbol is located.

Kernel Symbol Table: Records the address and name of all symbols (functions, global variables, etc.) in the kernel. This symbol table is embedded in the kernel image, so that the kernel can get a symbol name corresponding to the symbol address at any time during operation.

Modules can export symbols to the kernel symbol table using the following macros:

  1. EXPORT_SYMBOL(Symbol name);
  2. EXPORT_SYMBOL_GPL(Symbol name)

EXPORT_SYMBOL(Symbol name); EXPORT_SYMBOL_GPL(Symbol name)

Exported symbols can be used by other modules, howeverBe sure to make a statement before using it.. EXPORT_SYMBOL_GPL()Applicable only to inclusion GPL The module of permission.

Code demo:

  1. //hello.c file, define two functions for export
  2. #include <<a href="http://lib.csdn.net/base/linux "class='replace_word'title="Linux knowledge base "target='_blank' style='color:#df3434; font-weight: bold;'>Linux</a>/init.h>
  3. #include <linux/module.h>
  4. MODULE_LICENSE("Dual BSD/GPL");
  5. int add_integar(int a,int b)
  6. {
  7. return a + b;
  8. }
  9. int sub_integar(int a,int b)
  10. {
  11. return a - b;
  12. }
  13. EXPORT_SYMBOL(add_integar);
  14. EXPORT_SYMBOL(sub_integar);
  15. //test.c. Functions derived from the hello module are invoked
  16. #include <linux/init.h>
  17. #include <linux/module.h>
  18. MODULE_LICENSE("Dual BSD/GPL");
  19. extern int add_integar(int ,int); //Declare the function to be called
  20. extern int sub_integar(int ,int); //Declare the function to be called
  21. int result(void)
  22. {
  23. int a,b;
  24. a = add_integar(1,1);
  25. b = sub_integar(1,1);
  26. printk("%d/n",a);
  27. printk("%d/n",b);
  28. return 0;
  29. }

//hello.c file, define two functions for export #include <<a href="http://Lib.csdn.net/base/linux "class='replace_word'title=" Linux knowledge base "target='_blank' style='color:#df3434; font-weight: bold;'>Linux</a>/init.h> #include <linux/module.h> MODULE_LICENSE("Dual BSD/GPL"); int add_integar(int a,int b) { return a + b; } int sub_integar(int a,int b) { return a - b; } EXPORT_SYMBOL(add_integar); EXPORT_SYMBOL(sub_integar); //test.c is used to call the functions derived from the hello module #include <linux/init.h> #include <linux/module.h> MODULE_LICENSE("Dual BSD/GPL"); extern int add_integar(int ,int); //Declare the function to be called extern int sub_integar(int ,int); //Declare the function to be called int result(void) { int a,b; a = add_integar(1,1); b = sub_integar(1,1); printk("%d/n",a); printk("%d/n",b); return 0; }

make After that, add in hello Module, reload test Modular.

Then? cat /proc/kallsyms | grep integer

Show:

  1. [root@localhost test]# cat /proc/kallsyms |grep integar
  2. e053d000 u add_integar [test]
  3. e053d004 u sub_integar [test]
  4. e053d02c r __ksymtab_sub_integar [hello]
  5. e053d03c r __kstrtab_sub_integar [hello]
  6. e053d034 r __ksymtab_add_integar [hello]
  7. e053d048 r __kstrtab_add_integar [hello]
  8. e053d000 T add_integar [hello]
  9. e053d004 T sub_integar [hello]

Items in the list:

It should be easy to see that the first column is the symbol address, the second column is the type, the third column is the symbol name, and the fourth column is the file.

Note: If all symbolic addresses are found to be 0, that is because of system protection. Use root permission to view.

The type of the second column:

Some symbols are capitalized and some are lowercase. The capitalized symbols are global.

  1. b symbol in uninitialized data area (BSS)
  2. c Ordinary symbol, is an uninitialized region
  3. d symbol in initialization data area
  4. g symbols for small object s, in the initialization data area
  5. i Symbols that do not directly refer to other symbols
  6. n Debugging Symbol
  7. r symbol in read-only data area
  8. The s symbol is for small object s in the uninitialized data area
  9. t symbol in code segment
  10. u symbol undefined

8 January 2019, 15:48 | Views: 1485

Add new comment

For adding a comment, please log in
or create account

0 comments