Skip to content

Commit

Permalink
Revert "lib c/cpp: Move .ctor .init_array handling from C++ to kernel"
Browse files Browse the repository at this point in the history
Unfortunately this breaks native_sim fuzzing due (presumably) to
interactions with the .ctors emitted by the libc/sanitizer layer.

See SOF discussion at thesofproject/sof#9278

This reverts commit 6e977ae.

Signed-off-by: Andy Ross <andyross@google.com>
  • Loading branch information
andyross committed Jul 3, 2024
1 parent 650227d commit b9725da
Show file tree
Hide file tree
Showing 22 changed files with 131 additions and 149 deletions.
8 changes: 4 additions & 4 deletions include/zephyr/arch/arc/v2/linker.ld
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,12 @@ SECTIONS {
#include <snippets-rodata.ld>
#include <zephyr/linker/kobject-rom.ld>

#if defined(CONFIG_CPP) && !defined(CONFIG_STATIC_INIT_GNU) && defined(__MWDT_LINKER_CMD__)
#if defined(CONFIG_CPP) && !defined(CONFIG_CPP_STATIC_INIT_GNU) && defined(__MWDT_LINKER_CMD__)
. = ALIGN(4);
_fctors = .;
KEEP(*(.ctors*))
_ectors = .;
#endif /* CONFIG_CPP && !CONFIG_STATIC_INIT_GNU && __MWDT_LINKER_CMD__ */
#endif /* CONFIG_CPP && !CONFIG_CPP_STATIC_INIT_GNU && __MWDT_LINKER_CMD__ */

/* This extra MPU alignment of RAMABLE_REGION is only required if we put ROMABLE_REGION and
* RAMABLE_REGION into the same (continuous) memory - otherwise we can get beginning of the
Expand Down Expand Up @@ -311,11 +311,11 @@ SECTIONS {
#endif

/DISCARD/ : {
#if defined(CONFIG_CPP) && !defined(CONFIG_STATIC_INIT_GNU) && defined(__MWDT_LINKER_CMD__)
#if defined(CONFIG_CPP) && !defined(CONFIG_CPP_STATIC_INIT_GNU) && defined(__MWDT_LINKER_CMD__)
*(.dtors*)
*(.fini*)
*(.eh_frame*)
#endif /* CONFIG_CPP && !CONFIG_STATIC_INIT_GNU && __MWDT_LINKER_CMD__ */
#endif /* CONFIG_CPP && !CONFIG_CPP_STATIC_INIT_GNU && __MWDT_LINKER_CMD__ */
*(.note.GNU-stack)
*(.got.plt)
*(.igot.plt)
Expand Down
2 changes: 1 addition & 1 deletion include/zephyr/linker/common-rom.ld
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#include <zephyr/linker/common-rom/common-rom-ztest.ld>

#include <zephyr/linker/common-rom/common-rom-init.ld>
#include <zephyr/linker/common-rom/common-rom-cpp.ld>

#include <zephyr/linker/common-rom/common-rom-net.ld>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* SPDX-License-Identifier: Apache-2.0 */

#ifdef CONFIG_STATIC_INIT_GNU
#ifdef CONFIG_CPP_STATIC_INIT_GNU
SECTION_PROLOGUE(_CTOR_SECTION_NAME,,)
{
/*
Expand Down Expand Up @@ -59,18 +59,4 @@
KEEP(*(SORT_BY_NAME(".init_array*")))
__zephyr_init_array_end = .;
} GROUP_ROM_LINK_IN(RAMABLE_REGION, ROMABLE_REGION)

#elif defined(CONFIG_TOOLCHAIN_SUPPORTS_STATIC_INIT_GNU)
/*
* If the code to invoke constructors is not enabled,
* make sure there aren't any in the application
*/
SECTION_PROLOGUE(init_array,,)
{
KEEP(*(SORT_BY_NAME(".ctors*")))
KEEP(*(SORT_BY_NAME(".init_array*")))
} GROUP_ROM_LINK_IN(RAMABLE_REGION, ROMABLE_REGION)

ASSERT (SIZEOF(init_array) == 0,
"GNU-style constructors required but STATIC_INIT_GNU not enabled")
#endif
1 change: 0 additions & 1 deletion kernel/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ list(APPEND kernel_files
errno.c
fatal.c
init.c
init_static.c
kheap.c
mem_slab.c
float.c
Expand Down
23 changes: 0 additions & 23 deletions kernel/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -983,29 +983,6 @@ config KERNEL_WHOLE_ARCHIVE
This option forces every object file in the libkernel.a archive
to be included, rather than searching the archive for required object files.

config TOOLCHAIN_SUPPORTS_STATIC_INIT_GNU
# As of today only ARC MWDT toolchain doesn't support GNU-compatible
# initialization of static objects, new toolchains can be added
# here if required.
def_bool "$(ZEPHYR_TOOLCHAIN_VARIANT)" != "arcmwdt"

config STATIC_INIT_GNU
bool "Support GNU-compatible initializers and constructors"
default y if CPP || NATIVE_BUILD || COVERAGE
depends on TOOLCHAIN_SUPPORTS_STATIC_INIT_GNU
depends on !CMAKE_LINKER_GENERATOR
help
GNU-compatible initialization of static objects. This is required for
C++ constructor support as well as for initializer functions as
defined by GNU-compatible toolchains. This increases the size
of Zephyr binaries by around 100 bytes. If you know your
application doesn't need any initializers, you can disable this
option.
The ARC MWDT toolchain, does not support or use this setting,
and has instead separate C++ constructor initialization code.
Note the option CMAKE_LINKER_GENERATOR does not yet support this feature
or CPP.

endmenu

rsource "Kconfig.device"
Expand Down
6 changes: 4 additions & 2 deletions kernel/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -433,8 +433,10 @@ static void bg_thread_main(void *unused1, void *unused2, void *unused3)
#endif /* CONFIG_STACK_POINTER_RANDOM */
boot_banner();

void z_init_static(void);
z_init_static();
#if defined(CONFIG_CPP)
void z_cpp_init_static(void);
z_cpp_init_static();
#endif /* CONFIG_CPP */

/* Final init level before app starts */
z_sys_init_run_level(INIT_LEVEL_APPLICATION);
Expand Down
87 changes: 0 additions & 87 deletions kernel/init_static.c

This file was deleted.

10 changes: 5 additions & 5 deletions lib/cpp/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,12 @@ config CPP_RTTI
endif # !MINIMAL_LIBCPP

config CPP_STATIC_INIT_GNU
bool
select STATIC_INIT_GNU
select DEPRECATED
# As of today only ARC MWDT toolchain doesn't support GNU-compatible
# initialization of CPP static objects, new toolchains can be added
# here if required.
def_bool "$(ZEPHYR_TOOLCHAIN_VARIANT)" != "arcmwdt"
help
GNU-compatible initialization of CPP static objects.
This option is deprecated in favour of STATIC_INIT_GNU
GNU-compatible initialization of CPP static objects

endif # CPP

Expand Down
6 changes: 5 additions & 1 deletion lib/cpp/abi/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# SPDX-License-Identifier: Apache-2.0

zephyr_sources_ifdef(CONFIG_STATIC_INIT_GNU
zephyr_sources(cpp_init.c)

zephyr_sources_ifdef(CONFIG_CPP_STATIC_INIT_GNU
cpp_init_array.c
cpp_ctors.c
cpp_dtors.c
)
43 changes: 43 additions & 0 deletions lib/cpp/abi/cpp_ctors.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright (c) 2012-2014 Wind River Systems, Inc.
*
* SPDX-License-Identifier: Apache-2.0
*/

/**
* @file - Constructor module
* @brief
* The ctors section contains a list of function pointers that execute the
* C++ constructors of static global objects. These must be executed before
* the application's main() routine.
*
* NOTE: Not all compilers put those function pointers into the ctors section;
* some put them into the init_array section instead.
*/

/* What a constructor function pointer looks like */

typedef void (*CtorFuncPtr)(void);

/* Constructor function pointer list is generated by the linker script. */

extern CtorFuncPtr __ZEPHYR_CTOR_LIST__[];
extern CtorFuncPtr __ZEPHYR_CTOR_END__[];

/**
*
* @brief Invoke all C++ style global object constructors
*
* This routine is invoked by the kernel prior to the execution of the
* application's main().
*/
void __do_global_ctors_aux(void)
{
unsigned int nCtors;

nCtors = (unsigned long)__ZEPHYR_CTOR_LIST__[0];

while (nCtors >= 1U) {
__ZEPHYR_CTOR_LIST__[nCtors--]();
}
}
31 changes: 31 additions & 0 deletions lib/cpp/abi/cpp_init.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (c) 2021 Synopsys, Inc.
*
* SPDX-License-Identifier: Apache-2.0
*
* Author: Evgeniy Paltsev
*/

#ifdef CONFIG_CPP_STATIC_INIT_GNU

void __do_global_ctors_aux(void);
void __do_init_array_aux(void);

void z_cpp_init_static(void)
{
__do_global_ctors_aux();
__do_init_array_aux();
}

#else

#ifdef __CCAC__
void __do_global_ctors_aux(void);

void z_cpp_init_static(void)
{
__do_global_ctors_aux();
}
#endif /* __CCAC__ */

#endif /* CONFIG_CPP_STATIC_INIT_GNU */
27 changes: 27 additions & 0 deletions lib/cpp/abi/cpp_init_array.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright (c) 2015 Wind River Systems, Inc.
*
* SPDX-License-Identifier: Apache-2.0
*/

/*
* @file
* @brief Execute initialization routines referenced in .init_array section
*/

typedef void (*func_ptr)(void);

extern func_ptr __zephyr_init_array_start[];
extern func_ptr __zephyr_init_array_end[];

/**
* @brief Execute initialization routines referenced in .init_array section
*/
void __do_init_array_aux(void)
{
for (func_ptr *func = __zephyr_init_array_start;
func < __zephyr_init_array_end;
func++) {
(*func)();
}
}
2 changes: 1 addition & 1 deletion soc/espressif/esp32/default.ld
Original file line number Diff line number Diff line change
Expand Up @@ -809,7 +809,7 @@ SECTIONS
. = ALIGN(4);
} GROUP_DATA_LINK_IN(RODATA_REGION, ROMABLE_REGION)

#include <zephyr/linker/common-rom/common-rom-init.ld>
#include <zephyr/linker/common-rom/common-rom-cpp.ld>
#include <zephyr/linker/common-rom/common-rom-kernel-devices.ld>
#include <zephyr/linker/common-rom/common-rom-ztest.ld>
#include <zephyr/linker/common-rom/common-rom-net.ld>
Expand Down
2 changes: 1 addition & 1 deletion soc/espressif/esp32/default_appcpu.ld
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ SECTIONS
. = ALIGN(4);
} GROUP_DATA_LINK_IN(RODATA_REGION, ROMABLE_DATA_REGION)

#include <zephyr/linker/common-rom/common-rom-init.ld>
#include <zephyr/linker/common-rom/common-rom-cpp.ld>
#include <zephyr/linker/common-rom/common-rom-kernel-devices.ld>
#include <zephyr/linker/common-rom/common-rom-ztest.ld>
#include <zephyr/linker/common-rom/common-rom-net.ld>
Expand Down
2 changes: 1 addition & 1 deletion soc/espressif/esp32c3/default.ld
Original file line number Diff line number Diff line change
Expand Up @@ -777,7 +777,7 @@ SECTIONS
} GROUP_DATA_LINK_IN(RODATA_REGION, ROMABLE_REGION)

#include <zephyr/linker/cplusplus-rom.ld>
#include <zephyr/linker/common-rom/common-rom-init.ld>
#include <zephyr/linker/common-rom/common-rom-cpp.ld>
#include <zephyr/linker/common-rom/common-rom-kernel-devices.ld>
#include <zephyr/linker/common-rom/common-rom-ztest.ld>
#include <zephyr/linker/common-rom/common-rom-net.ld>
Expand Down
2 changes: 1 addition & 1 deletion soc/espressif/esp32c3/mcuboot.ld
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ SECTIONS
. = ALIGN(4);
} > dram_seg

#include <zephyr/linker/common-rom/common-rom-init.ld>
#include <zephyr/linker/common-rom/common-rom-cpp.ld>
#include <zephyr/linker/common-rom/common-rom-kernel-devices.ld>
#include <zephyr/linker/common-rom/common-rom-debug.ld>
#include <zephyr/linker/common-rom/common-rom-misc.ld>
Expand Down
2 changes: 1 addition & 1 deletion soc/espressif/esp32c6/default.ld
Original file line number Diff line number Diff line change
Expand Up @@ -777,7 +777,7 @@ SECTIONS
} GROUP_DATA_LINK_IN(CACHED_REGION, ROMABLE_REGION)

#include <zephyr/linker/cplusplus-rom.ld>
#include <zephyr/linker/common-rom/common-rom-init.ld>
#include <zephyr/linker/common-rom/common-rom-cpp.ld>
#include <zephyr/linker/common-rom/common-rom-kernel-devices.ld>
#include <zephyr/linker/common-rom/common-rom-ztest.ld>
#include <zephyr/linker/common-rom/common-rom-net.ld>
Expand Down
2 changes: 1 addition & 1 deletion soc/espressif/esp32c6/mcuboot.ld
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ SECTIONS
. = ALIGN(4);
} > dram_seg

#include <zephyr/linker/common-rom/common-rom-init.ld>
#include <zephyr/linker/common-rom/common-rom-cpp.ld>
#include <zephyr/linker/common-rom/common-rom-kernel-devices.ld>
#include <zephyr/linker/common-rom/common-rom-debug.ld>
#include <zephyr/linker/common-rom/common-rom-misc.ld>
Expand Down
2 changes: 1 addition & 1 deletion soc/espressif/esp32s2/default.ld
Original file line number Diff line number Diff line change
Expand Up @@ -913,7 +913,7 @@ SECTIONS
} GROUP_DATA_LINK_IN(RODATA_REGION, ROMABLE_REGION)

#include <zephyr/linker/cplusplus-rom.ld>
#include <zephyr/linker/common-rom/common-rom-init.ld>
#include <zephyr/linker/common-rom/common-rom-cpp.ld>
#include <zephyr/linker/common-rom/common-rom-kernel-devices.ld>
#include <zephyr/linker/common-rom/common-rom-ztest.ld>
#include <zephyr/linker/common-rom/common-rom-net.ld>
Expand Down
Loading

0 comments on commit b9725da

Please sign in to comment.