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