内核符号表及模块层叠技术

内核符号表及模块层叠技术https://mp.weixin.qq.com/s?__biz=Mzg3NDkwMjc2NA==&mid=2247483794&idx=1&sn=100fd8f7fa7ab7415da9124d82929a70&chksm=cec8e411f9bf6d07f049f9fa69d318bfc8449dc42f7028c913a83fad7a4eb3dc624d02e449e4#rd

 本文将简单介绍驱动模块之间如何互动,调用其他模块的接口资源 ......

内核符号表

insmod会使用公共内核符号表来解析模块中未定义的符号。

当模块被装入内核后,它所导出的任何符号也会变成内核符号表的一部分。

公共内核符号表中包含了所有的全局内核项(即函数和变量)的地址,这是实现模块化驱动程序所必须的。

模块层叠技术的应用

在hello.ko模块中引用hello_print_comm.ko模块所导出的符号。

hello_print_comm.ko的源代码

#include <linux/init.h>
#include <linux/module.h>
#include <linux/sched.h>

static int hello_print_comm_init(void)
{
    printk(KERN_INFO "This process is \"%s\" (pid %i) \n", current->comm, current->pid);
    return 0;
}

static void hello_print_comm_exit(void)
{
    printk(KERN_INFO "This process is \"%s\" (pid %i) \n", current->comm, current->pid);
    return;
}

//导出符号到内核符号表
EXPORT_SYMBOL_GPL(hello_print_comm_init);
EXPORT_SYMBOL_GPL(hello_print_comm_exit);

module_init(hello_print_comm_init);
module_exit(hello_print_comm_exit);

MODULE_LICENSE("GPL");

 hello.ko的源代码

#include <linux/init.h>
#include <linux/module.h>
#include <linux/sched.h>

extern int hello_print_comm_init(void);
extern void hello_print_comm_exit(void);

static int __init hello_init(void)
{
    printk(KERN_INFO "Hello!\n");
    printk(KERN_INFO "Nice to meet you!\n");
    hello_print_comm_init();
    return 0;
}

static void __exit hello_exit(void)
{
    hello_print_comm_exit();
    printk(KERN_INFO "Goodbye!\n");
    return;
}

module_init(hello_init);
module_exit(hello_exit);

MODULE_LICENSE("GPL");

加载模块

[root@xxx]: /#:cd ./lib/modules/4.1.15/
[root@xxx]: /lib/modules/4.1.15#:ls
hello.ko             hello_print_comm.ko
[root@xxx]: /lib/modules/4.1.15#:
[root@xxx]: /lib/modules/4.1.15#:depmod 
[root@xxx]: /lib/modules/4.1.15#:ls
hello.ko             modules.alias        modules.symbols
hello_print_comm.ko  modules.dep
[root@xxx]: /lib/modules/4.1.15#:
[root@xxx]: /lib/modules/4.1.15#:insmod hello.ko 
[   63.687893] hello: Unknown symbol hello_print_comm_exit (err 0)
[   63.693856] hello: Unknown symbol hello_print_comm_init (err 0)
[   63.724682] hello: Unknown symbol hello_print_comm_exit (err 0)
[   63.730634] hello: Unknown symbol hello_print_comm_init (err 0)
insmod: can't insert 'hello.ko': unknown symbol in module, or unknown parameter

[root@xxx]: /lib/modules/4.1.15#:
[root@xxx]: /lib/modules/4.1.15#:modprobe -a hello.ko 
[   89.259356] This process is "modprobe" (pid 223) 
[   89.265090] Hello!
[   89.267905] Nice to meet you!
[   89.270891] This process is "modprobe" (pid 223) 
[root@xxx]: /lib/modules/4.1.15#:
[root@xxx]: /lib/modules/4.1.15#:modprobe -r hello.ko 
[  128.857764] This process is "modprobe" (pid 225) 
[  128.862497] Goodbye!
[  128.857999] This process is "modprobe" (pid 225)

结束语

希望本文可以帮助到大家!

如需获取文中的测试程序源码,可通过后台回复获取!水平有限,如有错漏的地方请不吝指出,谢谢!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值