Linux内核zImage怎么来的?

以下内容基于s5pv210硬件进行分析

  当我们配置好内核后执行make,在arch/arm/Makefile中有:

首先要生成依赖vmlinux,这个过程请参考我的随笔(Linux内核编译make做了什么?),得到vmlinux后make会执行:

  $(Q)$(MAKE) $(build)=$(boot) MACHINE=$(MACHINE) $(boot)/$@ 

  即:make  -f   scripts/Makefile.build  obj=arch/arm/boot  arch/arm/boot/zImage

打开arch/arm/boot/Makefile找到该目标如下:

由上图可知arch/arm/boot/zImage的依赖关系链为:

arch/arm/boot/zImage->arch/arm/boot/compressed/vmlinux>arch/arm/boot/image->vmlinux

  1.  vmlinux已经生成,

  2.  接着执行arch/arm/boot/image目标下的命令即可生成arch/arm/boot/image

  3.  再接着执行arch/arm/boot/compressed/vmlinux目标下的命令:

$(Q)$(MAKE) $(build)=$(obj)/compressed $@

即:make  -f   scripts/Makefile.build  obj=arch/arm/boot/compressed  arch/arm/boot/compressed/vmlinux

打开arch/arm/boot/compressed/Makefile,得到:

 

其中:suffix_y = gzip  HEAD = head.o  OBJS = misc.o decompress.o  lib1funcs =lib1funcs.o

在arch/arm/boot/compressed/Makefile,找到vmlinux.lds目标:

 

SEDFLAGS = s/TEXT_START/0/;s/BSS_START/ALIGN(4)/

该命令为将在arch/arm/boot/compressed/vmlinux.lds.in作为输入产生arch/arm/boot/compressed/vmlinux.lds,该命令的原理不做分析。

在arch/arm/boot/compressed/Makefile,找到arch/arm/boot/compressed/piggy.gzip.o目标:

 

先执行$(call if_changed,$(suffix_y)),即$(call if_changed,gzip),

if_changed是执行参数gzip命令的函数位于scripts/kbuild.include,而gzip命令定义位于scripts/Makefile.lib,如下:

 

该命令意思是将依赖集合中的FORCE出去,剩余的arch/arm/boot/Image通过cat传给gzip生成piggy.gzip为Image的压缩文件

piggy.gzip.o  head.o   misc.o decompress.o  lib1funcs.o由makefile.build以及所包含的makefile的下匹配规则产生。

最后回到arch/arm/boot/compressed/vmlinux目标下执行:$(call if_changed,ld)链接后成功得到arch/arm/boot/compressed/vmlinux

  4.  回到arch/arm/boot/Makefile/zImage执行命令:$(call if_changed,objcopy)得到zImage

总结

  简单的说,根目录下编译得到的vmlinux内核先生成image文件,用gzip工具将image压缩成piggy.gzip并且被包含到piggy.s中。通过编译生成的head.o,piggy.gzip.o,misc.o...链接成新的vmlinux,最后用objdump工具得到zimage。

 

posted @ 2019-07-18 23:50  根叔~  阅读(2219)  评论(0编辑  收藏  举报