Linux Device Drivers - Part 8 : Kernel Symbol Tables

In this session we learn about
1. A symbol in Linux
2. How it is exported ?
3. Symbol and Symbol Table relationship
4. Idea behind exporting a Symbol

1. A Symbol in Linux
In Linux, symbols are nothing but variables and functions that are needed to implement modularised drivers. Note that each and every symbol has its address in the memory.

To make our understanding clear, lets draw a small diagram which shows a Linux Kernel Module with symbols ( Variables and functions ) in it.
So in short, symbol is nothing but a variable or function in LKM

How Symbols are Exported ?
Exporting Kernel Symbols is typically done with 
  • EXPORT_SYMBOL()
  • EXPORT_SYMBOL_GPL()
EXPORT_SYMBOL(), which exports a given symbol to all loadable modules 

EXPORT_SYMBOL_GPL(), which exports a given symbol to only those modules that have a GPL-compatible license. (The first variation is far more common).

GPL here is General Public License is widely used for free software license.

EXPORT_SYMBOL_GPL functionality is same as EXPORT_SYMBOL, but it marks the exported symbol as usable only in modules licensed through either the General public License or a compatible one.

So, whenever you write a driver, it should be GPL licensed. That will be a great facility in case your module wants to use any other module’s functions that are already exported using GPL. 

3.Symbol and Symbol Table relation
Now we know about a symbol and how it is exported in Linux Kernel module. Lets learn about Kernel Symbol Table (also called Symbol Table in short).

Kernel symbol table is nothing but a look-up table between symbol names and their addresses in memory.

When a module is loaded into Kernel memory using insmod or modprobe utility, any symbol exported by the module becomes part of the Kernel symbol table. Exported symbols will become public Kernel symbols.

While insmoding, the insmod utility resolves undefined symbols against the table of public Kernel symbols. 

Kernel Symbol tables hold all the information needed to find program symbols, assign value to them, and relocate them.

Primary task of symbol is to associate a string with a value. For ex, printk symbol represents the address of the printk function in virtual address space where the function machine code resides.

This Kernel symbol table is loaded into memory as part of the Kernel boot process.

We will be learning more about Kernel Symbol Tables in coming Sessions.

4. Idea behind Exporting a Symbol
Whole idea of exporting a symbol is to make other modules benefit using them.
New modules can use symbols exported by your module, and you can stack new modules on top of other modules.  This concept is called Module stacking.
Module stacking is implemented in the mainstream kernel sources as well: like each input USB device module stacks on the usbcore and input modules.
Module loading order can be important, particularly if they are stacked (dependent on the symbols defined in other modules)
This mechanism help reduce development time by simplifying each layer

Check out video on this topi in my youtube channel. Don't forget to subscribe.

Comments

Popular posts from this blog

Linux Device Drivers - Part 4 : Linux Kernel Moduels (LKM) and types of LKM's

Linux Device Drivers Part - 5 : Building and Compiling Kernel Moduels

Linux Device Drivers - Part 13 : More on Device Numbers