I have a custom linker script for an stm32f4 chip, mostly working; the application start-up and run, global array and variable are correctly initialized BUT global object are not.
I found around some reference to ctor usage instead of __init_array but those seems to be older methodology.
In the LD I added the sections:
. = ALIGN(4);
.preinit_array :
{
PROVIDE_HIDDEN (__preinit_array_start = .);
KEEP (*(.preinit_array*))
PROVIDE_HIDDEN (__preinit_array_end = .);
} >FLASH
. = ALIGN(4);
.init_array :
{
PROVIDE_HIDDEN (__init_array_start = .);
KEEP (*(SORT(.init_array.*)))
KEEP (*(.init_array*))
PROVIDE_HIDDEN (__init_array_end = .);
} >FLASH
. = ALIGN(4);
.fini_array :
{
PROVIDE_HIDDEN (__fini_array_start = .);
KEEP (*(SORT(.fini_array.*)))
KEEP (*(.fini_array*))
PROVIDE_HIDDEN (__fini_array_end = .);
} >FLASH
and the full startup script:
extern unsigned long _data_flash;
extern unsigned long _data_begin;
extern unsigned long _data_end;
extern unsigned long _bss_begin;
extern unsigned long _bss_end;
extern void (**__preinit_array_start)();
extern void (**__preinit_array_end)();
extern void (**__init_array_start)();
extern void (**__init_array_end)();
inline void static_init()
{
for (void (**p)() = __preinit_array_start; p < __preinit_array_end; ++p)
(*p)();
for (void (**p)() = __init_array_start; p < __init_array_end; ++p)
(*p)();
}
void reset_handler(void)
{
unsigned long *source;
unsigned long *destination;
// default zero to undefined variables
for (destination = &_bss_begin; destination < &_bss_end;)
{
*(destination++) = 0;
}
// Copying data from Flash to RAM
source = &_data_flash;
for (destination = &_data_begin; destination < &_data_end;)
{
*(destination++) = *(source++);
}
SystemInit();
static_init();
// starting main program
main();
while(1)
{
; //loop forever, but we should never be there
}
}
Could it be the official startup script from ST are doing some black (and maybe proprietary) magic to make it work properly?
I got this same code work properly when using another compiler, so I exclude an issue in the code.
edit: after some debugging i tried:
printf("preinit %32x %32x\r\n", __preinit_array_start, __preinit_array_end);
printf("init %32x %32x\r\n", __init_array_start, __init_array_end);
printf("preinit2 %32x %32x\r\n", *__preinit_array_start, *__preinit_array_end);
printf("init2 %32x %32x\r\n", *__init_array_start, *__init_array_end);
that return:
preinit 080506E9 080506E9<\r><\n>
init 080506E9 00000000<\r><\n>
preinit2 BB4FF8E9 BB4FF8E9<\r><\n>
init2 BB4FF8E9 20000418<\r><\n>
This is quite interesting, seem "__init_array_end " somehow is not keept