[Reprinted] use of export_symbol

Source: Internet
Author: User

Export_symbol only appears in the 2.6 kernel. By default, non-static functions and variables in the 2.4 kernel are automatically imported to the kernel space, without the use of export_symbol () as a flag.
2.6 must be exported using export_symbol () (because 2.6 does not have any symbols by default ).

1. What is the role of export_symbol?
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, that is, you can use export_symbol to export a function as a symbol to other modules.
Here we will compare it with system. MAP:
In system. map, it is the function address during connection. After the connection is complete, during the 2.6 kernel running process, you do not know which symbol is at which address.
Export_symbol is used to save these symbols and their corresponding addresses. During kernel running, the corresponding addresses can be found. In the module loading process, the essence is to be able to dynamically connect to the kernel. If the symbols of the kernel or other modules are referenced in the module, the symbols export_symbol are required, in this way, the corresponding address connection can be found.

2. Usage
1. Use export_symbol (function name) after module function definition)
Second, use the extern statement in the module where the function is used.
Third, first load the module that defines the function, and then load the module that calls the function.

In addition, when compiling a module that calls an export function, there is usually a warning: "*****" [***********] undefined!
The same information is displayed after you run the dmesg command. At first, I thought that the module could not be loaded as long as this error exists. Then I checked it online and found that this was mainly because I had not dealt with the kernel while compiling the connection, and of course I could not find the symbol, however, because you generate a kernel module, the LD does not prompt an error, but provides a warning. It is hoped that the kernel can connect the symbol during insmod.

References:

-------------------------------------------------------------

Http://blog.chinaunix.net/u/12592/showart_461504.html

One module mod1 defines a function func1; another module mod2 defines a function func2, func2 calls func1.
In the mod1 module, export_symbol (func1 );
In the module mod2, extern int func1 ();
You can call func1 in mod2.

Refer:
Http://topic.csdn.net/u/20070910/09/ee2cff13-9179-41e3-9292-4fd73261f709.html
Http://www.dev-archive.com/msdn-archive/524/kernel-driver-5244619.shtm

Mod1.c
# Include <Linux/init. h>
# Include <Linux/module. h>
# Include <Linux/kernel. h>

Static int func1 (void)
{
Printk ("in FUNC: % s... \ n" ,__ func __);
Return 0;
}

Export_symbol (func1 );

Static int _ init hello_init (void)
{
Printk ("Module 1, Init! \ N ");
Return 0;
}

Static void _ exit hello_exit (void)
{
Printk ("Module 1, exit! \ N ");
}

Module_init (hello_init );
Module_exit (hello_exit );

######################################## #####################
Mod2.c
# Include <Linux/init. h>
# Include <Linux/kernel. h>
# Include <Linux/module. h>

Static int func2 (void)
{
Extern int func1 (void );
Func1 ();
Printk ("in FUNC: % s... \ n" ,__ func __);
Return 0;
}

Static int _ init hello_init (void)
{
Printk ("Module 2, Init! \ N ");
Func2 ();
Return 0;
}

Static void _ exit hello_exit (void)
{
Printk ("Module 2, exit! \ N ");
}

Module_init (hello_init );
Module_exit (hello_exit );

######################################## ########################
Makefile
Ifneq ($ (kernelrelease ),)
OBJ-M: = XXXX. o
Else
Kdir: =/lib/modules/$ (shell uname-R)/build
PWD: = $ (shell PWD)

Default:
$ (Make)-C $ (kdir) subdirs = $ (PWD) Modules

Clean:
Rm-RF module. symvers *. Ko *. O *. Mod. C. *. cmd. tmp_versions

Endif

######################################## ########################

# Insmod./mod1.ko
# Insmod./mod2.ko
# Rmmod mod2
# Rmmod mod1

Jan 11 11:59:17 wangyao-desktop kernel: [9886.801010] Module 2, exit!
Jan 11 11:59:21 wangyao-desktop kernel: [9891.450214] Module 1, exit!
Jan 11 12:05:29 wangyao-desktop kernel: [10258.385014] Module 1, Init!
Jan 11 12:05:38 wangyao-desktop kernel: [10267.465923] Module 2, Init!
Jan 11 12:05:38 wangyao-desktop kernel: [10267.465928] In FUNC: func1...
Jan 11 12:05:38 wangyao-desktop kernel: [10267.465930] In FUNC: func2...
Jan 11 12:05:50 wangyao-desktop kernel: [10280.091755] Module 2, exit!
Jan 11 12:05:57 wangyao-desktop kernel: [10287.332596] Module 1, exit!

It can be seen that the func2 function in mod2 successfully calls the func1 function in mod1.

Note:
When mod2 is compiled, a warning is displayed:
Root @ wangyao-desktop :~ /Modules/export_symbol/mod2 # Make
Make-C/lib/modules/2.6.22-14-generic/build subdirs =/root/modules/export_symbol/mod2 modules
Make [1]: Entering directory '/usr/src/linux-headers-2.6.22-14-generic'
Building modules, stage 2.
Modpost 1 modules
Warning: "func1" [/root/modules/export_symbol/mod2/mod2.ko] undefined!
Make [1]: Leaving directory '/usr/src/linux-headers-2.6.22-14-generic'

This is mainly because you haven't dealt with the kernel before compiling the connection. Of course, you cannot find the symbol. However, because you generated a kernel module, LD does not prompt an error, instead, it provides a warning, hoping that the kernel will be able to connect this symbol during insmod.

-----------------------------------------------------------

Http://www.lslnet.com/linux/f/docs1/i46/big5316526.htm

About export_symbol

In a file, you need to use the extern function in another file.

Why do we still need export_symbol?

Zookeeper


Re: Related to export_symbol

Export_symbol is used for the model generator.


Re: Related to export_symbol

I think it seems that as long as the function is not declared as static
It also exists in system. Map and is similar to export_symbol.

The modulo worker can also be used.


Re: Related to export_symbol

In system. map, it is the response address. After the token is completed, you do not know which token is in that address during the internal core operation. This file is used by the attacker. The content in the kernel is unknown.

The export_symbol operator stores these operators and corresponding addresses in the process of internal core operations, you can find the corresponding addresses of these operators.

In the process of adding a module, the module is subject to the inner core. If the inner core or other modulo operators are referenced in the module, the corresponding address must be export_symbol. Otherwise, it cannot be connected.


Re: Related to export_symbol

The value is 2.4. If the value is global in the value 2.4, the token is generated. In the value 2.6, you must use the export_symbol or its variant.


Re: Related to export_symbol

The internal kernel model is a special process made by the internal kernel itself,
Therefore, the single-statement extern statement is not allowed. The export_symbol statement must be used for the internal kernel line.

-------------------------------------------------------------

Http://hi.baidu.com/leal/blog/item/d3e1cafcb97c2dfdfd037fc2.html

System. Map [1] is a Linux kernel symbolic file that maintains the correspondence between kernel function names and non-stack variable names and their respective addresses.

If the kernel function or variable is to be called by the kernel module, the export_symbol macro must be used for processing, one of the functions is to connect this symbol to the _ ksymtab_xx_xx section of the binary file (see include/Linux/module. h. Use the '_ attribute _' keyword of the GCC compiler to implement [2]). When the kernel loads a module, it first checks whether the kernel functions called by this module have export (see _ find_symbol () kernel/module. C ).

For example, fc5 patches the vanilla kernel by default so that it does not use the export sys_open symbol. You can search the system. map file corresponding to the kernel to check whether the _ ksymtab_sys_open symbol exists.

[1] The system. Map File
Http://www.dirac.org/linux/system.map/

[2] using gnu c _ attribute __
Http://www.unixwiz.net/techtips/gnu-c-attributes.html

------------------------------------------------------------

Http://www.linuxsir.org/bbs/thread347677.html

The module is written. If the function declaration is not static, it should be global in my understanding.
Export_symbol ()

It seems that the system needs to generate a link similar to the c00021_printk class, because the system function itself actually has its prefix.

The module is dynamically loaded and requires a symbol table that exists during running to locate the symbol. The symbol table is generally used to locate the symbol address during static connections. The export_symbol macro is used to place the symbols and addresses in the static symbol table in the runtime symbol table (in a section) for searching for symbols during runtime.
Let's take a look at the definition of export_symbol.

-------------------------------------------------------------

Http://www.unixresources.net/linux/clf/linuxK/archive/00/00/71/60/716080.html

This function is provided in Driver/Char/console. C:

/* Console_sem is held (kernel t via vc_init ())*/

Void reset_terminal (INT currcons, int do_clear)

{

Top = 0;

Bottom = video_num_lines;

......

At the end of console. C, there are:

/*
* Visible symbols for Modules
*/

Export_symbol (fg_console );
Export_symbol (lele_blk_hook );
Export_symbol (hide_cursor );
Export_symbol (reset_terminal); // This

Drivers/Char/makefile also contains:

# All of the (potential) objects that export symbols.

# This list comes from 'grep-l export_symbol *. [HC] '.

Export-objs: = busmouse. O console. O keyboard. O sysrq. o

Misc. O Pty. O random. O selection. O serial. o

Sonypi. O tty_io.o tty_ioctl.o generic_serial.o

Au1000_gpio.o hp_psaux.o NVRAM. O scx200.o

I want to call in kernel/power/UI. C:

Void pm_restore_console (void)

{

If (test_action_state (suspend_no_output ))

Return;

// Reset_terminal (suspend_console, 1 );

Reset_terminal (tty_major, 1 );

However, when compiling the kernel, it is strange to say undefined reset_terminal.

/Usr/bin/MIPS-Linux-LD-G 0-static-t arch/MIPS/lD. Script ARCH/MIPS/kernel/head. o
ARCH/MIPS/kernel/init_task.o init/Main. O init/version. O init/do_mounts.o -- start-group
ARCH/MIPS/kernel. o arch/MIPS/MM/mm. O kernel/kernel. O mm/mm. O fs/fs. o ipc/IPC. o
ARCH/MIPS/Math-emu/fpu_emulator.o ARCH/MIPS/emma2_se/emma2.o Drivers/Char. o
Drivers/block. O Drivers/MISC/Misc. O drivers/NET/net. O Drivers/IDE/idedriver. o
Drivers/PCI/driver. O Drivers/MTD/mtdlink. O drivers/NET/wireless/wireless_net.o
Drivers/USB/usbdrv. O Drivers/Media/media. O Drivers/MD/mddev. o net/network. o
ARCH/MIPS/lib. A/home/work/data3/standby/dv_kernel_suspend2/Linux/lib.
-- End-group-O vmlinux
Kernel/kernel. O: In function 'pm _ restore_console ':
Kernel/kernel. O (. Text + 0x16030): Undefined reference to 'reset _ Terminal'
Kernel/kernel. O (. Text + 0x16030): Relocation truncated to fit: r_mips_26 reset_terminal
Make [1]: *** [kallsyms] Error 1
Make [1]: Leaving directory '/home/work/data3/standby/dv_kernel_suspend2/Linux'
Make: *** [vmlinux] Error 2

Let's make a diagnosis. -I added dexport_symtab. Or not.

Export_symbol ()
The export symbol is used to connect to the loaded module. It is irrelevant to the export when compiling the kernel itself. check whether the corresponding header file is included, console. is C compiled into a module or a kernel? If you do not compile to the kernel, you only need
Contains the header file defining reset_terminal, which can be compiled.

That's right.
Found the cause, console. O compilation is wrapped by our own macros (we changed something), and the result is console. O is not compiled, and reset_terminal () cannot be found.
Therefore, there are:
Export-objs: = busmouse. O console. O keyboard. O sysrq. o
Misc. O Pty. O random. O selection. O serial. o
Sonypi. O tty_io.o tty_ioctl.o generic_serial.o
Au1000_gpio.o hp_psaux.o NVRAM. O scx200.o
No,
There is also an obj-y = + console. o

I will add:
This was the case when I was doing 2.4 in the past,
I remember that for 2.4, the default non-static functions and variables will be automatically imported to the kernel space without the use of export_symbol () for marking.

2.6 must be exported using export_symbol () (because 2.6 does not have any symbols by default ).

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/macrossdzh/archive/2009/09/28/4601648.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.