zoukankan      html  css  js  c++  java
  • 内核驱动——符号表的导出

    1.EXPORT_SYMBOL的使用

    这个函数可以实现模块间的函数接口以及全局变量。

    一、在函数定义后使用EXPORT_SYMBOL(函数名)

    二、在调用该函数的模块中extern这个函数

    三、首先加载定义这个函数的模块,再加载调用这个函数的模块

    2.例子

    hello1.c

    #include <linux/init.h>
    #include <linux/module.h>
    
    MODULE_LICENSE("GPL");
    
    static int num = 10;
    static void show(void)
    {
        printk(KERN_ALERT "show(),num=%d
    ", num);
    }
    
    static int hello_init(void)
    {
        printk(KERN_ALERT "hello
    ");
        return 0;
    }
    static void hello_exit(void)
    {
        printk(KERN_ALERT "Goodbye
    ");
    }
    
    EXPORT_SYMBOL(show);
    
    module_init(hello_init);
    module_exit(hello_exit);

    show.c

    #include <linux/module.h>
    
    MODULE_LICENSE("GPL");
    
    extern void show(void);
    //在这个模块中调用hello模块中的show函数,要在hello中使用export_symbol
    //1.要先编译被调用的模块,将Module.symvers拷贝到b模块下
    //2.编译b模块
    //3.加载a模块,再加载b模块
    //4.卸载b模块,再卸载a模块
    static int show_init(void)
    {
        printk(KERN_ALERT "show_init
    ");
        show();
        return 0;
    }
    
    static void show_exit(void)
    {
        printk(KERN_ALERT "show_exit
    ");
    }
    
    module_init(show_init);
    module_exit(show_exit);

    3.现象

    # insmod hello1.ko 
    # insmod show.ko 
    # rmmod show
    # rmmod hello1
    # dmesg | tail -5
    [ 6039.628712] hello
    [ 6047.007841] show_init
    [ 6047.010223] show(),num=10
    [ 6051.315539] show_exit
    [ 6055.772762] Goodbye
  • 相关阅读:
    SQL中JOIN 的用法
    ava中普通代码块,构造代码块,静态代码块区别及示例
    javabean的内省技术和BeanUtils的使用
    Tomcat服务器学习和使用(一)
    增强For循环
    JAVA单态设计模式
    关于枚举的整理
    java中遍历MAP的几种方法
    equals和==的区别
    深入剖析Java中的装箱和拆箱
  • 原文地址:https://www.cnblogs.com/ZQQH/p/8556006.html
Copyright © 2011-2022 走看看