本文档是根据自己对makefile理解整理的笔记,文中如有错误,还请不吝留言指正,不胜感激!
基于u-boot 1.1.6版本, 文中所述‘顶层目录’为/u-boot-1.1.6/
在u-boot 顶层目录下的Makefile中第240行起第一个目标:
ALL = $(obj)u-boot.srec $(obj)u-boot.bin $(obj)System.map $(U_BOOT_NAND) all: $(ALL)
U_BOOT_NAND 为空 obj为空,上面的语句展开为:
all: u-boot.srec u-boot.bin System.map
Makefile中下面的三条语句
$(obj)u-boot.hex: $(obj)u-boot
$(OBJCOPY) ${OBJCFLAGS} -O ihex $< $@
$(obj)u-boot.srec: $(obj)u-boot
$(OBJCOPY) ${OBJCFLAGS} -O srec $< $@
$(obj)u-boot.bin: $(obj)u-boot
$(OBJCOPY) ${OBJCFLAGS} -O binary $< $@
#两个变量在顶层的config.mk中有定义:
OBJCOPY = $(CROSS_COMPILE)objcopy #顶层Makefile中定义CROSS_COMPILE ifeq ($(ARCH),arm) CROSS_COMPILE = arm-linux- endif #在include/config.mk中定义 ARCH = arm CPU = arm920t BOARD = 100ask24x0 SOC = s3c24x0 OBJCFLAGS += --gap-fill=0xff
#--gap-fill = 0xff是objcopy的参数,表示在拷贝过程中,用0xff来填充段与段之间的空隙
#objcopy 文件格式转换
#例如:
#arm-linux-objcopy -O <format> src_file des_file
# arm-linux-objcopy: arm平台格式转换
# -O:创建一个输出文件
# format:指定输出文件格式为 ihex: HEX格式文件
# srec: S-record格式文件
# binary:BIN格式文件
# des_file:输出文件名
# src_file:源文件名
# $< :第一个依赖文件
# $@ :目标文件
根据上面变量的赋值,展开为:
u-boot.hex: u-boot
arm-linux-objcopy --gap-fill=0xff -O ihex u-boot u-boot.hex
u-boot.srec: u-boot
arm-linux-objcopy --gap-fill=0xff -O srec u-boot u-boot.srec
u-boot.bin: u-boot
arm-linux-objcopy --gap-fill=0xff -O binary u-boot u-boot.bin
Makefile中下面的语句
$(obj)u-boot.dis: $(obj)u-boot
$(OBJDUMP) -d $< > $@
#OBJDUMP在顶层的config.mk中有定义:
OBJDUMP = $(CROSS_COMPILE)objdump
#上面的语句展开为: u-boot.dis: u-boot arm-linux-objdump -d u-boot > u-boot.dis
下面摘出链接语句的部分来分析
cd $(LNDIR) && $(LD) $(LDFLAGS) $$UNDEF_SYM $(__OBJS)
--start-group $(__LIBS) --end-group $(PLATFORM_LIBS)
-Map u-boot.map -o u-boot
#根据顶层Makefile中定义LNDIR := $(OBJTREE) OBJTREE := $(CURDIR)可知LNDIR即为当前目录
#顶层的config.mk中定义:
LD = $(CROSS_COMPILE)ld
LDFLAGS += -Bstatic -T $(LDSCRIPT) -Ttext $(TEXT_BASE) $(PLATFORM_LDFLAGS)
#顶层Makefile中定义
LDSCRIPT := $(TOPDIR)/board/$(BOARDDIR)/u-boot.lds
#在board/100ask24x0/config.mk中有:
TEXT_BASE = 0x33F80000
#顶层Makefile中定义
PLATFORM_LDFLAGS =
LIBS = lib_generic/libgeneric.a
LIBS += board/$(BOARDDIR)/lib$(BOARD).a
LIBS += cpu/$(CPU)/lib$(CPU).a
ifdef SOC
LIBS += cpu/$(CPU)/$(SOC)/lib$(SOC).a
endif
LIBS += lib_$(ARCH)/lib$(ARCH).a
LIBS += fs/cramfs/libcramfs.a fs/fat/libfat.a fs/fdos/libfdos.a fs/jffs2/libjffs2.a
fs/reiserfs/libreiserfs.a fs/ext2/libext2fs.a
LIBS += net/libnet.a
LIBS += disk/libdisk.a
LIBS += rtc/librtc.a
LIBS += dtt/libdtt.a
LIBS += drivers/libdrivers.a
LIBS += drivers/nand/libnand.a
LIBS += drivers/nand_legacy/libnand_legacy.a
LIBS += drivers/usb/libusb.a
LIBS += drivers/sk98lin/libsk98lin.a
LIBS += common/libcommon.a
LIBS += $(BOARDLIBS)
cd .
arm-linux-ld -Bstatic -T/board/100ask24x0/u-boot.lds -Ttext 0x33F80000 $$UNDEF_SYM cpu/arm920t/start.o --start-group $(__LIBS) --end-group -Map u-boot.map -o u-boot
命令 静态链接 链接脚本 代码段链接地址 OBJ文件 lib文件 内存映射文件 目标
下面的两个目标暂时没完全读懂啥意思,先贴出来,多谢大神们留言解答
$(obj)u-boot.img: $(obj)u-boot.bin
./tools/mkimage -A $(ARCH) -T firmware -C none
-a $(TEXT_BASE) -e 0
-n $(shell sed -n -e 's/.*U_BOOT_VERSION//p' $(VERSION_FILE) |
sed -e 's/"[ ]*$$/ for $(BOARD) board"/')
-d $< $@
$(obj)u-boot: depend version $(SUBDIRS) $(OBJS) $(LIBS) $(LDSCRIPT)
UNDEF_SYM=`$(OBJDUMP) -x $(LIBS) |sed -n -e 's/.*(__u_boot_cmd_.*)/-u1/p'|sort|uniq`;
cd $(LNDIR) && $(LD) $(LDFLAGS) $$UNDEF_SYM $(__OBJS)
--start-group $(__LIBS) --end-group $(PLATFORM_LIBS)
-Map u-boot.map -o u-boot