1 /*
2  * Copyright (c) 2013-2014, Wind River Systems, Inc.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /**
8  * @file
9  * @brief GCC toolchain linker defs
10  *
11  * This header file defines the necessary macros used by the linker script for
12  * use with the GCC linker.
13  */
14 
15 #ifndef ZEPHYR_INCLUDE_LINKER_LINKER_TOOL_GCC_H_
16 #define ZEPHYR_INCLUDE_LINKER_LINKER_TOOL_GCC_H_
17 
18 #include <zephyr/sys/mem_manage.h>
19 
20 #if defined(CONFIG_ARM)
21 	#if defined(CONFIG_BIG_ENDIAN)
22 		#define OUTPUT_FORMAT_ "elf32-bigarm"
23 	#else
24 		#define OUTPUT_FORMAT_ "elf32-littlearm"
25 	#endif
26 	OUTPUT_FORMAT(OUTPUT_FORMAT_)
27 #elif defined(CONFIG_ARM64)
28 	OUTPUT_FORMAT("elf64-littleaarch64")
29 #elif defined(CONFIG_ARC)
30 	#if defined(CONFIG_ISA_ARCV3) && defined(CONFIG_64BIT)
31 		OUTPUT_FORMAT("elf64-littlearc64")
32 	#elif defined(CONFIG_ISA_ARCV3) && !defined(CONFIG_64BIT)
33 		OUTPUT_FORMAT("elf32-littlearc64")
34 	#else
35 		OUTPUT_FORMAT("elf32-littlearc", "elf32-bigarc", "elf32-littlearc")
36 	#endif
37 #elif defined(CONFIG_X86)
38 	#if defined(CONFIG_X86_64)
39 		OUTPUT_FORMAT("elf64-x86-64")
40 		OUTPUT_ARCH("i386:x86-64")
41 	#else
42 		OUTPUT_FORMAT("elf32-i386", "elf32-i386", "elf32-i386")
43 		OUTPUT_ARCH("i386")
44 	#endif
45 #elif defined(CONFIG_NIOS2)
46 	OUTPUT_FORMAT("elf32-littlenios2", "elf32-bignios2", "elf32-littlenios2")
47 #elif defined(CONFIG_RISCV)
48 	OUTPUT_ARCH("riscv")
49 #ifdef CONFIG_64BIT
50 	OUTPUT_FORMAT("elf64-littleriscv")
51 #else
52 	OUTPUT_FORMAT("elf32-littleriscv")
53 #endif
54 #elif defined(CONFIG_XTENSA)
55 	/* Not needed */
56 #elif defined(CONFIG_MIPS)
57 	OUTPUT_ARCH("mips")
58 #elif defined(CONFIG_ARCH_POSIX)
59 	/* Not needed */
60 #elif defined(CONFIG_SPARC)
61 	OUTPUT_FORMAT("elf32-sparc")
62 #else
63 	#error Arch not supported.
64 #endif
65 
66 /*
67  * The GROUP_START() and GROUP_END() macros are used to define a group
68  * of sections located in one memory area, such as RAM, ROM, etc.
69  * The <where> parameter is the name of the memory area.
70  */
71 #define GROUP_START(where)
72 #define GROUP_END(where)
73 
74 /**
75  * @def GROUP_LINK_IN
76  *
77  * Route memory to a specified memory area
78  *
79  * The GROUP_LINK_IN() macro is located at the end of the section
80  * description and tells the linker that this section is located in
81  * the memory area specified by 'where' argument.
82  *
83  * This macro is intentionally undefined for CONFIG_MMU systems when
84  * CONFIG_KERNEL_VM_BASE is not the same as CONFIG_SRAM_BASE_ADDRESS,
85  * as both the LMA and VMA destinations must be known for all sections
86  * as this corresponds to physical vs. virtual location.
87  *
88  * @param where Destination memory area
89  */
90 #if defined(CONFIG_ARCH_POSIX)
91 #define GROUP_LINK_IN(where)
92 #elif !defined(Z_VM_KERNEL)
93 #define GROUP_LINK_IN(where) > where
94 #endif
95 
96 /**
97  * @def GROUP_ROM_LINK_IN
98  *
99  * Route memory for a read-only section
100  *
101  * The GROUP_ROM_LINK_IN() macro is located at the end of the section
102  * description and tells the linker that this a read-only section
103  * that is physically placed at the 'lregion` argument.
104  *
105  * If CONFIG_XIP is active, the 'lregion' area is flash memory.
106  *
107  * If CONFIG_MMU is active, the vregion argument will be used to
108  * determine where this is located in the virtual memory map, otherwise
109  * it is ignored.
110  *
111  * @param vregion Output VMA (only used if CONFIG_MMU where LMA != VMA)
112  * @param lregion Output LMA
113  */
114 #if defined(CONFIG_ARCH_POSIX)
115 #define GROUP_ROM_LINK_IN(vregion, lregion)
116 #elif defined(Z_VM_KERNEL)
117 #define GROUP_ROM_LINK_IN(vregion, lregion) > vregion AT > lregion
118 #else
119 #define GROUP_ROM_LINK_IN(vregion, lregion) > lregion
120 #endif
121 
122 /**
123  * @def GROUP_DATA_LINK_IN
124  *
125  * Route memory for read-write sections that are loaded.
126  *
127  * Used for initialized data sections that on XIP platforms must be copied at
128  * startup.
129  *
130  * @param vregion Output VMA
131  * @param lregion Output LMA (only used if CONFIG_MMU if VMA != LMA,
132  *		  or CONFIG_XIP)
133  */
134 #if defined(CONFIG_ARCH_POSIX)
135 #define GROUP_DATA_LINK_IN(vregion, lregion)
136 #elif defined(CONFIG_XIP) || defined(Z_VM_KERNEL)
137 #define GROUP_DATA_LINK_IN(vregion, lregion) > vregion AT > lregion
138 #else
139 #define GROUP_DATA_LINK_IN(vregion, lregion) > vregion
140 #endif
141 
142 /**
143  * @def GROUP_NOLOAD_LINK_IN
144  *
145  * Route memory for read-write sections that are NOT loaded; typically this
146  * is only used for 'BSS' and 'noinit'.
147  *
148  * @param vregion Output VMA
149  * @param lregion Output LMA (only used if CONFIG_MMU if VMA != LMA,
150  *		  corresponds to physical location)
151  */
152 #if defined(CONFIG_ARCH_POSIX)
153 #define GROUP_NOLOAD_LINK_IN(vregion, lregion)
154 #elif defined(Z_VM_KERNEL)
155 #define GROUP_NOLOAD_LINK_IN(vregion, lregion) > vregion AT > lregion
156 #elif defined(CONFIG_XIP)
157 #define GROUP_NOLOAD_LINK_IN(vregion, lregion) > vregion AT > vregion
158 #else
159 #define GROUP_NOLOAD_LINK_IN(vregion, lregion) > vregion
160 #endif
161 
162 /**
163  * @def SECTION_PROLOGUE
164  *
165  * The SECTION_PROLOGUE() macro is used to define the beginning of a section.
166  *
167  * On MMU systems where VMA != LMA there is an implicit ALIGN_WITH_INPUT
168  * specified.
169  *
170  * @param name Name of the output section
171  * @param options Section options, such as (NOLOAD), or left blank
172  * @param align Alignment directives, such as SUBALIGN(). ALIGN() itself is
173  *              not allowed. May be blank.
174  */
175 #ifdef Z_VM_KERNEL
176 /* If we have a virtual memory map we need ALIGN_WITH_INPUT in all sections */
177 #define SECTION_PROLOGUE(name, options, align) \
178 	name options : ALIGN_WITH_INPUT align
179 #else
180 #define SECTION_PROLOGUE(name, options, align) \
181 	name options : align
182 #endif
183 
184 /**
185  * @def SECTION_DATA_PROLOGUE
186  *
187  * Same as for SECTION_PROLOGUE(), except that this one must be used
188  * for data sections which on XIP platforms will have differing
189  * virtual and load addresses (i.e. they'll be copied into RAM at
190  * program startup).  Such a section must also use
191  * GROUP_DATA_LINK_IN to specify the correct output load address.
192  *
193  * This is equivalent to SECTION_PROLOGUE() on non-XIP systems.
194  * On XIP systems there is an implicit ALIGN_WITH_INPUT specified.
195  *
196  * @param name Name of the output section
197  * @param options Section options, or left blank
198  * @param align Alignment directives, such as SUBALIGN(). ALIGN() itself is
199  *              not allowed. May be blank.
200  */
201 #if defined(CONFIG_XIP)
202 #define SECTION_DATA_PROLOGUE(name, options, align) \
203 	name options : ALIGN_WITH_INPUT align
204 #else
205 #define SECTION_DATA_PROLOGUE(name, options, align) \
206 	SECTION_PROLOGUE(name, options, align)
207 #endif
208 
209 #define COMMON_SYMBOLS *(COMMON)
210 
211 #endif /* ZEPHYR_INCLUDE_LINKER_LINKER_TOOL_GCC_H_ */
212