1.进入linux内核源码目录下,打开Makefile文件,搜索vmlinux
cmd_link-vmlinux = \
$(CONFIG_SHELL) $< "$(LD)" "$(KBUILD_LDFLAGS)" "$(LDFLAGS_vmlinux)"; \
$(if $(ARCH_POSTLINK), $(MAKE) -f $(ARCH_POSTLINK) $@, true)
vmlinux: scripts/link-vmlinux.sh autoksyms_recursive $(vmlinux-deps) FORCE
+$(call if_changed,link-vmlinux)
分析: .进入linux内核源码目录下,scripts/目录下,打开Kbuild.include文件,搜索if_changed
218 if_changed = $(if $(newer-prereqs)$(cmd-check), \
219 $(cmd); \
183 cmd = @set -e; $(echo-cmd) $($(quiet)redirect) $(cmd_$(1))
解释:
@set -e:编译时遇到任何错误就退出
$(cmd_$(1)) :调用cmd_link_vmlinux
2.进入linux内核源码目录下,scripts目录下,打开linux-vmlinux.sh文件
重点解析上面这句话:
$(CONFIG_SHELL)
<
"
< "
<"(LD)" “
(
K
B
U
I
L
D
L
D
F
L
A
G
S
)
"
"
(KBUILD_LDFLAGS)" "
(KBUILDLDFLAGS)""(LDFLAGS_vmlinux)”;
$(if $(ARCH_POSTLINK), $(MAKE) -f $(ARCH_POSTLINK) $@, true)
$@是生成的目标文件vmlinux
${LD} ${KBUILD_LDFLAGS} -r -o ${1} ${objects}
可知生成的都是built-in.a和lib.a文件
3.进入linux内核源码目录下,打开Makefile文件,搜索built-in.a和lib.a文件
KBUILD_VMLINUX_OBJS := $(head-y) $(patsubst %/,%/built-in.a, $(core-y))
KBUILD_VMLINUX_OBJS += $(addsuffix built-in.a, $(filter %/, $(libs-y)))
KBUILD_VMLINUX_OBJS += $(patsubst %/, %/lib.a, $(filter %/, $(libs-y)))
KBUILD_VMLINUX_LIBS := $(filter-out %/, $(libs-y))
KBUILD_VMLINUX_LIBS := $(patsubst %/,%/lib.a, $(libs-y))
KBUILD_VMLINUX_OBJS += $(patsubst %/,%/built-in.a, $(drivers-y))