How to get data segment of Linux kernel from LKM

2019-08-10 20:50发布

问题:

I'm writing a kernel module which involves the tasklist_lock, __bss_start.

These symbols are not exported. I'm pretty sure even if not exported, we can access the symbols from text sections using kernsym_lookup_name()

Reference How my custom module on linux 3.2.28 can make a call to print_cpu_info?

$ vim System.map
...
80017be0 T register_undef_hook
80017c28 T unregister_undef_hook
80017c70 T do_unexp_fiq
...
806eb000 D mmlist_lock
806eb040 D tasklist_lock
806eb080 d softirq_vec
....

T represents text symbol.
D and d represents data segment symbol.

I'm able to access register_undef_hook() and unregister_undef_hook() using kallsyms_lookup_name().

But not tasklist_lock.

Please share your knowledge to access tasklist_lock from kernel module(LKM).

回答1:

See this noble post

#include <linux/module.h>
#include <linux/kallsyms.h>
#include <linux/string.h>

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Access non-exported symbols");
MODULE_AUTHOR("Stephen Zhang");

static int __init lkm_init(void)
{
    char *sym_name = "__bss_start";
    unsigned long sym_addr = kallsyms_lookup_name(sym_name);
    char filename[256];

    strncpy(filename, (char *)sym_addr, 255);

    printk(KERN_INFO "[%s] %s (0x%lx): %s\n", __this_module.name, sym_name, sym_addr, filename);

    return 0;
}

static void __exit lkm_exit(void)
{
}

module_init(lkm_init);
module_exit(lkm_exit);