0

Out of curiosity I tried to compile a standalone lib (without any include from libc) I wrote and then link it into a kernel module without the "-mcmodel=kernel" flag. I saw this thread , so I tried with -fpic but then I receive "undefined reference to _global_offset_table_". I also saw this thread but it has no answers.

I would like to know if it is possible to compile it, and how?

Thanks,

yeger
  • 351
  • 2
  • 15
  • `-mcmodel=kernel` is not the only compiler parameter which differ compiling for the Linux kernel from a user program. There are **much more** compiler options and definitions required by the kernel object. And you cannot use any library when build the kernel. See e.g. this question: https://stackoverflow.com/questions/22818842/where-is-the-standard-kernel-libraries-to-let-kernel-module-link-to. – Tsyvarev Sep 04 '19 at 18:09
  • Thanks for the comment, I didn't see this link before. I wrote a small library that doesn't use any functionality from libc. Can you please elaborate about the "much more compiler options"? Thanks! – yeger Sep 04 '19 at 18:17
  • Just use standard process for build kernel modules using KBuild (Kernel Build) system. And with command `make` use `V=1` option. It will show exact command line used for compile the source files, so you can see all compiler options used. – Tsyvarev Sep 04 '19 at 18:26
  • Actually, this was my starting point. I used V=1 to receive all the flags, and start to remove them, and now I'm focusing on this one. Do you about any flag that is required (and not just optional)? – yeger Sep 04 '19 at 18:28
  • @Tsyvarev, it's not true. Till some moment this or last year U-Boot, for example, used to be linked with *libgcc.a*. So, it all depends to the category of library. – 0andriy Sep 04 '19 at 19:57
  • @0andriy: It could be argued whether U-boot is treated as a part of the kernel: it is more a "loader" than an "OS kernel". As far as I understand, U-boot is *linked* neither with the Linux kernel core nor with any Linux kernel module. – Tsyvarev Sep 04 '19 at 21:04
  • @yeger: You definitely may *manually* (using `gcc` without Kbuild) compile any Linux kernel component. It could be required to use all compiler options used by Kbuild, or only part of them... but this would be *manual* compiling in any case: Kbuild doesn't support libraries. So, what do you want from us? Do you want us to list **all needed compiler options**? As I said, there are many of them. They depends on the kernel version, target architecture, configuration - it could be quite a long answer which describes the all the options. – Tsyvarev Sep 04 '19 at 21:15
  • @Tsyvarev I want to manually compile my lib, without any kernel source, and without the `-mcmodel=kernel` flag. Then, I want to take this lib, and compile and link it into a kernel module that uses it. For example, I'll compile the `a.c` "library" with `gcc a.c -o a.o` then copy it into the kernel module that uses it `mv a.o path/to/a.o_shipped`, and then compile the kernel module. Please let me know if it is explain it better, and I'll edit the original post. Thanks! – yeger Sep 04 '19 at 21:20

0 Answers0