Loading and run addresses & relocations (2020)

2018 wrote the content, look back a bit in 2020, before the discovery of some of the fallacies, plus some layout problems, so here to open a record to do it again, as if it was their own these years of growth compared. (The theme is linked to the address in the script used in conjunction with some of the concepts derived actual use, linker script content can refer  http://ftp.gnu.org/old-gnu/Manuals/ld-2.9.1/html_mono /ld.html#IDX237  )

2018 titled compile and run addresses, there should be more accurate load address and address when you run (or relocation address), you also need to introduce a memory address .

Need to know is the program of choice for running bare metal need to be stored in memory and can be NorFlash or NandFlash and can be powered down to save all kinds of static memory, stored in the memory when the program reads the decoding program execution, the program started, the general bare board programs are beginning to take from 0 address code, our program also naturally need to start from the stored memory address 0, where 0 is the program memory address , with only the memory address of the memory write about, it can be arbitrarily determined, you can also write a program storage intermediate position, of course, this remains a need exists boot program address 0, so in order to program the bare board to function properly, we will save the program to address 0.

But after the program up and running, for various reasons, can not run in the memory address of the original, the reason here might be:

1. We use NorFlash memory is read-only memory (for running purposes, the use of chip NorFlash manual provides command can also be written only), which causes global variables used in the program or static local variables and other data and bss segment data can not be modified, thus affecting the logic of the program.

2. Use a NandFlash, to S3C2440 chip, for example, when NandFlash use as a boot Flash, 2440 NandFlash mechanisms exist to copy the contents into the first 4K chip SRAM, SRAM address as 0 to run the program at this time if our program more than the size of 4K, it will cause the program not to run.

It is because of the presence of the above reasons, we need a program relocated to the available memory, typically SDRAM, the SDRAM is assumed here that base address is 0x3000, then we need a mechanism for the program, so that after running the program 0x3000 address operation, this mechanism is called relocations , 0x3000 for the program running address .

Run-time address is the address of the program is running, regardless of where the program is stored in order to address what is running, the program will consider themselves address the runtime to run at, all global variables and static local variables and other data section or bss segment data to run when addressing an address to access, such as a global variable a, run-time address is 0x301F, the code will need to use all of the variable to the value 0x301F this address to access or assignment, this time a problem arises, our program storage 0 address, not before relocation, 0x3000 and the subsequent address data is unknown, this time to 0x301F the value is undefined behavior. Similarly, if there is a function functionA, his run-time address is 0x321F, may his memory address in 0x11FF, but not before positioning using absolute jump instruction to jump to the address is undefined runtime behavior.

Is simply a program stored in the address 0 is the starting place is to start running the program up and running at address 0, but the program feel (actually compile time to determine the address of each instruction, you can see disassembly) has address 0x3000 run in the run time, access to global variables is accessed through run-time address, if the direct use problems. The program has been able to function properly, because of the use position-independent code, has been relatively jump, if you are using absolutely jump, jump directly is also a problem.

Relocations corresponding area is filled with data and address codes at run-time, the well is determined at compile time, the memory address has all required "material." 1. General relocations comprising program memory address from I copied my own address to the runtime; 2. clear the bss section like.

Finally, load address , for the bare board program, file structure compiled output bin structure NorFlash 0 stored at the same address, i.e., the relative position of the load address and the address stored at the address structure, which also affects the size of the bin file, Usually the discharge can be made compact, and then relocated to copy the data to the runtime load address from the address corresponding to an address of the runtime.

Over the past two years found that their ability to describe still no progress, a myriad of thoughts to implement all the characters changed like, or want some help attach a chart to.

The following diagram describes the use of a linker script file final distribution of memory.

We assume that as shown in the code segment load address is 0, the data segment load address 0x80, while the runtime addresses are 0x3000 and 0x31FF, and assuming that the code length is 0x80, then after loading the code and data segments immediately, bin size does not empty as the sum of segment sizes stored in the zero address NorFlash (here is not loaded at address 0, memory address to 0, memory address we have set ourselves, want to deposit Shucks save, load address just a opposing configuration, but will simplify the code uniform workload to operational requirements chip is 0), as follows:

 You can clearly see the situation relocated much of what the load address and run-time address by the figure.

Expression by capacity constraints, some not well described clearly, if in doubt welcome, also welcome the discussion pointed out that the exchange is not stated correctly. Did not forget the early heart.

Published 19 original articles · won praise 7 · views 6923

Guess you like

Origin blog.csdn.net/G_METHOD/article/details/104508545
run