Export_symbol usage in Linux

Source: Internet
Author: User

The functions or symbols defined in the export_symbol label are public to all kernel code. You can directly call them in your kernel module without modifying the kernel code.


Export_symbol can be used to export a function as a symbol to other modules.


.

You can also manually modify the kernel source code to export other functions for re-compilation and testing after loading the new kernel.

Linux symbol export method:

[1] If we want export the symbol in a module, just useExport_symbol (xxxx
)


In the C or H file.
And compile the module by adding the compile flag-Dexport_symtab

.
Then we can useXxxx

In the other module.

[2] If we want export some symbol in kernel that is not in a module suchXxxx

In/ARCH/PPC/FEC. c

.
Firstly, define
Xxxx


InFEC. c

;
Secondly, make a new file which contain"Extern"

Define
Xxxx


(For example, extern int
Xxxx


);
Lastly, inPpc_ksyms.c

We recommend des the new file, and addExport_symbol

(
Xxxx


).
Then we can use
Xxxx


.

Notes for use:
The # include <Linux/module. h> file is required in the. c file using export_symbol.
// Write the function first
Func_a ()
{

}
// Use export_symbol again
Export_symbol (func_a); the "/proc/kallsyms" file of linux2.6 corresponds to the kernel symbol table and records the memory address of the symbol and symbol. The module can use the following macro export symbol to the kernel symbol table:
Export_symbol (symbol name );
Export_symbol_gpl (symbol name)
The exported symbols can be used by other modules, but you must declare them before using them. Export_symbol_gpl () is only applicable to modules that contain GPL permission.
Here is a code Demonstration:
A file is a hello. c file and defines two functions for export.

# Ifndef _ KERNEL _
# DEFINE _ KERNEL _
# Endif

# Ifndef Module
# Define Module
# Endif
# Include <Linux/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 );

Another file is test. C, which is used to call the function exported by the hello module.
# Ifndef _ KERNEL _
# DEFINE _ KERNEL _
# Endif

# 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", );
Printk ("% d/N", B );
Return 0;
}
Both files must have the corresponding makefile for compilation. The template is as follows:
Ifeq ($ (kernelrelease ),)

# Assume the source tree is where the running kernel was built
# You shoshould set kerneldir in the environment if it's elsewhere
Kerneldir? =/Lib/modules/$ (shell uname-R)/build

# The current directory is passed to sub-makes as argument
PWD: = $ (shell PWD)

Modules:
$ (Make)-C $ (kerneldir) M = $ (PWD) Modules

Modules_install:
$ (Make)-C $ (kerneldir) M = $ (PWD) modules_install

Clean:
Rm-RF *. O *~ Core. depend. *. CMD *. Ko *. Mod. C. tmp_versions

. Phony: modules modules_install clean

Else
# Called from kernel build system: just declare what our modules are
OBJ-M: = Hello. o
Endif
Change hello. O to the corresponding file name. O. After making, load the hello module and then the test module. Then CAT/proc/kallsyms | grep integar is displayed:
F8eae000 U add_integar [hello2]
F8eae004 U sub_integar [hello2]
F8eae02c R _ ksymtab_sub_integar [Hello]
F8eae03c R _ kstrtab_sub_integar [Hello]
F8eae034 R _ ksymtab_add_integar [Hello]
F8eae048 R _ kstrtab_add_integar [Hello]
F8eae000 t add_integar [Hello]
F8eae004 t sub_integar [Hello]

We can see the name and memory address of the symbol table. The functions defined in the export_symbol label are public to all kernel code. You can directly call them in your kernel module without modifying the kernel code.

Address: http://blog.csdn.net/lisan04/archive/2009/04/16/4076013.aspx

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.