What is relocation? Why Relocation? (Embedded below)

First, you must know a few concepts.

1, link and run addresses.

① run address, the name suggests it is time to address the program running, that is, you use the tool to download code to the address of the RAM, also known as load address.

② link address, the address specified by the linker script. Why link script specified address it? You think about the c programming language, when we need to call a function of A, the compiler is how to find this A function? The compiler must be aware of where it is placed before they can find it. That is the role of the linker script, the script actually links have been designated A function number in an address before the program is executed, after all the function calls we will go there looking for the address number A function. Somewhat similar to the c language pointer variable.

2, the location of position-independent code symbol.

① the location of the code is the implementation of this code is correct or not need to depend on the current address with the address that is already bound, such as: ldr PC, _main, that PC must pointer jumps to _main ( is a function name address) to the address, code execution is equivalent to the success of this constraint by the address, if this address is not stored _main this function will be wrong.

② position-independent code, is where this code can be run, with the address of which has nothing to do with the location of the yard opposite.

Second, the relocation of some issues that need to understand.

1, What happens next with the link address to run address different circumstances?

A: A function to give the above example, when the link address with the address of a different run time, if the link address is 0x1000, run address (load address) is 0x0000, the linker script to specify the function A future is to be stored in the (base address + offset) = 0x1000 + 0x0001 = 0x1001 address, but the program download time to download the program yet to 0x0000, it is a function of the address a is actually stored in the (base address + offset) = 0x0000 + 0x0001 = 0x0001 this address. When the program runs to the location of the line code example: ldr PC, the address 0x1001 A, the compiler first will be specified in the linker script A A find function, but because the load address with a link address for different reasons, in fact, has the function A It was placed 0x0001, so the implementation will go wrong. So when these two different addresses when the time period of the implementation of the location of the error code will be unpredictable occurrence.

2, why the link address with a different address to run happens?

A: When a chip start, relying on internal SRAM, a snippet of code can run, but not because DDR initialization, doomed the start of operation of the address is in the internal SRAM. When we need to run an operating system, then the point of how enough memory to run it? So this time you need to initialize DDR is available, but because we know that the code is run in the future DDR above, the linker script to specify the link address DDR is certainly above address, which appeared to run the link address different from the address the situation.

3. What is relocation?

A: Because a such a problem, you need to use relocation in this way to solve the above problem. So what is to relocate it? Relocation is now run under the address link address different circumstances, the implementation of a position-independent code, the role of this position-independent code is the original code is copied to share the link address where to go, and then himself again to the new long jump that position just executed share code. This realization of the situation consistent with the running address the link address.

3. Why the relocation?

A: The link address is now run different address, in this case we have two options:
① use all position-independent code.
② relocation so that the two addresses are the same.
We know that if it is a small code, you can use ①, but a large code files difficult to ensure that all use position-independent code, which is unrealistic, so you must use relocation to solve this problem.

Published 24 original articles · won praise 27 · views 10000 +

Guess you like

Origin blog.csdn.net/gyyu32g/article/details/78508406