一 编译过程
仍然以2410的编译说明,执行如下步骤
(1)主Makefile修改变量如下
ARCH := arm
CROSS_COMPILE := arm-linux-
(2)make s3c2410_defconfig
(3)make zImage
最终生成用于uboot启动的内核应该是uImage,uImage是zImage通过uboot的mkimage工具加上一个文件头生成的,这里只分析到zImage。
二 make s3c2410_defconfig分析
主Makefile下有如下定义:
%config: scripts_basic outputmakefile FORCE
$(Q)$(MAKE) $(build)=scripts/kconfig $@
可以转化为
s3c2410_defconfig: scripts_basic outputmakefile FORCE
make -f ./scripts/Makefile.build obj=scripts/kconfig s3c2410_defconfig
其中scripts_basic用于生成编译内核使用的fixdep程序,outputmakefile和FORCE这里为空。
然后执行scripts/Makefile.build里面的目标,具体的目标包含在scripts/Kconfig/Makefile中,如下语句:
%_defconfig: $(obj)/conf
$(Q)$< $(silent) --defconfig=arch/$(SRCARCH)/configs/$@ $(Kconfig)
可以转化为
s3c2410_defconfig: scripts/kconfig/conf
scripts/kconfig/conf -s --defconfig=arch/arm/configs/s3c2410_defconfig Kconfig
三 make zImage分析
(1)zImage的
在主makefile中包含arch/arm/Makefile,并有如下定义,其中zImage为BOOT_TARGETS的一部分。
$(BOOT_TARGETS): vmlinux
$(Q)$(MAKE) $(build)=$(boot) MACHINE=$(MACHINE) $(boot)/$@
可以转化为
zImage: vmlinux
make -f ./scripts/Makefile.build obj=arch/arm/boot MACHINE=arch/arm/mach-s3c24xx arch/arm/boot/zImage
进入scripts/Makefile.build执行,scripts/Makefile.build包含了arch/arm/boot/Makefile,其中有如下定义:
$(obj)/zImage: $(obj)/compressed/vmlinux FORCE
$(call if_changed,objcopy)
@$(kecho) ' Kernel: $@ is ready'
可以转换为:
arch/arm/boot/zImage : arch/arm/boot/compressed/vmlinux FORCE
$(call if_changed,objcopy)
相当于执行cmd_objcopy,scripts/Makefile.lib中如下定义:
cmd_objcopy = $(OBJCOPY) $(OBJCOPYFLAGS) $(OBJCOPYFLAGS_$(@F)) $< $@
相当于之心arm-linux-objcopy -O binary -R .comment -S arch/arm/boot/compressed/vmlinux arch/arm/boot/zImage
实质是把elf格式的压缩的vmlinux转化为二进制格式的zImage
总结:生成需要arch/arm/boot/zImage,需要分析arch/arm/boot/compressed/vmlinux
(2)arch/arm/boot/compressed/vmlinux的生成
arch/arm/boot/Makefile有标号如下:
$(obj)/compressed/vmlinux: $(obj)/Image FORCE
$(Q)$(MAKE) $(build)=$(obj)/compressed $@
可以转换为
arch/arm/boot/compressed/vmlinux : arch/arm/boot/Image
make -f ./scripts/Makefile.build obj=arch/arm/boot/compressed arch/arm/boot/compressed/vmlinux
这次执行的Makefile.build包含了arch/arm/boot/compressed/Makefile,并在其中有如下定义:
$(obj)/vmlinux: $(obj)/vmlinux.lds $(obj)/$(HEAD) $(obj)/piggy.$(suffix_y).o
$(addprefix $(obj)/, $(OBJS)) $(lib1funcs) $(ashldi3)
$(bswapsdi2) FORCE
@$(check_for_multiple_zreladdr)
$(call if_changed,ld)
@$(check_for_bad_syms)
实质就是执行了cmd_ld,即如下链接命令:
cmd_ld = arm-linux-ld --defsym _kernel_bss_size=$(KBSS_SZ) --defsym zreladdr=$(ZRELADDR) --be8 -p --no-undefined -X -T
arch/arm/boot/compressed/vmlinux.lds arch/arm/boot/compressed/vmlinux.lds arch/arm/boot/compressed/head.o arch/arm/boot/compressed/piggy.gzip.o arch/arm/boot/compressed/$(OBJS)) arch/arm/boot/compressed/lib1funcs.o arch/arm/boot/compressed/ashldi3.o arch/arm/boot/compressed/bswapsdi2.o -o arch/arm/boot/compressed/vmlinux
总结 :生成需要arch/arm/boot/compressed/vmlinux,需要分析一些目标文件,下面分析piggy.o
注:有定义:suffix_$(CONFIG_KERNEL_GZIP) = gzip,当前suffix_y = gzip
(3) piggy.gzip.o的生成
在上一部分"arch/arm/boot/compressed/vmlinux的生成"中,$(obj)/piggy.$(suffix_y).曾经作为arch/arm/boot/compressed/vmlinux的依赖出现,所以会先生成这个$(obj)/piggy.$(suffix_y),即piggy.gzip.o,会依据scripts/Makefile.build的%.o的标号,由piggy.gzip.S生成piggy.gzip.o。实质piggy.gzip.S的内容就是piggy.gzip。
有如下定义
$(obj)/piggy.$(suffix_y): $(obj)/../Image FORCE
$(call if_changed,$(suffix_y))
可以转化为:
arch/arm/boot/compressed/piggy.gzip : arch/arm/boot/Image
$(call if_changed,gzip)
相当于执行cmd_gzip,在scripts/Makefile.lib有如下定义:
cmd_gzip = (cat $(filter-out FORCE,$^) | gzip -n -f -9 > $@) ||
(rm -f $@ ; false)
可以转化为:
cmd_zip = (cat arch/arm/boot/Image) | gzip -n -f -9 > arch/arm/boot/compressed/piggy.gzip || (rm -f arch/arm/boot/compressed/piggy.gzip; false)
将arch/arm/boot/Image压缩成piggy.gzip的命令。
(4)arch/arm/boot/Image的生成
arch/arm/boot/Image在步骤2"arch/arm/boot/compressed/vmlinux的生成"中已经是arch/arm/boot/compressed/vmlinux的依赖条件,那时候的obj为arch/arm/boot,所以包含了arch/arm/boot/Image/Makefile,并在其中有如下定义:
$(obj)/Image: vmlinux FORCE
$(call if_changed,objcopy)
@$(kecho) ' Kernel: $@ is ready'
可以转换为:
arch/arm/boot/Image : vmlinux
$(call if_changed,objcopy)
根据前面分析一直知道是一个将vmlinux这个elf格式的文件转化为二进制格式的Image
接下来就是vmlinux的生成,没有目录,说明是主目录下的vmlinux
(5)vmlinux的生成
在步骤1中zImage的生成的依赖为vmlinux,当时的目录恰好是主Makefile,在主Makefile有定义,恰好是链接目标文件的过程,这里不再详细说明,详见linux-2.6.22.6的分析。
(6)uImage生成的总结
uImage生成步骤与linux-2.6.22.6基本一致。如下:
(a)生成基本的目标文件
(b)将基本的目标文件链接成vmlinux
(c)将vmlinux这个elf格式的文件转化为直接可以运行的arch/arm/boot/Image
(d)将arch/arm/boot/Image压缩为arch/arm/boot/compressed/piggy.gzip.o
(e)将一些解压信息和arch/arm/boot/compressed/vmlinux/piggy.gzip.o一起链接为arch/arm/boot/compressed/vmlinux。
(f)将arch/arm/boot/compressed/vmlinux这个elf格式文件转化为可直接运行的arch/arm/boot/zImage
(g)用mkimage将zImage转化为uImage
上一篇:u-boot移植之一 uboot-2015.04 Makefile分析
下一篇:linux内核移植之二 linux-4.1.4的移植过程
推荐阅读最新更新时间:2024-11-09 12:42