Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How the Linux kernel accesses the functions and variables of another module

2025-04-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

Today, I will talk to you about how the Linux kernel accesses the functions and variables of another module, which may not be well understood by many people. In order to make you understand better, the editor has summarized the following for you. I hope you can get something from this article.

I. collation of questions

There are two modules in the kernel, one is A, and the other is B Magi A, which contains operation functions. Module B calls the functions of A module.

2. Analysis

This is a problem often encountered by driver engineers, which is actually a problem of module symbol export. It is relatively easy to implement this function with the help of EXPORT_SYMBOL ().

1. What is a symbol?

The symbols here mainly refer to global variables and functions, and static global variables can actually be accessed by another module.

two。 Why export symbols?

The ↓ Linux kernel manages kernel code in a modular manner. Each module in the kernel is independent of each other, that is to say, the global variables and functions of module A can not be accessed directly by module B.

Sometimes, when we write some module code, we find that some of the functions have been implemented by others, and we think that if only we could call the function interface that they have already implemented. So how can we do that?

It is exported by symbols, which means that you can export the function interfaces and global variables you implement for use by other modules.

In the world of the Linux kernel, if a module has been statically compiled into the kernel, its exported symbols will appear in the global kernel symbol table.

On Ubuntu 14.04 systems, the global symbol table of the Linux kernel is stored in the following files:

/ usr/src/linux-headers-3.2.0-29-generic-pae/Module.symvers

If you open this file, you can find that the contents are:

Addr- > symbol name-> Module name-> Macro of the exported symbol

3. How do I export symbols?

The Linux kernel provides us with two macros:

EXPORT_SYMBOL (name); EXPORT_SYMBOL_GPL (name)

Any of the above macro definitions makes the given symbol available outside the module; the GPL version of the macro definition only makes the symbol available to GPL-licensed modules; and the symbol must be output in the global part of the module file, outside of any function, because the macro definition extends to a declaration of a special-purpose variable that is expected to be global access.

4. When a module is compiled, how to find the symbols used?

a. In the symbol table in this module, look for symbols (function or variable implementation)

b. Look in the kernel global symbol table

c. Look in the Module.symvers file in the module directory

5. Case demonstration

Module An exports the global variable global_var and the function show symbols for module B.

A module # include # include static int global_var= 100; static void show (void) {printk ("show (): global_var=%d\ n", global_var);} static int hello_init (void) {printk ("module b: global_var=%d\ n", global_var); return 0;} static void hello_exit (void) {printk ("hello_exit\ n"); return;} EXPORT_SYMBOL (global_var); EXPORT_SYMBOL (show) MODULE_AUTHOR ("yikoulinux"); MODULE_LICENSE ("GPL"); module_init (hello_init); module_exit (hello_exit); B module # include # include extern int global_var; extern void show (void); static int hello_init (void) {printk ("module a: global_var=% d\ n", global_var); show (); return 0;} static void hello_exit (void) {printk ("hello_exit\ n"); return } MODULE_AUTHOR ("yikoulinux"); MODULE_LICENSE ("GPL"); module_init (hello_init); module_exit (hello_exit)

Debugging steps:

1. Compile module A, and then load module A. after module An is compiled, you will see a Module.symvers file in its current directory, where the symbols exported by module An are stored.

two。 Copy the Module.symvers file compiled by module A to the module B directory, then compile module B and load module B.

3. View the printed information of the module through dmesg. The print message is as follows:

From the results, we can see that we have accessed the global variable global_var and function show of module An in module B.

After reading the above, do you have any further understanding of how the Linux kernel accesses the functions and variables of another module? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 206

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report