MEMORY | INTERRUPT | TIMER | 并发与同步 | 进程管理 | 调度 | uboot | DTB | ARMV8 | ATF | Kernel Data Structure | PHY | LINUX2.6 | 驱动合集 | UART子系统 | USB专题 |

linux内核Image/zImage/uImage的编译过程(十四)

一. 基础知识

1.1 Image uImage与zImage的区别

linux内核经过编译后会生成一个elf格式的可执行程序,叫vmlinux或vmlinuz,这个就是原始的未经任何处理加工的原版内核elf文件;嵌入式系统部署时烧录的一般不是这个vmlinuz/vmlinux,而是要用objcopy工具去制作成烧录镜像格式,经过制作加工后的烧录镜像文件就叫Image。原则上Image就可以直接被烧录到Flash上进行启动执行(类似于u-boot.bin),但是实际上并不是这么简单。linux的大佬们觉得Image还是太大了!!所以对Image进行了压缩,并且在image压缩后的文件的前端附加了一部分解压缩代码,构成了一个压缩格式的镜像就叫zImage。运行的时候,通过zImage镜像头部的解压缩代码进行自解压,然后执行解压出来的内核镜像。

uboot为了启动linux内核,还发明了一种内核格式叫uImage。uImage是uboot专用的映像文件,它是在zImage之前加上一个长度为64字节的“头”,说明这个内核的版本、加载位置、生成时间、大小等信息;其0x40之后与zImage没区别

注意:
(1)uImage不关linux内核的事,linux内核只管生成zImage即可,这个加工过程其实就是在zImage前面加上64字节的uImage的头信息即可。如果 ${CROSS_COMPILE}mkimage且编译虚拟机的/usr/bin/mkimage都不存在,会提示"mkimage" command not found - U-Boot images will not be built。解决方案是去uboot/tools目录下执行cp mkimage /usr/bin/,复制mkimage工具到系统目录下。再去make uImage即可。
(2)原则上uboot启动时是执行uImage格式的内核镜像,但是实际上uboot中也可以支持zImage。是否支持就看是否定义了LINUX_ZIMAGE_MAGIC这个宏。所以大家可以看出:有些uboot是支持zImage启动的,有些则不支持。但是所有的uboot肯定都支持uImage启动(常用方式,有更多的优点)。

具体的实现流程见文章末尾的总结图。

二. uImage目标及其构建规则

(1)在顶层Makefile包含了 arch/arm/Makefile文件,uImage目标就定义在这个文件中。

zImage Image xipImage bootpImage uImage: vmlinux
	$(Q)$(MAKE) $(build)=$(boot) MACHINE=$(MACHINE) $(boot)/$@

参考build变量的定义,把上面的命令展开为(我用的mini6410开发板):

make -f scripts/Makefile.build obj=arch/arm/boot MACHINE=arch/arm/mach-s3c64xx/ arch/arm/boot/uImage

(2)由于obj=arch/arm/boot,所以会包含arch/arm/boot /Makefile 文件,这时Make又会在arch/arm/boot /Makefile中查找目标arch/arm/boot/uImage。arch/arm/boot/uImage目标的定义和构建规则,如下:

# arch/arm/boot/Makefile
MKIMAGE         := $(srctree)/scripts/mkuboot.sh

$(obj)/Image: vmlinux FORCE
	$(call if_changed,objcopy)
	@echo '  Kernel: $@ is ready'

$(obj)/compressed/vmlinux: $(obj)/Image FORCE
	$(Q)$(MAKE) $(build)=$(obj)/compressed $@

$(obj)/zImage:	$(obj)/compressed/vmlinux FORCE
	$(call if_changed,objcopy)
	@echo '  Kernel: $@ is ready'

quiet_cmd_uimage = UIMAGE  $@
      cmd_uimage = $(CONFIG_SHELL) $(MKIMAGE) -A arm -O linux -T kernel \
		   -C none -a $(LOADADDR) -e $(STARTADDR) \
		   -n 'Linux-$(KERNELRELEASE)' -d $< $@

$(obj)/uImage: LOADADDR=$(ZRELADDR)


$(obj)/uImage: STARTADDR=$(LOADADDR)

$(obj)/uImage:	$(obj)/zImage FORCE
	$(call if_changed,uimage)
	@echo '  Image $@ is ready'

可以看到目标arch/arm/boot/uImage的依赖是arch/arm/boot/zImage(下一小节讲),调用的是mkuboot.sh脚本。 mkuboot.sh的作用是去找到是否存在"mkimage",此工具是用来生成最后的uImage。mkuboot.sh 首先检查toolchain是否拥有mkimage (使用-z来判空),如果没有,再检查系统中是否拥有mkimage;如果没有则报错。关于mkimage的使用方法,在linux内核编译中常用的目标(二)中有详细的讲解。

(3)需要注意的是这个链接和入口地址定义在平台相关的目录中:

# arch/arm/mach-s3c64xx/Makefile.boot: 
  zreladdr-y	:= 0x50008000

下面只给出编译时实际执行的命令:

/bin/bash /home/hh/linux-2.6.38/scripts/mkuboot.sh -A arm -O linux -T kernel
 -C none -a 0x50008000 -e 0x50008000 -n 'Linux-2.6.38-FriendlyARM' 
 -d arch/arm/boot/zImage arch/arm/boot/uImage

三. uImage的依赖zImage

# arch/arm/boot/Makefile 

$(obj)/compressed/vmlinux: $(obj)/Image FORCE
	$(Q)$(MAKE) $(build)=$(obj)/compressed $@

$(obj)/zImage:	$(obj)/compressed/vmlinux FORCE
	$(call if_changed,objcopy)
	@echo '  Kernel: $@ is ready'

3.1 首先分析依赖$(obj)/compressed/vmlinux

参考build变量的定义,把上面的命令展开为(我用的mini6410开发板):

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

由于obj=arch/arm/boot/compressed,所以会包含arch/arm/boot /compressed/Makefile 文件,这时Make又会在arch/arm/boot /compressed/Makefile查找目标arch/arm/boot/compressed/vmlinux的定义和构建规则,如下:

HEAD	= head.o

ZTEXTADDR	:= 0
ZBSSADDR	:= ALIGN(4)

SEDFLAGS	= s/TEXT_START/$(ZTEXTADDR)/;s/BSS_START/$(ZBSSADDR)/

suffix_$(CONFIG_KERNEL_GZIP) = gzip       //定义在.config文件中
suffix_$(CONFIG_KERNEL_LZO)  = lzo
suffix_$(CONFIG_KERNEL_LZMA) = lzma

# For __aeabi_uidivmod
lib1funcs = $(obj)/lib1funcs.o

$(obj)/lib1funcs.S: $(srctree)/arch/$(SRCARCH)/lib/lib1funcs.S FORCE
	$(call cmd,shipped)

$(obj)/vmlinux: $(obj)/vmlinux.lds $(obj)/$(HEAD) $(obj)/piggy.$(suffix_y).o \
	 	$(addprefix $(obj)/, $(OBJS)) $(lib1funcs) FORCE
	$(call if_changed,ld)
	@:

$(obj)/piggy.$(suffix_y): $(obj)/../Image FORCE
	$(call if_changed,$(suffix_y))

$(obj)/piggy.$(suffix_y).o:  $(obj)/piggy.$(suffix_y) FORCE

CFLAGS_font.o := -Dstatic=

$(obj)/font.c: $(FONTC)
	$(call cmd,shipped)

(1) 依赖arch/arm/boot/compressed/vmlinux.lds

$(obj)/vmlinux.lds: $(obj)/vmlinux.lds.in arch/arm/boot/Makefile $(KCONFIG_CONFIG)
	@sed "$(SEDFLAGS)" < $< > $@

首先用sed的替代命令将 第一个依赖文件vmliux.lds.in中包含“TEXT_START”文本替换为“0”,将“BSS_START”替换为“ALIGN(4)”,然后将其替换后的文件输出到 vmlinux.lds文件中。

(2) 依赖arch/arm/boot/compressed/head.o
注意,这个arch/arm/boot/compressed/head.o和arch/arm/kernel/head.o是不同的,前者主要是在内核启动时自解压的初始化代码,后者才是真正的内核启动代码。它的构建规则如下:

#  scripts/Makefile.build
quiet_cmd_as_o_S = AS $(quiet_modtag)  $@
cmd_as_o_S       = $(CC) $(a_flags) -c -o $@ $<

$(obj)/%.o: $(src)/%.S FORCE
	$(call if_changed_dep,as_o_S)

调用cmd_as_o_S变量定义的规则,生成目标文件。下面只给出编译时实际执行的命令:

 arm-linux-gcc -Wp,-MD,arch/arm/boot/compressed/.head.o.d  -nostdinc -isystem 
 /home/hh/opt/FriendlyARM/toolschain/4.5.1/bin/../lib/gcc/arm-none-linux-gnueabi/4.5.1/include
 -I/home/hh/linux-2.6.38/arch/arm/include -Iinclude  -include include/generated/autoconf.h -D__KERNEL__ 
 -mlittle-endian -Iarch/arm/mach-s3c64xx/include -Iarch/arm/plat-samsung/include -D__ASSEMBLY__ 
 -mabi=aapcs-linux -mno-thumb-interwork -funwind-tables  -D__LINUX_ARM_ARCH__=6 -march=armv6k 
 -mtune=arm1136j-s -include asm/unified.h -msoft-float -gdwarf-2     
 -Wa,-march=all -DTEXT_OFFSET=0x00008000   
 -c -o arch/arm/boot/compressed/head.o arch/arm/boot/compressed/head.S

(3) 依赖arch/arm/boot/compressed/piggy.gzip.o
arch/arm/boot/Image经过gzip压缩后生成piggy.gzip,再借助piggy.gzip.S一起编译出的ELF可链接文件arch/arm/boot/compressed/ piggy.gzip.o。

# arch/arm/boot /compressed/Makefile

#suffix_$(CONFIG_KERNEL_GZIP) = gzip       //我使用的.config文件CONFIG_KERNEL_GZIP=y

$(obj)/piggy.$(suffix_y): $(obj)/../Image FORCE
	$(call if_changed,$(suffix_y))

$(obj)/piggy.$(suffix_y).o:  $(obj)/piggy.$(suffix_y) FORCE

arch/arm/boot/compressed/ piggy.gzip.o的构建规则同arch/arm/boot/compressed/head.o。

下面只给出编译时实际执行的命令:

  (cat arch/arm/boot/compressed/../Image | gzip -f -9 > arch/arm/boot/compressed/piggy.gzip) 
  || (rm -f arch/arm/boot/compressed/piggy.gzip ; false)
  
 arm-linux-gcc -Wp,-MD,arch/arm/boot/compressed/.piggy.gzip.o.d  -nostdinc -isystem 
 /home/hh/opt/FriendlyARM/toolschain/4.5.1/bin/../lib/gcc/arm-none-linux-gnueabi/4.5.1/include 
 -I/home/hh/linux-2.6.38/arch/arm/include -Iinclude  -include include/generated/autoconf.h -D__KERNEL__ 
 -mlittle-endian -Iarch/arm/mach-s3c64xx/include -Iarch/arm/plat-samsung/include -D__ASSEMBLY__ 
 -mabi=aapcs-linux -mno-thumb-interwork -funwind-tables  -D__LINUX_ARM_ARCH__=6 -march=armv6k 
 -mtune=arm1136j-s -include asm/unified.h -msoft-float -gdwarf-2     
 -Wa,-march=all    -c -o arch/arm/boot/compressed/piggy.gzip.o arch/arm/boot/compressed/piggy.gzip.S

(4)依赖arch/arm/boot/compressed/misc.o和 依赖arch/arm/boot/compressed/decompress.c
上面两个依赖是内核自解压的实现代码,真正的实现代码在arch/arm/boot/compressed/misc.c中。
下面只给出编译时实际执行的命令:

 arm-linux-gcc -Wp,-MD,arch/arm/boot/compressed/.misc.o.d  -nostdinc -isystem 
 /home/hh/opt/FriendlyARM/toolschain/4.5.1/bin/../lib/gcc/arm-none-linux-gnueabi/4.5.1/include 
 -I/home/hh/linux-2.6.38/arch/arm/include -Iinclude  -include include/generated/autoconf.h -D__KERNEL__ 
 -mlittle-endian -Iarch/arm/mach-s3c64xx/include -Iarch/arm/plat-samsung/include -Wall -Wundef -Wstrict-prototypes
  -Wno-trigraphs -fno-strict-aliasing -fno-common -Werror-implicit-function-declaration -Wno-format-security 
  -fno-delete-null-pointer-checks -Os -marm -fno-dwarf2-cfi-asm -mabi=aapcs-linux -mno-thumb-interwork 
  -funwind-tables -D__LINUX_ARM_ARCH__=6 -march=armv6k -mtune=arm1136j-s -msoft-float -Uarm 
  -Wframe-larger-than=1024 -fno-stack-protector -fomit-frame-pointer -g -Wdeclaration-after-statement 
  -Wno-pointer-sign -fno-strict-overflow -fconserve-stack -DCC_HAVE_ASM_GOTO -fpic -fno-builtin    
  -D"KBUILD_STR(s)=#s" -D"KBUILD_BASENAME=KBUILD_STR(misc)"  -D"KBUILD_MODNAME=KBUILD_STR(misc)" 
  -c -o arch/arm/boot/compressed/misc.o arch/arm/boot/compressed/misc.c
 
bin/arm-linux-gcc -Wp,-MD,arch/arm/boot/compressed/.decompress.o.d  -nostdinc -isystem 
/home/hh/opt/FriendlyARM/toolschain/4.5.1/bin/../lib/gcc/arm-none-linux-gnueabi/4.5.1/include 
-I/home/hh/linux-2.6.38/arch/arm/include -Iinclude  -include include/generated/autoconf.h -D__KERNEL__ 
-mlittle-endian -Iarch/arm/mach-s3c64xx/include -Iarch/arm/plat-samsung/include -Wall -Wundef -Wstrict-prototypes 
-Wno-trigraphs -fno-strict-aliasing -fno-common -Werror-implicit-function-declaration -Wno-format-security 
-fno-delete-null-pointer-checks -Os -marm -fno-dwarf2-cfi-asm -mabi=aapcs-linux -mno-thumb-interwork 
-funwind-tables -D__LINUX_ARM_ARCH__=6 -march=armv6k -mtune=arm1136j-s -msoft-float -Uarm 
-Wframe-larger-than=1024 -fno-stack-protector -fomit-frame-pointer -g -Wdeclaration-after-statement 
-Wno-pointer-sign -fno-strict-overflow -fconserve-stack -DCC_HAVE_ASM_GOTO -fpic -fno-builtin    -D"KBUILD_STR(s)=#s" -D"KBUILD_BASENAME=KBUILD_STR(decompress)"  -D"KBUILD_MODNAME=KBUILD_STR(decompress)" 
-c -o arch/arm/boot/compressed/decompress.o arch/arm/boot/compressed/decompress.c

(5)依赖arch/arm/boot/compressed/lib1funcs.o
lib1funcs.o它的构建规则同样定义在scripts/Makefile.build中,arch/arm/boot/compressed/lib1funcs.S的构建规则定义在arch/arm/boot /compressed/Makefile 中

#  scripts/Makefile.build
quiet_cmd_as_o_S = AS $(quiet_modtag)  $@
cmd_as_o_S       = $(CC) $(a_flags) -c -o $@ $<

$(obj)/%.o: $(src)/%.S FORCE
	$(call if_changed_dep,as_o_S)

# arch/arm/boot/compressed/Makefile
lib1funcs = $(obj)/lib1funcs.o

$(obj)/lib1funcs.S: $(srctree)/arch/$(SRCARCH)/lib/lib1funcs.S FORCE
	$(call cmd,shipped)

下面只给出编译时实际执行的命令:

cat /home/hh/linux-2.6.38/arch/arm/lib/lib1funcs.S > arch/arm/boot/compressed/lib1funcs.S
  
arm-linux-gcc -Wp,-MD,arch/arm/boot/compressed/.lib1funcs.o.d  -nostdinc -isystem /home/hh/opt/FriendlyARM/toolschain/4.5.1/bin/../lib/gcc/arm-none-linux-gnueabi/4.5.1/include 
-I/home/hh/linux-2.6.38/arch/arm/include -Iinclude  -include include/generated/autoconf.h -D__KERNEL__ 
-mlittle-endian -Iarch/arm/mach-s3c64xx/include -Iarch/arm/plat-samsung/include -D__ASSEMBLY__ 
-mabi=aapcs-linux -mno-thumb-interwork -funwind-tables  -D__LINUX_ARM_ARCH__=6 -march=armv6k 
-mtune=arm1136j-s -include asm/unified.h -msoft-float -gdwarf-2     
-Wa,-march=all   -c -o arch/arm/boot/compressed/lib1funcs.o arch/arm/boot/compressed/lib1funcs.S

(6)arch/arm/boot/compressed/vmlinux的构建

# arch/arm/boot/compressed/Makefile
$(obj)/vmlinux: $(obj)/vmlinux.lds $(obj)/$(HEAD) $(obj)/piggy.$(suffix_y).o \
	 	$(addprefix $(obj)/, $(OBJS)) $(lib1funcs) FORCE
	$(call if_changed,ld)
	@:

没什么好讲的,直接给出编译时实际执行的命令:

  /home/hh/opt/FriendlyARM/toolschain/4.5.1/bin/arm-linux-ld -EL    --defsym zreladdr=0x50008000 -p 
  --no-undefined -X -T arch/arm/boot/compressed/vmlinux.lds arch/arm/boot/compressed/head.o 
  arch/arm/boot/compressed/piggy.gzip.o arch/arm/boot/compressed/misc.o arch/arm/boot/compressed/decompress.o 
  arch/arm/boot/compressed/lib1funcs.o -o arch/arm/boot/compressed/vmlinux 

小结:
其实arch/arm/boot/compressed/vmlinux是由几个依赖根据arch/arm/boot/compressed/vmlinux.lds 脚本链接而成的:

  • $ (obj)/$ (HEAD): arch/arm/boot/compressed/head.o,在链接时处于vmlinux的最前面,其主要作用就是做一些必要的初始化工作,如初始化CPU、中断描述符表IDT 和内存页目录表GDT等等,最后跳到misc.c中的decompress_kernel函数进行内核的自解压工作。
  • $ (addprefix $ (obj)/, $ (OBJS)): arch/arm/boot/compressed/ misc.o和arch/arm/boot/compressed/ decompress.o,位于head.o之后,是内核自解压的实现代码。
  • 假定是gzip模式压缩:$ (obj)/piggy.$(suffix_y).o: arch/arm/boot/compressed/ piggy.gzip.o,其实是arch/arm/boot/Image经过gzip压缩后生成的(将arch/arm/boot/Image文件cat到标准输出,并通过管道传入gzip命令(gzip -n -f -9 )的标准输入,最后将gzip的输出重定向到目标piggy.gzip),再借助piggy.gzip.S一起编译出的ELF可链接文件。这个piggy.gzip文件有一个重要的特性:最后的四个字节,是文件压缩前的大小数据,存放格式是小端模式。这个数据在zImage自解压时会被用于程序得到内核解压后所需要的空间。

3.2 arch/arm/boot/zImage的构建

# arch/arm/boot/compressed/Makefile
$(obj)/zImage:	$(obj)/compressed/vmlinux FORCE
	$(call if_changed,objcopy)
	@echo '  Kernel: $@ is ready'

没什么好讲的,就是用objcopy工具去生成arch/arm/boot/zImage。直接给出编译时实际执行的命令:

arm-linux-objcopy -O binary -R .comment -S  arch/arm/boot/compressed/vmlinux arch/arm/boot/zImage
  Kernel: arch/arm/boot/zImage is ready

四. arch/arm/boot/Image目标及其构建规则

# arch/arm/Makefile 
zImage Image xipImage bootpImage uImage: vmlinux
	$(Q)$(MAKE) $(build)=$(boot) MACHINE=$(MACHINE) $(boot)/$@
	
# arch/arm/boot/Makefile 
$(obj)/Image: vmlinux FORCE
	$(call if_changed,objcopy)
	@echo '  Kernel: $@ is ready'

由上面规则可知。生成目标Image时调用了cmd_objcopy变量定义的命令。如下:

# scripts/Makefile.lib

# Objcopy
# ---------------------------------------------------------------------------

quiet_cmd_objcopy = OBJCOPY $@
cmd_objcopy = $(OBJCOPY) $(OBJCOPYFLAGS) $(OBJCOPYFLAGS_$(@F)) $< $@

编译过程很简单,就是调用了arm-linux-objcopy把elf格式文件vmlinux(真正的内核原始镜像文件)编译成Image。其中arm-linux-objcopy被用来复制一个目标文件的内容到另一个文件中。此选项可以进行格式的转换.在实际编程的,用的最多的就是将ELF格式的可执行文件转换为二进制文件。其中 -O binary 是输出的二进制格式文件;-S是不从源文件中复制重定位信息和符号信息到目标文件中。

下面给出编译过程打印:

arm-linux-objcopy -O binary -R .comment -S  vmlinux arch/arm/boot/Image
Kernel: arch/arm/boot/Image is ready

五. 总结

在这里插入图片描述

posted on 2022-11-02 22:23  BSP-路人甲  阅读(2586)  评论(0编辑  收藏  举报

导航