Ta Thanh Dinh
Ta Thanh Dinh

Reputation: 661

How do I link a static library when building a kernel module?

I want to build a Linux kernel module foo.ko from an existing file foo.c and a static library support.a. The library support.a is compiled from Rust so there is no support.c.

I've used the following Makefile

KERNEL_DIR := /lib/modules/$(shell uname -r)/build

obj-m += foo.o
foo-obs += support.a

all:
    $(MAKE) -C $(KERNEL_DIR) SUBDIRS=$(PWD) modules V=1

It seems that support.a is not linked; there are warnings that the functions called from foo.c (and implemented in support.a) are undefined.

Update 0: the Makefile works on Ubuntu LTS (I've tested on 18.04 and 14.04) but not on Fedora (both 29/30). The output in case of Fedora is:

...
make -C /lib/modules/5.1.11-200.fc29.x86_64/build SUBDIRS=/public/Github/rustyvisor modules
make[1] : on entre dans le répertoire « /usr/src/kernels/5.1.11-200.fc29.x86_64 »
Makefile:205: ================= WARNING ================
Makefile:206: 'SUBDIRS' will be removed after Linux 5.3
Makefile:207: Please use 'M=' or 'KBUILD_EXTMOD' instead
Makefile:208: ==========================================
  LD [M]  /public/Github/rustyvisor/rustyvisor.o
  Building modules, stage 2.
  MODPOST 1 modules
WARNING: /public/Github/rustyvisor/rustyvisor.o(.init.text+0xbb): Section mismatch in reference from the function init_module() to the function .exit.text:rustyvisor_exit()
The function __init init_module() references
a function __exit rustyvisor_exit().
This is often seen when error handling in the init function
uses functionality in the exit path.
The fix is often to remove the __exit annotation of
rustyvisor_exit() so it may be used outside an exit section.

WARNING: "rustyvisor_core_unload" [/public/Github/rustyvisor/rustyvisor.ko] undefined!
WARNING: "rustyvisor_load" [/public/Github/rustyvisor/rustyvisor.ko] undefined!
WARNING: "rustyvisor_core_load" [/public/Github/rustyvisor/rustyvisor.ko] undefined!
WARNING: "rustyvisor_unload" [/public/Github/rustyvisor/rustyvisor.ko] undefined!
  LD [M]  /public/Github/rustyvisor/rustyvisor.ko
make[1] : on quitte le répertoire « /usr/src/kernels/5.1.11-200.fc29.x86_64 »

Update 1: There is very similar question but this question is to ask why the Makefile doesn't work on Fedora but I discovered that it works on Ubuntu.

Upvotes: 0

Views: 2700

Answers (1)

Ta Thanh Dinh
Ta Thanh Dinh

Reputation: 661

Finally, I found a workaround for the problem (but I still don't understand why). On Fedora, the library name support.a should be changed to support.o, then the linker works!!!

Upvotes: 1

Related Questions