个人学习笔记,不保证正确性

。
不过这做为CSAPP第七章的阅读材料非常合适,特别是u-boot中对PIC和GOT的利用。我现在只熟悉MIPS,所以也只阅读了u-boot中与MIPS相关的部分。
u-boot代码下载: ftp://ftp.denx.de/pub/u-boot/u-boot-2009.03.tar.bz2
涉及到的文件 u-boot-2009.03/cpu/mips/start.S
u-boot-2009.03/lib_mips/board.c
不过还要结合一个lds文件来看,比如:u-boot-2009.03/board/qemu-mips/u-boot.lds
先从start.S中的_start开始跑,然后会到board.c中的void board_init_f(ulong bootflag),然后再回到start.S中的relocate_code (addr_sp, id, addr);在relocate代码之前要做很多事情,比如RAM的初始化,调用relocate_code函数时那三个参数是怎么确定的。。。具体的代码就不详细的注释了,不过关注一个地方:
/* Initialize $gp.
*/
bal 1f
nop
.word _gp
1:
lw gp, 0(ra)
这也许算是个小技巧。可以利用这个ra做很多事情,比如某些时候你可以利用它来判断自己是在FLASH里面还是在RAM里面。
在注释relocate_code函数之前,先来点假设吧,假设你编译代码的时候指定的CONFIG_SYS_MONITOR_BASE是0xbfc00000,u-boot编译出来是PIC的,也就是说它可以被relocate到内存中的其他地方执行。假设relocate到内存中的地址是0x800xxxxx。
/*
* void relocate_code (addr_sp, gd, addr_moni)
*
* This "function" does not return, instead it continues in RAM
* after relocating the monitor code.
*
* a0 = addr_sp
* a1 = gd
* a2 = destination address
*/
.globl relocate_code
.ent relocate_code
relocate_code:
move sp, a0 /* Set new stack pointer */
#假设CONFIG_SYS_MONITOR_BASE是0xbfc00000。
li t0, CONFIG_SYS_MONITOR_BASE
#in_ram是个标号,此刻是相对于0xbfc00000。
la t3, in_ram
#关注lds文件,在链接的时候会确定uboot_end_data的值,此刻取值是从FLASH中。
lw t2, -12(t3) /* t2 <-- uboot_end_data */
move t1, a2
move s2, a2 /* s2 <-- destination address */
/*
* Fix $gp:
*
* New $gp = (Old $gp - CONFIG_SYS_MONITOR_BASE) + Destination Address
*/
#把以前的gp寄存器保存起来。
move t6, gp
#因为我们链接的时候指定了TEXT_BASE,所以可以算出gp相对于TEXT_BASE的偏移。
sub gp, CONFIG_SYS_MONITOR_BASE
#这里的a2,就是我们假设会relocate到内存中的起始地址0x800xxxxx,此刻我们的TEXT_BASE变了,所以gp的地址要调整,但是它相对于TEXT_BASE的偏移是一定了,因此我们可以利用它相对CONFIG_SYS_MONITOR_BASE的偏移求出新的RAM地址。
add gp, a2 /* gp now adjusted */
#当然,不光是gp,其他的代码也要调整,所以把这个要调整的偏移记录下来。
sub s1, gp, t6 /* s1 <-- relocation offset */
/*
* t0 = source address
* t1 = target address
* t2 = source end address
*/
/*
* Save destination address and size for later usage in flush_cache()
*/
move s0, a1 /* save gd in s0 */
move a0, t1 /* a0 <-- destination addr */
sub a1, t2, t0 /* a1 <-- size */
/* On the purple board we copy the code earlier in a special way
* in order to solve flash problems
*/
#下面这段循环的代码就是把整个uboot.bin从FLASH复制到RAM中。
#ifndef CONFIG_PURPLE
1:
lw t3, 0(t0)
sw t3, 0(t1)
addu t0, 4
ble t0, t2, 1b
addu t1, 4 /* delay slot */
#endif
/* If caches were enabled, we would have to flush them here.
*/
#这里有点像see mips run中讲的,CPU自己写指令到内存中,然后再执行这些指令时,需要对cache做一些事情。
/* a0 & a1 are already set up for flush_cache(start, size) */
la t9, flush_cache
jalr t9
nop
/* Jump to where we've relocated ourselves.
*/
#s2的值:0x800xxxxx,此处也是根据in_ram标号在FLASH中的偏移算出它在RAM中的地址。
addi t0, s2, in_ram - _start
#跳到RAM中执行代码。
jr t0
nop
#以下几个变量要结合lds文件看
.word _gp
.word _GLOBAL_OFFSET_TABLE_
.word uboot_end_data
.word uboot_end
.word num_got_entries
in_ram:
/*
* Now we want to update GOT.
*
* GOT[0] is reserved. GOT[1] is also reserved for the dynamic object
* generated by GNU ld. Skip these reserved entries from relocation.
*/
#下面是从RAM中取值,不是从FLASH中了,虽然值的大小是一样的。
lw t3, -4(t0) /* t3 <-- num_got_entries */
lw t4, -16(t0) /* t4 <-- _GLOBAL_OFFSET_TABLE_ */
lw t5, -20(t0) /* t5 <-- _gp */
#这里用另外的一种方法算偏移,为什么没有利用前面的s1?
sub t4, t5 /* compute offset*/
add t4, t4, gp /* t4 now holds relocated _GLOBAL_OFFSET_TABLE_ */
addi t4, t4, 8 /* Skipping first two entries. */
li t2, 2
#调整GOT中的内容
1:
lw t1, 0(t4)
beqz t1, 2f
add t1, s1
sw t1, 0(t4)
2:
addi t2, 1
blt t2, t3, 1b
addi t4, 4 /* delay slot */
/* Clear BSS.
*/
#这里是从RAM中取值,不过取得的值是相对于0xbfc00000,所以需要调整。
lw t1, -12(t0) /* t1 <-- uboot_end_data */
lw t2, -8(t0) /* t2 <-- uboot_end */
add t1, s1 /* adjust pointers */
add t2, s1
#把BSS段清0。
sub t1, 4
1:
addi t1, 4
bltl t1, t2, 1b
sw zero, 0(t1) /* delay slot */
move a0, s0 /* a0 <-- gd */
#board_init_r中的r应该代表的是RAM,前面提到的board_init_f中的f应该代表的是FLASH,到此刻为止,运行C代码的环境已经构建完成。
la t9, board_init_r
jr t9
move a1, s2 /* delay slot */
.end relocate_code
要完全理解,lds文件很重要,因此也贴上来:
/*
* (C) Copyright 2003
* Wolfgang Denk Engineering,
*
* See file CREDITS for list of people who contributed to this
* project.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*/
/*
OUTPUT_FORMAT("elf32-bigmips", "elf32-bigmips", "elf32-bigmips")
*/
OUTPUT_FORMAT("elf32-tradbigmips", "elf32-tradbigmips", "elf32-tradbigmips")
OUTPUT_ARCH(mips)
ENTRY(_start)
SECTIONS
{
. = 0x00000000;
. = ALIGN(4);
.text :
{
*(.text)
}
. = ALIGN(4);
.rodata : { *(.rodata) }
. = ALIGN(4);
.data : { *(.data) }
. = .;
_gp = ALIGN(16) +0x7ff0;
.got : {
__got_start = .;
*(.got)
__got_end = .;
}
. = ALIGN(4);
.sdata : { *(.sdata) }
. = .;
.u_boot_cmd : {
__u_boot_cmd_start = .;
*(.u_boot_cmd)
__u_boot_cmd_end = .;
}
uboot_end_data = .;
num_got_entries = (__got_end - __got_start) >> 2;
. = ALIGN(4);
.sbss : { *(.sbss) }
.bss : { *(.bss) . = ALIGN(4); }