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
Previous article:Parameter passing rules for lighting an LED
Next article:System clock configuration
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- Brief Analysis of Automotive Ethernet Test Content and Test Methods
- How haptic technology can enhance driving safety
- Let’s talk about the “Three Musketeers” of radar in autonomous driving
- Why software-defined vehicles transform cars from tools into living spaces
- How Lucid is overtaking Tesla with smaller motors
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- Wi-Fi 6E has been launched. What is the difference between it and ordinary Wi-Fi?
- uboot operates gpio level
- This is the window that pops up when I open the component library in AD17. What's going on?
- RVB2601 Evaluation Board Trial 3: Ethernet Communication Test
- UNI-T UT70B Multimeter Schematic Diagram and Instruction Manual (EN)
- DIY Music Box (Ask a master to help come up with an attractive title)
- Can you analyze the circuit for me?
- RSL10 information summary, sharing and exchange
- RF PCB Simulation Cookbook
- This year's Mid-Autumn Festival and National Day will not leave Shanghai