如果我移除Linux模块初始化函数的__init前缀,会发生什么?

huangapple go评论75阅读模式
英文:

What if I remove the __init prefix of the linux module init function

问题

我知道__init会将Linux模块的初始化函数放入一个特殊的ELF部分,可以在加载模块后被内核覆盖。但我的问题是,如果我移除这个__init标记,会发生什么?我尝试编写一个简单的示例,没有__init前缀,那么它只会一直保留模块初始化函数的代码,直到卸载吗?

我尝试在Google、StackOverflow和其他地方搜索,但没有找到相同的问题和答案。
期待答案。

英文:

I have known that the __init will put the linux module init function into a special ELF section and can be overwrite by kernel after loading the module. But my question is that what if I remove this __init mark and what will happen? I try to write a simple demo without the __init prefix. So it only will keep the module init function code all the time until it is unloaded?

I tried search on google, stackoverflow and others, without finding same question and answer.
I expecting the answer.

答案1

得分: 1

如果你不将它标记为__init,内核构建系统将不知道它是一个初始化函数。因此,它将不会被放置在.init.text中,而是与其他函数一起放置在.text中。

当模块被加载到内核中后,在调用注册为module_init()的函数之后,内核将删除.init.text部分,所以如果你的函数没有标记为__init,它将继续存在于内存中。

通过使用/proc/kallsyms进行快速测试可以确认这一点。

  • 使用标记为__initmodinit函数:

    / # insmod test.ko
    / # cat /proc/kallsyms | grep -F [test]
    ffffffffc0000000 t modexit	[test]
    ffffffffc0000000 t cleanup_module	[test]
    
  • 使用标记为__initmodinit函数:

    / # insmod test.ko
    / # cat /proc/kallsyms | grep -F [test]
    ffffffffc0000000 t modinit	[test]
    ffffffffc0000003 t modexit	[test]
    ffffffffc0000003 t cleanup_module	[test]
    ffffffffc0000000 t init_module	[test]
    
英文:

If you don't mark it as __init the kernel build system will simply not know that it is an init function. As a result of this, it will not be placed in .init.text, but in .text along with other functions.

When the module is loaded into the kernel, after calling the function registered as module_init(), the kernel will remove the .init.text section, so in case your function is not marked as __init it will keep existing in memory.

A quick test with /proc/kallsyms confirms this.

  • With a modinit function marked as __init:

    / # insmod test.ko
    / # cat /proc/kallsyms | grep -F [test]
    ffffffffc0000000 t modexit	[test]
    ffffffffc0000000 t cleanup_module	[test]
    
  • With a modinit function not marked as __init:

    / # insmod test.ko
    / # cat /proc/kallsyms | grep -F [test]
    ffffffffc0000000 t modinit	[test]
    ffffffffc0000003 t modexit	[test]
    ffffffffc0000003 t cleanup_module	[test]
    ffffffffc0000000 t init_module	[test]
    

huangapple
  • 本文由 发表于 2023年5月17日 18:11:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76270960.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定