]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - ld/scripttempl/avr.sc
Support rodata in flash for more AVR devices
[thirdparty/binutils-gdb.git] / ld / scripttempl / avr.sc
1 # Copyright (C) 2014-2023 Free Software Foundation, Inc.
2 #
3 # Copying and distribution of this file, with or without modification,
4 # are permitted in any medium without royalty provided the copyright
5 # notice and this notice are preserved.
6
7 # RODATA_PM_OFFSET
8 # If empty and not HAVE_FLMAP, .rodata sections will be part of .data.
9 # This is for devices where it is not possible to use LD* instructions
10 # to read from flash.
11 #
12 # If non-empty, .rodata is not part of .data and the .rodata
13 # objects are assigned addresses at an offest of RODATA_PM_OFFSET.
14 # This is for devices that feature reading from flash by means of
15 # LD* instructions, provided the addresses are offset by
16 # __RODATA_PM_OFFSET__ (which defaults to RODATA_PM_OFFSET).
17
18 # HAVE_FLMAP
19 # The .rodata section is located in program memory. Devices from
20 # the AVR64* and AVR128* families (from avrxmega2 and avrxmega4)
21 # see a 32k segment of their program memory in their RAM address
22 # space. Which 32k segment is visible is determined by the
23 # bit-field NVMCTRL_CTRLB.FLMAP.
24 # Output section .rodata is placed in MEMORY region rodata.
25 # The LMA of the .rodata section can be set by means of:
26 # * __flmap specifies which 32k block is visible in RAM.
27 # * __RODATA_FLASH_START__ specifies the byte address of the
28 # rodata LMA and is used if __flmap is undefined.
29 # * When __flmap and __RODATA_FLASH_START__ are undefined, then an
30 # emulation-specific default is used (the last 32k block).
31
32 # MAYBE_FLMAP
33 # For devices from avrxmega2 and avrxmega4: The user can chose whether
34 # or not .rodata is located in flash (if HAVE_FLMAP) or located in
35 # in RAM (if not HAVE_FLMAP by means of -mrodata-in-ram). This is
36 # achieved by new emulations avrxmega2_flmap and avrxmega4_flmap that
37 # are selected by compiler option -mno-rodata-in-ram.
38 #
39 # In order to facilitate initialization of NVMCTRL_CTRLB.FLMAP in
40 # the startup code irrespective of HAVE_FLMAP, the follwing symbols
41 # are defined in order to communicate with the startup code.
42 # Notice that the hardware default for FLMAP is the last 32k block,
43 # so that explicit initialization of FLMAP is only required when the
44 # user wants to deviate from the defauls.
45 #
46 # __flmap = HAVE_FLMAP
47 # ? given by __flmap resp. __RODATA_FLASH_START__ >> 15
48 # : 0;
49 #
50 # __flmap_lsl4 = __flmap << 4;
51 #
52 # __flmap_init_label = HAVE_FLMAP
53 # ? __flmap_init_start
54 # : __flmap_noinit_start;
55 # Supposed to be used as a jump target for RJMP so that the code
56 # can initialize FLMAP / skip initialization of FLMAP depending
57 # on the chosen emulation, and without the need to support two code
58 # versions of crt<mcu>.o for the two possble emulations.
59 #
60 # __do_init_flmap = HAVE_FLMAP ? 1 : 0;
61 # Whether or not FLMAP is supposed to be initialized according
62 # to, and for the purpose of, .rodata in flash.
63 #
64 # Apart from that, the compiler (device-specs actually) defines the
65 # following macros:
66 #
67 # __AVR_HAVE_FLMAP__
68 # Defined if a device has the NVMCTRL_CTRLB.FLMAP bitfield
69 # *AND* if it's unknown at compile-time / assembler-time whether
70 # emulation avrxmega* is used or avrxmega*_flmap.
71
72 cat <<EOF
73 /* Copyright (C) 2014-2023 Free Software Foundation, Inc.
74
75 Copying and distribution of this script, with or without modification,
76 are permitted in any medium without royalty provided the copyright
77 notice and this notice are preserved. */
78
79 OUTPUT_FORMAT("${OUTPUT_FORMAT}","${OUTPUT_FORMAT}","${OUTPUT_FORMAT}")
80 OUTPUT_ARCH(${ARCH})
81 EOF
82
83 test -n "${RELOCATING}" && cat <<EOF
84 __TEXT_REGION_LENGTH__ = DEFINED(__TEXT_REGION_LENGTH__) ? __TEXT_REGION_LENGTH__ : $TEXT_LENGTH;
85 __DATA_REGION_ORIGIN__ = DEFINED(__DATA_REGION_ORIGIN__) ? __DATA_REGION_ORIGIN__ : $DATA_ORIGIN;
86 __DATA_REGION_LENGTH__ = DEFINED(__DATA_REGION_LENGTH__) ? __DATA_REGION_LENGTH__ : $DATA_LENGTH;
87
88 ${EEPROM_LENGTH+__EEPROM_REGION_LENGTH__ = DEFINED(__EEPROM_REGION_LENGTH__) ? __EEPROM_REGION_LENGTH__ : $EEPROM_LENGTH;}
89 __FUSE_REGION_LENGTH__ = DEFINED(__FUSE_REGION_LENGTH__) ? __FUSE_REGION_LENGTH__ : $FUSE_LENGTH;
90 __LOCK_REGION_LENGTH__ = DEFINED(__LOCK_REGION_LENGTH__) ? __LOCK_REGION_LENGTH__ : $LOCK_LENGTH;
91 __SIGNATURE_REGION_LENGTH__ = DEFINED(__SIGNATURE_REGION_LENGTH__) ? __SIGNATURE_REGION_LENGTH__ : $SIGNATURE_LENGTH;
92 ${USER_SIGNATURE_LENGTH+__USER_SIGNATURE_REGION_LENGTH__ = DEFINED(__USER_SIGNATURE_REGION_LENGTH__) ? __USER_SIGNATURE_REGION_LENGTH__ : $USER_SIGNATURE_LENGTH;}
93 ${RODATA_PM_OFFSET+__RODATA_PM_OFFSET__ = DEFINED(__RODATA_PM_OFFSET__) ? __RODATA_PM_OFFSET__ : $RODATA_PM_OFFSET;}
94 ${HAVE_FLMAP+__RODATA_VMA__ = ${RODATA_VMA};}
95 ${HAVE_FLMAP+__RODATA_LDS_OFFSET__ = DEFINED(__RODATA_LDS_OFFSET__) ? __RODATA_LDS_OFFSET__ : ${RODATA_LDS_OFFSET};}
96 ${HAVE_FLMAP+__RODATA_REGION_LENGTH__ = DEFINED(__RODATA_REGION_LENGTH__) ? __RODATA_REGION_LENGTH__ : ${RODATA_LENGTH};}
97 ${HAVE_FLMAP+__RODATA_ORIGIN__ = __RODATA_VMA__ + __RODATA_LDS_OFFSET__;}
98 MEMORY
99 {
100 text (rx) : ORIGIN = 0, LENGTH = __TEXT_REGION_LENGTH__
101 data (rw!x) : ORIGIN = __DATA_REGION_ORIGIN__, LENGTH = __DATA_REGION_LENGTH__
102 ${EEPROM_LENGTH+ eeprom (rw!x) : ORIGIN = 0x810000, LENGTH = __EEPROM_REGION_LENGTH__}
103 $FUSE_NAME (rw!x) : ORIGIN = 0x820000, LENGTH = __FUSE_REGION_LENGTH__
104 lock (rw!x) : ORIGIN = 0x830000, LENGTH = __LOCK_REGION_LENGTH__
105 signature (rw!x) : ORIGIN = 0x840000, LENGTH = __SIGNATURE_REGION_LENGTH__
106 ${USER_SIGNATURE_LENGTH+ user_signatures (rw!x) : ORIGIN = 0x850000, LENGTH = __USER_SIGNATURE_REGION_LENGTH__}
107 ${HAVE_FLMAP+ rodata (r!x) : ORIGIN = __RODATA_ORIGIN__, LENGTH = __RODATA_REGION_LENGTH__}
108 }
109 EOF
110
111 cat <<EOF
112 SECTIONS
113 {
114 /* Read-only sections, merged into text segment: */
115 ${TEXT_DYNAMIC+${DYNAMIC}}
116 .hash ${RELOCATING-0} : { *(.hash) }
117 .dynsym ${RELOCATING-0} : { *(.dynsym) }
118 .dynstr ${RELOCATING-0} : { *(.dynstr) }
119 .gnu.version ${RELOCATING-0} : { *(.gnu.version) }
120 .gnu.version_d ${RELOCATING-0} : { *(.gnu.version_d) }
121 .gnu.version_r ${RELOCATING-0} : { *(.gnu.version_r) }
122
123 .rel.init ${RELOCATING-0} : { *(.rel.init) }
124 .rela.init ${RELOCATING-0} : { *(.rela.init) }
125 .rel.text ${RELOCATING-0} :
126 {
127 *(.rel.text)
128 ${RELOCATING+*(.rel.text.*)}
129 ${RELOCATING+*(.rel.gnu.linkonce.t*)}
130 }
131 .rela.text ${RELOCATING-0} :
132 {
133 *(.rela.text)
134 ${RELOCATING+*(.rela.text.*)}
135 ${RELOCATING+*(.rela.gnu.linkonce.t*)}
136 }
137 .rel.fini ${RELOCATING-0} : { *(.rel.fini) }
138 .rela.fini ${RELOCATING-0} : { *(.rela.fini) }
139 .rel.rodata ${RELOCATING-0} :
140 {
141 *(.rel.rodata)
142 ${RELOCATING+*(.rel.rodata.*)}
143 ${RELOCATING+*(.rel.gnu.linkonce.r*)}
144 }
145 .rela.rodata ${RELOCATING-0} :
146 {
147 *(.rela.rodata)
148 ${RELOCATING+*(.rela.rodata.*)}
149 ${RELOCATING+*(.rela.gnu.linkonce.r*)}
150 }
151 .rel.data ${RELOCATING-0} :
152 {
153 *(.rel.data)
154 ${RELOCATING+*(.rel.data.*)}
155 ${RELOCATING+*(.rel.gnu.linkonce.d*)}
156 }
157 .rela.data ${RELOCATING-0} :
158 {
159 *(.rela.data)
160 ${RELOCATING+*(.rela.data.*)}
161 ${RELOCATING+*(.rela.gnu.linkonce.d*)}
162 }
163 .rel.ctors ${RELOCATING-0} : { *(.rel.ctors) }
164 .rela.ctors ${RELOCATING-0} : { *(.rela.ctors) }
165 .rel.dtors ${RELOCATING-0} : { *(.rel.dtors) }
166 .rela.dtors ${RELOCATING-0} : { *(.rela.dtors) }
167 .rel.got ${RELOCATING-0} : { *(.rel.got) }
168 .rela.got ${RELOCATING-0} : { *(.rela.got) }
169 .rel.bss ${RELOCATING-0} : { *(.rel.bss) }
170 .rela.bss ${RELOCATING-0} : { *(.rela.bss) }
171 .rel.plt ${RELOCATING-0} : { *(.rel.plt) }
172 .rela.plt ${RELOCATING-0} : { *(.rela.plt) }
173
174 /* Internal text space or external memory. */
175 .text ${RELOCATING-0} :
176 {
177 ${RELOCATING+*(.vectors)
178 KEEP(*(.vectors))
179
180 /* For data that needs to reside in the lower 64k of progmem. */
181 *(.progmem.gcc*)
182
183 /* PR 13812: Placing the trampolines here gives a better chance
184 that they will be in range of the code that uses them. */
185 . = ALIGN(2);
186 __trampolines_start = . ;
187 /* The jump trampolines for the 16-bit limited relocs will reside here. */
188 *(.trampolines)
189 *(.trampolines*)
190 __trampolines_end = . ;
191
192 /* avr-libc expects these data to reside in lower 64K. */
193 *libprintf_flt.a:*(.progmem.data)
194 *libc.a:*(.progmem.data)
195
196 *(.progmem.*)
197
198 . = ALIGN(2);
199
200 /* For code that needs to reside in the lower 128k progmem. */
201 *(.lowtext)
202 *(.lowtext*)}
203
204 ${CONSTRUCTING+ __ctors_start = . ; }
205 ${CONSTRUCTING+ *(.ctors) }
206 ${CONSTRUCTING+ __ctors_end = . ; }
207 ${CONSTRUCTING+ __dtors_start = . ; }
208 ${CONSTRUCTING+ *(.dtors) }
209 ${CONSTRUCTING+ __dtors_end = . ; }
210 ${RELOCATING+KEEP(SORT(*)(.ctors))
211 KEEP(SORT(*)(.dtors))
212
213 /* From this point on, we do not bother about whether the insns are
214 below or above the 16 bits boundary. */
215 *(.init0) /* Start here after reset. */
216 KEEP (*(.init0))
217 *(.init1)
218 KEEP (*(.init1))
219 *(.init2) /* Clear __zero_reg__, set up stack pointer. */
220 KEEP (*(.init2))
221 *(.init3)
222 KEEP (*(.init3))
223 *(.init4) /* Initialize data and BSS. */
224 KEEP (*(.init4))
225 *(.init5)
226 KEEP (*(.init5))
227 *(.init6) /* C++ constructors. */
228 KEEP (*(.init6))
229 *(.init7)
230 KEEP (*(.init7))
231 *(.init8)
232 KEEP (*(.init8))
233 *(.init9) /* Call main(). */
234 KEEP (*(.init9))}
235 *(.text)
236 ${RELOCATING+. = ALIGN(2);
237 *(.text.*)
238 . = ALIGN(2);
239 *(.fini9) /* _exit() starts here. */
240 KEEP (*(.fini9))
241 *(.fini8)
242 KEEP (*(.fini8))
243 *(.fini7)
244 KEEP (*(.fini7))
245 *(.fini6) /* C++ destructors. */
246 KEEP (*(.fini6))
247 *(.fini5)
248 KEEP (*(.fini5))
249 *(.fini4)
250 KEEP (*(.fini4))
251 *(.fini3)
252 KEEP (*(.fini3))
253 *(.fini2)
254 KEEP (*(.fini2))
255 *(.fini1)
256 KEEP (*(.fini1))
257 *(.fini0) /* Infinite loop after program termination. */
258 KEEP (*(.fini0))
259
260 /* For code that needs not to reside in the lower progmem. */
261 *(.hightext)
262 *(.hightext*)
263
264 *(.progmemx.*)
265
266 . = ALIGN(2);
267
268 /* For tablejump instruction arrays. We do not relax
269 JMP / CALL instructions within these sections. */
270 *(.jumptables)
271 *(.jumptables*)
272
273 _etext = . ;}
274 } ${RELOCATING+ > text}
275 EOF
276
277 # Devices like ATtiny816 allow to read from flash memory by means of LD*
278 # instructions provided we add an offset of __RODATA_PM_OFFSET__ to the
279 # flash addresses.
280
281 if test -n "$RODATA_PM_OFFSET"; then
282 cat <<EOF
283 .rodata ${RELOCATING+ ADDR(.text) + SIZEOF (.text) + __RODATA_PM_OFFSET__ } ${RELOCATING-0} :
284 {
285 *(.rodata)
286 ${RELOCATING+ *(.rodata*)
287 *(.gnu.linkonce.r*)}
288 } ${RELOCATING+AT> text}
289 EOF
290 fi
291
292 cat <<EOF
293 .data ${RELOCATING-0} :
294 {
295 ${RELOCATING+ PROVIDE (__data_start = .) ; }
296 *(.data)
297 ${RELOCATING+ *(.data*)
298 *(.gnu.linkonce.d*)}
299 EOF
300
301 # Classical devices that don't show flash memory in the SRAM address space
302 # need .rodata to be part of .data because the compiler will use LD*
303 # instructions and LD* cannot access flash.
304
305 if test -z "$RODATA_PM_OFFSET" && test -z "${HAVE_FLMAP}" && test -n "${RELOCATING}"; then
306 cat <<EOF
307 *(.rodata) /* We need to include .rodata here if gcc is used */
308 *(.rodata*) /* with -fdata-sections. */
309 *(.gnu.linkonce.r*)
310 EOF
311 fi
312
313 cat <<EOF
314 ${RELOCATING+. = ALIGN(2);}
315 ${RELOCATING+ _edata = . ; }
316 ${RELOCATING+ PROVIDE (__data_end = .) ; }
317 } ${RELOCATING+ > data ${RELOCATING+AT> text}}
318
319 .bss ${RELOCATING+ ADDR(.data) + SIZEOF (.data)} ${RELOCATING-0} :${RELOCATING+ AT (ADDR (.bss))}
320 {
321 ${RELOCATING+ PROVIDE (__bss_start = .) ; }
322 *(.bss)
323 ${RELOCATING+ *(.bss*)}
324 ${RELOCATING+ *(COMMON)}
325 ${RELOCATING+ PROVIDE (__bss_end = .) ; }
326 } ${RELOCATING+ > data}
327
328 ${RELOCATING+ __data_load_start = LOADADDR(.data); }
329 ${RELOCATING+ __data_load_end = __data_load_start + SIZEOF(.data); }
330
331 /* Global data not cleared after reset. */
332 .noinit ${RELOCATING+ ADDR(.bss) + SIZEOF (.bss)} ${RELOCATING-0}: ${RELOCATING+ AT (ADDR (.noinit))}
333 {
334 ${RELOCATING+ PROVIDE (__noinit_start = .) ; }
335 *(.noinit${RELOCATING+ .noinit.* .gnu.linkonce.n.*})
336 ${RELOCATING+ PROVIDE (__noinit_end = .) ; }
337 ${RELOCATING+ _end = . ; }
338 ${RELOCATING+ PROVIDE (__heap_start = .) ; }
339 } ${RELOCATING+ > data}
340 EOF
341
342 # Devices like AVR128DA32 and AVR64DA32 see a 32 KiB block of their program
343 # memory at 0x8000 (RODATA_LDS_OFFSET). Which portion will be determined by
344 # bitfield NVMCTRL_CTRLB.FLMAP.
345
346 if test -n "${MAYBE_FLMAP}" && test -n "${RELOCATING}"; then
347 cat <<EOF
348
349 __do_init_flmap = ${HAVE_FLMAP-0};
350
351 EOF
352 fi
353
354 if test -z "${HAVE_FLMAP}" && test -n "${RELOCATING}"; then
355 cat <<EOF
356
357 PROVIDE (__flmap_init_label = DEFINED(__flmap_noinit_start) ? __flmap_noinit_start : 0) ;
358 PROVIDE (__flmap = DEFINED(__flmap) ? __flmap : 0) ;
359 PROVIDE (__flmap_lsl4 = __flmap << 4) ;
360
361 EOF
362 fi
363
364 if test -n "${HAVE_FLMAP}"; then
365 cat <<EOF
366
367 ${RELOCATING+
368 PROVIDE (__flmap_init_label = DEFINED(__flmap_init_start) ? __flmap_init_start : 0) ;
369 /* User can specify position of .rodata in flash (LMA) by supplying
370 __RODATA_FLASH_START__ or __flmap, where __flmap takes precedence. */
371 __RODATA_FLASH_START__ = DEFINED(__flmap)
372 ? __flmap * 32K
373 : DEFINED(__RODATA_FLASH_START__) ? __RODATA_FLASH_START__ : ${RODATA_FLASH_START};
374 ASSERT (__RODATA_FLASH_START__ % 32K == 0, \"__RODATA_FLASH_START__ must be a multiple of 32 KiB\")
375 __flmap = ${FLMAP_MASK} & (DEFINED(__flmap) ? __flmap : __RODATA_FLASH_START__ >> 15);
376 __RODATA_FLASH_START__ = __flmap << 15;
377 __rodata_load_start = MAX (__data_load_end, __RODATA_FLASH_START__);
378 PROVIDE (__flmap_lsl4 = __flmap << 4) ;
379 __rodata_start = __RODATA_ORIGIN__ + __rodata_load_start - __RODATA_FLASH_START__;}
380
381 .rodata ${RELOCATING+ __rodata_start} ${RELOCATING-0} : ${RELOCATING+ AT (__rodata_load_start)}
382 {
383 *(.rodata)
384 ${RELOCATING+ *(.rodata*)}
385 ${RELOCATING+ *(.gnu.linkonce.r*)}
386 ${RELOCATING+ __rodata_end = ABSOLUTE(.) ;}
387 } ${RELOCATING+ > rodata}
388
389 ${RELOCATING+ __rodata_load_end = __rodata_load_start + __rodata_end - __rodata_start;}
390
391 EOF
392 fi
393
394 if test -n "${EEPROM_LENGTH}"; then
395 cat <<EOF
396
397 .eeprom ${RELOCATING-0}:
398 {
399 /* See .data above... */
400 KEEP(*(.eeprom*))
401 ${RELOCATING+ __eeprom_end = . ; }
402 } ${RELOCATING+ > eeprom}
403 EOF
404 fi
405
406 if test "$FUSE_NAME" = "fuse" ; then
407 cat <<EOF
408
409 .fuse ${RELOCATING-0}:
410 {
411 KEEP(*(.fuse))
412 ${RELOCATING+KEEP(*(.lfuse))
413 KEEP(*(.hfuse))
414 KEEP(*(.efuse))}
415 } ${RELOCATING+ > fuse}
416 EOF
417 fi
418
419 cat <<EOF
420
421 .lock ${RELOCATING-0}:
422 {
423 KEEP(*(.lock*))
424 } ${RELOCATING+ > lock}
425
426 .signature ${RELOCATING-0}:
427 {
428 KEEP(*(.signature*))
429 } ${RELOCATING+ > signature}
430 EOF
431
432 if test "$FUSE_NAME" = "config" ; then
433 cat <<EOF
434
435 .config ${RELOCATING-0}:
436 {
437 KEEP(*(.config*))
438 } ${RELOCATING+ > config}
439 EOF
440 fi
441
442 source_sh $srcdir/scripttempl/misc-sections.sc
443
444 cat <<EOF
445 .note.gnu.build-id ${RELOCATING-0} : { *(.note.gnu.build-id) }
446 EOF
447
448 source_sh $srcdir/scripttempl/DWARF.sc
449
450 cat <<EOF
451 }
452 EOF