1set(COMMON_ZEPHYR_LINKER_DIR ${ZEPHYR_BASE}/cmake/linker_script/common)
2
3set_ifndef(region_min_align CONFIG_CUSTOM_SECTION_MIN_ALIGN_SIZE)
4
5# Set alignment to CONFIG_ARM_MPU_REGION_MIN_ALIGN_AND_SIZE if not set above
6# to make linker section alignment comply with MPU granularity.
7set_ifndef(region_min_align CONFIG_ARM_MPU_REGION_MIN_ALIGN_AND_SIZE)
8
9# If building without MPU support, use default 4-byte alignment.. if not set above.
10set_ifndef(region_min_align 4)
11
12# Note, the `+ 0` in formulas below avoids errors in cases where a Kconfig
13#       variable is undefined and thus expands to nothing.
14math(EXPR FLASH_ADDR
15     "${CONFIG_FLASH_BASE_ADDRESS} + ${CONFIG_FLASH_LOAD_OFFSET} + 0"
16     OUTPUT_FORMAT HEXADECIMAL
17)
18
19if(CONFIG_FLASH_LOAD_SIZE GREATER 0)
20  math(EXPR FLASH_SIZE
21       "(${CONFIG_FLASH_LOAD_SIZE} + 0) - (${CONFIG_ROM_END_OFFSET} + 0)"
22       OUTPUT_FORMAT HEXADECIMAL
23  )
24else()
25  math(EXPR FLASH_SIZE
26       "(${CONFIG_FLASH_SIZE} + 0) * 1024 - (${CONFIG_FLASH_LOAD_OFFSET} + 0) - (${CONFIG_ROM_END_OFFSET} + 0)"
27       OUTPUT_FORMAT HEXADECIMAL
28  )
29endif()
30
31set(RAM_ADDR ${CONFIG_SRAM_BASE_ADDRESS})
32math(EXPR RAM_SIZE "(${CONFIG_SRAM_SIZE} + 0) * 1024" OUTPUT_FORMAT HEXADECIMAL)
33math(EXPR IDT_ADDR "${RAM_ADDR} + ${RAM_SIZE}" OUTPUT_FORMAT HEXADECIMAL)
34
35# ToDo: decide on the optimal location for this.
36# linker/ld/target.cmake based on arch, or directly in arch and scatter_script.cmake can ignore
37zephyr_linker(FORMAT "elf32-littlearm")
38zephyr_linker(ENTRY ${CONFIG_KERNEL_ENTRY})
39
40zephyr_linker_memory(NAME FLASH    FLAGS rx START ${FLASH_ADDR} SIZE ${FLASH_SIZE})
41zephyr_linker_memory(NAME RAM      FLAGS wx START ${RAM_ADDR}   SIZE ${RAM_SIZE})
42zephyr_linker_memory(NAME IDT_LIST FLAGS wx START ${IDT_ADDR}   SIZE 2K)
43
44dt_comp_path(paths COMPATIBLE "zephyr,memory-region")
45foreach(path IN LISTS paths)
46  zephyr_linker_dts_memory(PATH ${path})
47endforeach()
48
49if(CONFIG_XIP)
50  zephyr_linker_group(NAME ROM_REGION LMA FLASH)
51  set(rom_start ${FLASH_ADDR})
52  set(XIP_ALIGN_WITH_INPUT ALIGN_WITH_INPUT)
53else()
54  zephyr_linker_group(NAME ROM_REGION LMA RAM)
55  set(rom_start ${RAM_ADDR})
56endif()
57
58zephyr_linker_group(NAME RAM_REGION VMA RAM LMA ROM_REGION)
59zephyr_linker_group(NAME TEXT_REGION GROUP ROM_REGION SYMBOL SECTION)
60zephyr_linker_group(NAME RODATA_REGION GROUP ROM_REGION)
61zephyr_linker_group(NAME DATA_REGION GROUP RAM_REGION SYMBOL SECTION)
62
63# should go to a relocation.cmake - from include/linker/rel-sections.ld - start
64zephyr_linker_section(NAME  .rel.plt  HIDDEN)
65zephyr_linker_section(NAME  .rela.plt HIDDEN)
66zephyr_linker_section(NAME  .rel.dyn)
67zephyr_linker_section(NAME  .rela.dyn)
68# should go to a relocation.cmake - from include/linker/rel-sections.ld - end
69
70# Discard sections for GNU ld.
71zephyr_linker_section_configure(SECTION /DISCARD/ INPUT ".plt")
72zephyr_linker_section_configure(SECTION /DISCARD/ INPUT ".iplt")
73zephyr_linker_section_configure(SECTION /DISCARD/ INPUT ".got.plt")
74zephyr_linker_section_configure(SECTION /DISCARD/ INPUT ".igot.plt")
75zephyr_linker_section_configure(SECTION /DISCARD/ INPUT ".got")
76zephyr_linker_section_configure(SECTION /DISCARD/ INPUT ".igot")
77
78zephyr_linker_section(NAME .rom_start ADDRESS ${rom_start} GROUP ROM_REGION NOINPUT)
79
80zephyr_linker_section(NAME .text         GROUP TEXT_REGION)
81
82zephyr_linker_section_configure(SECTION .rel.plt  INPUT ".rel.iplt")
83zephyr_linker_section_configure(SECTION .rela.plt INPUT ".rela.iplt")
84
85zephyr_linker_section_configure(SECTION .text INPUT ".TEXT.*")
86zephyr_linker_section_configure(SECTION .text INPUT ".gnu.linkonce.t.*")
87
88zephyr_linker_section_configure(SECTION .text INPUT ".glue_7t")
89zephyr_linker_section_configure(SECTION .text INPUT ".glue_7")
90zephyr_linker_section_configure(SECTION .text INPUT ".vfp11_veneer")
91zephyr_linker_section_configure(SECTION .text INPUT ".v4_bx")
92
93if(CONFIG_CPP)
94  zephyr_linker_section(NAME .ARM.extab GROUP ROM_REGION)
95  zephyr_linker_section_configure(SECTION .ARM.extab INPUT ".gnu.linkonce.armextab.*")
96endif()
97
98zephyr_linker_section(NAME .ARM.exidx GROUP ROM_REGION)
99# Here the original linker would check for __GCC_LINKER_CMD__, need to check toolchain linker ?
100#if(__GCC_LINKER_CMD__)
101  zephyr_linker_section_configure(SECTION .ARM.exidx INPUT ".gnu.linkonce.armexidx.*")
102#endif()
103
104
105include(${COMMON_ZEPHYR_LINKER_DIR}/common-rom.cmake)
106include(${COMMON_ZEPHYR_LINKER_DIR}/thread-local-storage.cmake)
107
108zephyr_linker_section(NAME .rodata GROUP RODATA_REGION)
109zephyr_linker_section_configure(SECTION .rodata INPUT ".gnu.linkonce.r.*")
110if(CONFIG_USERSPACE AND CONFIG_XIP)
111  zephyr_linker_section_configure(SECTION .rodata INPUT ".kobject_data.rodata*")
112endif()
113zephyr_linker_section_configure(SECTION .rodata ALIGN 4)
114
115# ToDo - . = ALIGN(_region_min_align);
116# Symbol to add _image_ram_start = .;
117
118# This comes from ramfunc.ls, via snippets-ram-sections.ld
119zephyr_linker_section(NAME .ramfunc GROUP RAM_REGION SUBALIGN 8)
120# Todo: handle MPU_ALIGN(_ramfunc_size);
121
122# ToDo - handle if(CONFIG_USERSPACE)
123
124zephyr_linker_section(NAME .data GROUP DATA_REGION)
125zephyr_linker_section_configure(SECTION .data INPUT ".kernel.*")
126
127include(${COMMON_ZEPHYR_LINKER_DIR}/common-ram.cmake)
128#include(kobject.ld)
129
130if(NOT CONFIG_USERSPACE)
131  zephyr_linker_section(NAME .bss VMA RAM LMA FLASH TYPE BSS)
132  zephyr_linker_section_configure(SECTION .bss INPUT COMMON)
133  zephyr_linker_section_configure(SECTION .bss INPUT ".kernel_bss.*")
134  # As memory is cleared in words only, it is simpler to ensure the BSS
135  # section ends on a 4 byte boundary. This wastes a maximum of 3 bytes.
136  zephyr_linker_section_configure(SECTION .bss ALIGN 4)
137
138  zephyr_linker_section(NAME .noinit GROUP RAM_REGION TYPE NOLOAD NOINIT)
139  # This section is used for non-initialized objects that
140  # will not be cleared during the boot process.
141  zephyr_linker_section_configure(SECTION .noinit INPUT ".kernel_noinit.*")
142endif()
143
144include(${COMMON_ZEPHYR_LINKER_DIR}/ram-end.cmake)
145
146zephyr_linker_symbol(SYMBOL __ramfunc_region_start EXPR "ADDR(.ramfunc)")
147zephyr_linker_symbol(SYMBOL __kernel_ram_start EXPR "(@__bss_start@)")
148zephyr_linker_symbol(SYMBOL __kernel_ram_end  EXPR "(${RAM_ADDR} + ${RAM_SIZE})")
149zephyr_linker_symbol(SYMBOL __kernel_ram_size EXPR "(@__kernel_ram_end@ - @__bss_start@)")
150zephyr_linker_symbol(SYMBOL _image_ram_start  EXPR "(${RAM_ADDR})" SUBALIGN 32) # ToDo calculate 32 correctly
151zephyr_linker_symbol(SYMBOL ARM_LIB_STACKHEAP EXPR "(${RAM_ADDR} + ${RAM_SIZE})" SIZE -0x1000)
152
153set(VECTOR_ALIGN 4)
154if(CONFIG_CPU_CORTEX_M_HAS_VTOR)
155  math(EXPR VECTOR_ALIGN "4 * (16 + ${CONFIG_NUM_IRQS})")
156  if(${VECTOR_ALIGN} LESS 128)
157    set(VECTOR_ALIGN 128)
158  else()
159    pow2round(VECTOR_ALIGN)
160  endif()
161endif()
162
163zephyr_linker_section_configure(
164  SECTION .rom_start
165  INPUT ".exc_vector_table*"
166        ".gnu.linkonce.irq_vector_table*"
167        ".vectors"
168  OFFSET ${CONFIG_ROM_START_OFFSET}
169  KEEP FIRST
170  SYMBOLS _vector_start _vector_end
171  ALIGN ${VECTOR_ALIGN}
172  PRIO 50
173)
174
175dt_chosen(chosen_itcm PROPERTY "zephyr,itcm")
176if(DEFINED chosen_itcm)
177  dt_node_has_status(status_result PATH ${chosen_itcm} STATUS okay)
178  if(${status_result})
179    zephyr_linker_group(NAME ITCM_REGION VMA ITCM LMA ROM_REGION)
180
181    zephyr_linker_section(NAME .itcm GROUP ITCM_REGION SUBALIGN 4)
182  endif()
183endif()
184
185dt_chosen(chosen_dtcm PROPERTY "zephyr,dtcm")
186if(DEFINED chosen_dtcm)
187  dt_node_has_status(status_result PATH ${chosen_dtcm} STATUS okay)
188  if(${status_result})
189    zephyr_linker_group(NAME DTCM_REGION VMA DTCM LMA ROM_REGION)
190
191    zephyr_linker_section(NAME .dtcm_bss GROUP DTCM_REGION SUBALIGN 4 TYPE BSS)
192    zephyr_linker_section(NAME .dtcm_noinit GROUP DTCM_REGION SUBALIGN 4 TYPE NOLOAD NOINIT)
193    zephyr_linker_section(NAME .dtcm_data GROUP DTCM_REGION SUBALIGN 4)
194  endif()
195endif()
196
197zephyr_linker_section(NAME .ARM.attributes ADDRESS 0 NOINPUT)
198zephyr_linker_section_configure(SECTION .ARM.attributes INPUT ".ARM.attributes" KEEP)
199zephyr_linker_section_configure(SECTION .ARM.attributes INPUT ".gnu.attributes" KEEP)
200
201# armlink specific flags
202zephyr_linker_section_configure(SECTION .text ANY FLAGS "+RO" "+XO")
203zephyr_linker_section_configure(SECTION .data ANY FLAGS "+RW")
204zephyr_linker_section_configure(SECTION .bss ANY FLAGS "+ZI")
205
206include(${COMMON_ZEPHYR_LINKER_DIR}/debug-sections.cmake)
207
208dt_comp_path(paths COMPATIBLE "zephyr,memory-region")
209foreach(path IN LISTS paths)
210  zephyr_linker_dts_section(PATH ${path})
211endforeach()
212