Code relocation

Publisher:EnchantedBreezeLatest update time:2024-07-31 Source: cnblogs Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1 Description

Experimental platform: JZ2440

CPU: S3C2440

2 S3C2440 startup process

Figure 1 S3C2440A Memory Map after Reset

S3C2440 supports booting from a variety of storage devices: NOR/NAND Flash, EEPROM, etc. There is 4K SRAM inside the chip for booting the device. As for how the device is finally booted, it is implemented inside the chip by configuring the OM pin of the chip.

Excerpt from "S3C2440A_UserManual_Rev13":

Figure 2 BANK0 BUS WIDTH

For example, when you choose to boot with NOR Flash, the chip's 0 address (4K address space) will be directly mapped to Nor Flash, and the CPU will directly read instructions from Nor Flash and execute them; when you choose to boot with Nand Flash, S3C244 will copy the first 4K data of Nand Flash to the chip's internal SRAM, and then the CPU will read instructions from the internal SRAM and execute them. (NOR Flash is directly driven by the memory controller, while NAND Flash is driven by the NAND Flash controller, and the NAND Flash controller is driven by the memory controller, so NOR Flash is uniformly addressed by the CPU, while NAND Flash is not, so there is a difference in their startup methods).

3 Analyzing executable files

The difference between *.elf and *.bin files

bin file: a binary file that contains only machine code and can be run directly on the machine.

elf file: In addition to the machine code, it also contains other additional information, such as: the running address of the segment, the loading address, the relocation table, the symbol table, etc.; it can only run on a machine with an operating system and is executed after being parsed by the operating system (the machine code is extracted); it can be used for debugging.

Get the *.elf file by linking through "arm-linux-ld -T target.lds *.o -o *.elf"; then convert the *.elf into a *.bin file through "arm-linux-objcopy -O binary -S *.elf *.bin".

4 Linker Script

4.1 Program composition

The program consists of a code segment, a data segment, a read-only data segment, a BSS segment, and a comment segment. The BSS segment and the comment segment are not stored in the bin file or the elf file.

.text: Code segment; the executable part of the program.

.data: Data segment; global variables in the program.

.rodata: Read-only data segment; const type variables in the program.

.bss: BSS segment; global variables in the program that are not initialized or whose initial value is 0.

.COMMON: Comment section.

Note: Local variables are allocated on the stack as the function is called and released when the function exits.

4.2 Linker Script Description

When the link script is not used to instruct the linker to connect, the order of linking is based on the order in which the files are placed in the Makefile. Note that start.o should be placed first, otherwise the program will fail to run. Use the link script in the Makefile: [-T *.lds].

The format of the linker script is:

SECTIONS{

secname start Block(align) (NOLOAD) : AT(ldadr)

{

contents > region : phdr = fill

}

}

Example:

Figure 3 Linker script example

Note: In the link script, the starting address of each segment should be set to 4-byte alignment, because the assembly instruction will automatically align the address to be operated to 4 bytes. If we operate on an address that is not 4-byte aligned, unexpected errors will occur.

For example:

ldr r1, =0x32000000

ldr r2, =0

str r2, [r1]

The result we expect is [0x32000000] = 0; but the actual result is [0x30000000] = 0; this is not what we want.

5 Code Relocation

5.1 The significance of code relocation

Analysis from the perspective of NOR Flash startup:

The program can be executed directly on NOR Flash. NOR Flash can be read directly through the address, but cannot be written directly. It must be written through specific operations. Therefore, when executing the program on NOR Flash, its global variables cannot be changed. Therefore, we need to relocate the program burned on NOR Flash to SDRAM.

Analysis from the perspective of NAND Flash boot:

The program cannot be executed directly on NAND Flash; by configuring the OM pin, when NAND Flash is selected as the boot mode, the internal firmware of the chip will copy the first 4K of NAND Flash to the internal SRAM, and the program will start executing from address 0 of SRAM. When the program exceeds 4K, in order to ensure that the program can run normally, the first 4K code is generally copied to SDRAM, and then the program is executed on SDRAM.

Special note: An initialized array is declared inside the function, and the elements of the array are stored in the code segment; when this function is called, a pointer to a local variable (array name) is declared, and the pointer points to the array element at the first address of the code segment to complete the initialization action. Therefore, the array cannot be used before relocation.

5.2 Relocation Methods

5.2.1 Variables in lds files

By assigning values ​​to variables in the lds file, the address information of the corresponding segment can be obtained through external declaration in the C function.

Figure 4

In fact, the variables in the lds file are not saved in the program. When compiling the program, there is a symbol table to save the variables in the lds file. When the program needs to use the variables in the lds file, it declares external variables and obtains the values ​​of the variables by taking the values. For example: extern int name; target = &name;.

Figure 5 Symbol Table

5.2.2 Relocation Methods

Relocate only the data segment:

During program execution, only the data segment and BSS segment in the program may be modified by the code segment. Therefore, after the program is executed, only the data segment and BSS segment in the program can be relocated to SDRAM.

When the program is running, you only need to relocate the data segment and BSS segment stored at the load address to the location indicated by the runtime address.

Relocate the entire program:

After the program runs, relocate the entire program to SDRAM.

5.2.3 Position-independent codes

The b/bl instruction is a relative jump instruction. The target address of the jump is only related to the current PC value, and has nothing to do with the runtime address. Therefore, although the runtime address of the program burned into the NOR Flash points to the starting address of the SDRAM storage space (here is 0x30000000), since b/bl is a relative jump, as long as it does not involve the operation of global variables and static variables before the relocation operation is completed, the program can run normally. The code implemented by operating the relative address instruction is also called position-independent code.

Note that after relocation is completed, when you need to jump to the C function to execute the program, you should use an absolute jump (directly modify the PC value) instead of a relative jump, otherwise you will not be able to jump to SDRAM for execution.

Appendix 1 Program source code

Makefile

target.lds

Start.S

Relocate.c

Appendix 2 Reference Documents

S3C2440 User Manual

The GNU linker


Reference address:Code relocation

Previous article:Parameter passing rules for lighting an LED
Next article:System clock configuration

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号