Search

Exporting symbols from module

Linux kernel allows modules stacking, which basically means one module can use the symbols defined in other modules. But this is possible only when the symbols are exported.

In this post let us see how can a module export a symbol and how another module can make use of it.

A symbol can be exported by a module using the macro EXPORT_SYMBOL().

Let us make use of the very basic hello world module. In this module we have added function called "hello_export" and used the EXPORT_SYMBOL macro to export this function.

hello_export.c



Makefile required to compile this module is

Makefile



Compile it using the "make" command and then insert it into the kernel using insmod



All the symbols that the kernel is aware of is listed in /proc/kallsyms. Let us search for our symbol in this file.

From the output we can see that the symbol we exported is listed in the symbols recognized by the kernel. The first column gives the address of the column and the last column the module to which the symbol belongs to.

If you look into the Module.symvers file in directory where the module was compiled you will see a line similar to the following



Now let us write another module which will make use of the exported symbol. To make use of the symbol we just have to inform the module that the symbols being used is defined externally by using the "extern" keyword. Thus to use the hello_export function we will have to use



Once this is done we can use the function any where in the module, as shown in the module below.

hello_use_export.c



Use the same Makefile as shown above only change the module name to hello_use_export.o

Compile and load as done before.



If the function has got called successfully we should see the message "Hello from another module" in the kernel logs.



Thus we could call the function defined in another module successfully once it was exported by the module.

The dependecy of hello_use_export on hello_export can be found using the lsmod command



We know the modprobe command can load the module that is passed to it as well as the dependent modules, hence we can also see the working of modprobe using these modules.

Copy the two .ko files, hello_export.ko and hello_use_export.ko to /lib/modules/`uname -r`/



To use modprobe we have to update the dependencies in the modules.dep file in /lib/modules/`uname -r`/modules.dep . This can be done using the

command depmod.



The output "hello_use_export.ko: hello_export.ko" signifies that hello_use_export is dependent on hello_export.

Now remove the two modules we inserted before



Note: You will not be able to remove the modules in the reverse order cause hello_use_export will be using hello_export. Hence unless hello_use_export is not removed, hello_export can not be removed.

Now use the modprobe command to load hello_use_export, and we will see that hello_export gets loaded automatically.



We can see that even though we did not load the module "hello_export" it got loaded automatically because "hello_use_export" was dependent on it.

No comments:

Post a Comment