I've been wandering around linux-kernel for quite a while now, reading questions almost daily and answering interesting ones. In this time, I've seen a fair amount of users asking help about calling functions that are not exported by the kernel from a kernel module.
Here are some examples:
- How can I reference non-exported Linux kernel functions from a kernel module?
- How my custom module on linux 3.2.28 can make a call to print_cpu_info?
- Proper way of getting address of sys_call_table in recent kernels that do not export kallsyms_lookup_name
- LSM-Howto: Kernelmodule with non exported functions
- How to intercept not exported kernel function from loadable kernel module
- How do I interact with sysfs or a driver that exports to sysfs in my own kernel module?
I think a canonical Q&A for such questions would be useful, however none of the above qualify IMHO, because they are (1) often limited to a specific problem, and (2) even when they are not, while some of the provided answers are good, they all suggest different solutions, so there's bits and pieces scattered around, but no real "complete" answer.
I'd be willing to sit down and write a self-answered Q&A post explaining all possible approaches under different scenarios, but I thought I'd ask here first.
Is there any good Q&A that I missed to be used as canonical for this? Or should I actually post one and then start using that?
For reference, here is a draft of what I think a good canonical question for this could look like:
I am developing a Linux kernel module which would need to rely on some kernel function (or global symbol) that is not exported by the kernel for modules to use (i.e. there is no
EXPORT_SYMBOL()
for it).Indeed, if I try to compile my module, I get an error that looks like this:
$ make -C /lib/modules/$(uname -r)/build M=$(pwd) modules make[1]: Entering directory '/usr/src/linux-headers-5.10.0-14-amd64' CC [M] /home/user/mymodule/mymodule.c MODPOST /home/user/mymodule/Module.symvers ERROR: modpost: "some_function" [/home/user/mymodule/mymodule.ko] undefined! ...
Would it be possible to call such a function anyway? If so, how?
I understand that I should not rely on unexported functions in my module, but I am certainly not going to submit a patch to get my module included in the kernel source. I am writing a module for educational purposes, and not for production use.
What are my options?