Embedded-Linux-Device-Driver/C2/Exporting-Symbols-from-a-Module/English

From Script | Spoken-Tutorial
Revision as of 16:14, 17 July 2020 by Nirmala Venkat (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Visual cue Narration
Slide 1:

Welcome slide:

Welcome to the spoken tutorial on Exporting Symbols from a Module.
Slide 2:

Learning objectives:

In this tutorial, we will learn how to,
  • Export the symbols from a module.
  • Import the symbols in a module.
Slide 3:

System Requirements:

To record this tutorial, I am using,
  • VirtualBox version 5.2.
  • Ubuntu Linux 18.04 LTS operating system.
  • Linux kernel version 5.0.0-31 generic.
  • gedit text editor.
Slide 4:

Prerequisites:

To follow this tutorial, you should be familiar with:
  • C programming language
  • Basics of Linux kernel

If not, then go through the C/C++ and Linux spoken tutorials on this website

Slide 5:

What is symbol?

What is symbol?
  • Symbol is either a variable or a function.
  • Symbol exported by a module become a part of the kernel symbol table.
Point to the directory and file in desktop

Point to the files.

Go to the DeviceDriver directory in the desktop which we have created earlier.

Here, I have created a directory named ExportImportModule.

In this directory, I have saved three module files for demonstration.

I have named them as export dot c, export dot h, import dot c.

Slide:

Code files

* The files used in this tutorial are available in the Code Files link on this tutorial page.
  • Please download and extract them
  • Make a copy and then use them while practising
Open the terminal

Type >> cd Desktop/DeviceDriver/ExportImportModule

Open the terminal by pressing ALT+Ctrl+T keys simultaneously.

Press the Enter key after every command.

Go to the directory ExportImportModule as shown here.

Type >> gedit export.c Let us open the export.c module that exports the symbols.

Type gedit space export dot c.

Let us understand the code now.

Point to header files

Open the terminal.

Type >> clear

We have to include these header files at the start of a program.

The export.h file contains the exported symbols declaration.

Open the export.h file

Type >> gedit export.h

Highlight module_function() and num

Let us open the export.h file to see the source code.

Here, we have declared an exported function module_function and a variable num.

Highlight EXPORT_SYMBOL(name),

EXPORT_SYMBOL_GPL(name)

Highlight EXPORT_SYMBOL_GPL(name)

Highlight name

Let us switch back to export.c module

EXPORT_SYMBOL and EXPORT_SYMBOL_GPL are macros.

These macros are used to export the symbols from a module to other modules.

This macro exports the symbols to GPL_licensed modules only.

The symbols must be exported in the global part of the module’s file.

It makes them visible to the kernel.

Highlight EXPORT_SYMBOL(module_function)

Highlight module_function()

Highlight printk message.

The export.c module exports module_function using EXPORT_SYMBOL macro.

This function contains a printk message as shown here.

Highlight EXPORT_SYMBOL(num)

Highlight int num = 100;

Here, the num variable is exported using EXPORT_SYMBOL macro.

The num variable is defined as shown here.

Highlight init_func() function

Highlight exit_func() function

We have defined the init function and exit function with different printk messages.
Press >> Ctrl + S

Click on the close.

Now save the file and close the window.

Switch back to the terminal.

Type >> gedit import.c

Press Enter

Let us open import dot c module.
Highlight header files

Highlight #include “export.h”

We have to include these header files.

export dot h file contains the exported symbols declaration used by this module.

Highlight module_function()

Highlight num

Highlight init_func()

Highlight exit_func()

This module uses the function and variable exported by the export dot c.

module_function() and variable num are used in this module as shown.

We have defined init function and exit function with different printk messages.

Now save the file

Open the terminal.

Type >> clear

Switch back to the terminal.
Type >> gedit Makefile

Press Ctrl + S

Click on the close.

Open the terminal

Let us now create a Makefile to compile the modules.

Type gedit space Makefile

Type the code as shown.

Makefile is used to compile the export and import modules.

Save and close the file. Switch back to the terminal.

Type >> sudo su

Type >> password

Type >> make all

Type sudo space su to be a superuser

Now, type the system password.

Let us compile the modules.

Type make space all

Type >> clear Clear the screen
Type >> ls

Highlight the output

Let us check whether modules are compiled or not.

Type ls

As the modules are compiled, we can see their dot ko files are created here.

Type >> clear. Clear the screen.
Next, let us copy the dot ko files of modules to the current kernel directory.
Type >> cp export.ko /lib/modules/$(uname -r)

Press Enter

Type the command as shown.

It makes the export dot ko module visible to the depmod tool.

Clear the screen.

Type >> cp import.ko /lib/modules/$(uname -r)

Press Enter

Type >> clear

Similarly, copy the import dot ko file.

Type the command as shown.

Clear the screen.

Type >> depmod -a Type depmod space hyphen a.

This command will make the kernel recognize the newly added module.

Here, the depmod tool generates a module dot dep file.

Type >> modprobe import

Highlight modprobe.

Let us use a modprobe tool to load the modules into the kernel.

Type modprobe space import

Here, the import module depends on export module.

The modprobe tool also loads the export dot ko module.

Clear the screen.

Type >> lsmod | head -5 Let’s check whether our modules are loaded into the kernel or not.

Type lsmod space pipe space head space hyphen 5.

We can see their names appear here as they are loaded into the kernel.

Type >> clear Clear the screen.
Type >> dmesg | tail -5

Highlight the output

Let us see the “loaded” printk messages of modules.

Type dmesg space pipe space tail hyphen 5

It shows the printk messages of export and import module’s init function.

Type >> clear Clear the screen.
Type >> modprobe -r import Let us unload the modules from the kernel.

Type modprobe space hyphen r space import

The modprobe automatically unloads the export dot ko module.

Type >> lsmod | head -5

Show the output

Let us check whether our modules are unloaded from the kernel or not.

Type lsmod space pipe space head space hyphen 5.

Here, both the module’s names are not in this list.

It indicates that they are unloaded from the kernel successfully.

Type >> clear Clear the screen.
Type >> dmesg | tail -3

Type >> clear

Let us see “unloaded” printk messages of modules.

Type dmesg space pipe space tail space hyphen 3.

Now, we can see the messages from the exit function of both modules.

Clear the screen.

Type >> make clean Type make space clean command to remove all files created by compilation.
Type >> exit To go back to the regular user, type exit.
With this, we come to the end of this tutorial. Let us summarize.
Slide 6:

Summary:

In this tutorial, we learnt how to
  • Export the symbols from a module.
  • Import the symbols in a module.
Slide 7:

Assignment:

As an assignment,
  1. Open the export.c file
  2. Change the printk message of module_function.
  3. Compile the export and import modules.
  4. Load the modules using the modprobe tool.
  5. See the output using dmesg command.
Slide 8:

About Spoken Tutorial Project:

* The video at the following link summarizes the Spoken Tutorial project.
  • Please download and watch it.
Slide 9:

Spoken Tutorial workshops :

The Spoken Tutorial Project Team:
  • conducts workshops and
  • gives certificates.

For more details, please write to us.

Slide 10:

Forum questions :

Please post your timed queries in this forum.
Slide 11:

Forum for specific questions :

* Do you have any general or technical questions on Embedded Linux Device Driver?
  • Please visit the FOSSEE forum and post your question.
Slide 12:

Acknowledgment:

The Spoken Tutorial Project is funded by MHRD, Government of India.
Slide 13:

Thank you slide:

This tutorial has been contributed by FOSSEE and Spoken Tutorial Project, IIT Bombay.

This is Mayuri Panchakshari signing off.

Thanks for watching.

Contributors and Content Editors

Nancyvarkey, Nirmala Venkat