依赖关系和引用:
如果模块B使用了模块A提供的函数,那么模块A和模块B之间就存在关系。为正确管理这些依赖关系,内核需要引入另一个数据结构:
- /* modules using other modules */
- struct module_use
- {
- struct list_head list;
- struct module *module_which_uses;
- };
依赖关系的网络通过module_use和module数据结构的modules_which_use_me成员共同建立起来。对每个使用了模块A中函数的模块B,都会创建一个module_use的新实例。该实例将添加到模块A的modules_which_use_me链表中。Module_which_uses指向模块B的module实例。根据这些信息,内核很容易计算出使用特定模块的其他内核模块。我们回到前面的两个hello kernel代码,hello.c中用了一个外部变量hello_data,这个变量来自hello_h.c中,为hello_h.c的全局静态导出变量。所以hello模块依赖hello_h模块。我们正常操作:先插入模块hello_h然后插入hello模块,先移除hello模块,在移除hello_h:

很容易想到上面的操作顺序是不能改变的。
上面的依赖关系可以画出如下图:

当然,有数据结构必然有操作这些数据结构的函数,数据结构放这里了,对于他的操作就不看了。无非是从hello找到hello_h。
模块的二进制结构:
我们使用readelf –S hello.ko > readelf.txt命令来看看hello.ko模块文件的二进制结构,输出如下:
Section Headers:
[Nr] Name Type Addr Off Size ES Flg Lk Inf Al
[ 0] NULL 00000000 000000 000000 00 0 0 0
[ 1] .note.gnu.build-i NOTE 00000000 000034 000024 00 A 0 0 4
[ 2] .text PROGBITS 00000000 000058 00005c 00 AX 0 0 4
[ 3] .rel.text REL 00000000 01106c 000070 08 29 2 4
[ 4] .rodata.str1.1 PROGBITS 00000000 0000b4 000051 01 AMS 0 0 1
[ 5] .modinfo PROGBITS 00000000 000108 0000c4 00 A 0 0 4
[ 6] __mcount_loc PROGBITS 00000000 0001cc 000008 00 A 0 0 4
[ 7] .rel__mcount_loc REL 00000000 0110dc 000010 08 29 6 4
[ 8] .data PROGBITS 00000000 0001d4 000000 00 WA 0 0 4
[ 9] .gnu.linkonce.thi PROGBITS 00000000 0001d4 000164 00 WA 0 0 4
[10] .rel.gnu.linkonce REL 00000000 0110ec 000010 08 29 9 4
[11] .bss NOBITS 00000000 000338 000000 00 WA 0 0 4
[12] .note.GNU-stack PROGBITS 00000000 000338 000000 00 X 0 0 1
[13] .comment PROGBITS 00000000 000338 00005a 00 0 0 1
[14] .debug_aranges PROGBITS 00000000 000392 000020 00 0 0 1
[15] .rel.debug_arange REL 00000000 0110fc 000010 08 29 14 4
[16] .debug_pubnames PROGBITS 00000000 0003b2 00006a 00 0 0 1
[17] .rel.debug_pubnam REL 00000000 01110c 000010 08 29 16 4
[18] .debug_info PROGBITS 00000000 00041c 0090fe 00 0 0 1
[19] .rel.debug_info REL 00000000 01111c 0043d0 08 29 18 4
[20] .debug_abbrev PROGBITS 00000000 00951a 000550 00 0 0 1
[21] .debug_line PROGBITS 00000000 009a6a 000858 00 0 0 1
[22] .rel.debug_line REL 00000000 0154ec 000008 08 29 21 4
[23] .debug_frame PROGBITS 00000000 00a2c4 000084 00 0 0 4
[24] .rel.debug_frame REL 00000000 0154f4 000020 08 29 23 4
[25] .debug_str PROGBITS 00000000 00a348 005507 01 MS 0 0 1
[26] .debug_pubtypes PROGBITS 00000000 00f84f 00121c 00 0 0 1
[27] .rel.debug_pubtyp REL 00000000 015514 000010 08 29 26 4
[28] .shstrtab STRTAB 00000000 010a6b 000128 00 0 0 1
[29] .symtab SYMTAB 00000000 015524 000240 10 30 30 4
[30] .strtab STRTAB 00000000 015764 0000e1 00 0 0 1
Key to Flags:
W (write), A (alloc), X (execute), M (merge), S (strings)
I (info), L (link order), G (group), x (unknown)
O (extra OS processing required) o (OS specific), p (processor specific)
模块使用ELF二进制文件结构,模块中包含了几个额外的段,普通的程序或库中不会出现。
__ksymtab段包含一个符号表,包括了模块导出的所有符号,该段中导出的符号可以由内核的所遇部分使用(不考虑许可证)。另外还有__ksymtab_gpl、__ksymtab_gpl_future和其命名意思相同。
上面的.modinfo存储了在加载当前模块之前,内核中必须线性加载的所有其他模块名称,换句话说,该特定模块依赖的所有模块名称。
.gun.linkonce.this_module提供了sruct module的一个实例,其中存储了模块的名称和指向二进制文件中的初始化函数和清理函数的指针。根据本段,内核即可判断特定的二进制文件是否为模块。如果没有该段,则拒绝装载文件。
还有其他的段在上面的hello模块中没有出现,暂时不做分析了。
