KLD(4)

πŸ’– KLD(4) β€” the kld dynamic kernel linker facility πŸ’–

okay bestie, welcome to the Kernel Interfaces Manual, section 4. this is the kld page and it's genuinely iconic, like the way your kernel can just... grow new powers at runtime?? main character energy fr fr. 🌸


✨ NAME ✨

kld β€” the dynamic kernel linker facility πŸ¦‹


✨ DESCRIPTION ✨

so here's the tea πŸ«–: the old LKM (Loadable Kernel Modules) facility got deprecated in FreeBSD 3.0 and above, in favor of the kld interface. and like, this new one does the same cute thing as its predecessor β€” it lets the system administrator dynamically add and remove functionality from a running system. no reboot, no drama. πŸ’… and it's also a huge slay for software developers, because it helps them develop new parts of the kernel without constantly rebooting to test their changes. imagine. the suffering. ended. 😭

various types of modules can be loaded into the system, bestie. there are several defined module types (listed below ⬇️) that can be added to the system in a predefined way. plus there's a generic type, for which the module itself handles loading and unloading. independent king behavior. πŸ‘‘

the FreeBSD system makes extensive use of loadable kernel modules and provides loadable versions of:

  • most file systems πŸ—‚οΈ
  • the NFS client and server 🌐
  • all the screen-savers ✨
  • the iBCS2 and Linux emulators 🐧

kld modules are placed by default in the /boot/kernel directory, along with their matching kernel. they live together. it's giving domestic bliss. 🏑

the kld interface is used through the kldload(8), kldunload(8) and kldstat(8) programs:

  • kldload(8) can load either a.out(5) or ELF formatted loadable modules πŸš€
  • kldunload(8) unloads any given loaded module β€” if no other module is dependent upon the given module (clingy modules stay, sorry 🫢)
  • kldstat(8) is used to check the status of the modules currently loaded into the system πŸ”
⚠️ BIG WARNING, PAY ATTENTION BESTIE ⚠️ β€” kernel modules may only be loaded or unloaded if the system security level kern.securelevel is less than one. less than one. that's the rule. no cap. πŸ’€

✨ MODULE TYPES ✨

🧚 Device Driver modules

new block and character device drivers may be loaded into the system with kld. and the cute part? πŸŽ€ device nodes for the loaded drivers are automatically created when a module is loaded and destroyed when it is unloaded, by devfs(5). automatic. no manual labor. we love to see it.

you can also specify userland programs that will run when new devices become available as a result of loading modules, or when existing devices go away when modules are unloaded β€” by configuring devd(8). so like, you get notifications. it's giving personal assistant energy. πŸ’Œ


✨ FILES ✨

  • /boot/kernel β€” directory containing module binaries built for the kernel also residing in the directory 🏠
  • /usr/include/sys/module.h β€” file containing definitions required to compile a kld module πŸ“
  • /usr/share/examples/kld β€” example source code implementing a sample kld module πŸ§ͺ

✨ SEE ALSO ✨

go read these next, bestie, iykyk πŸ“š:

kldfind(2), kldfirstmod(2), kldload(2), kldnext(2), kldstat(2), kldunload(2), devfs(5), devd(8), kldload(8), kldstat(8), kldunload(8), sysctl(8)


✨ HISTORY ✨

the kld facility appeared in FreeBSD 3.0 and was designed as a replacement for the lkm facility β€” which was similar in functionality to the loadable kernel modules facility provided by SunOS 4.1.3. glow up of the century tbh. 🌈


✨ AUTHORS ✨

the kld facility was originally implemented by Doug Rabson <dfr@FreeBSD.org> πŸ‘‘πŸ’– thank you for your service king.


✨ BUGS ✨

okay so here's where it gets real. read carefully bestie, these are the gotchas 😭:

  • if a module B is dependent on another module A, but is not compiled with module A as a dependency, then kldload(8) fails to load module B β€” even if module A is already present in the system. πŸ’€
  • if multiple modules are dependent on module A, and are compiled with module A as a dependency, then kldload(8) loads an instance of module A when any of the modules are loaded. πŸ™ˆ
  • if a custom entry point is used for a module, and the module is compiled as an 'ELF' binary, then kldload(8) fails to execute the entry point. πŸ’…
  • kldload(8) points the user to read dmesg(8) for any error encountered while loading a module. (so like, always check the receipts πŸ“œ)
  • when system internal interfaces change, old modules often cannot detect this, and such modules when loaded will often cause crashes or mysterious failures. 😭 not the mysterious failures. the horror.
🫢 stay safe out there and check your dependencies fr fr.

and that's the whole page, bestie!! this one came from the <strong>Debian</strong> folks, dated <strong>January 13, 2014</strong> β€” so when you're reading this in the group chat at 2am, just know the vibes are preserved and the facts are all here. periodt. πŸ’–βœ¨πŸŽ€πŸ¦‹

πŸ“¦ where this page comes from