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