u-boot编译流程简要分析
0、说明
本文基于U-Boot 2022.01-v2.07
版本进行分析。
1、u-boot的编译过程
-
1.1、参考资料
u-boot的编译系统介绍可以参考:U-Boot 完全分析与移植,这篇文章。 -
1.2、编译入口
在执行make
编译u-boot时,由于没有指定目标,因此执行的是顶层Makefile中的第一个目标。在顶层Makefile中,从文件开头往下查找第一个编译目标,会找到如下部分:# That's our default target when none is given on the command line PHONY := _all _all:
会发现这里的
_all
没有做任何操作,也没有任何依赖,继续往下找,会发现_all
会重载:# Use make M=dir to specify directory of external module to build # Old syntax make ... SUBDIRS=$PWD is still supported # Setting the environment variable KBUILD_EXTMOD take precedence ifdef SUBDIRS KBUILD_EXTMOD ?= $(SUBDIRS) endififeq ("$(origin M)", "command line") KBUILD_EXTMOD := $(M) endif# If building an external module we do not care about the all: rule # but instead _all depend on modules PHONY += all ifeq ($(KBUILD_EXTMOD),) _all: all else _all: modules endif
由于执行
make
是我们没有指定M=dir
,因此这里的KBUILD_EXTMOD
就为空,ifeq ($(KBUILD_EXTMOD),)
就为真,最终_all:
被重载为_all: all
,如下:# 找到的第一个目标 PHONY := _all _all:# KBUILD_EXTMOD为空,进行了重载 PHONY += all _all: all
这里
_all
依赖于all
,需要确定all
做了哪些事情,继续往下找:all: .binman_stamp inputs ifeq ($(CONFIG_BINMAN),y)$(call if_changed,binman) endif
这里会发现,
all
依赖于.binman_stamp
和inputs
,重点在inputs
:PHONY += inputs inputs: $(INPUTS-y)
继续看
INPUTS-y
的赋值:INPUTS-y += u-boot.srec u-boot.bin u-boot.sym System.map binary_size_check
INPUTS-y
有多个值,重点关注u-boot.bin
,其取决于uboot的配置项。在我的配置项中,其取值如下:u-boot.bin: u-boot-dtb.bin FORCE$(call if_changed,copy)
继续看
u-boot-dtb.bin
,就在它上面:u-boot-dtb.bin: u-boot-nodtb.bin dts/dt.dtb FORCE$(call if_changed,cat)
dts/dt.dtb
是来编译设备树的,重点看u-boot-nodtb.bin
:u-boot-nodtb.bin: u-boot FORCE$(call if_changed,objcopy_uboot)$(BOARD_SIZE_CHECK)
继续看
u-boot
:u-boot: $(u-boot-init) $(u-boot-main) $(u-boot-keep-syms-lto) u-boot.lds FORCE+$(call if_changed,u-boot__) ifeq ($(CONFIG_KALLSYMS),y)$(call cmd,smap)$(call cmd,u-boot__) common/system_map.o endif
u-boot-keep-syms-lto
不用管,u-boot.lds
是用于生成链接脚本的,暂且不管。重点看u-boot-init
和u-boot-main
:u-boot-init := $(head-y) u-boot-main := $(libs-y)
head-y
的定义在arch\arm\Makefile文件中,用于编译对应目录中的start.s文件:head-y := arch/arm/cpu/$(CPU)/start.o