Difference between revisions of "Embedded-Linux-Device-Driver/C2/Exporting-Symbols-from-a-Module/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with " {| style="border-spacing:0;" | style="border:0.5pt solid #000000;padding:0.176cm;"| '''Visual cue''' | style="border:0.5pt solid #000000;padding:0.176cm;"| '''Narration '''...")
 
 
Line 53: Line 53:
  
 
Point to the''' files.'''
 
Point to the''' files.'''
| style="border:0.5pt solid #000000;padding:0.176cm;"| Go to the '''DeviceDriver directory''' in the '''desktop '''which we have created earlier.
+
| style="border:0.5pt solid #000000;padding:0.176cm;"| Go to the '''DeviceDriver directory''' in the '''Desktop '''which we have created earlier.
  
 
Here, I have created a '''directory '''named''' ExportImportModule.'''
 
Here, I have created a '''directory '''named''' ExportImportModule.'''
Line 65: Line 65:
  
 
Code files
 
Code files
| style="border:0.5pt solid #000000;padding:0.176cm;"| * The '''files''' used in this tutorial are available in the '''Code Files '''link on this tutorial page.
+
| style="border:0.5pt solid #000000;padding:0.176cm;"|  
 +
* The files used in this tutorial are available in the '''Code Files '''link on this tutorial page.
 
* Please download and extract them
 
* Please download and extract them
 
* Make a copy and then use them while practising
 
* Make a copy and then use them while practising
Line 75: Line 76:
 
| style="border:0.5pt solid #000000;padding:0.176cm;"| Open the '''terminal''' by pressing '''ALT+Ctrl+T '''keys simultaneously.
 
| style="border:0.5pt solid #000000;padding:0.176cm;"| Open the '''terminal''' by pressing '''ALT+Ctrl+T '''keys simultaneously.
  
Press the '''Enter''' key after every command.
+
Press the '''Enter''' key after every '''command'''.
  
 
Go to the directory''' ExportImportModule '''as shown here.
 
Go to the directory''' ExportImportModule '''as shown here.
Line 85: Line 86:
 
Type '''gedit space export dot c'''.
 
Type '''gedit space export dot c'''.
  
Let us understand the '''code''' now.
+
Let us understand the code now.
 
|-
 
|-
 
| style="border:0.5pt solid #000000;padding:0.176cm;"| Point to '''header files'''
 
| style="border:0.5pt solid #000000;padding:0.176cm;"| Point to '''header files'''
Line 102: Line 103:
  
 
Highlight '''module_function() '''and '''num'''
 
Highlight '''module_function() '''and '''num'''
| style="border:0.5pt solid #000000;padding:0.176cm;"| Let us open the '''export.h''' file to see the '''source code.'''
+
| style="border:0.5pt solid #000000;padding:0.176cm;"| 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.'''
+
Here, we have declared an exported''' function module_function '''and a '''variable num.'''
  
 
|-
 
|-
Line 114: Line 115:
  
 
Highlight''' name'''
 
Highlight''' name'''
| style="border:0.5pt solid #000000;padding:0.176cm;"| Let us switch back to '''export.c module'''
+
| style="border:0.5pt solid #000000;padding:0.176cm;"| Let us switch back to '''export.c module'''.
  
 
'''EXPORT_SYMBOL '''and''' EXPORT_SYMBOL_GPL '''are '''macros.'''
 
'''EXPORT_SYMBOL '''and''' EXPORT_SYMBOL_GPL '''are '''macros.'''
Line 131: Line 132:
  
 
Highlight '''printk''' message.
 
Highlight '''printk''' message.
| style="border:0.5pt solid #000000;padding:0.176cm;"| The '''export.c''' module exports '''module_function''' using '''EXPORT_SYMBOL macro'''.
+
| style="border:0.5pt solid #000000;padding:0.176cm;"| The '''export.c module''' exports '''module_function''' using '''EXPORT_SYMBOL macro'''.
  
 
This '''function''' contains a '''printk''' message as shown here.
 
This '''function''' contains a '''printk''' message as shown here.
Line 139: Line 140:
  
 
Highlight '''int num = 100;'''
 
Highlight '''int num = 100;'''
| style="border:0.5pt solid #000000;padding:0.176cm;"| Here, the '''num''' variable is exported using '''EXPORT_SYMBOL''' '''macro'''.
+
| style="border:0.5pt solid #000000;padding:0.176cm;"| Here, the '''num variable''' is exported using '''EXPORT_SYMBOL macro'''.
  
The '''num''' '''variable''' is defined as shown here.
+
The '''num variable''' is defined as shown here.
  
 
|-
 
|-
Line 147: Line 148:
  
 
Highlight''' exit_func() function'''
 
Highlight''' exit_func() function'''
| style="border:0.5pt solid #000000;padding:0.176cm;"| We have defined''' '''the''' init function''' and '''exit function''' with different '''printk''' messages.  
+
| style="border:0.5pt solid #000000;padding:0.176cm;"| We have defined the''' init function''' and '''exit function''' with different '''printk''' messages.  
  
 
|-
 
|-
Line 153: Line 154:
  
 
Click on the '''close.'''
 
Click on the '''close.'''
| style="border:0.5pt solid #000000;padding:0.176cm;"| Now save the '''file '''and close the window.
+
| style="border:0.5pt solid #000000;padding:0.176cm;"| Now save the file and close the window.
  
 
Switch back to the''' terminal.'''
 
Switch back to the''' terminal.'''
Line 170: Line 171:
 
| style="border:0.5pt solid #000000;padding:0.176cm;"| We have to include these '''header files.'''
 
| style="border:0.5pt solid #000000;padding:0.176cm;"| We have to include these '''header files.'''
  
'''export dot h file''' contains the exported symbols declaration used by this''' module.'''
+
'''export dot h file''' contains the exported '''symbols declaration''' used by this''' module.'''
  
 
|-
 
|-
Line 182: Line 183:
 
| style="border:0.5pt solid #000000;padding:0.176cm;"| This '''module '''uses the '''function''' and '''variable''' exported by the '''export dot c.'''
 
| style="border:0.5pt solid #000000;padding:0.176cm;"| 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.
+
'''module_function() '''and''' variable num '''are used in this '''module''' as shown.
  
 
We have defined '''init function''' and '''exit function '''with different '''printk''' messages.
 
We have defined '''init function''' and '''exit function '''with different '''printk''' messages.
  
Now save the''' file '''
+
Now save the file.
  
 
|-
 
|-
Line 205: Line 206:
 
Type '''gedit space Makefile'''
 
Type '''gedit space Makefile'''
  
Type the '''code '''as shown.
+
Type the code as shown.
  
 
'''Makefile''' is used to '''compile '''the''' export '''and '''import modules.'''
 
'''Makefile''' is used to '''compile '''the''' export '''and '''import modules.'''
  
Save''' '''and close the '''file. '''Switch back to the''' terminal.'''
+
Save and close the file. Switch back to the''' terminal.'''
  
 
|-
 
|-
Line 218: Line 219:
 
Type >>''' make all'''
 
Type >>''' make all'''
  
| style="border:0.5pt solid #000000;padding:0.176cm;"| Type '''sudo''' '''space''' '''su '''to be a''' superuser'''  
+
| style="border:0.5pt solid #000000;padding:0.176cm;"| Type '''sudo space su '''to be a''' superuser'''  
  
Now''', '''type the system '''password'''.
+
Now, type the system '''password'''.
  
Let us compile the '''modules.'''
+
Let us '''compile''' the '''modules.'''
  
 
Type '''make space all'''
 
Type '''make space all'''
Line 229: Line 230:
 
| style="border:0.5pt solid #000000;padding:0.176cm;"| Type >> '''clear'''
 
| style="border:0.5pt solid #000000;padding:0.176cm;"| Type >> '''clear'''
  
| style="border:0.5pt solid #000000;padding:0.176cm;"| Clear the screen
+
| style="border:0.5pt solid #000000;padding:0.176cm;"| Clear the screen.
  
 
|-
 
|-
Line 235: Line 236:
  
 
Highlight the output
 
Highlight the output
| style="border:0.5pt solid #000000;padding:0.176cm;"| Let us check whether '''modules '''are compiled or not.
+
| style="border:0.5pt solid #000000;padding:0.176cm;"| Let us check whether '''modules '''are '''compiled''' or not.
  
Type '''ls '''
+
Type '''ls '''.
  
As the '''modules '''are compiled, we can see their '''dot ko files '''are created here.
+
As the '''modules '''are '''compiled''', we can see their '''dot ko files '''are created here.
  
 
|-
 
|-
 
| style="border:0.5pt solid #000000;padding:0.176cm;"| Type >> '''clear.'''
 
| style="border:0.5pt solid #000000;padding:0.176cm;"| Type >> '''clear.'''
  
| style="border:0.5pt solid #000000;padding:0.176cm;"| Clear the '''screen'''.
+
| style="border:0.5pt solid #000000;padding:0.176cm;"| Clear the screen.
 
|-
 
|-
 
| style="border:0.5pt solid #000000;padding:0.176cm;"|  
 
| style="border:0.5pt solid #000000;padding:0.176cm;"|  
| style="border:0.5pt solid #000000;padding:0.176cm;"| Next, let us copy the '''dot ko files '''of '''modules''' to the current '''kernel''' '''directory.'''
+
| style="border:0.5pt solid #000000;padding:0.176cm;"| Next, let us copy the '''dot ko files '''of '''modules''' to the current '''kernel directory.'''
  
 
|-
 
|-
Line 253: Line 254:
  
 
Press Enter
 
Press Enter
| style="border:0.5pt solid #000000;padding:0.176cm;"| Type the '''command '''as shown'''.'''
+
| style="border:0.5pt solid #000000;padding:0.176cm;"| Type the '''command '''as shown.
  
 
It makes the '''export dot ko module '''visible to the''' depmod tool.'''
 
It makes the '''export dot ko module '''visible to the''' depmod tool.'''
Line 265: Line 266:
  
 
Type >>''' clear'''
 
Type >>''' clear'''
| style="border:0.5pt solid #000000;padding:0.176cm;"| Similarly, copy the '''import dot ko file. '''
+
| style="border:0.5pt solid #000000;padding:0.176cm;"| Similarly, copy the '''import dot ko''' file.
  
Type the '''command '''as shown'''.'''
+
Type the '''command '''as shown.
  
 
Clear the screen.
 
Clear the screen.
Line 276: Line 277:
 
| style="border:0.5pt solid #000000;padding:0.176cm;"| Type''' depmod space hyphen a.'''
 
| style="border:0.5pt solid #000000;padding:0.176cm;"| Type''' depmod space hyphen a.'''
  
This command will''' '''make the '''kernel recognize''' the newly added '''module.'''
+
This '''command''' will make the '''kernel''' recognize the newly added '''module.'''
  
Here, the '''depmod tool '''generates a''' module dot dep file. '''
+
Here, the '''depmod tool '''generates a''' module dot dep''' file.
  
 
|-
 
|-
Line 284: Line 285:
  
 
Highlight '''modprobe.'''
 
Highlight '''modprobe.'''
| style="border:0.5pt solid #000000;padding:0.176cm;"| Let us use a modprobe''' tool''' to load the '''modules''' into the '''kernel.'''
+
| style="border:0.5pt solid #000000;padding:0.176cm;"| Let us use a '''modprobe tool''' to load the '''modules''' into the '''kernel.'''
  
 
Type''' modprobe space import '''
 
Type''' modprobe space import '''
Line 290: Line 291:
 
Here, the '''import module '''depends on '''export module.'''
 
Here, the '''import module '''depends on '''export module.'''
  
The''' modprobe''' tool also loads the''' export dot ko module.'''
+
The''' modprobe tool''' also loads the''' export dot ko module.'''
  
 
Clear the screen.
 
Clear the screen.
Line 306: Line 307:
 
| style="border:0.5pt solid #000000;padding:0.176cm;"| Type >> '''clear'''
 
| style="border:0.5pt solid #000000;padding:0.176cm;"| Type >> '''clear'''
  
| style="border:0.5pt solid #000000;padding:0.176cm;"| Clear the''' screen.'''
+
| style="border:0.5pt solid #000000;padding:0.176cm;"| Clear the screen.
 
|-
 
|-
 
| style="border:0.5pt solid #000000;padding:0.176cm;"| Type >>''' dmesg | tail -5'''
 
| style="border:0.5pt solid #000000;padding:0.176cm;"| Type >>''' dmesg | tail -5'''
Line 320: Line 321:
 
| style="border:0.5pt solid #000000;padding:0.176cm;"| Type >> '''clear'''
 
| style="border:0.5pt solid #000000;padding:0.176cm;"| Type >> '''clear'''
  
| style="border:0.5pt solid #000000;padding:0.176cm;"| Clear the '''screen'''.
+
| style="border:0.5pt solid #000000;padding:0.176cm;"| Clear the screen.
 
|-
 
|-
 
| style="border:0.5pt solid #000000;padding:0.176cm;"| Type >> '''modprobe -r import'''
 
| style="border:0.5pt solid #000000;padding:0.176cm;"| Type >> '''modprobe -r import'''
Line 328: Line 329:
 
Type''' modprobe space hyphen r space import'''
 
Type''' modprobe space hyphen r space import'''
  
The''' modprobe''' automatically unloads the '''export dot ko '''module.
+
The''' modprobe''' automatically unloads the '''export dot ko module'''.
  
 
|-
 
|-
Line 344: Line 345:
 
|-
 
|-
 
| style="border:0.5pt solid #000000;padding:0.176cm;"| Type >> '''clear'''
 
| style="border:0.5pt solid #000000;padding:0.176cm;"| Type >> '''clear'''
| style="border:0.5pt solid #000000;padding:0.176cm;"| Clear the '''screen'''.
+
| style="border:0.5pt solid #000000;padding:0.176cm;"| Clear the screen.
  
 
|-
 
|-
Line 361: Line 362:
 
| style="border:0.5pt solid #000000;padding:0.176cm;"| Type >> '''make clean'''
 
| style="border:0.5pt solid #000000;padding:0.176cm;"| Type >> '''make clean'''
  
| style="border:0.5pt solid #000000;padding:0.176cm;"| Type '''make space clean '''command to remove all '''files '''created by '''compilation.'''
+
| style="border:0.5pt solid #000000;padding:0.176cm;"| Type '''make space clean command''' to remove all files created by '''compilation.'''
  
 
|-
 
|-
 
| style="border:0.5pt solid #000000;padding:0.176cm;"| Type >> '''exit'''
 
| style="border:0.5pt solid #000000;padding:0.176cm;"| Type >> '''exit'''
| style="border:0.5pt solid #000000;padding:0.176cm;"| To go back to''' '''the''' regular user, '''type '''exit.'''
+
| style="border:0.5pt solid #000000;padding:0.176cm;"| To go back to the''' regular user, '''type '''exit.'''
  
 
|-
 
|-
 
| style="border:0.5pt solid #000000;padding:0.176cm;"|  
 
| style="border:0.5pt solid #000000;padding:0.176cm;"|  
| style="border:0.5pt solid #000000;padding:0.176cm;"| With this, we come to the end of this tutorial. Let us summarize.
+
| style="border:0.5pt solid #000000;padding:0.176cm;"| With this, we come to the end of this tutorial.  
 +
 
 +
Let us summarize.
  
 
|-
 
|-
Line 389: Line 392:
 
# Change the '''printk message''' of '''module_function.'''
 
# Change the '''printk message''' of '''module_function.'''
 
# Compile the '''export '''and''' import modules.'''
 
# Compile the '''export '''and''' import modules.'''
# Load the modules using the modprobe''' tool.'''
+
# Load the '''modules''' using the '''modprobe tool.'''
 
# See the output using '''dmesg command.'''
 
# See the output using '''dmesg command.'''
  
Line 396: Line 399:
  
 
About Spoken Tutorial Project:
 
About Spoken Tutorial Project:
| style="border:0.5pt solid #000000;padding:0.176cm;"| * The video at the following link summarizes the Spoken Tutorial project.
+
| style="border:0.5pt solid #000000;padding:0.176cm;"|  
 +
* The video at the following link summarizes the Spoken Tutorial project.
  
 
* Please download and watch it.
 
* Please download and watch it.
Line 403: Line 407:
  
 
Spoken Tutorial workshops :
 
Spoken Tutorial workshops :
| style="border:0.5pt solid #000000;padding:0.176cm;"| The''' Spoken Tutorial Project''' Team:
+
| style="border:0.5pt solid #000000;padding:0.176cm;"| The''' Spoken Tutorial Project''' Team conducts workshops and gives certificates.
 
+
* conducts workshops and
+
* gives certificates.
+
  
 
For more details, please write to us.
 
For more details, please write to us.
Line 420: Line 421:
 
Forum for specific questions :
 
Forum for specific questions :
  
| style="border:0.5pt solid #000000;padding:0.176cm;"| * Do you have any general or technical questions on Embedded Linux Device Driver?
+
| style="border:0.5pt solid #000000;padding:0.176cm;"|  
 +
* Do you have any general or technical questions on Embedded Linux Device Driver?
 
* Please visit the FOSSEE forum and post your question.
 
* Please visit the FOSSEE forum and post your question.
 
|-
 
|-

Latest revision as of 00:12, 21 July 2020

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