1/* SPDX-License-Identifier: Apache-2.0 */
2
3#ifdef CONFIG_CPP_STATIC_INIT_GNU
4	SECTION_PROLOGUE(_CTOR_SECTION_NAME,,)
5	{
6		/*
7		 * The compiler fills the constructor pointers table below,
8		 * hence symbol __CTOR_LIST__ must be aligned on word
9		 * boundary.  To align with the C++ standard, the first element
10		 * of the array contains the number of actual constructors. The
11		 * last element is NULL.
12		 *
13		 * The __CTOR_LIST__ and __CTOR_END__ symbols are always defined
14		 * to result in an empty list. This is necessary to fix an issue
15		 * where the glibc process initialization code on native_posix
16		 * platforms calls constructors before Zephyr loads (issue #39347).
17		 *
18		 * Zephyr's start-up code uses the __ZEPHYR_CTOR_LIST__ and
19		 * __ZEHPYR_CTOR_END__ symbols, so these need to be correctly set.
20		 */
21#ifdef CONFIG_64BIT
22		. = ALIGN(8);
23		__ZEPHYR_CTOR_LIST__ = .;
24		QUAD((__ZEPHYR_CTOR_END__ - __ZEPHYR_CTOR_LIST__) / 8 - 2)
25		KEEP(*(SORT_BY_NAME(".ctors*")))
26		__CTOR_LIST__ = .;
27		QUAD(0)
28		__ZEPHYR_CTOR_END__ = .;
29		QUAD(0)
30		__CTOR_END__ = .;
31#else
32		. = ALIGN(4);
33		__ZEPHYR_CTOR_LIST__ = .;
34		LONG((__ZEPHYR_CTOR_END__ - __ZEPHYR_CTOR_LIST__) / 4 - 2)
35		KEEP(*(SORT_BY_NAME(".ctors*")))
36		__CTOR_LIST__ = .;
37		LONG(0)
38		__ZEPHYR_CTOR_END__ = .;
39		LONG(0)
40		__CTOR_END__ = .;
41#endif
42	} GROUP_ROM_LINK_IN(RAMABLE_REGION, ROMABLE_REGION)
43
44	SECTION_PROLOGUE(init_array,,)
45	{
46		/*
47		* Similar to the schenanigans required for the __CTOR_LIST__ and
48		* __CTOR_END__ symbols we define __init_array_start and __init_array_end
49		* to the same address to define an empty list. This prevents the glibc
50		* startup code from calling any global constructors before Zephyr loads.
51		*
52		* Zephyr's start-up code uses the __zephyr_init_array_start and
53		* __zephyr_init_array_end sybmols, so these need to be set correctly.
54		*/
55		. = ALIGN(4);
56		__init_array_start = .;
57		__init_array_end = .;
58		__zephyr_init_array_start = .;
59		KEEP(*(SORT_BY_NAME(".init_array*")))
60		__zephyr_init_array_end = .;
61	} GROUP_ROM_LINK_IN(RAMABLE_REGION, ROMABLE_REGION)
62#endif
63