在中罗列出了最后链接生成vmlinux的过程。可以看到在每个子目录下都有一个built-in.o文件。对于此产生了疑问built-in.o文件是根据什么产生的。
arm-linux-ld -EL -p --no-undefined -X -o vmlinux -T arch/arm/kernel/vmlinux.ldsarch/arm/kernel/head.o arch/arm/kernel/init_task.o init/built-in.o --start-group usr/built-in.o arch/arm/kernel/built-in.o arch/arm/mm/built-in.o arch/arm/common/built-in.o arch/arm/mach-s3c2410/built-in.o arch/arm/mach-s3c2400/built-in.o arch/arm/mach-s3c2412/built-in.o arch/arm/mach-s3c2440/built-in.o arch/arm/mach-s3c2442/built-in.o arch/arm/mach-s3c2443/built-in.o arch/arm/nwfpe/built-in.o arch/arm/plat-s3c24xx/built-in.o kernel/built-in.o mm/built-in.o fs/built-in.o ipc/built-in.o security/built-in.o crypto/built-in.o block/built-in.o arch/arm/lib/lib.a lib/lib.a arch/arm/lib/built-in.o lib/built-in.o drivers/built-in.o sound/built-in.o net/built-in.o --end-group .tmp_kallsyms2.o
1、测试,在linux目录下添加一个test文件夹,在文件夹里面添加了test.c 和 Makefile。测试是否也会生成built-in.o文件
2、从顶层Makefile文件开始分析built-in.o原理
3、递归规则
1、测试,在linux目录下添加一个test文件夹,在文件夹里面添加了test.c 和 Makefile。测试是否也会生成built-in.o文件
a、mkdir test,创建test文件夹
b、cd test,进入test文件夹
d、vim Makefile,在里面创建Makefile
##Makefile for the linux kernel makefile experiment.#obj-y := test.o
e、vim test.c,在里面创建C文件
#includeint test_global = 0;
f、修改顶层Makefile,在core-y后面增加test/目录
-core-y += kernel/ mm/ fs/ ipc/ security/ crypto/ block/+core-y += kernel/ mm/ fs/ ipc/ security/ crypto/ block/ test/
g、make uImage后观察到在链接的过程中多出了test/built-in.o 文件。测试成功
arm-linux-ld -EL -p --no-undefined -X -o .tmp_vmlinux1 -T arch/arm/kernel/vmlinux.lds arch/arm/kernel/head.o arch/arm/kernel/init_task.o init/built-in.o --start-group usr/built-in.o arch/arm/kernel/built-in.o arch/arm/mm/built-in.o arch/arm/common/built-in.o arch/arm/mach-s3c2410/built-in.o arch/arm/mach-s3c2400/built-in.o arch/arm/mach-s3c2412/built-in.o arch/arm/mach-s3c2440/built-in.o arch/arm/mach-s3c2442/built-in.o arch/arm/mach-s3c2443/built-in.o arch/arm/nwfpe/built-in.o arch/arm/plat-s3c24xx/built-in.o kernel/built-in.o mm/built-in.o fs/built-in.o ipc/built-in.o security/built-in.o crypto/built-in.o block/built-in.o test/built-in.o arch/arm/lib/lib.a lib/lib.a arch/arm/lib/built-in.o lib/built-in.o drivers/built-in.o sound/built-in.o net/built-in.o --end-group
2、从顶层Makefile文件开始分析built-in.o原理,在测试成功后,要分析是怎么做到更改了几个文件就能产生test/built-in.o并且编译进内核的。
从顶层Makefile开始分析,vmlinux-dirs包含了所有的linux子目录
vmlinux-dirs := $(patsubst %/,%,$(filter %/, $(init-y) $(init-m) \ $(core-y) $(core-m) $(drivers-y) $(drivers-m) \ $(net-y) $(net-m) $(libs-y) $(libs-m)))
$(patsubst, , )名称:模式字符串替换函数——patsubst。功能:查找 中的单词(单词以“空格”、“Tab”或“回车”“ 换行”分隔)是否符合模式 ,如果匹配的话,则以 替换。这里, 可以包括通配符“%”,表示任意长度的字串。如果 中也包含“%”,那么, 中的这个“%”将是 中的那个“%”所代表的字串。(可以用“\”来转义,以“\%”来表示真实含义的“%”字符)返回:函数返回被替换过后的字符串。
$(filter, )名称:过滤函数——filter。功能:以 模式过滤 字符串中的单词,保留符合模式 的单词。可以有多个模式。返回:返回符合模式 的字串。
另外在arch/arm/Makefile下也定义了一些文件夹:
171 core-y += arch/arm/kernel/ arch/arm/mm/ arch/arm/common/172 core-y += $(MACHINE)173 core-$(CONFIG_ARCH_S3C2410) += arch/arm/mach-s3c2400/174 core-$(CONFIG_ARCH_S3C2410) += arch/arm/mach-s3c2412/175 core-$(CONFIG_ARCH_S3C2410) += arch/arm/mach-s3c2440/176 core-$(CONFIG_ARCH_S3C2410) += arch/arm/mach-s3c2442/177 core-$(CONFIG_ARCH_S3C2410) += arch/arm/mach-s3c2443/178 core-$(CONFIG_FPE_NWFPE) += arch/arm/nwfpe/179 core-$(CONFIG_FPE_FASTFPE) += $(FASTFPE_OBJ)180 core-$(CONFIG_VFP) += arch/arm/vfp/181182 # If we have a common platform directory, then include it in the build.183 core-$(CONFIG_PLAT_IOP) += arch/arm/plat-iop/184 core-$(CONFIG_ARCH_OMAP) += arch/arm/plat-omap/185 core-$(CONFIG_PLAT_S3C24XX) += arch/arm/plat-s3c24xx/186187 drivers-$(CONFIG_OPROFILE) += arch/arm/oprofile/188 drivers-$(CONFIG_ARCH_CLPS7500) += drivers/acorn/char/189 drivers-$(CONFIG_ARCH_L7200) += drivers/acorn/char/190191 libs-y := arch/arm/lib/ $(libs-y)
展开得到
vmlinux-dir = init usr \arch/arm/kernel arch/arm/mm arch/arm/common arch/arm/mach-s3c2410 arch/arm/mach-s3c2400 arch/arm/mach-s3c2412 arch/arm/mach-s3c2440 \arch/arm/mach-s3c2442 arch/arm/mach-s3c2443 arch/arm/nwfpe arch/arm/plat-s3c24xx \kernel mm fs ipc security crypto block test \arch/arm/lib lib arch/arm/lib \drivers sound net \
继续看顶层Makefile生成vmlinux-dirs的规则如下:
764 $(vmlinux-dirs): prepare scripts765 $(Q)$(MAKE) $(build)=$@
以创建的test目录为例:展开得到。它的意思是调用scripts/Makefile.build。令参数obj=test,然后make。
make -f scripts/Makefile.build obj=test
接着进入scripts/Makefile.build继续分析,它的默认目标是__build:
07 PHONY := __build08 __build:83 __build: $(if $(KBUILD_BUILTIN),$(builtin-target) $(lib-target) $(extra-y)) \84 $(if $(KBUILD_MODULES),$(obj-m)) \85 $(subdir-ym) $(always)86 @:
接着看builtin-target、lib-target,它们被定义的条件是obj-%、lib-%不能为空
73 ifneq ($(strip $(lib-y) $(lib-m) $(lib-n) $(lib-)),)74 lib-target := $(obj)/lib.a75 endif7677 ifneq ($(strip $(obj-y) $(obj-m) $(obj-n) $(obj-) $(lib-target)),)78 builtin-target := $(obj)/built-in.o79 endif
名称:去空格函数——strip。功能:去掉字串中开头和结尾的空字符。返回:返回被去掉空格的字符串值。
接着看生成builtin-target的规则,lib-target的规则类似,还是在scripts/Makefile.build里找到如下规则
254 # To build objects in subdirs, we need to descend into the directories255 $(sort $(subdir-obj-y)): $(subdir-ym) ;256257 #258 # Rule to compile a set of .o files into one .o file259 #260 ifdef builtin-target261 quiet_cmd_link_o_target = LD $@262 # If the list of objects to link is empty, just create an empty built-in.o263 cmd_link_o_target = $(if $(strip $(obj-y)),\264 $(LD) $(ld_flags) -r -o $@ $(filter $(obj-y), $^),\265 rm -f $@; $(AR) rcs $@)266267 $(builtin-target): $(obj-y) FORCE268 $(call if_changed,link_o_target)269270 targets += $(builtin-target)271 endif # builtin-target
调用if_changed函数,进入scripts/Kbuild.include中
167 if_changed = $(if $(strip $(any-prereq) $(arg-check)), \168 @set -e; \169 $(echo-cmd) $(cmd_$(1)); \170 echo 'cmd_$@ := $(make-cmd)' > $(dot-target).cmd)
可以看到最后调用的是cmd_link_o_target 脚本,回到scripts/Makefile.build中:
257 #258 # Rule to compile a set of .o files into one .o file259 #260 ifdef builtin-target261 quiet_cmd_link_o_target = LD $@262 # If the list of objects to link is empty, just create an empty built-in.o263 cmd_link_o_target = $(if $(strip $(obj-y)),\264 $(LD) $(ld_flags) -r -o $@ $(filter $(obj-y), $^),\265 rm -f $@; $(AR) rcs $@)
其中obj-y=test.o定义在test目录下的Makefile中,如下在scripts/Makefile.build中include了$(kbuild-dir)/Makefile,即test/Makefile
16 kbuild-dir := $(if $(filter /%,$(src)),$(src),$(srctree)/$(src))17 include $(if $(wildcard $(kbuild-dir)/Kbuild), $(kbuild-dir)/Kbuild, $(kbuild-dir)/Makefile)
将规则展开得到:最终得到了test/built-in.o
cmd_link_o_target = arm-linux-ld -EL -r -o test/built-in.o test.o arm-linux-ar rcs test/built-in.o
3、递归规则。如果在子目录下还有子目录编译系统会怎么操作呢,下面就讲述递归的规则:
继续看scripts/Makefile.build,它包含了scripts/Makefile.lib
19 include scripts/Makefile.lib
接着看到scripts/Makefile.lib,里面定义了subdir-ym 这个变量表示的是如果obj-y含有文件夹则将文件夹放到subdir-ym中
24 __subdir-y := $(patsubst %/,%,$(filter %/, $(obj-y)))25 subdir-y += $(__subdir-y)26 __subdir-m := $(patsubst %/,%,$(filter %/, $(obj-m)))27 subdir-m += $(__subdir-m)28 obj-y := $(patsubst %/, %/built-in.o, $(obj-y))29 obj-m := $(filter-out %/, $(obj-m))3031 # Subdirectories we need to descend into3233 subdir-ym := $(sort $(subdir-y) $(subdir-m))
继续回到scripts/Makefile.build,看到有关subdir-y的规则
322 $(subdir-ym):333 $(Q)$(MAKE) $(build)=$@
可以看到这个规则与第二部分的规则很类似,这是一个递归调用,scripts/Makefile.build调用自己生成subdir-ym/built-in.o。具体分析与第二部分一样。下面截图自第二部分分析
子目录下的built-in.o 就是这么生成的。参考自