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 <kumar.gala@linaro.org>
Signed-off-by: Torsten Rasmussen <Torsten.Rasmussen@nordicsemi.no>
This commit is contained in:
Torsten Rasmussen 2021-05-25 23:05:57 +02:00 committed by Kumar Gala
commit 7d37fe21d1
5 changed files with 35 additions and 4 deletions

View file

@ -13,6 +13,15 @@ endif()
set_ifndef(LINKERFLAGPREFIX -Wl) 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
"<CMAKE_CXX_COMPILER> <FLAGS> <CMAKE_CXX_LINK_FLAGS> <LINK_FLAGS> ${LIBGCC_DIR}/crtbegin.o <OBJECTS> -o <TARGET> <LINK_LIBRARIES> ${LIBGCC_DIR}/crtend.o")
endif()
# Run $LINKER_SCRIPT file through the C preprocessor, producing ${linker_script_gen} # 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 # NOTE: ${linker_script_gen} will be produced at build-time; not at configure-time

View file

@ -130,7 +130,6 @@ SECTIONS
*(.text) *(.text)
*(".text.*") *(".text.*")
*(.gnu.linkonce.t.*) *(.gnu.linkonce.t.*)
*(.eh_frame)
#include <linker/kobject-text.ld> #include <linker/kobject-text.ld>
} GROUP_LINK_IN(ROMABLE_REGION) } GROUP_LINK_IN(ROMABLE_REGION)

View file

@ -316,8 +316,6 @@ SECTIONS
*(.text) *(.text)
*(".text.*") *(".text.*")
*(.gnu.linkonce.t.*) *(.gnu.linkonce.t.*)
*(.eh_frame_hdr)
*(.eh_frame)
*(.init) *(.init)
*(.fini) *(.fini)
*(.eini) *(.eini)

View file

@ -9,4 +9,24 @@
{ {
*(.gcc_except_table .gcc_except_table.*) *(.gcc_except_table .gcc_except_table.*)
} GROUP_DATA_LINK_IN(RAMABLE_REGION, ROMABLE_REGION) } 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 */

View file

@ -534,6 +534,11 @@ SECTIONS
#include <linker/common-ram.ld> #include <linker/common-ram.ld>
.tm_clone_table :
{
*(.tm_clone_table)
} >sram0_seg :sram0_phdr
.bss (NOLOAD) : ALIGN(8) .bss (NOLOAD) : ALIGN(8)
{ {
. = ALIGN (8); . = ALIGN (8);