上一节我们介绍了vmlinux的编译过程。vmlinux是一个ELF文件,上百M,无法直接flash到板子上。不同架构最终生成的启动镜像略有区别,一般地:

  • 通过编译生成​​vmlinux​​​和​​System.map​
  • 通过​​objcopy​​​移除​​vmlinux​​中不必要段,输出binary格式Image
  • 再对Image进行压缩,输出不同格式的压缩文件,比如gzip对应的​​Image.gz​​,
  • 最后通过工具加上BootLoader可以识别的header用于启动引导。

我们重点关注arm64架构的编译情况。

根目录的Makefile include了不同架构的Makefile文件:

​https://elixir.bootlin.com/linux/v5.4.200/source/Makefile#L583​

include arch/$(SRCARCH)/Makefile

arm64下的Makefile中:

# Default target when executing plain make
boot := arch/arm64/boot
KBUILD_IMAGE := $(boot)/Image.gz

all: Image.gz


Image: vmlinux
$(Q)$(MAKE) $(build)=$(boot) $(boot)/$@

Image.%: Image
$(Q)$(MAKE) $(build)=$(boot) $(boot)/$@

zinstall install:
$(Q)$(MAKE) $(build)=$(boot) $@

由此可以发现,arm64的启动镜像为压缩后的​​Image.gz​​.

再进一步走到boot目录下的Makefile​​/​​​​arch​​​/​​arm64​​​/​​boot​​​/​​Makefile​

OBJCOPYFLAGS_Image :=-O binary -R .note -R .note.gnu.build-id -R .comment -S

targets := Image Image.bz2 Image.gz Image.lz4 Image.lzma Image.lzo

$(obj)/Image: vmlinux FORCE
$(call if_changed,objcopy)

$(obj)/Image.bz2: $(obj)/Image FORCE
$(call if_changed,bzip2)

$(obj)/Image.gz: $(obj)/Image FORCE
$(call if_changed,gzip)
...

install:
$(CONFIG_SHELL) $(srctree)/$(src)/install.sh $(KERNELRELEASE) \
$(obj)/Image System.map "$(INSTALL_PATH)"

zinstall:
$(CONFIG_SHELL) $(srctree)/$(src)/install.sh $(KERNELRELEASE) \
$(obj)/Image.gz System.map "$(INSTALL_PATH)"

install脚本如下​​/​​​​arch​​​/​​arm64​​​/​​boot​​​/​​install.sh​

# Arguments:
# $1 - kernel version
# $2 - kernel image file
# $3 - kernel map file
# $4 - default install path (blank if root directory)

...
if [ "$(basename $2)" = "Image.gz" ]; then
# Compressed install
echo "Installing compressed kernel"
base=vmlinuz
else
# Normal install
echo "Installing normal kernel"
base=vmlinux
fi

if [ -f $4/$base-$1 ]; then
mv $4/$base-$1 $4/$base-$1.old
fi
cat $2 > $4/$base-$1

用一张图总结如下:

kernel启动镜像不是vmlinux,而是它!_根目录