日期:2014-05-16  浏览次数:20666 次

关于编写内核模块导出符号的问题
各位大大:

我在编写内核模块的时候遇到一个疑惑:是不是在模块中用EXPORT_SYMBOL导出的符号都会出现在/proc/kallsyms中呢?

我编写了下面两个内核模块

C/C++ code

// hello.c
#include <linux/init.h>
#include <linux/module.h>

MODULE_LICENSE("Dual BSD/GPL");

static int hello_init(void)
{
    printk(KERN_ALERT "Hello, world\n");
    return 0;
}

static void hello_exit(void)
{
    printk(KERN_ALERT "Goodbye, cruel world\n");
}

int say_hello(void)
{
    printk(KERN_ALERT "Want me say hello again?\n");
    return 0;
}

EXPORT_SYMBOL(say_hello);

module_init(hello_init);
module_exit(hello_exit);



C/C++ code

// greeting.c
#include <linux/init.h>
#include <linux/module.h>

MODULE_LICENSE("Dual BSD/GPL");

extern int say_hello(void);

static int greeting_init(void)
{
    say_hello();
    return 0;
}

static void greeting_exit(void)
{
    printk("No more greetings.\n");
}

module_init(greeting_init);
module_exit(greeting_exit);



我这样写两个模块,理论上的话greeting模块依赖hello模块的say_hello函数,而且say_hello已经用EXPORT_SYMBOL导出了,但是我用 cat /proc/kallsyms | grep hello 只有:
C/C++ code
$ cat /proc/kallsyms | grep hello
f7cf1000 t hello_exit    [hello]
f7cf100c t hello_init    [hello]
f7cf1000 t cleanup_module    [hello]
f7cf100c t init_module    [hello]
fbc9f74e t br_hello_timer_expired    [bridge]
fbca0027 t show_hello_timer    [bridge]
fbca00fb t store_hello_time    [bridge]
fbca0264 t set_hello_time    [bridge]
fbca01ee t show_hello_time    [bridge]

里面没有say_hello这个函数

所以我就有开头的疑惑:是不是EXPORT_SYMBOL导出的函数都会在/proc/kallsyms里?【另外,greeting模块的确调用了say_hello函数并在/var/log/kern.log里有输出】



------解决方案--------------------
我把你的hello.c做测试,显示如下信息。
cat /proc/kallsyms | grep hello
ffffffffa05f8000 t hello_exit [hello]
ffffffffa05f8020 t hello_init [hello]
ffffffffa05f9030 r __ksymtab_say_hello [hello]
ffffffffa05f908f r __kstrtab_say_hello [hello]
ffffffffa05f9040 r __kcrctab_say_hello [hello]
ffffffffa05fa000 d __this_module [hello]
ffffffffa05f8000 t cleanup_module [hello]
ffffffffa05f8020 t init_module [hello]

然后把hello.c文件中EXPORT_SYMBOL(say_hello);注释掉,显示信息如下:
cat /proc/kallsyms | grep hello
ffffffffa0609000 t hello_exit [hello]
ffffffffa0609020 t hello_init [hello]
ffffffffa060b000 d __this_module [hello]
ffffffffa0609000 t cleanup_module [hello]
ffffffffa0609020 t init_module [hello]

所以,应该有export成功。