embedded - How can I use crt0.o to setup .bss and .data in my bare metal C program? -
i wrote bare metal c program running on stm32f4. it's nothing fancy, usual led-blinky-program. in project have written initialization routines clearing .bss
section , initializing .data
section myself.
this wasn't complicated. in linker script, instructed linker create symbols marking start , end of .data
, .bss
section.
.data 0x20001000 : align(4) { __etext = loadaddr(.data); __data_start__ = addr(.data) ; *(.data*) ; __data_end__ = addr(.data) + sizeof(.data) ; } >ram at>rom .bss : align(4) { __bss_start__ = addr(.bss) ; *(.bss*) ; __bss_end__ = addr(.bss) + sizeof(.bss) ; } >ram
then using these symbols in code:
extern unsigned int __etext; extern unsigned int __data_start__; extern unsigned int __data_end__; extern unsigned int __bss_start__; extern unsigned int __bss_end__; void reset_handler() { unsigned int * src; unsigned int * dest; src = &__etext; dest = &__data_start__; /* copy .data */ while (dest < &__data_end__) *(dest++) = *(src++); /* 0 bss. */ (dest = &__bss_start__; dest < &__bss_end__; dest++) *dest = 0; }
now use crt0
purpose of setting-up .bss
, .data
. (i heard setting things main purpose of crt0
.)
how can that? basic principle of defining symbols in linker script , using in code same?
tl;dr
how can use crt0
setup .bss
, .data
section?
in general, in linker command file/script...
the posted script has few problems.
suggest similar following. (use actual origin , length parameters) (note .text, .data, .bss not sections created. there many more , should listed appropriately )
you should @ http://www.math.utah.edu/docs/info/ld_3.html#sec18 details , examples linker command files
/* simple memory layout */ /* there separate memory items */ /* each memory mapped peripheral */ /* external ram, etc etc etc */ memory { rom : origin = 0, length = 256k ram : origin = 0x40000000, length = 4m } sections { rom :0 (noload) block(4) { } ram : { .text : block(4) { .textstart = .; *(.text) .textend = .; } .bss : block(4) { .bssstart = .; *(.bss) .bssend = .; } .data : block(4) { .datastart = .; *(.data) .dataend = .; } } }