Mini440之uboot移植流程分析(三)
所谓的relocation,就是重定位,u-boot运行后会将自身代码拷贝到SDRAM的另一个位置继续运行。
但基于以前的理解,一个完整可运行的bin文件,link时指定的链接地址,load时的加载地址,运行时的运行地址,这3个地址应该是一致的。
relocation后运行地址不同于加载地址,特别是链接地址,ARM的寻址会不会出现问题?
u-boot启动后会计算出一个靠近SDRAM顶端的地址,也就是gd->relocaddr,将自身代码拷贝到该地址,继续运行。
但是这样会有一个问题,relocation后u-boot的运行地址跟其链接地址不一致,compiler会在link时确定了其中全局变量的绝对地址,链接地址 加载地址 运行地址应该一致,这样看来,arm在寻址这些变量时找到的应该是relocation之前的地址,这样relocation就没有意义了!
因此我们在进行u-boot重定义需要包含代码段、rodata、data以及全局变量、函数的重定位。
在进行代码分析之前,建议你先看一下uboot的relocation原理详细分析。
一、u-boot重定位
1.1 u-boot重定位
我们已经分析到了_main(arch/arm/lib/crt0.S)中board_init_f函数的执行,接着继续分析_main。
#if ! defined(CONFIG_SPL_BUILD) /* * Set up intermediate environment (new sp and gd) and call * relocate_code(addr_moni). Trick here is that we'll return * 'here' but relocated. */ ldr sp, [r9, #GD_START_ADDR_SP] /* sp = gd->start_addr_sp */ #if defined(CONFIG_CPU_V7M) /* v7M forbids using SP as BIC destination */ mov r3, sp bic r3, r3, #7 mov sp, r3 #else bic sp, sp, #7 /* 8-byte alignment for ABI compliance */ #endif ldr r9, [r9, #GD_BD] /* r9 = gd->bd */ sub r9, r9, #GD_SIZE /* new GD is below bd */ adr lr, here ldr r0, [r9, #GD_RELOC_OFF] /* r0 = gd->reloc_off */ add lr, lr, r0 #if defined(CONFIG_CPU_V7M) orr lr, #1 /* As required by Thumb-only */ #endif ldr r0, [r9, #GD_RELOCADDR] /* r0 = gd->relocaddr */ b relocate_code here: /* * now relocate vectors */ bl relocate_vectors /* Set up final (full) environment */ bl c_runtime_cpu_setup /* we still call old routine here */ #endif
其中GD_START_ADDR_SP 定义如下:
#define GENERATED_GBL_DATA_SIZE 176 /* (sizeof(struct global_data) + 15) & ~15 @ */ #define GENERATED_BD_INFO_SIZE 80 /* (sizeof(struct bd_info) + 15) & ~15 @ */ #define GD_SIZE 168 /* sizeof(struct global_data) @ */ #define GD_BD 0 /* offsetof(struct global_data, bd) @ */ #define GD_RELOCADDR 44 /* offsetof(struct global_data, relocaddr) @ */ #define GD_RELOC_OFF 64 /* offsetof(struct global_data, reloc_off) @ */ #define GD_START_ADDR_SP 60 /* offsetof(struct global_data, start_addr_sp) @ */
以上代码主要做了以下事情:
- 设置栈指针为gd->start_addr_sp,并且8字节对齐;
- 设置r9=gd->bd,在内存图上可以看出,新的gd结构在bd结构的下面紧挨着,所以减去gd的大小就是新的gd起始地址,r9变成了重定位后的新地址的gd结构了;
- 将here标号的地址值(adr取的是相对当前PC的偏移地址)读取到LR中,将重定位偏移值gd->reloc_off加载到R0寄存器中;
- 链接寄存器LR加上偏移值R0后,LR的地址就变成重定位后的地址了;
- 将重定位地址gd->relocaddr加载到R0中,作为参数传给relocate_code;
- 执行重定位,从relocate_code回来后,就直接运行在重定位后的u-boot中了,here标号已经是重定位后那个here标号了;
上面代码主要是用来将u-boot重定位到gd->relocaddr位置。
具体如图所示:这里为了方便绘制,将内存地址拆分成了两部分,右侧地址存放的是u-boot的运行地址。当Mini2440从NAND启动时,NAND启动的时候会将NAND前4kb代码复制到片内SRAM中,然后从0x00地址开始运行。
二、relocate_code(arch/arm/lib/relocate.S)
2.1.text、.rodata、.data、.rel.dyn重定位(arch/arm/lib/relocate.S)
/* * void relocate_code(addr_moni) * * This function relocates the monitor code. * * NOTE: * To prevent the code below from containing references with an R_ARM_ABS32 * relocation record type, we never refer to linker-defined symbols directly. * Instead, we declare literals which contain their relative location with * respect to relocate_code, and at run time, add relocate_code back to them. */ ENTRY(relocate_code) ldr r1, =__image_copy_start /* r1 <- SRC &__image_copy_start */ subs r4, r0, r1 /* r4 <- relocation offset */ beq relocate_done /* skip relocation */ ldr r2, =__image_copy_end /* r2 <- SRC &__image_copy_end */ copy_loop: ldmia r1!, {r10-r11} /* copy from source address [r1] */ stmia r0!, {r10-r11} /* copy to target address [r0] */ cmp r1, r2 /* until source end address [r2] */ blo copy_loop
根据u-boot.lds我们绘制各个段在内存中的分布图:
以上代码主要重定位__image_copy_start、__image_copy_end的数据到新的地址:
- r0为u-boot重定位后的位置gd->reloacaddr;
- r1设置为__image_copy_start,为u-boot链接起始地址(链接地址需要和运行地址一致,此时链接地址就是目前u-boot运行的起始地址);
- r4设置为u-boot重定位偏移地址gd->reloc_off;
- 如果r4=0,表示r0=r1,不再进行重定位;
- r2设置为__image_copy_end;
- 以r1为起始地址(也就是目前u-boot的起始地址),加载[r1],[r1+4]字到r10,r11;
- 以r0为起始地址(也就是重定位后的的新地址),加载r10,r11的值到[r0],[r0+4]中;
- 比较是否读取到结束地址__image_copy_end,一直循环,直到拷贝结束;
除了代码段外,这里我们重定位了只读数据段和初始化数据段:
- rodata{}:声明只读数据段,简称rodata段,存放常量,字符常量,const常量,据说还存放调试信息;
- .data{}:声明初始化数据段(Initialized data segment),简称data段,存放程序中已经初始化全局与初始化静态变量;
至于什么需要重定位这两个段,我们以为例,我们定义了const常量为例:
const u8 sunflower_320x240[] = { ... }
LCD项目反汇编:
Disassembly of section .rodata: 300056340x18>: 30005634: aeb0d2ce cdpge 2, 11, cr13, cr0, cr14, {6} 30005638: f5c1e3c4 .word 0xf5c1e3c4 3000563c: 4920e0d1 stmdbmi r0!, {r0, r4, r6, r7, sp, lr, pc} 30005640: 564f4c20 .word 0x564f4c20 30005644: 4f592045 svcmi 0x00592045 30005648: 00002055 .word 0x00002055 3000564c : 3000564c: 73229322 .word 0x73229322 30005650: 73229322 .word 0x73229322 30005654: 93229322 .word 0x93229322 30005658: 93229322 .word 0x93229322 3000565c: 93227322 .word 0x93227322 30005660: 93229322 .word 0x93229322 ...
我们在代码中使用到了到了sunflower_320x240变量:
/* 文件必须采用GBK编码 */ lcd_draw_bmp(0,0,LCD_WIDTH,LCD_HEIGHT, sunflower_320x240); 300017fc: e59f3050 ldr r3, [pc, #80] ; 3000185430001800: e58d3000 str r3, [sp] 30001804: e3a00000 mov r0, #0 ; 0x0 30001808: e3a01000 mov r1, #0 ; 0x0 3000180c: e3a02d05 mov r2, #320 ; 0x140 30001810: e3a030f0 mov r3, #240 ; 0xf0 30001814: ebfffe7c bl 3000120c char *data = "我爱你刘燕 I LOVE YOU "; 30001818: e59f3038 ldr r3, [pc, #56] ; 300018583000181c: e50b3008 str r3, [fp, #-8] lcd_draw_char_lib(100, 50, 0xFF00FF, data, strlen(data)); 30001820: e51b0008 ldr r0, [fp, #-8] 30001824: eb0003c8 bl 3000274c 30001828: e1a03000 mov r3, r0 3000182c: e58d3000 str r3, [sp] 30001830: e3a00064 mov r0, #100 ; 0x64 30001834: e3a01032 mov r1, #50 ; 0x32 30001838: e3a028ff mov r2, #16711680 ; 0xff0000 3000183c: e28220ff add r2, r2, #255 ; 0xff 30001840: e51b3008 ldr r3, [fp, #-8] 30001844: ebffff83 bl 30001658 } 30001848: e24bd004 sub sp, fp, #4 ; 0x4 3000184c: e8bd4800 pop {fp, lr} 30001850: e12fff1e bx lr 30001854: 3000564c .word 0x3000564c 30001858: 30005634 .word 0x30005634
可以看到sunflower_320x240变量地址是保存到[pc,#80]地址处的,sunflower_320x240在c语言中是一个char类型指针,该指针保存的就是sunflower_320x240数据所在的地址:
30001854: 3000564c .word 0x3000564c
进行rodata重定位可以实现将sunflower_320x240数据的重定位,但是却没有完全实现变量sunflower_320x240的重定位,地址0x30001854重定位后为:
30001854+重定位偏移: 3000564c .word 0x3000564c => 我们需要将这个地址值+重定位偏移
2.2 .rel.dyn重定位(arch/arm/lib/relocate.S)
/* * fix .rel.dyn relocations */ ldr r2, =__rel_dyn_start /* r2 <- SRC &__rel_dyn_start */ ldr r3, =__rel_dyn_end /* r3 <- SRC &__rel_dyn_end */ fixloop: ldmia r2!, {r0-r1} /* (r0,r1) <- (SRC location,fixup) */ and r1, r1, #0xff cmp r1, #23 /* relative fixup? */ bne fixnext /* relative fix: increase location by offset */ add r0, r0, r4 ldr r1, [r0] add r1, r1, r4 str r1, [r0] fixnext: cmp r2, r3 blo fixloop relocate_done: #ifdef __XSCALE__ /* * On xscale, icache must be invalidated and write buffers drained, * even with cache disabled - 4.2.7 of xscale core developer's manual */ mcr p15, 0, r0, c7, c7, 0 /* invalidate icache */ mcr p15, 0, r0, c7, c10, 4 /* drain write buffer */ #endif /* ARMv4- don't know bx lr but the assembler fails to see that */ #ifdef __ARM_ARCH_4__ mov pc, lr #else bx lr #endif ENDPROC(relocate_code)
这里主要用于重定位.rel.dyn段,至于为什么需要定义这个段,实际上就是解决上面我们介绍的类似sunflower_320x240全局变量重定位存在的问题。这里仍然以sunflower_320x240为例:
- r2设置为__rel_dyn_start;
- r3设置为__rel_dyn_end;
- 以r2为起始地址(也就是动态符号表的起始地址),加载[r2],[r2+4]到r0,r1中,那么 r0得到的就是0x30001854;
- 取出r1中数据的低8位、23用来检查这个符号是不是需要被重定位,不需要的话就跳过;
- r4是重定位偏移,设置r0=r0+r4,也就是新的u-boot里面sunflower_320x240全局变量的地址;
- 将r0地址内的数据存到r1中,即0x3000564c,这个值就是sunflower_320x240全局变量数据的存放地址;
- 给r1加上偏移r4,将加了偏移后的值(变量的地址)写回,这样新的u-boot就能正确访问了;
三、 relocate_vectors
接下来重定位向量表,这个很简单,就是操作协处理器:
/* * Default/weak exception vectors relocation routine * * This routine covers the standard ARM cases: normal (0x00000000), * high (0xffff0000) and VBAR. SoCs which do not comply with any of * the standard cases must provide their own, strong, version. */ .section .text.relocate_vectors,"ax",%progbits .weak relocate_vectors ENTRY(relocate_vectors) #ifdef CONFIG_CPU_V7M /* * On ARMv7-M we only have to write the new vector address * to VTOR register. */ ldr r0, [r9, #GD_RELOCADDR] /* r0 = gd->relocaddr */ ldr r1, =V7M_SCB_BASE str r0, [r1, V7M_SCB_VTOR] #else #ifdef CONFIG_HAS_VBAR /* * If the ARM processor has the security extensions, * use VBAR to relocate the exception vectors. */ ldr r0, [r9, #GD_RELOCADDR] /* r0 = gd->relocaddr */ mcr p15, 0, r0, c12, c0, 0 /* Set VBAR */ #else /* * Copy the relocated exception vectors to the * correct address * CP15 c1 V bit gives us the location of the vectors: * 0x00000000 or 0xFFFF0000. */ ldr r0, [r9, #GD_RELOCADDR] /* r0 = gd->relocaddr */ mrc p15, 0, r2, c1, c0, 0 /* V bit (bit[13]) in CP15 c1 */ ands r2, r2, #(1 << 13) ldreq r1, =0x00000000 /* If V=0 */ ldrne r1, =0xFFFF0000 /* If V=1 */ ldmia r0!, {r2-r8,r10} stmia r1!, {r2-r8,r10} ldmia r0!, {r2-r8,r10} stmia r1!, {r2-r8,r10} #endif #endif bx lr ENDPROC(relocate_vectors)
四、设置环境参数
然后是清bss段,这是C语言运行环境需要的,然后执行board_init_r:
#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_FRAMEWORK) # ifdef CONFIG_SPL_BUILD /* Use a DRAM stack for the rest of SPL, if requested */ bl spl_relocate_stack_gd cmp r0, #0 movne sp, r0 movne r9, r0 # endif ldr r0, =__bss_start /* this is auto-relocated! */ #ifdef CONFIG_USE_ARCH_MEMSET ldr r3, =__bss_end /* this is auto-relocated! */ mov r1, #0x00000000 /* prepare zero to clear BSS */ subs r2, r3, r0 /* r2 = memset len */ bl memset #else ldr r1, =__bss_end /* this is auto-relocated! */ mov r2, #0x00000000 /* prepare zero to clear BSS */ clbss_l:cmp r0, r1 /* while not at end of BSS */ #if defined(CONFIG_CPU_V7M) itt lo #endif strlo r2, [r0] /* clear 32-bit BSS word */ addlo r0, r0, #4 /* move to next */ blo clbss_l #endif #if ! defined(CONFIG_SPL_BUILD) bl coloured_LED_init bl red_led_on #endif /* call board_init_r(gd_t *id, ulong dest_addr) */ mov r0, r9 /* gd_t */ ldr r1, [r9, #GD_RELOCADDR] /* dest_addr */ /* call board_init_r */ #if defined(CONFIG_SYS_THUMB_BUILD) ldr lr, =board_init_r /* this is auto-relocated! */ bx lr #else ldr pc, =board_init_r /* this is auto-relocated! */ #endif /* we should not return here. */ #endif
到了这里,u-boot重定位就结束了,其实u-boot重定位的知识远不止代码中的这点儿,有很多的细节,建议读者有时间的话,多去网上查阅一下相关的资料,比如位置无关代码与位置有关代码,为什么需要重定位等问题,这样对重定位才有更加深入和深刻的理解。
参考文章:
[1]u-boot2020.04移植(5、u-boot重定位)
[2]嵌入式Linux学习:重定位(Relocation)