Relocations (the location of the code) (memory address, the address operation, link address)

Table of Contents

1, a brief analysis of the code start-up process

2, memory address, run address, link address

2.1, memory address

2.2, the link address

2.3, run address

2.4, the difference between link and run addresses of

3, the location of the code, regardless of the position code is explained in detail

4, relocations

 


Reference article: https://blog.csdn.net/lizuobin2/article/details/52049892#

 

1, a brief analysis of the code start-up process

Question 1: download go?

There will be instructions on the data sheet, the location is not where we want to go out into the download where to download and start downloading the wrong position when he could not find the code in which, typically downloaded to a storage device as follows: 1, internal comes the FLASH , 2, flared NANDFLASH (the EMMC) ,. 3, extended NORFLASH,  . 4, the SD card , ( memory address )

1, most microcontroller (STM32) is downloaded into the FLASH internal built, is strictly specified by the start address (e.g., 0x08000000 )

2, most of the run Android or linux the ARM typically downloaded to the SD card or flared FLASH (EMMC) are, however, the starting position in the SD card or EMMC is strictly defined (such as the 0-th block stored starting, or a skip, a stored starting from the first position)

Question 2: From which position to load the code, load go running?

1, divided into two or more storage devices and one storage device is directly run code (own internal FLASH, external expansion NORFLASH) a device is not directly addressing operation code (SD card, NANDFLASH, EMMC)

2, generally have a chip enable pin , we need to correct the position of our download code configuration start pin high and low , so that the power to correct startup code

3, for the direct operation of the code storage device, pre-load the code does not need to , but directly run the code from the starting position of the apparatus, as to whether the code needs to be loaded into the late run to see other desired operating position address programmer (link written in script inside link address ), if desired nothing (with the default link address translation software, translation software and default link address and address of the actual operation are the same) it does not require any action to copy the code, the code from scratch to run to the end ( the STM32 microcontrollers and other chips such is the case ) , if the programmer wants part of the code to run in a different location, you need to modify the post-code link address rather than the system has been the default address, copy the code in the latter part of the pre-code to the desired position, then the long-jump to a position to run over (this is as follows: we sometimes feel too slow FLASH, need to copy the code to RAM to run) (relocations)

4, for not directly run the code storage device, the internal firmware chips early part of the code will be loaded into the default memory location (internal RAM) to run , why is a part of it, because this condition is generally code for a large (such as running linux limited system) internal RAM resources, so only part of the code is loaded into the internal RAM, the other part of most of the code needs to be loaded into the external expansion of RAM to run, so the programmer to write a linker script (own link address ) you want to put part of the code behind can be loaded into the address he desires to run, and running pre-programmers to write code ( position independent code ) included in the copy function, the copy function copies the rear part of the code to a desired position ( relocations after), pre-finished code at runtime achieve long jump , jump to the desired location to run link

to sum up:

Code is loaded from where according to the specified pin to start, where to run the code loaded into points early and late, pre-loaded into the default run basically run the place, post code to load and run according to their own needs position (most microcontroller or system default, most ARM late run large systems require relocations)

2, memory address, run address, link address

2.1, memory address

Codes is to position memory (FLASH) of

2.2, the link address

This address is the location of the linker script to run inside the specified code (expected by the programmer to run address)

For example, we want the code in the future loaded into our extended memory to run (but not always in a position to run just on the electric system specified)

2.3, run address

The current program is being address running (PC)

Theoretical terms we can put anywhere in the code is loaded into memory and then jump to the appropriate location to run, as will the problems, to see our link address is currently running and address are the same and are running code or location of the position unrelated code, if the address is the location of the code is inconsistent and an error may occur (see explanation behind the location of the code and unrelated code)

Chip microcontroller or the like (STM32)

Most Harvard architecture single chip (including STM32) is no additional load the code into the RAM to run, the power is directly (FLASH) running from the location code download our                 

Generally speaking the code for the microcontroller: memory address = address link address = run

2.4, the difference between link and run addresses of

Disassembly of the fact that we see the link address , and the address of the actual operation of the internal code to see how the code is loaded, loaded go running,

We assume that the link address is 0x33f800000 if we put the code loaded into the 0x00000000 address the actual operation as shown:

 

Machine code which contains the address of variables and functions, in the end is the absolute value of the link address or jump address or offset value plus or jump into the PC according to the current position of the location of the code and the position-independent code:

1, according to my understanding is, position-independent code is absolutely nothing to this code and link address, or jump values ​​are needed to address the current PC + offset address, so always be able to find the right position, so there's address is relative address can also be understood as a dynamic address, address change operation that the address has changed, you've changed and I changed the so-called

2, while the location of the code which is an absolute link address, link address to each according to value, or if the variable is a function of just the address stored in the link, you can find the correct position to realize the value of a jump or, if the variable or function is loaded into ram position and our position is not the same link address, then go to the link address value or an error will certainly jump

 

Question 1: link address must be equal to the address and run it?

回答:按前面的分析,前期代码(一般是些基本的硬件初始化代码,如果需要重定位还包括拷贝函数代码)运行地址和链接地址可以不相等后期代码的链接地址必须要等于运行地址,也就是说后期代码当前的实际运行地址和链接地址不相等就有可能出错,因为:前期代码大部分是位置无关码,而位置无关码无论加载到RAM中的什么位置都可以正常运行,后期的代码中有些代码是位置有关码,位置有关码的实际运行地址和我们的期望运行地址(链接地址)不一样就有可能出错,

 

举两个例子说明一下:

1、比如在一个函数中读取一个全局变量的值,读取时会根据变量的链接地址而不是实际存放地址去取值,如果实际存放的地址=链接地址那么取到的值肯定是正确的,如果链接地址和实际的存放地址不一样那么取出的值肯定是错误的。

2、再比如:一个c语言函数调用另外c语言一个函数,如果两个函数的运行地址相差很大(大于32M)编译器就会生成位置有关码的跳转指令跳转到那个被调用的函数位置,如果那个函数的此时不在我们链接指定的位置处,那么跳转肯定出现错误

 

总结:前期代码运行地址可以和链接地址不一样,后期代码运行地址必须和链接地址一样

 

3、位置有关码,位置无关码详细解释

 

点击进入详细解释

 

4、代码重定位

我们希望后期代码在我们指定的位置运行(比如为了加快代码运行速度,把代码从FLASH上面加载到外部RAM中运行),我们会在前期代码中用位置无关码拷贝代码到链接地址处,然后长跳转到链接地址处运行,这样一个过程就是代码重定位

问题1:为什么一开始不直接把代码加载到RAM运行呢?

回答:一般我们外扩的RAM是需要通过代码初始化才能正常工作的,所以前期是不能直接加载到RAM中运行的

问题2:stm32可不可以偏要把代码放到内部RAM运行呢?

回答:肯定是可以的,但是要设置好链接地址让编译器按你的链接地址编译,并且在前期编写拷贝函数把代码拷贝到链接地址(RAM)中运行

 

 

 

 

 

 

 

发布了139 篇原创文章 · 获赞 114 · 访问量 7万+

Guess you like

Origin blog.csdn.net/shenlong1356/article/details/104597292