Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Dynamically load a function while cross compiling in Linux

If I have a function that is in one Linux Kernel, but not others, how would I go about using the same source code to compile for different kernels?

For example, if functionA is an old function which both kernels support, but functionB is only supported in newer kernels and I want to use functionB when I can for the systems that support it, how do I have only one piece of code?

Is there a way to dynamically try to load the function by name functionB and then if it fails, go ahead and dynamically load functionA?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can only compile a module for a specific kernel version, other kernels will refuse to load it. You will therefore need to compile it once for every kernel version that you need to support, thus you can simply use compile-time macros such as LINUX_VERSION_CODE and KERNEL_VERSION():

static void do_something(void)
{
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5,0,0)
        functionB();
#else
        functionA();
#endif
}

Furthermore, make sure to check whether functionB() is behind some CONFIG_ option: in such case you also want to wrap everything in a #ifdef CONFIG_THAT_ENABLES_FUNCTION_B.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading