在所有可加载的内核模块中,当给出make时,除了modulename.ko之外,它还会生成一个名为modulename.mod.c的文件.
以下代码摘录来自.mod.c文件,其中包含{number,function}对.
这个号码有什么意义?这个数字是如何由编译器生成的?
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" },...
...
}
CRC Symbol
{ 0xa6d8dcb5,... ...
是一个符号列表及其相应的CRC.这有两个主要用途:
>所有导出符号的全局列表.
>加载ko模块时的模块版本检查.
模块版本控制背后的基本原理
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.
编译成功后,所有导出符号的所有CRC的全局列表都存储在Linux内核源目录的Module.symvers文件中.本质上,此检查确保从内核模块调用的任何导出符号都存在于模块所期望的相同位置(内核中的偏移量).
modpost工具在编译Linux内核期间生成CRC.它由modpost script调用.整个过程在Documentation/kbuild/modules.txt:438详细解释.
entire source code of the modpost tool在Linux内核源代码中可用. add_depends()
是负责在每个* .mod.c文件中生成整个__versions部分的相关函数.