15

When building the Linux kernel from sources one could decide if a certain functionality is statically built into the kernel or packed into a module for dynamic insertion by .config.

If on the other hand I have sources for any 3rd party module, like for example a packaged device driver, is it possible to programmatically integrate this code into the kernel statically instead? And not load the kernel module from the root filesystem?

dronus
  • 10,774
  • 8
  • 54
  • 80
  • Notice, from legal point of view, you cannot statically link proprietary modules. Linux kernel compilation process will produce a fatal error if you do (and warn you if license is not defined). – Jérôme Pouiller Mar 23 '18 at 16:36

1 Answers1

12

Sure, you just need to do a bit of hacking to move the external module into the kernel source tree, tweak the Makefiles/Kconfig a bit so that the code is built-in, and then build your kernel image. For example, let's say you move the module source into drivers/blah. Then you should add a line to then end of drivers/Makefile like

obj-y += blah/

and you should make sure that drivers/blah/Makefile is set up to build your module in, with something like

obj-y += mymodule.o
mymodule-objs := src.o other.o

and so on, where your Makefile is set up however it needs to be to build the particular module you're working on. Note: You have to use the output file name for mymodule-objs and not the input filename!

Community
  • 1
  • 1
Roland
  • 6,227
  • 23
  • 29
  • If I insert the module source by this way, will it result in obsolete module insertion- and removal dependent code built into the kernel? – dronus Sep 29 '11 at 19:00
  • Not sure I completely understand your question -- in any case if you add the source to your kernel, then the only extra code built into the kernel is the code from the module source you add. So if the module is properly written, you shouldn't get anything you don't need. – Roland Oct 03 '11 at 23:37
  • 1
    I thought that a module's source code contain code related to it's 'moduleness', like code that cares about the proper insertion and removal with insmod/rmmod for example. And code that gives the module a name or tell its dependencies on other modules. So my question is if the code is now inserted statically to the kernel, will there be any handicap by the remains of such code? – dronus Oct 04 '11 at 10:51
  • No, there isn't code like that. You have an module_init function, but you need initialization code even if the code is linked directly into the kernel. There is unload handling code, but if the module is written correctly, those functions will be annotated `__exit` and therefore won't be linked into the kernel image. – Roland Oct 05 '11 at 18:55
  • Thus a module has the lifecycle handlers defined via `module_init(..)` and `module_exit(...)` macros, while the same code statically linked relies on the `__init` annotation, right? – dronus Oct 05 '11 at 19:04