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/* Aliases */
18#define RAMABLE_REGION    dram_seg
19#define RAMABLE_REGION_1  dram_seg
20#define RODATA_REGION     dram_seg
21#define ROMABLE_REGION    dram_seg
22
23MEMORY
24{
25  iram_loader_seg (RWX) : org = BOOTLOADER_IRAM_LOADER_SEG_START,
26                          len = BOOTLOADER_IRAM_LOADER_SEG_LEN
27  iram_seg        (RWX) : org = BOOTLOADER_IRAM_SEG_START,
28                          len = BOOTLOADER_IRAM_SEG_LEN
29  dram_seg        (RW)  : org = BOOTLOADER_DRAM_SEG_START,
30                          len = BOOTLOADER_DRAM_SEG_LEN
31
32#ifdef CONFIG_GEN_ISR_TABLES
33  IDT_LIST(RW): org = 0x3ebfe010, len = 0x2000
34#endif
35}
36
37ENTRY(CONFIG_KERNEL_ENTRY)
38
39SECTIONS
40{
41  .iram0.loader_text :
42  {
43    _loader_text_start = ABSOLUTE(.);
44    *(.stub .gnu.warning .gnu.linkonce.literal.* .gnu.linkonce.t.*.literal .gnu.linkonce.t.*)
45
46    *libarch__xtensa__core.a:xtensa_asm2_util.*(.literal .text .literal.* .text.*)
47    *liblib__libc__common.a:abort.*(.literal .text .literal.* .text.*)
48    *libdrivers__timer.a:xtensa_sys_timer.*(.literal .text .literal.* .text.*)
49    *libarch__common.a:dynamic_isr.*(.literal .text .literal.* .text.*)
50    *libarch__common.a:sw_isr_common.*(.literal .text .literal.* .text.*)
51
52    *libapp.a:flash_map_extended.*(.literal .text .literal.* .text.*)
53    *libzephyr.a:printk.*(.literal.printk .literal.vprintk .literal.char_out .text.printk .text.vprintk .text.char_out)
54    *libzephyr.a:cbprintf_nano.*(.literal .text .literal.* .text.*)
55    *libzephyr.a:cpu.*(.literal .text .literal.* .text.*)
56    *libzephyr.a:mmu_hal.*(.literal .text .literal.* .text.*)
57    *libzephyr.a:flash_map.*(.literal .text .literal.* .text.*)
58    *libzephyr.a:esp_rom_spiflash.*(.literal .text .literal.* .text.*)
59
60    *libzephyr.a:heap.*(.literal .text .literal.* .text.*)
61
62    *libkernel.a:kheap.*(.literal .text .literal.* .text.*)
63    *libkernel.a:mempool.*(.literal .text .literal.* .text.*)
64    *libkernel.a:device.*(.literal .text .literal.* .text.*)
65    *libkernel.a:timeout.*(.literal .text .literal.* .text.*)
66
67    *libzephyr.a:esp_loader.*(.literal .text .literal.* .text.*)
68    *libzephyr.a:mmu_hal.*(.literal .text .literal.* .text.*)
69
70    *(.literal.esp_intr_disable .literal.esp_intr_disable.* .text.esp_intr_disable .text.esp_intr_disable.*)
71    *(.literal.default_intr_handler .text.default_intr_handler .iram1.*.default_intr_handler)
72    *(.literal.esp_log_timestamp .text.esp_log_timestamp)
73    *(.literal.esp_log_early_timestamp .text.esp_log_early_timestamp)
74    *(.literal.esp_system_abort .text.esp_system_abort)
75
76    *(.fini.literal)
77    *(.fini)
78    *(.gnu.version)
79
80    /* CPU will try to prefetch up to 16 bytes of
81     * of instructions. This means that any configuration (e.g. MMU, PMS) must allow
82     * safe access to up to 16 bytes after the last real instruction, add
83     * dummy bytes to ensure this
84     */
85    . += 16;
86
87    _text_end = ABSOLUTE(.);
88    _etext = .;
89    . = ALIGN(4) + 16;
90    _loader_text_end = ABSOLUTE(.);
91    _iram_text_end = ABSOLUTE(.);
92    _iram_end = ABSOLUTE(.);
93  } > iram_loader_seg
94
95  .iram0.vectors : ALIGN(4)
96  {
97    /* Vectors go to IRAM */
98    _init_start = ABSOLUTE(.);
99    /* Vectors according to builds/RF-2015.2-win32/esp108_v1_2_s5_512int_2/config.html */
100    . = 0x0;
101    KEEP(*(.WindowVectors.text));
102    . = 0x180;
103    KEEP(*(.Level2InterruptVector.text));
104    . = 0x1c0;
105    KEEP(*(.Level3InterruptVector.text));
106    . = 0x200;
107    KEEP(*(.Level4InterruptVector.text));
108    . = 0x240;
109    KEEP(*(.Level5InterruptVector.text));
110    . = 0x280;
111    KEEP(*(.DebugExceptionVector.text));
112    . = 0x2c0;
113    KEEP(*(.NMIExceptionVector.text));
114    . = 0x300;
115    KEEP(*(.KernelExceptionVector.text));
116    . = 0x340;
117    KEEP(*(.UserExceptionVector.text));
118    . = 0x3C0;
119    KEEP(*(.DoubleExceptionVector.text));
120    . = 0x400;
121    _invalid_pc_placeholder = ABSOLUTE(.);
122    *(.*Vector.literal)
123
124    *(.UserEnter.literal);
125    *(.UserEnter.text);
126    . = ALIGN (16);
127    *(.entry.text)
128    *(.init.literal)
129    *(.init)
130    . = ALIGN (4);
131    _init_end = ABSOLUTE(.);
132
133    _iram_start = ABSOLUTE(.);
134  } > iram_seg
135
136  .iram0.text :
137  {
138    . = ALIGN(4);
139
140    *(.iram1 .iram1.*)
141    *(.iram0.literal .iram.literal .iram.text.literal .iram0.text .iram.text)
142
143    *(.literal .text .literal.* .text.*)
144    . = ALIGN(4);
145
146    *(.irom0.text) /* catch stray ICACHE_RODATA_ATTR */
147    . = ALIGN(4) + 16;
148  } > iram_seg
149
150  .dram0.data : ALIGN(16)
151  {
152    . = ALIGN(4);
153    __data_start = ABSOLUTE(.);
154
155    #include <snippets-rodata.ld>
156    . = ALIGN(4);
157
158    #include <snippets-rwdata.ld>
159    . = ALIGN(4);
160
161    *(.data)
162    *(.data.*)
163    *(.gnu.linkonce.d.*)
164    *(.data1)
165    *(.sdata)
166    *(.sdata.*)
167    *(.gnu.linkonce.s.*)
168    *(.sdata2)
169    *(.sdata2.*)
170    *(.gnu.linkonce.s2.*)
171    *libzephyr.a:mmu_hal.*(.rodata .rodata.*)
172    *libzephyr.a:rtc_clk.*(.rodata .rodata.*)
173
174    KEEP(*(.jcr))
175    *(.dram1 .dram1.*)
176    . = ALIGN(4);
177    *(.rodata)
178    *(.rodata.*)
179
180    *(.irom1.text) /* catch stray ICACHE_RODATA_ATTR */
181    *(.gnu.linkonce.r.*)
182    *(.rodata1)
183    __XT_EXCEPTION_TABLE_ = ABSOLUTE(.);
184    *(.xt_except_table)
185    *(.gcc_except_table .gcc_except_table.*)
186    *(.gnu.linkonce.e.*)
187    *(.gnu.version_r)
188    . = (. + 3) & ~ 3;
189    __eh_frame = ABSOLUTE(.);
190    KEEP(*(.eh_frame))
191    . = (. + 7) & ~ 3;
192
193    /*  C++ exception handlers table:  */
194    __XT_EXCEPTION_DESCS_ = ABSOLUTE(.);
195    *(.xt_except_desc)
196    *(.gnu.linkonce.h.*)
197    __XT_EXCEPTION_DESCS_END__ = ABSOLUTE(.);
198    *(.xt_except_desc_end)
199    *(.dynamic)
200    *(.gnu.version_d)
201    . = ALIGN(4);
202    __rodata_region_end = ABSOLUTE(.);
203    /* Literals are also RO data. */
204    _lit4_start = ABSOLUTE(.);
205    *(*.lit4)
206    *(.lit4.*)
207    *(.gnu.linkonce.lit4.*)
208    _lit4_end = ABSOLUTE(.);
209    . = ALIGN(4);
210    *(.rodata_wlog)
211    *(.rodata_wlog*)
212    _thread_local_end = ABSOLUTE(.);
213    . = ALIGN(4);
214  } > dram_seg
215
216  #include <zephyr/linker/common-rom/common-rom-kernel-devices.ld>
217  #include <zephyr/linker/common-rom/common-rom-debug.ld>
218  #include <zephyr/linker/common-rom/common-rom-misc.ld>
219  #include <snippets-sections.ld>
220
221  #include <zephyr/linker/cplusplus-rom.ld>
222  #include <snippets-data-sections.ld>
223  #include <zephyr/linker/common-ram.ld>
224  #include <snippets-ram-sections.ld>
225  #include <zephyr/linker/cplusplus-ram.ld>
226  #include <zephyr/linker/common-rom/common-rom-logging.ld>
227
228  .noinit (NOLOAD):
229  {
230    . = ALIGN(8);
231    *(.noinit)
232    *(.noinit.*)
233    . = ALIGN(8) ;
234  } > dram_seg
235
236  /* Shared RAM */
237  .bss (NOLOAD):
238  {
239    . = ALIGN (8);
240    _bss_start = ABSOLUTE(.); /* required by bluetooth library */
241    __bss_start = ABSOLUTE(.);
242
243    *(.dynsbss)
244    *(.sbss)
245    *(.sbss.*)
246    *(.gnu.linkonce.sb.*)
247    *(.scommon)
248    *(.sbss2)
249    *(.sbss2.*)
250    *(.gnu.linkonce.sb2.*)
251    *(.dynbss)
252    *(.bss)
253    *(.bss.*)
254    *(.share.mem)
255    *(.gnu.linkonce.b.*)
256    *(COMMON)
257    . = ALIGN (8);
258    __bss_end = ABSOLUTE(.);
259    _bss_end = ABSOLUTE(.);
260    _end = ABSOLUTE(.);
261  } > dram_seg
262
263  ASSERT(((__bss_end - ORIGIN(dram_seg)) <= LENGTH(dram_seg)), "DRAM segment data does not fit.")
264
265#include <zephyr/linker/debug-sections.ld>
266
267  .xtensa.info  0 :  { *(.xtensa.info) }
268  .xt.insn 0 :
269  {
270    KEEP (*(.xt.insn))
271    KEEP (*(.gnu.linkonce.x.*))
272  }
273  .xt.prop 0 :
274  {
275    KEEP (*(.xt.prop))
276    KEEP (*(.xt.prop.*))
277    KEEP (*(.gnu.linkonce.prop.*))
278  }
279  .xt.lit 0 :
280  {
281    KEEP (*(.xt.lit))
282    KEEP (*(.xt.lit.*))
283    KEEP (*(.gnu.linkonce.p.*))
284  }
285  .xt.profile_range 0 :
286  {
287    KEEP (*(.xt.profile_range))
288    KEEP (*(.gnu.linkonce.profile_range.*))
289  }
290  .xt.profile_ranges 0 :
291  {
292    KEEP (*(.xt.profile_ranges))
293    KEEP (*(.gnu.linkonce.xt.profile_ranges.*))
294  }
295  .xt.profile_files 0 :
296  {
297    KEEP (*(.xt.profile_files))
298    KEEP (*(.gnu.linkonce.xt.profile_files.*))
299  }
300
301#ifdef CONFIG_GEN_ISR_TABLES
302#include <zephyr/linker/intlist.ld>
303#endif
304}
305