From 1b793ad039ae8915bfb5e0c88f343a12ae956461 Mon Sep 17 00:00:00 2001 From: Bernard Xiong Date: Sat, 1 Nov 2014 09:15:07 +0800 Subject: [PATCH 1/5] [BSP] use RT_USING_LIBC instead of RT_USING_NEWLIB --- bsp/lpc176x/rtconfig.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bsp/lpc176x/rtconfig.h b/bsp/lpc176x/rtconfig.h index 07553e2470..f93328fd37 100644 --- a/bsp/lpc176x/rtconfig.h +++ b/bsp/lpc176x/rtconfig.h @@ -90,8 +90,8 @@ // //
-// -// #define RT_USING_NEWLIB +// +// #define RT_USING_LIBC // // #define RT_USING_PTHREADS //
From 3bf721a6fbb15405f9738a27740977b6ac175f62 Mon Sep 17 00:00:00 2001 From: Bernard Xiong Date: Sat, 1 Nov 2014 09:18:21 +0800 Subject: [PATCH 2/5] [BSP] Update C++ compiling options --- bsp/lpc176x/SConstruct | 1 + bsp/lpc176x/rtconfig.py | 6 +++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/bsp/lpc176x/SConstruct b/bsp/lpc176x/SConstruct index 4afcf96602..681e6469c4 100644 --- a/bsp/lpc176x/SConstruct +++ b/bsp/lpc176x/SConstruct @@ -14,6 +14,7 @@ TARGET = 'rtthread-lpc17xx.' + rtconfig.TARGET_EXT env = Environment(tools = ['mingw'], AS = rtconfig.AS, ASFLAGS = rtconfig.AFLAGS, CC = rtconfig.CC, CCFLAGS = rtconfig.CFLAGS, + CXX = rtconfig.CXX, CXXFLAGS = rtconfig.CXXFLAGS, AR = rtconfig.AR, ARFLAGS = '-rc', LINK = rtconfig.LINK, LINKFLAGS = rtconfig.LFLAGS) diff --git a/bsp/lpc176x/rtconfig.py b/bsp/lpc176x/rtconfig.py index 17607f215e..b999555a09 100644 --- a/bsp/lpc176x/rtconfig.py +++ b/bsp/lpc176x/rtconfig.py @@ -26,7 +26,8 @@ BUILD = 'debug' if PLATFORM == 'gcc': # toolchains PREFIX = 'arm-none-eabi-' - CC = PREFIX + 'gcc' + CC = PREFIX + 'gcc' + CXX = PREFIX + 'g++' AS = PREFIX + 'gcc' AR = PREFIX + 'ar' LINK = PREFIX + 'gcc' @@ -37,6 +38,7 @@ if PLATFORM == 'gcc': DEVICE = ' -mcpu=cortex-m3 -mthumb' CFLAGS = DEVICE + CXXFLAGS = CFLAGS AFLAGS = ' -c' + DEVICE + ' -x assembler-with-cpp' LFLAGS = DEVICE + ' -Wl,--gc-sections,-Map=rtthread-lpc17xx.map,-cref,-u,Reset_Handler -T rtthread-lpc17xx.ld' @@ -54,6 +56,7 @@ if PLATFORM == 'gcc': elif PLATFORM == 'armcc': # toolchains CC = 'armcc' + CXX = 'armcc' AS = 'armasm' AR = 'armar' LINK = 'armlink' @@ -65,6 +68,7 @@ elif PLATFORM == 'armcc': LFLAGS = DEVICE + ' --info sizes --info totals --info unused --info veneers --list rtthread-lpc17xx.map --scatter rtthread-lpc17xx.sct' CFLAGS += ' -I' + EXEC_PATH + '/ARM/RV31/INC' + CXXFLAGS = CFLAGS LFLAGS += ' --libpath ' + EXEC_PATH + '/ARM/RV31/LIB' EXEC_PATH += '/arm/bin40/' From 796c49347e275065b8e902d031f5b27b5ada4eb1 Mon Sep 17 00:00:00 2001 From: Bernard Xiong Date: Sat, 1 Nov 2014 09:26:56 +0800 Subject: [PATCH 3/5] [BSP] Fix the gcc compiling issue in LPC408x bsp --- .../GCC/startup_LPC407x_8x_177x_8x.s | 42 ++++++++++--------- bsp/lpc408x/SConstruct | 1 + bsp/lpc408x/rtconfig.h | 4 +- bsp/lpc408x/rtconfig.py | 7 +++- 4 files changed, 31 insertions(+), 23 deletions(-) diff --git a/bsp/lpc408x/Libraries/Device/NXP/LPC407x_8x_177x_8x/Source/Templates/GCC/startup_LPC407x_8x_177x_8x.s b/bsp/lpc408x/Libraries/Device/NXP/LPC407x_8x_177x_8x/Source/Templates/GCC/startup_LPC407x_8x_177x_8x.s index f78e553073..7278ca26fa 100644 --- a/bsp/lpc408x/Libraries/Device/NXP/LPC407x_8x_177x_8x/Source/Templates/GCC/startup_LPC407x_8x_177x_8x.s +++ b/bsp/lpc408x/Libraries/Device/NXP/LPC407x_8x_177x_8x/Source/Templates/GCC/startup_LPC407x_8x_177x_8x.s @@ -102,18 +102,18 @@ __cs3_interrupt_vector_cortex_m: .long DMA_IRQHandler /* 42: General Purpose DMA */ .long I2S_IRQHandler /* 43: I2S */ .long ENET_IRQHandler /* 44: Ethernet */ - .long MCI_IRQHandler /* 45: SD/MMC Card */ + .long MCI_IRQHandler /* 45: SD/MMC Card */ .long MCPWM_IRQHandler /* 46: Motor Control PWM */ .long QEI_IRQHandler /* 47: Quadrature Encoder Interface */ .long PLL1_IRQHandler /* 48: PLL1 Lock (USB PLL) */ - .long USBActivity_IRQHandler /* 49: USB Activity */ - .long CANActivity_IRQHandler /* 50: CAN Activity */ - .long UART4_IRQHandler /* 51: UART4 */ - .long SSP2_IRQHandler /* 52: SSP2 */ - .long LCD_IRQHandler /* 53: LCD */ - .long GPIO_IRQHandler /* 54: GPIO */ - .long PWM0_IRQHandler /* 55: PWM0 */ - .long EEPROM_IRQHandler /* 56: EEPROM */ + .long USBActivity_IRQHandler /* 49: USB Activity */ + .long CANActivity_IRQHandler /* 50: CAN Activity */ + .long UART4_IRQHandler /* 51: UART4 */ + .long SSP2_IRQHandler /* 52: SSP2 */ + .long LCD_IRQHandler /* 53: LCD */ + .long GPIO_IRQHandler /* 54: GPIO */ + .long PWM0_IRQHandler /* 55: PWM0 */ + .long EEPROM_IRQHandler /* 56: EEPROM */ .size __cs3_interrupt_vector_cortex_m, . - __cs3_interrupt_vector_cortex_m @@ -130,22 +130,24 @@ CRP_Value: .section .cs3.reset,"x",%progbits .thumb_func .globl __cs3_reset_cortex_m + .globl Reset_Handler .type __cs3_reset_cortex_m, %function __cs3_reset_cortex_m: +Reset_Handler: .fnstart .ifdef RAM_MODE /* Clear .bss section (Zero init) */ - MOV R0, #0 - LDR R1, =__bss_start__ - LDR R2, =__bss_end__ - CMP R1,R2 - BEQ BSSIsEmpty + MOV R0, #0 + LDR R1, =__bss_start__ + LDR R2, =__bss_end__ + CMP R1,R2 + BEQ BSSIsEmpty LoopZI: - CMP R1, R2 - BHS BSSIsEmpty - STR R0, [R1] - ADD R1, #4 - BLO LoopZI + CMP R1, R2 + BHS BSSIsEmpty + STR R0, [R1] + ADD R1, #4 + BLO LoopZI BSSIsEmpty: LDR R0, =SystemInit BLX R0 @@ -154,7 +156,7 @@ BSSIsEmpty: .else LDR R0, =SystemInit BLX R0 - LDR R0,=_start + LDR R0,=main BX R0 .endif .pool diff --git a/bsp/lpc408x/SConstruct b/bsp/lpc408x/SConstruct index 5ee8731aea..fb728723bc 100644 --- a/bsp/lpc408x/SConstruct +++ b/bsp/lpc408x/SConstruct @@ -15,6 +15,7 @@ TARGET = 'rtthread-%s.%s' % (rtconfig.BOARD_NAME, rtconfig.TARGET_EXT) env = Environment(tools = ['mingw'], AS = rtconfig.AS, ASFLAGS = rtconfig.AFLAGS, CC = rtconfig.CC, CCFLAGS = rtconfig.CFLAGS, + CXX = rtconfig.CXX, CXXFLAGS = rtconfig.CXXFLAGS, AR = rtconfig.AR, ARFLAGS = '-rc', LINK = rtconfig.LINK, LINKFLAGS = rtconfig.LFLAGS) env.PrependENVPath('PATH', rtconfig.EXEC_PATH) diff --git a/bsp/lpc408x/rtconfig.h b/bsp/lpc408x/rtconfig.h index 23f4e757c3..defd591de3 100644 --- a/bsp/lpc408x/rtconfig.h +++ b/bsp/lpc408x/rtconfig.h @@ -104,8 +104,8 @@ // //
-// -// #define RT_USING_NEWLIB +// +// #define RT_USING_LIBC // #define RT_USING_PTHREADS //
diff --git a/bsp/lpc408x/rtconfig.py b/bsp/lpc408x/rtconfig.py index 4d97373f4e..e66b2449a3 100644 --- a/bsp/lpc408x/rtconfig.py +++ b/bsp/lpc408x/rtconfig.py @@ -29,7 +29,8 @@ BUILD = 'debug' if PLATFORM == 'gcc': # toolchains PREFIX = 'arm-none-eabi-' - CC = PREFIX + 'gcc' + CC = PREFIX + 'gcc' + CXX = PREFIX + 'g++' AS = PREFIX + 'gcc' AR = PREFIX + 'ar' LINK = PREFIX + 'gcc' @@ -52,11 +53,14 @@ if PLATFORM == 'gcc': else: CFLAGS += ' -O2' + CXXFLAGS = CFLAGS + POST_ACTION = OBJCPY + ' -O binary $TARGET rtthread.bin\n' + SIZE + ' $TARGET \n' elif PLATFORM == 'armcc': # toolchains CC = 'armcc' + CXX = 'armcc' AS = 'armasm' AR = 'armar' LINK = 'armlink' @@ -70,6 +74,7 @@ elif PLATFORM == 'armcc': CFLAGS += ' -I' + EXEC_PATH + '/ARM/RV31/INC' LFLAGS += ' --libpath ' + EXEC_PATH + '/ARM/RV31/LIB' + CXXFLAGS = CFLAGS EXEC_PATH += '/arm/bin40/' From 38be10cf89958872e15e650ba1271b234554d7e6 Mon Sep 17 00:00:00 2001 From: Bernard Xiong Date: Sat, 1 Nov 2014 14:06:51 +0800 Subject: [PATCH 4/5] [BSP] fix GCC compiling issue when enable C++ support --- bsp/lpc408x/rtconfig.py | 2 +- bsp/lpc408x/rtthread-lpc408x.ld | 23 ++++++++++++++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/bsp/lpc408x/rtconfig.py b/bsp/lpc408x/rtconfig.py index e66b2449a3..a2a6fac53f 100644 --- a/bsp/lpc408x/rtconfig.py +++ b/bsp/lpc408x/rtconfig.py @@ -33,7 +33,7 @@ if PLATFORM == 'gcc': CXX = PREFIX + 'g++' AS = PREFIX + 'gcc' AR = PREFIX + 'ar' - LINK = PREFIX + 'gcc' + LINK = PREFIX + 'g++' TARGET_EXT = 'elf' SIZE = PREFIX + 'size' OBJDUMP = PREFIX + 'objdump' diff --git a/bsp/lpc408x/rtthread-lpc408x.ld b/bsp/lpc408x/rtthread-lpc408x.ld index 021d51a504..adcfa03c36 100644 --- a/bsp/lpc408x/rtthread-lpc408x.ld +++ b/bsp/lpc408x/rtthread-lpc408x.ld @@ -38,10 +38,25 @@ SECTIONS __vsymtab_end = .; . = ALIGN(4); + PROVIDE(__ctors_start__ = .); + /* old GCC version uses .ctors */ + KEEP(*(SORT(.ctors.*))) + KEEP(*(.ctors)) + /* new GCC version uses .init_array */ + KEEP (*(SORT(.init_array.*))) + KEEP (*(.init_array)) + PROVIDE(__ctors_end__ = .); + . = ALIGN(4); _etext = .; } > CODE = 0 + .ARM.extab : + { + *(.ARM.extab*) + } > CODE + + /* The .ARM.exidx section is used for C++ exception handling. */ /* .ARM.exidx is sorted, so has to go in its own output section. */ __exidx_start = .; .ARM.exidx : @@ -57,6 +72,12 @@ SECTIONS .data : AT (_sidata) { + . = ALIGN(4); + PROVIDE(__dtors_start__ = .); + KEEP(*(SORT(.dtors.*))) + KEEP(*(.dtors)) + PROVIDE(__dtors_end__ = .); + . = ALIGN(4); /* This is used by the startup in order to initialize the .data secion */ _sdata = . ; @@ -68,7 +89,7 @@ SECTIONS . = ALIGN(4); /* This is used by the startup in order to initialize the .data secion */ _edata = . ; - } >DATA + } > DATA .stack : { From 1974bec8bf09e2e4e26bfc67ae0664c1b032eaae Mon Sep 17 00:00:00 2001 From: Bernard Xiong Date: Sat, 1 Nov 2014 14:12:58 +0800 Subject: [PATCH 5/5] [C++] fix the ctors initialization issue --- components/cplusplus/README.md | 42 ++++++++++++++++++++++++++++++++- components/cplusplus/SConscript | 4 ++-- components/cplusplus/crt.h | 1 + components/cplusplus/crt_init.c | 24 +++++++++++++++++++ 4 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 components/cplusplus/crt_init.c diff --git a/components/cplusplus/README.md b/components/cplusplus/README.md index cd133effaa..44851db56b 100644 --- a/components/cplusplus/README.md +++ b/components/cplusplus/README.md @@ -10,4 +10,44 @@ Because RT-Thread RTOS is used in embedded system mostly, there are some rules f 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. 5. Multiple inheritance is strongly discouraged, as it can cause intolerable confusion. -*NOTE*: For armcc compiler, the libc must be enable. +*NOTE*: The libc must be enable. + +About GNU GCC compiler + +please add following string in your ld link script: +// in your .text section + PROVIDE(__ctors_start__ = .); + /* old GCC version uses .ctors */ + KEEP(*(SORT(.ctors.*))) + KEEP(*(.ctors)) + /* new GCC version uses .init_array */ + KEEP (*(SORT(.init_array.*))) + KEEP (*(.init_array)) + PROVIDE(__ctors_end__ = .); + + . = ALIGN(4); + +// as a standalone section if you use ARM target. + + /* The .ARM.exidx section is used for C++ exception handling. */ + /* .ARM.exidx is sorted, so has to go in its own output section. */ + __exidx_start = .; + .ARM.exidx : + { + *(.ARM.exidx* .gnu.linkonce.armexidx.*) + + /* This is used by the startup in order to initialize the .data secion */ + _sidata = .; + } > CODE + __exidx_end = .; + + /* .data section which is used for initialized data */ + +// in your .data section + PROVIDE(__dtors_start__ = .); + KEEP(*(SORT(.dtors.*))) + KEEP(*(.dtors)) + PROVIDE(__dtors_end__ = .); + + . = ALIGN(4); + diff --git a/components/cplusplus/SConscript b/components/cplusplus/SConscript index eb95aaa3c2..b41537a52b 100644 --- a/components/cplusplus/SConscript +++ b/components/cplusplus/SConscript @@ -3,9 +3,9 @@ from building import * cwd = GetCurrentDir() -src = Glob('*.cpp') +src = Glob('*.cpp') + Glob('*.c') CPPPATH = [cwd] -group = DefineGroup('CPlusPlus', src, depend = ['RT_USING_CPLUSPLUS'], CPPPATH = CPPPATH) +group = DefineGroup('CPlusPlus', src, depend = ['RT_USING_CPLUSPLUS', 'RT_USING_LIBC'], CPPPATH = CPPPATH) Return('group') diff --git a/components/cplusplus/crt.h b/components/cplusplus/crt.h index 04fba47a60..fd287c2ad5 100644 --- a/components/cplusplus/crt.h +++ b/components/cplusplus/crt.h @@ -11,5 +11,6 @@ void operator delete(void * ptr); void operator delete[] (void *ptr); extern "C" void __cxa_pure_virtual(void); +extern "C" int cplusplus_system_init(void); #endif diff --git a/components/cplusplus/crt_init.c b/components/cplusplus/crt_init.c new file mode 100644 index 0000000000..20f81cdb54 --- /dev/null +++ b/components/cplusplus/crt_init.c @@ -0,0 +1,24 @@ +#include + +int cplusplus_system_init(void) +{ +#if defined(__GNUC__) && !defined(__CC_ARM) + extern unsigned char __ctors_start__; + extern unsigned char __ctors_end__; + typedef void (*func)(void); + + /* .ctors initalization */ + func *ctors_func; + + for (ctors_func = (func *)&__ctors_start__; + ctors_func < (func *)&__ctors_end__; + ctors_func ++) + { + (*ctors_func)(); + } +#endif + + return 0; +} +INIT_COMPONENT_EXPORT(cplusplus_system_init); +