1/*
2 * Copyright (c) 2024 Espressif Systems (Shanghai) Co., Ltd.
3 * SPDX-License-Identifier: Apache-2.0
4 */
5
6#include <zephyr/devicetree.h>
7#include <zephyr/linker/sections.h>
8#include <zephyr/linker/linker-defs.h>
9#include <zephyr/linker/linker-tool.h>
10
11#include "memory.h"
12
13/* Disable all romable LMA */
14#undef GROUP_DATA_LINK_IN
15#define GROUP_DATA_LINK_IN(vregion, lregion) > vregion
16
17#define RAMABLE_REGION dram_seg
18#define RODATA_REGION  dram_seg
19#define ROMABLE_REGION dram_seg
20
21/* Global symbols required for espressif hal build */
22MEMORY
23{
24  iram_seg        (RX) : org = BOOTLOADER_IRAM_SEG_START,
25                         len = BOOTLOADER_IRAM_SEG_LEN
26  iram_loader_seg (RX) : org = BOOTLOADER_IRAM_LOADER_SEG_START,
27                         len = BOOTLOADER_IRAM_LOADER_SEG_LEN
28  dram_seg        (RW) : org = BOOTLOADER_DRAM_SEG_START,
29                         len = BOOTLOADER_DRAM_SEG_LEN
30
31#ifdef CONFIG_GEN_ISR_TABLES
32  IDT_LIST(RW): org = 0x3ebfe010, len = 0x2000
33#endif
34}
35
36/*  Default entry point:  */
37ENTRY(CONFIG_KERNEL_ENTRY)
38
39SECTIONS
40{
41  .iram0.loader_text :
42  {
43    . = ALIGN (16);
44    _loader_text_start = ABSOLUTE(.);
45    *(.stub .gnu.warning .gnu.linkonce.literal.* .gnu.linkonce.t.*.literal .gnu.linkonce.t.*)
46
47    *libapp.a:flash_map_extended.*(.literal .text .literal.* .text.*)
48    *libzephyr.a:cbprintf_nano.*(.literal .text .literal.* .text.*)
49    *libzephyr.a:cpu.*(.literal .text .literal.* .text.*)
50    *libzephyr.a:mmu_hal.*(.literal .text .literal.* .text.*)
51    *libzephyr.a:flash_map.*(.literal .text .literal.* .text.*)
52    *libzephyr.a:esp_rom_spiflash.*(.literal .text .literal.* .text.*)
53
54    *libzephyr.a:heap.*(.literal .text .literal.* .text.*)
55
56    *libkernel.a:kheap.*(.literal .text .literal.* .text.*)
57    *libkernel.a:mempool.*(.literal .text .literal.* .text.*)
58
59    *libzephyr.a:esp_loader.*(.literal .text .literal.* .text.*)
60    *libzephyr.a:mmu_hal.*(.literal .text .literal.* .text.*)
61
62    *(.literal.esp_intr_disable .literal.esp_intr_disable.* .text.esp_intr_disable .text.esp_intr_disable.*)
63    *(.literal.default_intr_handler .text.default_intr_handler .iram1.*.default_intr_handler)
64    *(.literal.esp_log_timestamp .text.esp_log_timestamp)
65    *(.literal.esp_log_early_timestamp .text.esp_log_early_timestamp)
66    *(.literal.esp_system_abort .text.esp_system_abort)
67
68    *(.fini.literal)
69    *(.fini)
70    *(.gnu.version)
71    _loader_text_end = ABSOLUTE(.);
72    _iram_end = ABSOLUTE(.);
73  } > iram_loader_seg
74
75  .iram0.text :
76  {
77    /* Vectors go to IRAM */
78    _iram_start = ABSOLUTE(.);
79    _init_start = ABSOLUTE(.);
80    __text_region_start = ABSOLUTE(.);
81
82    KEEP(*(.exception_vectors.text));
83    . = ALIGN(256);
84
85    _invalid_pc_placeholder = ABSOLUTE(.);
86
87    _iram_text_start = ABSOLUTE(.);
88
89    KEEP(*(.exception.entry*)); /* contains _isr_wrapper */
90    *(.exception.other*)
91    . = ALIGN(4);
92
93    *(.entry.text)
94    *(.init.literal)
95    *(.init)
96    . = ALIGN(4);
97    *(.iram1 .iram1.*)
98    *(.iram0.literal .iram.literal .iram.text.literal .iram0.text .iram.text)
99
100    /* C6 memprot requires 512 B alignment for split lines */
101    . = ALIGN (16);
102    _init_end = ABSOLUTE(.);
103    . = ALIGN(16);
104    *(.iram.data)
105    *(.iram.data*)
106    . = ALIGN(16);
107    *(.iram.bss)
108    *(.iram.bss*)
109
110    . = ALIGN(16);
111
112    *(.literal .text .literal.* .text.*)
113    *(.stub .gnu.warning .gnu.linkonce.literal.* .gnu.linkonce.t.*.literal .gnu.linkonce.t.*)
114    *(.irom0.text) /* catch stray ICACHE_RODATA_ATTR */
115    *(.fini.literal)
116    *(.fini)
117    *(.gnu.version)
118
119    /* CPU will try to prefetch up to 16 bytes of
120     * of instructions. This means that any configuration (e.g. MMU, PMS) must allow
121     * safe access to up to 16 bytes after the last real instruction, add
122     * dummy bytes to ensure this
123     */
124    . += 16;
125
126    _text_end = ABSOLUTE(.);
127    __text_region_end = ABSOLUTE(.);
128    _etext = .;
129
130    /* Similar to _iram_start, this symbol goes here so it is
131     * resolved by addr2line in preference to the first symbol in
132     * the flash.text segment.
133     */
134    _flash_cache_start = ABSOLUTE(0);
135  } > iram_seg
136
137  .dram0.data :
138  {
139    . = ALIGN(4);
140    __data_start = ABSOLUTE(.);
141    *(.data)
142    *(.data.*)
143    *(.gnu.linkonce.d.*)
144    *(.data1)
145#ifdef CONFIG_RISCV_GP
146    __global_pointer$ = . + 0x800;
147#endif /* CONFIG_RISCV_GP */
148    *(.sdata)
149    *(.sdata.*)
150    *(.gnu.linkonce.s.*)
151    *(.sdata2)
152    *(.sdata2.*)
153    *(.gnu.linkonce.s2.*)
154    *libzephyr.a:mmu_hal.*(.rodata .rodata.*)
155    *libzephyr.a:rtc_clk.*(.rodata .rodata.*)
156    KEEP(*(.jcr))
157    *(.dram1 .dram1.*)
158    . = ALIGN(4);
159
160    #include <snippets-rwdata.ld>
161    . = ALIGN(4);
162
163    *(.rodata_desc .rodata_desc.*)
164    *(.rodata_custom_desc .rodata_custom_desc.*)
165
166    . = ALIGN(4);
167    #include <snippets-rodata.ld>
168    . = ALIGN(4);
169
170    *(.rodata)
171    *(.rodata.*)
172    *(.irom1.text) /* catch stray ICACHE_RODATA_ATTR */
173    *(.gnu.linkonce.r.*)
174    *(.rodata1)
175    __XT_EXCEPTION_TABLE_ = ABSOLUTE(.);
176    *(.xt_except_table)
177    *(.gcc_except_table .gcc_except_table.*)
178    *(.gnu.linkonce.e.*)
179    *(.gnu.version_r)
180    . = (. + 3) & ~ 3;
181    __eh_frame = ABSOLUTE(.);
182    KEEP(*(.eh_frame))
183    . = (. + 7) & ~ 3;
184
185    /* C++ exception handlers table: */
186    __XT_EXCEPTION_DESCS_ = ABSOLUTE(.);
187    *(.xt_except_desc)
188    *(.gnu.linkonce.h.*)
189    __XT_EXCEPTION_DESCS_END__ = ABSOLUTE(.);
190    *(.xt_except_desc_end)
191    *(.dynamic)
192    *(.gnu.version_d)
193    __rodata_region_end = .;
194    _rodata_end = ABSOLUTE(.);
195    /* Literals are also RO data. */
196    _lit4_start = ABSOLUTE(.);
197    *(*.lit4)
198    *(.lit4.*)
199    *(.gnu.linkonce.lit4.*)
200    _lit4_end = ABSOLUTE(.);
201    . = ALIGN(4);
202    _thread_local_start = ABSOLUTE(.);
203    *(.tdata)
204    *(.tdata.*)
205    *(.tbss)
206    *(.tbss.*)
207    *(.srodata)
208    *(.srodata.*)
209    *(.rodata)
210    *(.rodata.*)
211    *(.rodata_wlog)
212    *(.rodata_wlog*)
213    _thread_local_end = ABSOLUTE(.);
214    /* _rodata_reserved_end = ABSOLUTE(.); */
215    . = ALIGN(4);
216  } > dram_seg
217
218  #include <zephyr/linker/common-rom/common-rom-init.ld>
219  #include <zephyr/linker/common-rom/common-rom-kernel-devices.ld>
220  #include <zephyr/linker/common-rom/common-rom-debug.ld>
221  #include <zephyr/linker/common-rom/common-rom-misc.ld>
222  #include <snippets-sections.ld>
223
224  #include <zephyr/linker/cplusplus-rom.ld>
225  #include <zephyr/linker/thread-local-storage.ld>
226  #include <snippets-data-sections.ld>
227  #include <zephyr/linker/common-ram.ld>
228  #include <snippets-ram-sections.ld>
229  #include <zephyr/linker/cplusplus-ram.ld>
230
231  #include <zephyr/linker/common-rom/common-rom-logging.ld>
232
233  .noinit (NOLOAD):
234  {
235    . = ALIGN(4);
236    *(.noinit)
237    *(.noinit.*)
238    . = ALIGN(4);
239  } > dram_seg
240
241  /* Shared RAM */
242  .bss (NOLOAD):
243  {
244    . = ALIGN (8);
245    _bss_start = ABSOLUTE(.);
246    __bss_start = ABSOLUTE(.);
247    *(.dynsbss)
248    *(.sbss)
249    *(.sbss.*)
250    *(.gnu.linkonce.sb.*)
251    *(.scommon)
252    *(.sbss2)
253    *(.sbss2.*)
254    *(.gnu.linkonce.sb2.*)
255    *(.dynbss)
256    *(.bss)
257    *(.bss.*)
258    *(.share.mem)
259    *(.gnu.linkonce.b.*)
260    *(COMMON)
261    . = ALIGN (8);
262    __bss_end = ABSOLUTE(.);
263    _bss_end = ABSOLUTE(.);
264  } > dram_seg
265
266  /* linker rel sections*/
267  #include <zephyr/linker/rel-sections.ld>
268
269#ifdef CONFIG_GEN_ISR_TABLES
270  #include <zephyr/linker/intlist.ld>
271#endif
272
273#include <zephyr/linker/debug-sections.ld>
274  /DISCARD/ : { *(.note.GNU-stack) }
275
276  SECTION_PROLOGUE(.riscv.attributes, 0,)
277    {
278    KEEP(*(.riscv.attributes))
279    KEEP(*(.gnu.attributes))
280    }
281}
282