TQ2440的U-Boot-1.1.6Makefile结构浅析

2013-6-4

其实基于TQ2440开发板U-BOOT的移植,网上已经有很多教程了,我也亲手移植过,那是一年多以前的事情了,现在也已忘得差不多,当时也就是按照移植手册一步一步来,更多关注的是“怎么样”,至于具体的“为什么”要这么移植,却没有进行分析,上午面试的时候被深深鄙视,觉着还是要一步一步来,扎实点,移植不能只着眼于“怎么样”,还要经常问问自己“为什么”要这么搞,原因在哪里。还有,“实践才能够真正的掌握”,这是上午面试官给我说的话

 

1. U-BOOT-1.1.6下载地址

 http://download.csdn.net/detail/forsakening/5517815

ps:不知道为何官方ftp网站上不去了

 

2. 阅读README

a) 获取和arm s3c2440相关的文件 (README中介绍了uboot文件夹下的内容)

- board  Board dependent files
- common Misc architecture independent functions
- cpu  CPU specific files
  - arm920t Files specific to ARM 920 CPUs
    - s3c24x0 Files specific to Samsung S3C24X0 CPUs
- disk  Code for disk drive partition handling
- doc  Documentation (don't expect too much)
- drivers Commonly used device drivers
- dtt  Digital Thermometer and Thermostat drivers
- examples Example code for standalone applications, etc.
- include Header Files
- lib_arm Files generic to ARM  architecture
- lib_generic Files generic to all  architectures
- net  Networking code
- post  Power On Self Test
- rtc  Real Time Clock drivers
- tools  Tools to build S-Record or U-Boot images, etc.

 

b) 如何编译U-boot

U-Boot is intended to be  simple  to  build.  After  installing	 the
sources	 you must configure U-Boot for one specific board type. This
is done by typing:

	make NAME_config

Finally, type "make all", and you should get some working U-Boot
images ready for download to / installation on your system:

- "u-boot.bin" is a raw binary image
- "u-boot" is an image in ELF binary format
- "u-boot.srec" is in Motorola S-Record format

也就是说分为两步1) make NAME_config ; 2) make all

 

若是新的硬件,u-boot没有直接支持的话,那就要修改u-boot,如何修改,README文档也给了简单的介绍:

If the system board that you have is not listed, then you will need
to port U-Boot to your hardware platform. To do this, follow these
steps:

1.  Add a new configuration option for your board to the toplevel
    "Makefile" and to the "MAKEALL" script, using the existing
    entries as examples. Note that here and at many other places
    boards and other names are listed in alphabetical sort order. Please
    keep this order.
2.  Create a new directory to hold your board specific code. Add any
    files you need. In your board directory, you will need at least
    the "Makefile", a "<board>.c", "flash.c" and "u-boot.lds".
3.  Create a new configuration file "include/configs/<board>.h" for
    your board
3.  If you're porting U-Boot to a new CPU, then also create a new   /* 这里竟然标号错了,嘿嘿 官方的也有错哦~ */
    directory to hold your CPU specific code. Add any files you need.
4.  Run "make <board>_config" with your new name.
5.  Type "make", and you should get a working "u-boot.srec" file
    to be installed on your target system.
6.  Debug and solve any problems that might arise.
    [Of course, this last step is much harder than it sounds.]

其实README文档是最好的资料,比如

On ARM, the following registers are used:

	R0:	function argument word/integer result
	R1-R3:	function argument word
	R9:	GOT pointer
	R10:	stack limit (used only if stack checking if enabled)
	R11:	argument (frame) pointer
	R12:	temporary workspace
	R13:	stack pointer
	R14:	link register
	R15:	program counter

    ==> U-Boot will use R8 to hold a pointer to the global data

这里就解释了gd_t结构地址是保存在R8寄存器中的,相对应于代码:

#define DECLARE_GLOBAL_DATA_PTR     register volatile gd_t *gd asm ("r8")


3. 查看Makefile

make smdk2410_config做了些什么:

参考:http://www.cnblogs.com/heaad/archive/2010/07/17/1779806.html (uboot版本非1.1.6,用作参考,针对于mini2440开发板的,对于mkconfig讲解很细致)

已保存为本地文件:http://download.csdn.net/detail/forsakening/5518051

总结一下make smdk2410_config主要做了以下事情:

1) 建立符号链接:

ln -s asm-arm asm
ln -s arch-smdk24x0 asm-arm/arch
ln -s proc-armv asm-arm/proc

2) 构建include/config.mk文件

ARCH   = arm
CPU    = arm920t
BOARD  = smdk2410
SOC    = s3c24x0

3) 创建与目标板相关的文件include/config.h

/* Automatically generated - do not edit */
#include <configs/smdk2410.h>

 

make all做了些什么:

ALL = $(obj)u-boot.srec $(obj)u-boot.bin $(obj)System.map $(U_BOOT_NAND)

all:		$(ALL)

$(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 $< $@

$(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.dis:	$(obj)u-boot
		$(OBJDUMP) -d $< > $@

$(obj)u-boot:		depend version $(SUBDIRS) $(OBJS) $(LIBS) $(LDSCRIPT)
		UNDEF_SYM=`$(OBJDUMP) -x $(LIBS) |sed  -n -e 's/.*\(__u_boot_cmd_.*\)/-u\1/p'|sort|uniq`;\
		cd $(LNDIR) && $(LD) $(LDFLAGS) $$UNDEF_SYM $(__OBJS) \
			--start-group $(__LIBS) --end-group $(PLATFORM_LIBS) \
			-Map u-boot.map -o u-boot

make all的依赖文件是$(ALL),其最终的依赖文件是u-boot,这个生成的其实是个elf格式的文件,生成u-boot的这一串代码看起来非常复杂,还好网上有前辈已经解释:http://www.21ic.com/app/embed/201005/59363_3.htm

$(obj)u-boot:		depend version $(SUBDIRS) $(OBJS) $(LIBS) $(LDSCRIPT)
		UNDEF_SYM=`$(OBJDUMP) -x $(LIBS) |sed  -n -e 's/.*\(__u_boot_cmd_.*\)/-u\1/p'|sort|uniq`;\
		cd $(LNDIR) && $(LD) $(LDFLAGS) $$UNDEF_SYM $(__OBJS) \
			--start-group $(__LIBS) --end-group $(PLATFORM_LIBS) \
			-Map u-boot.map -o u-boot

为了搞清楚每个依赖文件是如何具体生产的,先弄清楚根目录下的config.mk和rules.mk两个文件:

makefile结构:
顶层\
	config.mk 依次包含和定义:
		PLATFORM_CPPFLAGS+= -D__ARM__  
		arm_config.mk : PLATFORM_CPPFLAGS += -DCONFIG_ARM -D__ARM__
		cpu/arm920t/config.mk :编译选项
		board/smdk2410/config.mk : TEXT_BASE = 0x33F80000	   
		LDSCRIPT := $(TOPDIR)/board/$(BOARDDIR)/u-boot.lds
		
		$(obj)%.s:	%.S
			$(CPP) $(AFLAGS) -o $@ $<
		$(obj)%.o:	%.S
			$(CC) $(AFLAGS) -c -o $@ $<
		$(obj)%.o:	%.c
			$(CC) $(CFLAGS) -c -o $@ $<
	总结:config.mk主要用来定义一些编译选项,如编译参数,连接地址,.o .c文件的生成方式
	
	rules.mk: 用来处理自动依赖depend
	_depend:	$(obj).depend

	$(obj).depend:	$(src)Makefile $(TOPDIR)/config.mk $(SRCS)
			@rm -f $@
			@for f in $(SRCS); do \
				g=`basename $$f | sed -e 's/\(.*\)\.\w/\1.o/'`; \
				$(CC) -M $(HOST_CFLAGS) $(CPPFLAGS) -MQ $(obj)$$g $$f >> $@ ; \
			done	
	参见:http://blog.csdn.net/lihancheng/article/details/4063408
1) depend
#遍历SUBDIRS下的每个目录,生成各个子目录的.depend文件,.depend列出每个目标文件的依赖文件。生成方法,调用每个子目录的make _depend
depend dep:
		for dir in $(SUBDIRS) ; do $(MAKE) -C $$dir _depend ; done
2)  version
#依赖目标version:生成版本信息到版本文件VERSION_FILE中
version:
		@echo -n "#define U_BOOT_VERSION \"U-Boot " > $(VERSION_FILE); \
		echo -n "$(U_BOOT_VERSION)" >> $(VERSION_FILE); \
		echo -n $(shell $(CONFIG_SHELL) $(TOPDIR)/tools/setlocalversion \
			 $(TOPDIR)) >> $(VERSION_FILE); \
		echo "\"" >> $(VERSION_FILE)
3) SUBDIRS
#伪目标SUBDIRS: 执行tools ,examples ,post,postcpu 子目录下面的make文件
#对应的执行指令为(以post为例):
#make -C post all
$(SUBDIRS):
		$(MAKE) -C $@ all
#进入post目录,执行当前目录下的make all命令,阅读makefile得到:
#/post/rules.mk
include $(TOPDIR)/config.mk

SRCS 	:= $(AOBJS:.o=.S) $(COBJS:.o=.c)
OBJS	:= $(addprefix $(obj),$(AOBJS) $(COBJS))
LIB	:= $(obj)$(LIB)

CPPFLAGS += -I$(TOPDIR)

all:	$(LIB)

$(LIB):	$(obj).depend $(OBJS)
	$(AR) $(ARFLAGS) $@ $(OBJS)

#所以,make all的时候,依赖于.depend 和AOBJS,COBJS,而AOBJS,COBJS这两个目标由.o确立,这个.o的建立规则在顶层config.mk中已经建立,所以一开始需要先生成.o文件,实际执行为:
#make[1]: Entering directory `/share/my-u-boot/my-u-boot-1.1.6/post'
arm-linux-gcc  -D__ASSEMBLY__ -g  -Os   -fno-strict-aliasing  -fno-common -ffixed-r8 -msoft-float  -D__KERNEL__ -DTEXT_BASE=0x33F80000  -I/share/my-u-boot/my-u-boot-1.1.6/include -fno-builtin -ffreestanding -nostdinc -isystem /opt/EmbedSky/4.3.3/bin/../lib/gcc/arm-none-linux-gnueabi/4.3.3/include -pipe  -DCONFIG_ARM -D__ARM__ -march=armv4 -mabi=apcs-gnu -c -o cache_8xx.o cache_8xx.S 可以看出即是由.S生成.o文件,这是顶层config.mk建立的规则
#然后再执行$(AR) $(ARFLAGS) $@ $(OBJS),实际执行为:
arm-linux-ar crv libpost.a cache_8xx.o cache.o ....
#即是生成libpost.a,因为$@代表了$(LIB),在本层的Makefile中定义LIB	= libpost.a
4) OBJS
#依赖目标$(OBJS),即cpu/arm920t/start.o,因为前面已经定义OBJS  = cpu/$(CPU)/start.o
$(OBJS):
		$(MAKE) -C cpu/$(CPU) $(if $(REMOTE_BUILD),$@,$(notdir $@))
#进入cpu/arm920t目录,执行make -C cpu/arm920t start.o,
#其实这里的makefile结构和前面post下的makefile结构差不多,包含两部分,一部分包含config.mk用于生成目标文件.o,另一部分是包含rules.mk用于生成依赖文件.depend
#命令实际执行为:
#arm-linux-gcc  -D__ASSEMBLY__ -g  -Os   -fno-strict-aliasing  -fno-common -ffixed-r8 -msoft-float  -D__KERNEL__ -DTEXT_BASE=0x33F80000  -I/share/my-u-boot/my-u-boot-1.1.6/include -fno-builtin -ffreestanding -nostdinc -isystem /opt/EmbedSky/4.3.3/bin/../lib/gcc/arm-none-linux-gnueabi/4.3.3/include -pipe  -DCONFIG_ARM -D__ARM__ -march=armv4 -mabi=apcs-gnu -c -o start.o start.S
5) LIBS
#依赖目标$(LIBS),这个目标太多,都是每个子目录的库文件*.a ,通过执行相应子目录下的make来完成:
$(LIBS):
		$(MAKE) -C $(dir $(subst $(obj),,$@))
#以LIBS  = lib_generic/libgeneric.a为例
#其中的makefile和前述差不多:1.包含顶层config.mk用于声明编译选项 2.第二部分为生成.depend
#其命令实际执行为:(先生成.o,在生成库.a文件)
#arm-linux-gcc -g  -Os   -fno-strict-aliasing  -fno-common -ffixed-r8 -msoft-float  -D__KERNEL__ -DTEXT_BASE=0x33F80000  -I/share/my-u-boot/my-u-boot-1.1.6/include -fno-builtin -ffreestanding -nostdinc -isystem /opt/EmbedSky/4.3.3/bin/../lib/gcc/arm-none-linux-gnueabi/4.3.3/include -pipe  -DCONFIG_ARM -D__ARM__ -march=armv4 -mabi=apcs-gnu -Wall -Wstrict-prototypes -c -o bzlib.o bzlib.c
生成库.a:
arm-linux-ar crv libgeneric.a bzlib.o ...
6) LDSCRIPT

前面已述,在config.mk中

LDSCRIPT := $(TOPDIR)/board/$(BOARDDIR)/u-boot.lds
7)  最终展开执行
最终展开为:
UNDEF_SYM=`arm-linux-objdump -x lib_generic/libgeneric.a board/smdk2410/libsmdk2410.a cpu/arm920t/libarm920t.a cpu/arm920t/s3c24x0/libs3c24x0.a lib_arm/libarm.a fs/cramfs/libcramfs.a fs/fat/libfat.a fs/fdos/libfdos.a fs/jffs2/libjffs2.a fs/reiserfs/libreiserfs.a fs/ext2/libext2fs.a net/libnet.a disk/libdisk.a rtc/librtc.a dtt/libdtt.a drivers/libdrivers.a drivers/nand/libnand.a drivers/nand_legacy/libnand_legacy.a drivers/sk98lin/libsk98lin.a post/libpost.a post/cpu/libcpu.a common/libcommon.a |sed  -n -e 's/.*\(__u_boot_cmd_.*\)/-u\1/p'|sort|uniq`;\
		cd /share/my-u-boot/my-u-boot-1.1.6 && arm-linux-ld -Bstatic -T /share/my-u-boot/my-u-boot-1.1.6/board/smdk2410/u-boot.lds -Ttext 0x33F80000  $UNDEF_SYM cpu/arm920t/start.o \
			--start-group lib_generic/libgeneric.a board/smdk2410/libsmdk2410.a cpu/arm920t/libarm920t.a cpu/arm920t/s3c24x0/libs3c24x0.a lib_arm/libarm.a fs/cramfs/libcramfs.a fs/fat/libfat.a fs/fdos/libfdos.a fs/jffs2/libjffs2.a fs/reiserfs/libreiserfs.a fs/ext2/libext2fs.a net/libnet.a disk/libdisk.a rtc/librtc.a dtt/libdtt.a drivers/libdrivers.a drivers/nand/libnand.a drivers/nand_legacy/libnand_legacy.a drivers/sk98lin/libsk98lin.a post/libpost.a post/cpu/libcpu.a common/libcommon.a --end-group -L /opt/EmbedSky/4.3.3/bin/../lib/gcc/arm-none-linux-gnueabi/4.3.3/armv4t -lgcc \
			-Map u-boot.map -o u-boot
arm-linux-objcopy --gap-fill=0xff -O srec u-boot u-boot.srec
arm-linux-objcopy --gap-fill=0xff -O binary u-boot u-boot.bin

注:具体是如何展开的,需要对make及shell语法要求比较高,水平有限,只能大概知道编译的整个过程是什么样的,具体语法分析搞不定- -! 

 

4. 链接

关于start.s中的__bss_start 等,这些都是由编译器产生的,如__bss_start ,这是在.lds文件中定义的

	. = ALIGN(4);
	__bss_start = .;
	.bss : { *(.bss) }
	_end = .;

说明__bss_start是.bss段的开始位置,这个位置是如何确定的呢?
---->在执行完上述操作之后,会生成u-boot.bin,同时也会生成u-boot.map文件,.map文件是uboot中所有符号在运行状态的地址,在这里我们可以看见编译完成的程序在内存中的位置是什么样的

.u_boot_cmd    0x33f96148       0x60 common/libcommon.a(command.o)
                0x33f96160                __u_boot_cmd_echo
                0x33f96190                __u_boot_cmd_question_mark
                0x33f96148                __u_boot_cmd_version
                0x33f96178                __u_boot_cmd_help
                0x33f961a8                __u_boot_cmd_end = .
                0x33f961a8                . = ALIGN (0x4)
                0x33f961a8                __bss_start = .

.bss            0x33f961a8     0x4724
 *(.bss)
 .bss           0x33f961a8        0x0 cpu/arm920t/start.o
 .bss           0x33f961a8        0x0 board/smdk2410/libsmdk2410.a(lowlevel_init.o)

而.bss .u_boot_cmd段都是编译器自动生成(?应该是有相应的编译选项去实现? 不是太确定),并且根据链接地址及链接文件编译完成的

 

5.  u-boot内存使用情况

 

6. 参考

【1】uboot的结构分析:http://webjs.blog.sohu.com/192797903.html
【2】depend/自动生成依赖: http://blog.csdn.net/lihancheng/article/details/4063408

【3】相当详细的uboot编译过程解析:http://www.21ic.com/app/embed/201005/59363_3.htm

总结:想要彻底看懂uboot的makefile结构,makefile规则,shell,sed(用的好多),各种编译选项,链接(ld),打包(ar),map文件,lds文件,depend自动生成,这些都是必不可少的呀!

 

未解决的问题:

1. uboot_cmd段是如何生成的?以及got段有什么作用?

2. 在编译生成的map文件中,每个符号对应于一个地址,为什么代码(如start.s)可以识别这个符号呢,如-_bss_start  ,在符号表里面是存在的,start.s中也用到了这个地址,但是start.s要如何获得呢?代码在运行的时候,去哪里搜寻这个符号?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值