From 7d37fe21d13d23ed938d44efaf0421949110e37e Mon Sep 17 00:00:00 2001 From: Torsten Rasmussen Date: Tue, 25 May 2021 23:05:57 +0200 Subject: [PATCH] cmake: c++ exceptions linking support Fixes part of: #32448 This commit updates the CMake CMAKE_CXX_LINK_EXECUTABLE to include crtbegin.o and crtend.o at the right locations when linking with gcc. It also updates linker scripts to ensure proper location of the exception header frame sections. This ensure proper handling of exceptions for those architectures - x86 - xtensa - riscv32 Signed-off-by: Kumar Gala Signed-off-by: Torsten Rasmussen --- cmake/linker/ld/target.cmake | 9 +++++++++ include/arch/riscv/common/linker.ld | 1 - include/arch/x86/ia32/linker.ld | 2 -- include/linker/cplusplus-ram.ld | 22 +++++++++++++++++++++- soc/xtensa/sample_controller/linker.ld | 5 +++++ 5 files changed, 35 insertions(+), 4 deletions(-) diff --git a/cmake/linker/ld/target.cmake b/cmake/linker/ld/target.cmake index 8cd1c6c48ff..e31e0eb7c28 100644 --- a/cmake/linker/ld/target.cmake +++ b/cmake/linker/ld/target.cmake @@ -13,6 +13,15 @@ endif() set_ifndef(LINKERFLAGPREFIX -Wl) +if(CONFIG_EXCEPTIONS) + # When building with C++ Exceptions, it is important that crtbegin and crtend + # are linked at specific locations. + # The location is so important that we cannot let this be controlled by normal + # link libraries, instead we must control the link command specifically as + # part of toolchain. + set(CMAKE_CXX_LINK_EXECUTABLE + " ${LIBGCC_DIR}/crtbegin.o -o ${LIBGCC_DIR}/crtend.o") +endif() # Run $LINKER_SCRIPT file through the C preprocessor, producing ${linker_script_gen} # NOTE: ${linker_script_gen} will be produced at build-time; not at configure-time diff --git a/include/arch/riscv/common/linker.ld b/include/arch/riscv/common/linker.ld index f5db23cd278..106658b056f 100644 --- a/include/arch/riscv/common/linker.ld +++ b/include/arch/riscv/common/linker.ld @@ -130,7 +130,6 @@ SECTIONS *(.text) *(".text.*") *(.gnu.linkonce.t.*) - *(.eh_frame) #include } GROUP_LINK_IN(ROMABLE_REGION) diff --git a/include/arch/x86/ia32/linker.ld b/include/arch/x86/ia32/linker.ld index eafe1bc089c..cc8e7671295 100644 --- a/include/arch/x86/ia32/linker.ld +++ b/include/arch/x86/ia32/linker.ld @@ -316,8 +316,6 @@ SECTIONS *(.text) *(".text.*") *(.gnu.linkonce.t.*) - *(.eh_frame_hdr) - *(.eh_frame) *(.init) *(.fini) *(.eini) diff --git a/include/linker/cplusplus-ram.ld b/include/linker/cplusplus-ram.ld index fd696df5938..43d3ef1ce04 100644 --- a/include/linker/cplusplus-ram.ld +++ b/include/linker/cplusplus-ram.ld @@ -9,4 +9,24 @@ { *(.gcc_except_table .gcc_except_table.*) } GROUP_DATA_LINK_IN(RAMABLE_REGION, ROMABLE_REGION) -#endif + +#if defined (CONFIG_EXCEPTIONS) + SECTION_PROLOGUE(.eh_frame_hdr,,) + { + *(.eh_frame_hdr) + } + + SECTION_PROLOGUE(.eh_frame,,) + { + KEEP (*(EXCLUDE_FILE (*crtend.o) .eh_frame)) + KEEP (*(.eh_frame)) + } GROUP_LINK_IN(ROMABLE_REGION) + + SECTION_PROLOGUE(.tm_clone_table,,) + { + KEEP (*(EXCLUDE_FILE (*crtend.o) .tm_clone_table)) + KEEP (*(.tm_clone_table)) + } GROUP_DATA_LINK_IN(RAMABLE_REGION, ROMABLE_REGION) +#endif /* CONFIG_EXCEPTIONS */ + +#endif /* CONFIG_CPLUSPLUS */ diff --git a/soc/xtensa/sample_controller/linker.ld b/soc/xtensa/sample_controller/linker.ld index 215c2f7247e..76edfe3919a 100644 --- a/soc/xtensa/sample_controller/linker.ld +++ b/soc/xtensa/sample_controller/linker.ld @@ -534,6 +534,11 @@ SECTIONS #include + .tm_clone_table : + { + *(.tm_clone_table) + } >sram0_seg :sram0_phdr + .bss (NOLOAD) : ALIGN(8) { . = ALIGN (8);