1/* SPDX-License-Identifier: Apache-2.0 */
2
3#ifdef CONFIG_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.
15		 * Instead, Zephyr's start-up code uses the __ZEPHYR_CTOR_LIST__ and
16		 * __ZEHPYR_CTOR_END__ symbols.
17		 * In this way, in native_simulator based targets, the host glibc process
18		 * initialization code will not call the constructors before Zephyr loads.
19		 */
20#ifdef CONFIG_64BIT
21		. = ALIGN(8);
22		__ZEPHYR_CTOR_LIST__ = .;
23		QUAD((__ZEPHYR_CTOR_END__ - __ZEPHYR_CTOR_LIST__) / 8 - 2)
24		KEEP(*(SORT_BY_NAME(".ctors*")))
25		__CTOR_LIST__ = .;
26		QUAD(0)
27		__ZEPHYR_CTOR_END__ = .;
28		QUAD(0)
29		__CTOR_END__ = .;
30#else
31		. = ALIGN(4);
32		__ZEPHYR_CTOR_LIST__ = .;
33		LONG((__ZEPHYR_CTOR_END__ - __ZEPHYR_CTOR_LIST__) / 4 - 2)
34		KEEP(*(SORT_BY_NAME(".ctors*")))
35		__CTOR_LIST__ = .;
36		LONG(0)
37		__ZEPHYR_CTOR_END__ = .;
38		LONG(0)
39		__CTOR_END__ = .;
40#endif
41	} GROUP_ROM_LINK_IN(RAMABLE_REGION, ROMABLE_REGION)
42
43	SECTION_PROLOGUE(init_array,,)
44	{
45		/*
46		* Similar to the schenanigans required for the __CTOR_LIST__ and
47		* __CTOR_END__ symbols we define __init_array_start and __init_array_end
48		* to the same address to define an empty list. This prevents the glibc
49		* startup code from calling any global constructors before Zephyr loads.
50		*
51		* Zephyr's start-up code uses the __zephyr_init_array_start and
52		* __zephyr_init_array_end symbols, so these need to be set correctly.
53		*/
54		. = ALIGN(4);
55		__init_array_start = .;
56		__init_array_end = .;
57		__zephyr_init_array_start = .;
58		KEEP(*(SORT_BY_NAME(".init_array*")))
59		__zephyr_init_array_end = .;
60	} GROUP_ROM_LINK_IN(RAMABLE_REGION, ROMABLE_REGION)
61
62#elif defined(CONFIG_TOOLCHAIN_SUPPORTS_STATIC_INIT_GNU) && !defined(CONFIG_NATIVE_APPLICATION)
63	/*
64	 * If the code to invoke constructors is not enabled,
65	 * make sure there aren't any in the application
66	 */
67	SECTION_PROLOGUE(init_array,,)
68	{
69		KEEP(*(SORT_BY_NAME(".ctors*")))
70		KEEP(*(SORT_BY_NAME(".init_array*")))
71	} GROUP_ROM_LINK_IN(RAMABLE_REGION, ROMABLE_REGION)
72
73	ASSERT (SIZEOF(init_array) == 0,
74		"GNU-style constructors required but STATIC_INIT_GNU not enabled")
75#endif
76