0

If I write a simple library, e.g.

void swap(int *a, int *b) {
    int temp;
    temp = *a;
    *a = *b;
    *b = temp;
}

After compiled to a static library, say "swap.a", is it possible to call this function and link from a kernel code?


This question says linking shared library in kernel is impossible, because loading a shared library demands a loader, which is running on user space. But I don't understand why static library cannot be used in kernel space, as the loader is not needed.

Community
  • 1
  • 1
BugRepairMan
  • 221
  • 2
  • 9
  • The answer doesn't matter, the function should be optimised out anyway – wildplasser Jul 03 '16 at 17:38
  • @wildplasser Could you explain why? – BugRepairMan Jul 03 '16 at 17:41
  • Your function effectively does nothing. To check this, you could build a main() function that calls the function and print the values of the variables before and after a call to this function. – wildplasser Jul 03 '16 at 17:49
  • @wildplasser yes, it's a mistake. I have corrected it. – BugRepairMan Jul 03 '16 at 17:56
  • What do you mean by "from a kernel code"? Everything runs **from** a kernel piece of code since the kernel is the entity that prepares the process structure, maps the process memory, schedules the process and fills the CPU stack (and fires the process) when the scheduler hits it. Do you want to run a function in `kenel space` maybe (as opposed to in `user space`)? – grochmal Jul 03 '16 at 19:06
  • Searching by "linux kernel library" would give many results. E.g., [this one](http://stackoverflow.com/questions/31928255/linking-shared-library-in-linux-kernel) which claims that linking kernel with library is impossible. – Tsyvarev Jul 03 '16 at 19:18
  • @grochmal Yes, I wanna call the function in kernel space. – BugRepairMan Jul 03 '16 at 20:23
  • @Tsyvarev Thanks for the hint. I update the question. – BugRepairMan Jul 03 '16 at 22:05
  • Looking at a Linux kernel source directory for an ARM build, there are two static libraries: **./lib/lib.a** and **./arch/arm/lib/lib.a**. If you want to incorporate your own static library, then you'd have to hack the makefiles. Perhaps it would be easier to append you stuff to the kernel library. There is a lib/Kconfig menu to configure exactly what you want to build into the kernel library. – sawdust Jul 03 '16 at 22:42
  • Why do you need to include a library in kernel space anyway? – tangrs Jul 04 '16 at 06:34
  • Kernel can't call the function outside of its realm. There is a mechanism called **modules** which would be either built-in or loaded dynamically. – 0andriy Jul 04 '16 at 08:29

0 Answers0