Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meaning of version info in .mod.c file in Linux kernel

In all loadable kernel modules, when make is given it produces a file called modulename.mod.c in addition to modulename.ko.

Following code excerpt is from a .mod.c file, which contains a {number, function} pair. What is the significance of this number? How this number is generated by the compiler?

static const struct modversion_info ____versions[]
__used
__attribute__((section("__versions"))) = {

        { 0xa6d8dcb5, "module_layout" },
        { 0x16c2b958, "register_netdevice" },
        { 0x609f1c7e, "synchronize_net" },
        { 0x90a60c63, "kmem_cache_destroy" },
        { 0x402b8281, "__request_module" },
        { 0x844a8af7, "netdev_info" },
        { 0xdfdb0ee8, "kmalloc_caches" },
        { 0x12da5bb2, "__kmalloc" },
        { 0x92d42843, "cfg80211_cqm_rssi_notify" },
        { 0xc86289e8, "perf_tp_event" },
...
...
}
like image 796
user2552690 Avatar asked Jul 29 '13 10:07

user2552690


People also ask

What is the file mod C?

mod. c is a file that basically contains the information about the module which are required at the time of inserting, like version number and symbols which are not defined locally. If symbols are not defined in existing running kernel or version is not matched with the running kernel, the module insertion will fail.

What is Mod o file?

mod.o". The modulename. mod. c is a file that basically contains the information about the module (Version information etc). The modulename.o and the modulename.


1 Answers

The __versions section contained in all the individual *.mod.c files

  CRC         Symbol
{ 0xa6d8dcb5, "module_layout" },
{ 0x16c2b958, "register_netdevice" },
  ...         ...

is a list of symbols and their corresponding CRCs. This has 2 main uses :

  1. Global list of all exported symbols.
  2. Module versioning check when loading ko modules.

Rationale behind Module versioning

Module versioning is enabled by the CONFIG_MODVERSIONS tag, and is used as a simple ABI consistency check. A CRC value of the full prototype for an exported symbol is created. When a module is loaded/used, the CRC values contained in the kernel are compared with similar values in the module; if they are not equal, the kernel refuses to load the module as it indicates that the module is built with reference to a different version of the Linux kernel source.

Upon successful compilation, The global list of all the CRCs of all the exported symbols is stored in the file Module.symvers in the Linux kernel source directory. Essentially this check ensures that any exported symbol that is being called from a kernel module exists in the same location (offset in the kernel) as expected by the module.

The modpost tool generates the CRCs during compilation of the Linux Kernel. It is invoked by the modpost script. The entire process is explained in detail at the Documentation/kbuild/modules.rst:450.

The entire source code of the modpost tool is available in the Linux kernel source. add_depends() is the relevant function responsible for generating the entire __versions section in each of the *.mod.c files.

like image 91
TheCodeArtist Avatar answered Oct 10 '22 20:10

TheCodeArtist