Take the 2-minute tour ×

So I believe the kernel module code can use whatever in the statically compiled kernel code as long as they are exposed. But if the statically compiled kernel code wants to use a global variable in the module code, is that possible?

For example, we have a global variable called "int a " in one kernel module code(whatever loadable kernel module). In the statically compiled kernel code(e.g., in the /linux/sched/fair.c), I want to access that variable.

This will cause the compiling error since the modules are compiled at last(after the statically compiled kernel code is compiled) and are not loaded at beginning.

What if I first declare this variable in a statically compiled header file? But before the module is loaded, that variable will be meaningless.


share|improve this question
Please add some code or make the question clearer. –  HonkyTonk Sep 19 '12 at 16:01
add comment

2 Answers

up vote 1 down vote accepted

Different solutions could be possible depending on what exactly you need. I assume you control at least the statically linked code and can change it if needed.

Way 1

If the statically linked code could export a function (something like set_my_good_var_ptr()), the dynamically loaded module could call that function to pass the address of the needed variable to the former.

Or, perhaps, the statically linked code could provide an interface that the dynamically loaded module could use to provide the get/set callbacks thus allowing to access the variable.

If all this is not suitable for your project (e.g. if you cannot change the code of the dynamically loaded module), the following might help, although I would not call it a good practice.

Way 2

Watch for the kernel module providing that variable to load (see register_module_notifier() function, for example).

Note that the notification function will be called after the module has loaded but before its initialization function is called.

When the notification function is called, you could use kallsyms_lookup_name() or kallsyms_on_each_symbol() to get the address of the variable you need.

This requires CONFIG_KALLSYMS and CONFIG_KALLSYMS_ALL to be set in the kernel configuration. If one or both these options are not set, it is still doable but somewhat more difficult (e.g. find the symbol in the binary file of the module, get the address of the ELF section the symbol belongs to and the offset in it and pass all this to your code, etc.)

After you have found the address of the variable, the statically linked code will have to determine somehow when the variable can actually be used (when it is initialized, etc.). How to do that depends on what the modules involved actually do, I can give no advice for that.

share|improve this answer
add comment

I think the kernel and any module are able to use find_symbol (defined in kernel/module.c) to discover the address of any other symbol in the kernel or any loaded module, statically compiled or not.

share|improve this answer
add comment

Your Answer


