您的位置:首页 > 其它

how does startup code work

2013-03-08 15:01 363 查看
from : http://www.keil.com/forum/20201/


 startup files in Keil

Can You explain me something. Why in ARM microcontrollers each IDE use startup files?

Where from the compiler knows that the first think is to execude the code from startup. Each C language programms should start from main() funcion and here the program starts from startup?

If the startup does not exists it works fine too but we have to make Heap, stack and other by ourself but in main()?

If I add different startup called whatever i want and add it to group in project in will be done the as first too so it means that the name of the startupp file is not important.

Can you explain me how does it work? (for example in Keil)

The IDE does not use any startup file.

It's the programs you develop that are using startup files.

When you write programs for a PC, there are also startup files. But since the tools then knows the target is a PC, the startup file can have a fixed contents. So it is hidden inside the language runtime library. And some of that "startup file" is actually moved
all the way to the BIOS when the computer boots. Every single motherboard have a custom "startup file"/BIOS explicitly adapted to the available hardware. An to that, you also have a custom hardware driver layer used by the OS.

For embedded projects, each target hardware is so different that only you, the developer, can know what is needed to prepare the hardware in a way that the C runtime library can then be used.

Each C program should start from main() - that is a bit of a simplification. If you look at the C language standard, you will see that there are numbe of rules that must be fulfilled before the program reaches main(). For example - all global variables should
be zero-initialized. And main() must be directly able to call other functions, so some magic must have set up the stack.

A C program is linked with a C RTL - C Runtime Library. And it must have been properly initialized before the program reaches main().

The linker configuration file contains information about what object files that should be linked first.

And the startup file (normally in assembler, but could be in C for Cortex-class processors) contains information like:
AREA    RESET, CODE, READONLY


to inform the linker that a generated code block should match the requirements for the linker configuration file.

As I mentioned, there is a linker configuration file telling the linker how to order things.

It may look like:
; *************************************************************
; *** Scatter-Loading Description File generated by uVision ***
; *************************************************************

LR_IROM1 0x00000000 0x00001000  {    ; load region size_region
ER_IROM1 0x00000000 0x00001000  {  ; load address = execution address
*.o (RESET, +First)
*(InRoot$Sections)
.ANY (+RO)
}
RW_IRAM1 0x40000000 0x00008000  {  ; RW data
.ANY (+RW +ZI)
}
RW_IRAM2 0x7FE00000 0x00004000  {
adc.o (+ZI)
fram.o (+ZI)
.ANY (+RW +ZI)
}
}

LR_IROM2 0x00003000 0x0003D000  {
ER_IROM2 0x00003000 0x0003D000  {  ; load address = execution address
.ANY (+RO)
}
}


Notice the "(RESET, +First)" part?

Match that up with the line in the startup assembler file:
AREA    RESET, CODE, READONLY


So it doesn't matter what the startup file is named - if it contains an area "RESET" it will match the region placement of the linker configuration file.

But you can use the IDE to specify segment namings for C files too, and add other sorting orders in the linker configuration (scatter) file.

All that is required, is that you have a suitable startup code located where the physical processor initially jumps or reads a reset vector.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: