/proc/kallsyms
サンプル
#include <linux/module.h> #include <linux/kernel.h> static int val1 = 1; static int val2; static int val3; const static int val4 = 1; int __weak val5; int __weak func1(void) { return 1; } static int func2(void) { return 1; } int func3(void) { return 1; } static int __init babakaka_init( void ) { printk("val1:%x\n", &val1); printk("val2:%x\n", &val2); printk("val3:%x\n", &val3); printk("val4:%x\n", &val4); printk("val5:%x\n", &val5); printk("func1:%x\n", &func1); printk("func2:%x\n", &func2); printk("func3:%x\n", &func3); return 0; } static void __exit babakaka_exit( void ) { } EXPORT_SYMBOL(val3); EXPORT_SYMBOL(func2); module_init(babakaka_init); module_exit(babakaka_exit);検証
[root@localhost lkm]# insmod babakaka.ko [root@localhost lkm]# dmesg : [ 1747.914420] val1:e0b6d000 [ 1747.914433] val2:e0b6d1a0 [ 1747.914440] val3:e0b6d1a4 [ 1747.914446] val4:e0b6c080 [ 1747.914452] val5:e0b6d19c [ 1747.914458] func1:e0b6b000 [ 1747.914465] func2:e0b6b010 [ 1747.914471] func3:e0b6b020 [root@localhost lkm]# cat /proc/kallsyms | grep babakaka e0b6b010 T func2 [babakaka] <-EXPORT_SYMBOされたコード e0b6d000 d val1 [babakaka] <-初期化データ e0b6d1a0 b val2 [babakaka] <-未初期データ e0b6d1a4 B val3 [babakaka] <-EXPORT_SYMBOLされた未初期データ e0b6c080 r val4 [babakaka] <-読み込み専用データ e0b6b030 t babakaka_exit [babakaka] e0b6c024 r __ksymtab_func2 [babakaka] e0b6c084 r __kstrtab_func2 [babakaka] e0b6c02c r __ksymtab_val3 [babakaka] e0b6c08a r __kstrtab_val3 [babakaka] e0b6d020 d __this_module [babakaka] e0b6b030 t cleanup_module [babakaka] e0b6b020 t func3 [babakaka] <-コード e0b6d19c v val5 [babakaka] <-weakデータ e0b6b000 w func1 [babakaka] <-weakコード実装
#define __weak __attribute__((weak)) #define EXPORT_SYMBOL(sym) \ __EXPORT_SYMBOL(sym, "") #define __EXPORT_SYMBOL(sym, sec) \ extern typeof(sym) sym; \ __CRC_SYMBOL(sym, sec) \ static const char __kstrtab_##sym[] \ __attribute__((section("__ksymtab_strings"), aligned(1))) \ = MODULE_SYMBOL_PREFIX #sym; \ static const struct kernel_symbol __ksymtab_##sym \ __used \ __attribute__((section("___ksymtab" sec "+" #sym), unused)) \ = { (unsigned long)&sym, __kstrtab_##sym }LKMでEXPORT_SYMBOLされているオブジェクトのタイプ属性は大文字となります。カーネル本体でのEXPORTされているオブジェクトには影響しません。
static const struct seq_operations kallsyms_op = { .start = s_start, .next = s_next, .stop = s_stop, .show = s_show }; static int s_show(struct seq_file *m, void *p) { struct kallsym_iter *iter = m->private; if (!iter->name[0]) return 0; if (iter->module_name[0]) { char type; type = iter->exported ? toupper(iter->type) : tolower(iter->type); seq_printf(m, "%pK %c %s\t[%s]\n", (void *)iter->value, type, iter->name, iter->module_name); } else seq_printf(m, "%pK %c %s\n", (void *)iter->value, iter->type, iter->name); return 0; }