`

Android培训班(96)内核解压过程9

 
阅读更多

前面已经把定位的数据通过加载LC0结构来加载到寄存器里,已经具备了定位的条件。那么内核进行重定位主要做些什么事情呢?要了解整个过程,当然要学习编译原理,因为进行重定位之后,主要是为了建立C语言的运行环境的需求。由于C语言是基于栈式的语言,又有全局变量,说明内存结构至少有两个,一个是全局数据区,一个是栈。因此,重定位就是修改全局数据区和栈的访问。在全局数据的内存的表达方式,GCC是使用GOTGLOBALOFFSET TABLE)来组织的,所以需要修改GOT表里每一项的数据地址,才可以让C语言编译出来的程序可以访问到正确的地址。

由于栈的内存需要更新和写入,所以需要栈指向的内存是可读写的。但ROM不具备随机写入的,所以需要栈重定位。

/*

* We're running at a different address. We need to fix

* up various pointers:

* r5 - zImage base address

* r6 - GOT start

* ip - GOT end

*/

add r5,r5, r0

add r6,r6, r0

add ip,ip, r0

这段代码就是重新计算GOT的开始位置和结束位置。


#ifndefCONFIG_ZBOOT_ROM

/*

* If we're running fully PIC === CONFIG_ZBOOT_ROM = n,

* we need to fix up pointers into the BSS region.

* r2 - BSS start

* r3 - BSS end

* sp - stack pointer

*/

add r2,r2, r0

add r3,r3, r0

add sp,sp, r0

这段代码是主要重定位堆和栈的位置。


/*

* Relocate all entries in the GOT table.

*/

1: ldr r1,[r6, #0] @ relocate entries in the GOT

add r1,r1, r0 @ table. This fixes up the

str r1,[r6], #4 @ C references.

cmp r6,ip

blo 1b

#else


/*

* Relocate entries in the GOT table. We only relocate

* the entries that are outside the (relocated) BSS region.

*/

1: ldr r1,[r6, #0] @ relocate entries in the GOT

cmp r1,r2 @ entry < bss_start ||

cmphs r3,r1 @ _end < entry

addlo r1,r1, r0 @ table. This fixes up the

str r1,[r6], #4 @ C references.

cmp r6,ip

blo 1b

#endif

这段代码主要就是重新计算GOT表里每一项的数据项地址。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics