7

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" },
...
...
}

1 Answer 1

7

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.