how does compiler/linker resolves kernel API like 'printk' called from a module on linux

前端 未结 1 1363
遥遥无期
遥遥无期 2021-01-14 05:04

I have written a sample hello.ko kernel module:

#include       /* Needed by all modules */
#include       /* Need         


        
相关标签:
1条回答
  • 2021-01-14 05:49

    The Linux kernel's module loader basically contains a special-purpose runtime linker. The .ko file is really an object file like any other, and as such it comes with a symbol table. If you run nm on it (nm <path/to/some_module.ko>), you'll see a lot of symbols marked "U", that is, "undefined". This includes symbols for core kernel functions that are used by the module, such as printk, __kmalloc, kfree, etc. but in many cases also symbols implemented by other modules.

    When the module is loaded, the kernel runs through the module's undefined symbols and looks them up in the (run-time) symbol table, patching the relevant memory locations, much like any other linker. If any undefined symbols aren't already in the symbol table, loading will fail (easy to demo by using insmod instead of modprobe as it won't load dependencies). It also adds any symbols exported by the module to the symbol table for other modules to use, tracking dependencies so you can't yank out a module used by another. Ilya Matveychikov has linked to the relevant code in the module loader in the comments, which will help if you want to know all the gory details.

    0 讨论(0)
提交回复
热议问题