[riot-devel] RIOT and static initializers in C++

Joakim Gebart joakim.gebart at eistec.se
Fri Sep 19 19:31:20 CEST 2014


This is platform dependent. Short answer: You need to call every
function in the init_array list during startup, after .data and .bss
has been copied, but before you call main().

Long answer:

The C++ compiler will add pointers to a call to the constructor of
each static/global object to be initialized when the C runtime is
starting. This is done via the .init_array section (on ARM at least).
Thus in order to get working C++ global objects you'll need support
both in the linker script (ldscript) of the target, as well as the
startup code. If you want to see an example of how it can be done,
look at my WIP Mulle port for an implementation for Cortex-M
processors.

https://github.com/gebart/RIOT/blob/mulle/cpu/k60/startup.c function
call_init_array()

and something like the below in the linker script seem to work (see
https://github.com/gebart/RIOT/tree/mulle/cpu/k60/ldscripts):

/* preinit data */
PROVIDE_HIDDEN (__preinit_array_start = .);
KEEP(*(SORT(.preinit_array.*)))
KEEP(*(.preinit_array))
PROVIDE_HIDDEN (__preinit_array_end = .);
. = ALIGN(4);

/* init data */
PROVIDE_HIDDEN (__init_array_start = .);
KEEP(*(SORT(.init_array.*)))
KEEP(*(.init_array))
PROVIDE_HIDDEN (__init_array_end = .);
. = ALIGN(4);

/* fini data */
PROVIDE_HIDDEN (__fini_array_start = .);
KEEP(*(SORT(.fini_array.*)))
KEEP(*(.fini_array))
PROVIDE_HIDDEN (__fini_array_end = .);
. = ALIGN(4);

Best regards,
Joakim Gebart
Software and Hardware Engineer
Eistec AB

Aurorum 1C
977 75 Luleå
Tel: +46(0)70-570 66 35
joakim.gebart at eistec.se
www.eistec.se


On Fri, Sep 19, 2014 at 5:15 PM, Hiesgen, Raphael
<Raphael.Hiesgen at haw-hamburg.de> wrote:
> Hi,
>
> static initializers don’t seem to work on RIOT. Setting a breakpoint in the constructor reveals, that it is never called. I believe the static initialization should occur before calling main. If I am not mistaken, RIOT calls the function startup before the main.
>
> I attached an example program [1]. To try it on RIOT, copy the code into the cpp example. If foo is initialized correctly, data should be set to 1. However, the example prints 0.
>
> Usually the compiler should take care of the initialization. Does someone know why this initialization does not happen on RIOT?
>
> Thank you
> Raphael
>
> [1] https://gist.github.com/josephnoir/c948b78bf586ae4fa361
> _______________________________________________
> devel mailing list
> devel at riot-os.org
> http://lists.riot-os.org/mailman/listinfo/devel


More information about the devel mailing list