我需要一些KBuild
实施细节建议,这些建议与构建外部模块有关。
Linux Kernel 5.0.0-32
这是我的LKM
Makefile:
obj-m += pfsw.o
pfsw-objs := src/init.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
看一下scripts/Makefile.build
的实现并使用-d
选项打印调试输出,我发现正在执行的该Makefile的目标是__build
:
__build: $(if $(KBUILD_BUILTIN),$(builtin-target) $(lib-target) $(extra-y)) \
$(if $(KBUILD_MODULES),$(obj-m) $(modorder-target)) \
$(subdir-ym) $(always)
@:
由于我正在构建外部LKM,因此前提条件是$(obj-m)
和$(modorder-target)
。我从数据库中获得了它们的值:
obj-m := /home/memyself/lkm/procfs_write_perm/pfsw.o
modorder-target := /home/memyself/lkm/procfs_write_perm/modules.order
因此,要执行__build
,必须先构建先决条件/home/memyself/lkm/procfs_write_perm/pfsw.o
。 Makefile.build
中定义了以下$(obj)/%.o:
模式规则:
$(obj)/%.o: $(src)/%.c $(recordmcount_source) $(objtool_dep) FORCE
$(call cmd,force_checksrc)
$(call if_changed_rule,cc_o_c)
我添加了调试输出以打印目标自动变量的名称:
$(obj)/%.o: $(src)/%.c $(recordmcount_source) $(objtool_dep) FORCE
@echo "$@"
$(call cmd,force_checksrc)
$(call cmd,force_check_kmsg)
$(call if_changed_rule,cc_o_c)
,并预期将打印/home/memyself/lkm/procfs_write_perm/pfsw.o
,但实际上已打印/home/memyself/lkm/procfs_write_perm/src/init.o
。
看起来有些神奇...
问题:为什么构建目标/home/memyself/lkm/procfs_write_perm/pfsw.o
会导致构建/home/memyself/lkm/procfs_write_perm/src/init.o
?该代码在哪里指定?
我知道有一个real-obj-m
变量正好包含该值,但是grep
在代码库中,我并没有发现它取决于某些东西...
答案 0 :(得分:0)
正如@Tsyvarev在评论中所暗示的,有一条从其obj-m
构建-objs
的规则。
分析行为时,我错误地考虑了一个隐式模式规则来构建obj-m
,在我的情况下,该规则会扩展为/home/memyself/lkm/procfs_write_perm/pfsw.o
。使用make -p
运行构建,可能会在输出中注意到以下条目:
/home/memyself/lkm/procfs_write_perm/pfsw.o: FORCE /home/memyself/lkm/procfs_write_perm/src/init.o
# Implicit rule search has not been done.
# Implicit/static pattern stem: ''
# File is an intermediate prerequisite.
# Last modified 2019-12-18 21:13:48.337755924
# File has been updated.
# Successfully updated.
# automatic
# @ := /home/memyself/lkm/procfs_write_perm/pfsw.o
# automatic
# % :=
# automatic
# * :=
# automatic
# + := FORCE /home/memyself/lkm/procfs_write_perm/src/init.o
# automatic
# | :=
# automatic
# < := FORCE
# automatic
# ^ := FORCE /home/memyself/lkm/procfs_write_perm/src/init.o
# automatic
# ? := FORCE /home/memyself/lkm/procfs_write_perm/src/init.o
# variable set hash-table stats:
# Load=8/32=25%, Rehash=0, Collisions=13/382=3%
# recipe to execute (from 'scripts/Makefile.build', line 474):
$(call if_changed,link_multi-m)
@{ echo $(@:.o=.ko); echo $(filter %.o,$^); \
$(cmd_undef_syms); } > $(MODVERDIR)/$(@F:.o=.mod)
因此,有一条非隐式规则指定要构建的配方/home/memyself/lkm/procfs_write_perm/pfsw.o
,这意味着对于该特定情况,不应考虑隐式规则$(obj)/%.o:
。
上面指定的make
数据库条目还包含要执行的配方的行号和其来源文件。在这种情况下
recipe to execute (from 'scripts/Makefile.build', line 474):
指向scripts/Makefile.build
条目,在我的情况下为
$(multi-used-m): FORCE
$(call if_changed,link_multi-m) # <------ This is the line the database points to
@{ echo $(@:.o=.ko); echo $(filter %.o,$^); \
$(cmd_undef_syms); } > $(MODVERDIR)/$(@F:.o=.mod)
$(call multi_depend, $(multi-used-m), .o, -objs -y -m)