s3c2440 bare - relocations (1 incorporated relocation, why relocations)

1. The introduction of relocation (Why relocations)

We know s3c2440 of cpu begin fetching instructions from 0 to perform address, when you start from nor, 0 corresponding to the address nor, nor memory can be read like the same, but the same can not write like a memory. We can execute instruction fetch from nor.

Example 1: When the start nand, nand in our previous 4K becomes automatically loaded into instruction sram go, when the address corresponds to 0 sram.

So if our program is larger than 4K, from nand start, sram copy only the first 4K in the code nand, then how to solve this problem?
Relocatable code then needs to go sdram, sdram larger capacity, and can be accessed directly cpu.

Example 2: We know that the program contains:

代码段(.text)
数据段(.data):存放初始值不为0的全局变量/静态变量
rodata段(.rodata):const修饰的全局变量或静态变量
bss段(.bss):存放初始值为0或者未初始化的全局变量/静态变量
commen段(.commen):注释

Consider the following code into a bin file.

#include "s3c2440_soc.h"
#include "uart.h"
#include "init.h"

int g_Char = 'A'; //.data
int g_CharB = 'B'; //.data
int g_CharC = 'C'; //.data
int g_CharD = 'D'; //.data


const int g_roval = 'C'; //.rodata
int g_A = 0; //bss
int g_B; //bss

int main(void)
{
    uart0_init();
    while (1)
    {
        putchar(g_Char);
        g_Char++;         /* nor启动时, 此代码无效,由于nor启动,nor上不可写 */
        delay(1000000);
    }
    return 0;
}

We put it to the nand and nor are burning to see what is the difference?

1. Burn to nor: We found that the program has output 'AAAAAAA'.

2. Burn to nand: We found that the program has no output.

We found nor start of g_Char ++ invalid, nand start the program without any output, why?
We take a look at the anti-compilation of the program:

We found .text sections of the program is to start from 0 address, then decodes the address from 0 cpu instruction fetch, execute.
When starting from nor, 0 corresponding to the address nor; When starting from nand, 0 corresponding to SRAM address, it is executed from cpu nor fetch can start both from nand.

However, in view of our next .data section, find the start address of the .data section is 0x8474 (ie address g_Char variable to 0x8474).
Then:

1) When the device is programmed into nor, .data section in the region nor a certain period, since the same memory as nor I can read,
but not write directly to memory as the same, so the 'g_Char' modified invalid.

2)当把程序烧录进nand, .data段在nand的某一区域,nand启动时硬件会自动把nand上的前4K数据copy到SRAM,然后cpu从sram取指令执行。但是.data段的起始地址0x8474>0x1000,超过了4K,cpu没法把.data段也copy到SRAM,所以当访问'g_Char'时,发生了异常(abt数据访问终止,这个异常后面我会在“异常与中断”里面专门讲解)。

那我们再仔细看看反汇编,发现.rodata段和.text段是连续的,但是.rodata段和.data段中间有一段"空洞"。用图形表示更形象,bin文件的内容分布如下所示:

那么我们怎么去掉空洞,让.data段了紧接着.rodata段呢?

用链接脚本(这个下节会讲),但现在直接在编译的时候用 "-Tdata 0x800",这样指定.data段基地址为0x800,这样nand启动时.data就能自动copy到SRAM了。再看下反汇编的.data段:

这时我们烧录程序到nand,从nand启动,发现能输出‘ABCDEFG’...

通过上面2个例子,现在总结下为什么要代码重定位:
1.nand启动,前4K代码被自动copy到sram,当程序大于4K的时候需要重定位代码到sdram。
2.nor启动, 全局变量在nor上,不能像内存一样直接写该全局变量,那么也需要重定位到sdram。

Guess you like

Origin www.cnblogs.com/fuzidage/p/12038514.html