一切的一切都是make读取makefile编译链接的,就好像孙悟空逃不出如来佛祖的手掌,vmlinux的出世也是在makefile的安排之下。那就现在看看makefile# SHELL used by kbuildCONFIG_SHELL := $(shell if [ -x "$$BASH" ]; then echo $$BASH; else if [ -x /bin/bash ]; then echo /bin/bash; else echo sh; fi ; fi)# Final link of vmlinuxcmd_link-vmlinux = $(CONFIG_SHELL) $< $(LD) $(LDFLAGS) $(LDFLAGS_vmlinux)quiet_cmd_link-vmlinux = LINK$@vmlinux: scripts/link-vmlinux.sh vmlinux_prereq $(vmlinux-deps) FORCE+$(call if_changed,link-vmlinux)看完这一段差点一口老血吐出来,我真是没有想到他们尽然可以这么玩。。。 注:对if_changed函数的分析可以看我的这篇文章if_changed这个cmd_link-vmlinux就是把第一个依赖作为脚本传给了系统使用的shell,由系统shell执行。好吧,我也是醉了,道行就这么又深了一点点。回到正题,我们先看看vmlinux相关的都是谁。vmlinux一共依赖两个 一个是vmlinux_prereq,另一个是vmlinux-deps。那分头分析。
vmlinux_prereq
# Include targets which we want to execute sequentially if the rest of the# kernel build went well. If CONFIG_TRIM_UNUSED_KSYMS is set, this might be# evaluated more than once.PHONY += vmlinux_prereqvmlinux_prereq: $(vmlinux-deps) FORCEifdef CONFIG_HEADERS_CHECK$(Q)$(MAKE) -f $(srctree)/Makefile headers_checkendififdef CONFIG_BUILD_DOCSRC$(Q)$(MAKE) $(build)=Documentationendififdef CONFIG_GDB_SCRIPTS$(Q)ln -fsn `cd $(srctree) && /bin/pwd`/scripts/gdb/vmlinux-gdb.pyendififdef CONFIG_TRIM_UNUSED_KSYMS$(Q)$(CONFIG_SHELL) $(srctree)/scripts/adjust_autoksyms.sh "$(MAKE) KBUILD_MODULES=1 -f $(srctree)/Makefile vmlinux_prereq"endif这个看觉和最后的vmlinux关系不大, 咱暂时就不看了。
这个head-y定义根据架构不同而不同,比如在x86架构下,该定义为:head-y := arch/x86/kernel/head_$(BITS).ohead-y += arch/x86/kernel/head$(BITS).ohead-y += arch/x86/kernel/head.o恩,就一个架构,都有三个文件,好多~在搜索的时候又发现这么一句在文档中:$(head-y) lists objects to be linked first in vmlinux.这个提醒了我一下,在链接的时候,文件出现的顺序是有影响的。