1 /*
2  * Copyright (c) 2013-2014, Wind River Systems, Inc.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /*
8  * DESCRIPTION
9  * Platform independent, commonly used macros and defines related to linker
10  * script.
11  *
12  * This file may be included by:
13  * - Linker script files: for linker section declarations
14  * - C files: for external declaration of address or size of linker section
15  * - Assembly files: for external declaration of address or size of linker
16  *   section
17  */
18 
19 #ifndef ZEPHYR_INCLUDE_LINKER_LINKER_DEFS_H_
20 #define ZEPHYR_INCLUDE_LINKER_LINKER_DEFS_H_
21 
22 #include <zephyr/toolchain.h>
23 #include <zephyr/toolchain/common.h>
24 #include <zephyr/linker/sections.h>
25 #include <zephyr/sys/util.h>
26 #include <offsets.h>
27 
28 /* We need to dummy out DT_NODE_HAS_STATUS when building the unittests.
29  * Including devicetree.h would require generating dummy header files
30  * to match what gen_defines creates, so it's easier to just dummy out
31  * DT_NODE_HAS_STATUS.
32  */
33 #ifdef ZTEST_UNITTEST
34 #define DT_NODE_HAS_STATUS(node, status) 0
35 #else
36 #include <zephyr/devicetree.h>
37 #endif
38 
39 #ifdef _LINKER
40 /*
41  * generate a symbol to mark the start of the objects array for
42  * the specified object and level, then link all of those objects
43  * (sorted by priority). Ensure the objects aren't discarded if there is
44  * no direct reference to them
45  */
46 #define CREATE_OBJ_LEVEL(object, level)				\
47 		__##object##_##level##_start = .;		\
48 		KEEP(*(SORT(.z_##object##_##level?_*)));	\
49 		KEEP(*(SORT(.z_##object##_##level??_*)));
50 
51 /*
52  * link in shell initialization objects for all modules that use shell and
53  * their shell commands are automatically initialized by the kernel.
54  */
55 
56 #elif defined(_ASMLANGUAGE)
57 
58 /* Assembly FILES: declaration defined by the linker script */
59 GDATA(__bss_start)
60 GDATA(__bss_num_words)
61 #ifdef CONFIG_XIP
62 GDATA(__data_region_load_start)
63 GDATA(__data_region_start)
64 GDATA(__data_region_num_words)
65 #endif
66 
67 #else /* ! _ASMLANGUAGE */
68 
69 #include <zephyr/types.h>
70 /*
71  * Memory owned by the kernel, to be used as shared memory between
72  * application threads.
73  *
74  * The following are extern symbols from the linker. This enables
75  * the dynamic k_mem_domain and k_mem_partition creation and alignment
76  * to the section produced in the linker.
77 
78  * The policy for this memory will be to initially configure all of it as
79  * kernel / supervisor thread accessible.
80  */
81 extern char _app_smem_start[];
82 extern char _app_smem_end[];
83 extern char _app_smem_size[];
84 extern char _app_smem_rom_start[];
85 extern char _app_smem_num_words[];
86 
87 #ifdef CONFIG_LINKER_USE_PINNED_SECTION
88 extern char _app_smem_pinned_start[];
89 extern char _app_smem_pinned_end[];
90 extern char _app_smem_pinned_size[];
91 extern char _app_smem_pinned_num_words[];
92 #endif
93 
94 /* Memory owned by the kernel. Start and end will be aligned for memory
95  * management/protection hardware for the target architecture.
96  *
97  * Consists of all kernel-side globals, all kernel objects, all thread stacks,
98  * and all currently unused RAM.
99  *
100  * Except for the stack of the currently executing thread, none of this memory
101  * is normally accessible to user threads unless specifically granted at
102  * runtime.
103  */
104 extern char __kernel_ram_start[];
105 extern char __kernel_ram_end[];
106 extern char __kernel_ram_size[];
107 
108 /* Used by z_bss_zero or arch-specific implementation */
109 extern char __bss_start[];
110 extern char __bss_end[];
111 
112 /* Used by z_data_copy() or arch-specific implementation */
113 #ifdef CONFIG_XIP
114 extern char __data_region_load_start[];
115 extern char __data_region_start[];
116 extern char __data_region_end[];
117 #endif /* CONFIG_XIP */
118 
119 #ifdef CONFIG_MMU
120 /* Virtual addresses of page-aligned kernel image mapped into RAM at boot */
121 extern char z_mapped_start[];
122 extern char z_mapped_end[];
123 #endif /* CONFIG_MMU */
124 
125 /* Includes text and rodata */
126 extern char __rom_region_start[];
127 extern char __rom_region_end[];
128 extern char __rom_region_size[];
129 
130 /* Includes all ROMable data, i.e. the size of the output image file. */
131 extern char _flash_used[];
132 
133 /* datas, bss, noinit */
134 extern char _image_ram_start[];
135 extern char _image_ram_end[];
136 extern char _image_ram_size[];
137 
138 extern char __text_region_start[];
139 extern char __text_region_end[];
140 extern char __text_region_size[];
141 
142 extern char __rodata_region_start[];
143 extern char __rodata_region_end[];
144 extern char __rodata_region_size[];
145 
146 extern char _vector_start[];
147 extern char _vector_end[];
148 
149 #ifdef CONFIG_SW_VECTOR_RELAY
150 extern char __vector_relay_table[];
151 #endif
152 
153 #ifdef CONFIG_COVERAGE_GCOV
154 extern char __gcov_bss_start[];
155 extern char __gcov_bss_end[];
156 extern char __gcov_bss_size[];
157 #endif	/* CONFIG_COVERAGE_GCOV */
158 
159 /* end address of image, used by newlib for the heap */
160 extern char _end[];
161 
162 #if DT_NODE_HAS_STATUS(DT_CHOSEN(zephyr_ccm), okay)
163 extern char __ccm_data_rom_start[];
164 extern char __ccm_start[];
165 extern char __ccm_data_start[];
166 extern char __ccm_data_end[];
167 extern char __ccm_bss_start[];
168 extern char __ccm_bss_end[];
169 extern char __ccm_noinit_start[];
170 extern char __ccm_noinit_end[];
171 extern char __ccm_end[];
172 #endif
173 
174 #if DT_NODE_HAS_STATUS(DT_CHOSEN(zephyr_itcm), okay)
175 extern char __itcm_start[];
176 extern char __itcm_end[];
177 extern char __itcm_size[];
178 extern char __itcm_load_start[];
179 #endif
180 
181 #if DT_NODE_HAS_STATUS(DT_CHOSEN(zephyr_dtcm), okay)
182 extern char __dtcm_data_start[];
183 extern char __dtcm_data_end[];
184 extern char __dtcm_bss_start[];
185 extern char __dtcm_bss_end[];
186 extern char __dtcm_noinit_start[];
187 extern char __dtcm_noinit_end[];
188 extern char __dtcm_data_load_start[];
189 extern char __dtcm_start[];
190 extern char __dtcm_end[];
191 #endif
192 
193 #if DT_NODE_HAS_STATUS(DT_CHOSEN(zephyr_ocm), okay)
194 extern char __ocm_data_start[];
195 extern char __ocm_data_end[];
196 extern char __ocm_bss_start[];
197 extern char __ocm_bss_end[];
198 extern char __ocm_start[];
199 extern char __ocm_end[];
200 extern char __ocm_size[];
201 #endif
202 
203 /* Used by the Security Attribution Unit to configure the
204  * Non-Secure Callable region.
205  */
206 #ifdef CONFIG_ARM_FIRMWARE_HAS_SECURE_ENTRY_FUNCS
207 extern char __sg_start[];
208 extern char __sg_end[];
209 extern char __sg_size[];
210 #endif /* CONFIG_ARM_FIRMWARE_HAS_SECURE_ENTRY_FUNCS */
211 
212 /*
213  * Non-cached kernel memory region, currently only available on ARM Cortex-M7
214  * with a MPU. Start and end will be aligned for memory management/protection
215  * hardware for the target architecture.
216  *
217  * All the functions with '__nocache' keyword will be placed into this
218  * section.
219  */
220 #ifdef CONFIG_NOCACHE_MEMORY
221 extern char _nocache_ram_start[];
222 extern char _nocache_ram_end[];
223 extern char _nocache_ram_size[];
224 #endif /* CONFIG_NOCACHE_MEMORY */
225 
226 /* Memory owned by the kernel. Start and end will be aligned for memory
227  * management/protection hardware for the target architecture.
228  *
229  * All the functions with '__ramfunc' keyword will be placed into this
230  * section, stored in RAM instead of FLASH.
231  */
232 #ifdef CONFIG_ARCH_HAS_RAMFUNC_SUPPORT
233 extern char __ramfunc_start[];
234 extern char __ramfunc_end[];
235 extern char __ramfunc_size[];
236 extern char __ramfunc_load_start[];
237 #endif /* CONFIG_ARCH_HAS_RAMFUNC_SUPPORT */
238 
239 /* Memory owned by the kernel. Memory region for thread privilege stack buffers,
240  * currently only applicable on ARM Cortex-M architecture when building with
241  * support for User Mode.
242  *
243  * All thread privilege stack buffers will be placed into this section.
244  */
245 #ifdef CONFIG_USERSPACE
246 extern char z_priv_stacks_ram_start[];
247 extern char z_priv_stacks_ram_end[];
248 extern char z_user_stacks_start[];
249 extern char z_user_stacks_end[];
250 extern char z_kobject_data_begin[];
251 #endif /* CONFIG_USERSPACE */
252 
253 #ifdef CONFIG_THREAD_LOCAL_STORAGE
254 extern char __tdata_start[];
255 extern char __tdata_end[];
256 extern char __tdata_size[];
257 extern char __tdata_align[];
258 extern char __tbss_start[];
259 extern char __tbss_end[];
260 extern char __tbss_size[];
261 extern char __tbss_align[];
262 extern char __tls_start[];
263 extern char __tls_end[];
264 extern char __tls_size[];
265 #endif /* CONFIG_THREAD_LOCAL_STORAGE */
266 
267 #ifdef CONFIG_LINKER_USE_BOOT_SECTION
268 /* lnkr_boot_start[] and lnkr_boot_end[]
269  * must encapsulate all the boot sections.
270  */
271 extern char lnkr_boot_start[];
272 extern char lnkr_boot_end[];
273 
274 extern char lnkr_boot_text_start[];
275 extern char lnkr_boot_text_end[];
276 extern char lnkr_boot_text_size[];
277 extern char lnkr_boot_data_start[];
278 extern char lnkr_boot_data_end[];
279 extern char lnkr_boot_data_size[];
280 extern char lnkr_boot_rodata_start[];
281 extern char lnkr_boot_rodata_end[];
282 extern char lnkr_boot_rodata_size[];
283 extern char lnkr_boot_bss_start[];
284 extern char lnkr_boot_bss_end[];
285 extern char lnkr_boot_bss_size[];
286 extern char lnkr_boot_noinit_start[];
287 extern char lnkr_boot_noinit_end[];
288 extern char lnkr_boot_noinit_size[];
289 #endif /* CONFIG_LINKER_USE_BOOT_SECTION */
290 
291 #ifdef CONFIG_LINKER_USE_PINNED_SECTION
292 /* lnkr_pinned_start[] and lnkr_pinned_end[] must encapsulate
293  * all the pinned sections as these are used by
294  * the MMU code to mark the physical page frames with
295  * Z_PAGE_FRAME_PINNED.
296  */
297 extern char lnkr_pinned_start[];
298 extern char lnkr_pinned_end[];
299 
300 extern char lnkr_pinned_text_start[];
301 extern char lnkr_pinned_text_end[];
302 extern char lnkr_pinned_text_size[];
303 extern char lnkr_pinned_data_start[];
304 extern char lnkr_pinned_data_end[];
305 extern char lnkr_pinned_data_size[];
306 extern char lnkr_pinned_rodata_start[];
307 extern char lnkr_pinned_rodata_end[];
308 extern char lnkr_pinned_rodata_size[];
309 extern char lnkr_pinned_bss_start[];
310 extern char lnkr_pinned_bss_end[];
311 extern char lnkr_pinned_bss_size[];
312 extern char lnkr_pinned_noinit_start[];
313 extern char lnkr_pinned_noinit_end[];
314 extern char lnkr_pinned_noinit_size[];
315 
316 __pinned_func
317 static inline bool lnkr_is_pinned(uint8_t *addr)
318 {
319 	if ((addr >= (uint8_t *)lnkr_pinned_start) &&
320 	    (addr < (uint8_t *)lnkr_pinned_end)) {
321 		return true;
322 	} else {
323 		return false;
324 	}
325 }
326 
327 __pinned_func
328 static inline bool lnkr_is_region_pinned(uint8_t *addr, size_t sz)
329 {
330 	if ((addr >= (uint8_t *)lnkr_pinned_start) &&
331 	    ((addr + sz) < (uint8_t *)lnkr_pinned_end)) {
332 		return true;
333 	} else {
334 		return false;
335 	}
336 }
337 
338 #endif /* CONFIG_LINKER_USE_PINNED_SECTION */
339 
340 #endif /* ! _ASMLANGUAGE */
341 
342 #endif /* ZEPHYR_INCLUDE_LINKER_LINKER_DEFS_H_ */
343