xref: /nrf52832-nimble/rt-thread/components/cplusplus/crt_init.c (revision 104654410c56c573564690304ae786df310c91fc)
1 /*
2  * Copyright (c) 2006-2018, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6 * Change Logs:
7 * Date           Author       Notes
8 * 2014-12-03     Bernard      Add copyright header.
9 * 2014-12-29     Bernard      Add cplusplus initialization for ARMCC.
10 * 2016-06-28     Bernard      Add _init/_fini routines for GCC.
11 * 2016-10-02     Bernard      Add WEAK for cplusplus_system_init routine.
12 */
13 
14 #include <rtthread.h>
15 
16 #ifdef __CC_ARM
17 extern void $Super$$__cpp_initialize__aeabi_(void);
18 /* we need to change the cpp_initialize order */
$Sub$$__cpp_initialize__aeabi_(void)19 void $Sub$$__cpp_initialize__aeabi_(void)
20 {
21     /* empty */
22 }
23 #elif defined(__GNUC__) && !defined(__CS_SOURCERYGXX_MAJ__)
24 /* The _init()/_fini() routines has been defined in codesourcery g++ lite */
_init()25 RT_WEAK void _init()
26 {
27 }
28 
_fini()29 RT_WEAK void _fini()
30 {
31 }
32 
33 RT_WEAK void *__dso_handle = 0;
34 
35 #endif
36 
37 RT_WEAK
cplusplus_system_init(void)38 int cplusplus_system_init(void)
39 {
40 #if defined(__GNUC__) && !defined(__CC_ARM)
41     typedef void (*pfunc) ();
42     extern pfunc __ctors_start__[];
43     extern pfunc __ctors_end__[];
44     pfunc *p;
45 
46     for (p = __ctors_start__; p < __ctors_end__; p++)
47         (*p)();
48 
49 #elif defined(__CC_ARM)
50     /* If there is no SHT$$INIT_ARRAY, calling
51      * $Super$$__cpp_initialize__aeabi_() will cause fault. At least until Keil5.12
52      * the problem still exists. So we have to initialize the C++ runtime by ourself.
53      */
54     typedef void PROC();
55     extern const unsigned long SHT$$INIT_ARRAY$$Base[];
56     extern const unsigned long SHT$$INIT_ARRAY$$Limit[];
57 
58     const unsigned long *base = SHT$$INIT_ARRAY$$Base;
59     const unsigned long *lim  = SHT$$INIT_ARRAY$$Limit;
60 
61     for (; base != lim; base++)
62     {
63         PROC *proc = (PROC*)((const char*)base + *base);
64         (*proc)();
65     }
66 #endif
67 
68     return 0;
69 }
70 INIT_COMPONENT_EXPORT(cplusplus_system_init);
71 
72