Skip to content

Commit 1974bec

Browse files
committed
[C++] fix the ctors initialization issue
1 parent 38be10c commit 1974bec

File tree

4 files changed

+68
-3
lines changed

4 files changed

+68
-3
lines changed

components/cplusplus/README.md

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,44 @@ Because RT-Thread RTOS is used in embedded system mostly, there are some rules f
1010
4. Static class variables are discouraged. The time and place to call their constructor function could not be precisely controlled and make multi-threaded programming a nightmare.
1111
5. Multiple inheritance is strongly discouraged, as it can cause intolerable confusion.
1212

13-
*NOTE*: For armcc compiler, the libc must be enable.
13+
*NOTE*: The libc must be enable.
14+
15+
About GNU GCC compiler
16+
17+
please add following string in your ld link script:
18+
// in your .text section
19+
PROVIDE(__ctors_start__ = .);
20+
/* old GCC version uses .ctors */
21+
KEEP(*(SORT(.ctors.*)))
22+
KEEP(*(.ctors))
23+
/* new GCC version uses .init_array */
24+
KEEP (*(SORT(.init_array.*)))
25+
KEEP (*(.init_array))
26+
PROVIDE(__ctors_end__ = .);
27+
28+
. = ALIGN(4);
29+
30+
// as a standalone section if you use ARM target.
31+
32+
/* The .ARM.exidx section is used for C++ exception handling. */
33+
/* .ARM.exidx is sorted, so has to go in its own output section. */
34+
__exidx_start = .;
35+
.ARM.exidx :
36+
{
37+
*(.ARM.exidx* .gnu.linkonce.armexidx.*)
38+
39+
/* This is used by the startup in order to initialize the .data secion */
40+
_sidata = .;
41+
} > CODE
42+
__exidx_end = .;
43+
44+
/* .data section which is used for initialized data */
45+
46+
// in your .data section
47+
PROVIDE(__dtors_start__ = .);
48+
KEEP(*(SORT(.dtors.*)))
49+
KEEP(*(.dtors))
50+
PROVIDE(__dtors_end__ = .);
51+
52+
. = ALIGN(4);
53+

components/cplusplus/SConscript

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
from building import *
44

55
cwd = GetCurrentDir()
6-
src = Glob('*.cpp')
6+
src = Glob('*.cpp') + Glob('*.c')
77
CPPPATH = [cwd]
88

9-
group = DefineGroup('CPlusPlus', src, depend = ['RT_USING_CPLUSPLUS'], CPPPATH = CPPPATH)
9+
group = DefineGroup('CPlusPlus', src, depend = ['RT_USING_CPLUSPLUS', 'RT_USING_LIBC'], CPPPATH = CPPPATH)
1010

1111
Return('group')

components/cplusplus/crt.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ void operator delete(void * ptr);
1111
void operator delete[] (void *ptr);
1212

1313
extern "C" void __cxa_pure_virtual(void);
14+
extern "C" int cplusplus_system_init(void);
1415

1516
#endif

components/cplusplus/crt_init.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <rtthread.h>
2+
3+
int cplusplus_system_init(void)
4+
{
5+
#if defined(__GNUC__) && !defined(__CC_ARM)
6+
extern unsigned char __ctors_start__;
7+
extern unsigned char __ctors_end__;
8+
typedef void (*func)(void);
9+
10+
/* .ctors initalization */
11+
func *ctors_func;
12+
13+
for (ctors_func = (func *)&__ctors_start__;
14+
ctors_func < (func *)&__ctors_end__;
15+
ctors_func ++)
16+
{
17+
(*ctors_func)();
18+
}
19+
#endif
20+
21+
return 0;
22+
}
23+
INIT_COMPONENT_EXPORT(cplusplus_system_init);
24+

0 commit comments

Comments
 (0)