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