1# Copyright (C) 2014-2020 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# Unusual variables checked by this code:
8#	NOP - four byte opcode for no-op (defaults to 0)
9#	DATA_ADDR - if end-of-text-plus-one-page isn't right for data start
10#	OTHER_READWRITE_SECTIONS - other than .data .bss .ctors .sdata ...
11#		(e.g., .PARISC.global)
12#	OTHER_SECTIONS - at the end
13#	EXECUTABLE_SYMBOLS - symbols that must be defined for an
14#		executable (e.g., _DYNAMIC_LINK)
15#	TEXT_START_SYMBOLS - symbols that appear at the start of the
16#		.text section.
17#	DATA_START_SYMBOLS - symbols that appear at the start of the
18#		.data section.
19#	OTHER_BSS_SYMBOLS - symbols that appear at the start of the
20#		.bss section besides __bss_start.
21#	EMBEDDED - whether this is for an embedded system.
22#
23# When adding sections, do note that the names of some sections are used
24# when specifying the start address of the next.
25#
26test -z "$ENTRY" && ENTRY=_start
27test -z "${BIG_OUTPUT_FORMAT}" && BIG_OUTPUT_FORMAT=${OUTPUT_FORMAT}
28test -z "${LITTLE_OUTPUT_FORMAT}" && LITTLE_OUTPUT_FORMAT=${OUTPUT_FORMAT}
29if [ -z "$MACHINE" ]; then OUTPUT_ARCH=${ARCH}; else OUTPUT_ARCH=${ARCH}:${MACHINE}; fi
30test "$LD_FLAG" = "N" && DATA_ADDR=.
31
32CTOR=".ctors ${CONSTRUCTING-0} :
33  {
34    ${CONSTRUCTING+ PROVIDE (__CTOR_LIST__ = .); }
35    ${CONSTRUCTING+${CTOR_START}}
36    KEEP (*(.ctors))
37
38    ${CONSTRUCTING+${CTOR_END}}
39    ${CONSTRUCTING+ PROVIDE(__CTOR_END__ = .); }
40  } ${RELOCATING+ > ${TEXT_MEMORY}}"
41
42DTOR="  .dtors	${CONSTRUCTING-0} :
43  {
44    ${CONSTRUCTING+ PROVIDE(__DTOR_LIST__ = .); }
45    KEEP (*(.dtors))
46    ${CONSTRUCTING+ PROVIDE(__DTOR_END__ = .); }
47  } ${RELOCATING+ > ${TEXT_MEMORY}}"
48
49
50VECTORS="
51  /* If the 'vectors_addr' symbol is defined, it indicates the start address
52     of interrupt vectors.  This depends on the 68HC11 operating mode:
53
54			Addr
55     Single chip	0xffc0
56     Extended mode	0xffc0
57     Bootstrap		0x00c0
58     Test		0xbfc0
59
60     In general, the vectors address is 0xffc0.  This can be overriden
61     with the '-defsym vectors_addr=0xbfc0' ld option.
62
63     Note: for the bootstrap mode, the interrupt vectors are at 0xbfc0 but
64     they are redirected to 0x00c0 by the internal PROM.  Application's vectors
65     must also consist of jump instructions (see Motorola's manual).  */
66
67  PROVIDE (_vectors_addr = DEFINED (vectors_addr) ? vectors_addr : 0xffc0);
68  .vectors DEFINED (vectors_addr) ? vectors_addr : 0xffc0 :
69  {
70    KEEP (*(.vectors))
71  }"
72
73#
74# We provide two emulations: a fixed on that defines some memory banks
75# and a configurable one that includes a user provided memory definition.
76#
77case $GENERIC_BOARD in
78  yes|1|YES)
79	MEMORY_DEF="
80/* Get memory banks definition from some user configuration file.
81   This file must be located in some linker directory (search path
82   with -L<dir>). See fixed memory banks emulation script.  */
83INCLUDE memory.x;
84"
85	;;
86  *)
87MEMORY_DEF="
88/* Fixed definition of the available memory banks.
89   See generic emulation script for a user defined configuration.  */
90MEMORY
91{
92  page0 (rwx) : ORIGIN = 0x0, LENGTH = 256
93  text  (rx)  : ORIGIN = ${ROM_START_ADDR}, LENGTH = ${ROM_SIZE}
94  data        : ORIGIN = ${RAM_START_ADDR}, LENGTH = ${RAM_SIZE}
95  eeprom      : ORIGIN = ${EEPROM_START_ADDR}, LENGTH = ${EEPROM_SIZE}
96}
97
98/* Setup the stack on the top of the data memory bank.  */
99PROVIDE (_stack = ${RAM_START_ADDR} + ${RAM_SIZE} - 1);
100"
101	;;
102esac
103
104STARTUP_CODE="
105    /* Startup code.  */
106    KEEP (*(.install0))	/* Section should setup the stack pointer.  */
107    KEEP (*(.install1))	/* Place holder for applications.  */
108    KEEP (*(.install2))	/* Optional installation of data sections in RAM.  */
109    KEEP (*(.install3))	/* Place holder for applications.  */
110    KEEP (*(.install4))	/* Section that calls the main.  */
111"
112
113FINISH_CODE="
114    /* Finish code.  */
115    KEEP (*(.fini0))	/* Beginning of finish code (_exit symbol).  */
116    KEEP (*(.fini1))	/* Place holder for applications.  */
117    KEEP (*(.fini2))	/* C++ destructors.  */
118    KEEP (*(.fini3))	/* Place holder for applications.  */
119    KEEP (*(.fini4))	/* Runtime exit.  */
120"
121
122PRE_COMPUTE_DATA_SIZE="
123/* SCz: this does not work yet... This is supposed to force the loading
124   of _map_data.o (from libgcc.a) when the .data section is not empty.
125   By doing so, this should bring the code that copies the .data section
126   from ROM to RAM at init time.
127
128  ___pre_comp_data_size = SIZEOF(.data);
129  __install_data_sections = ___pre_comp_data_size > 0 ?
130		__map_data_sections : 0;
131*/
132"
133
134INSTALL_RELOC="
135  .install0 0 : { *(.install0) }
136  .install1 0 : { *(.install1) }
137  .install2 0 : { *(.install2) }
138  .install3 0 : { *(.install3) }
139  .install4 0 : { *(.install4) }
140"
141
142FINISH_RELOC="
143  .fini0 0 : { *(.fini0) }
144  .fini1 0 : { *(.fini1) }
145  .fini2 0 : { *(.fini2) }
146  .fini3 0 : { *(.fini3) }
147  .fini4 0 : { *(.fini4) }
148"
149
150BSS_DATA_RELOC="
151  .data1 0 : { *(.data1) }
152
153  /* We want the small data sections together, so single-instruction offsets
154     can access them all, and initialized data all before uninitialized, so
155     we can shorten the on-disk segment size.  */
156  .sdata   0 : { *(.sdata) }
157  .sbss    0 : { *(.sbss) }
158  .scommon 0 : { *(.scommon) }
159"
160
161SOFT_REGS_RELOC="
162  .softregs 0 : { *(.softregs) }
163"
164
165cat <<EOF
166/* Copyright (C) 2014-2020 Free Software Foundation, Inc.
167
168   Copying and distribution of this script, with or without modification,
169   are permitted in any medium without royalty provided the copyright
170   notice and this notice are preserved.  */
171
172${RELOCATING+/* Linker script for 68HC12 executable (PROM).  */}
173${RELOCATING-/* Linker script for 68HC12 object file (ld -r).  */}
174
175OUTPUT_FORMAT("${OUTPUT_FORMAT}", "${BIG_OUTPUT_FORMAT}",
176	      "${LITTLE_OUTPUT_FORMAT}")
177OUTPUT_ARCH(${OUTPUT_ARCH})
178${RELOCATING+ENTRY(${ENTRY})}
179
180${RELOCATING+${LIB_SEARCH_DIRS}}
181${RELOCATING+${EXECUTABLE_SYMBOLS}}
182${RELOCATING+${MEMORY_DEF}}
183
184SECTIONS
185{
186  .hash        ${RELOCATING-0} : { *(.hash)		}
187  .dynsym      ${RELOCATING-0} : { *(.dynsym)		}
188  .dynstr      ${RELOCATING-0} : { *(.dynstr)		}
189  .gnu.version		${RELOCATING-0} : { *(.gnu.version) }
190  .gnu.version_d	${RELOCATING-0} : { *(.gnu.version_d) }
191  .gnu.version_r	${RELOCATING-0} : { *(.gnu.version_r) }
192
193  .rel.text    ${RELOCATING-0} :
194    {
195      *(.rel.text)
196      ${RELOCATING+*(.rel.text.*)}
197      ${RELOCATING+*(.rel.gnu.linkonce.t.*)}
198    }
199  .rela.text   ${RELOCATING-0} :
200    {
201      *(.rela.text)
202      ${RELOCATING+*(.rela.text.*)}
203      ${RELOCATING+*(.rela.gnu.linkonce.t.*)}
204    }
205  .rel.data    ${RELOCATING-0} :
206    {
207      *(.rel.data)
208      ${RELOCATING+*(.rel.data.*)}
209      ${RELOCATING+*(.rel.gnu.linkonce.d.*)}
210    }
211  .rela.data   ${RELOCATING-0} :
212    {
213      *(.rela.data)
214      ${RELOCATING+*(.rela.data.*)}
215      ${RELOCATING+*(.rela.gnu.linkonce.d.*)}
216    }
217  .rel.rodata  ${RELOCATING-0} :
218    {
219      *(.rel.rodata)
220      ${RELOCATING+*(.rel.rodata.*)}
221      ${RELOCATING+*(.rel.gnu.linkonce.r.*)}
222    }
223  .rela.rodata ${RELOCATING-0} :
224    {
225      *(.rela.rodata)
226      ${RELOCATING+*(.rela.rodata.*)}
227      ${RELOCATING+*(.rela.gnu.linkonce.r.*)}
228    }
229  .rel.sdata   ${RELOCATING-0} :
230    {
231      *(.rel.sdata)
232      ${RELOCATING+*(.rel.sdata.*)}
233      ${RELOCATING+*(.rel.gnu.linkonce.s.*)}
234    }
235  .rela.sdata   ${RELOCATING-0} :
236    {
237      *(.rela.sdata)
238      ${RELOCATING+*(.rela.sdata.*)}
239      ${RELOCATING+*(.rela.gnu.linkonce.s.*)}
240    }
241  .rel.sbss    ${RELOCATING-0} :
242    {
243      *(.rel.sbss)
244      ${RELOCATING+*(.rel.sbss.*)}
245      ${RELOCATING+*(.rel.gnu.linkonce.sb.*)}
246    }
247  .rela.sbss   ${RELOCATING-0} :
248    {
249      *(.rela.sbss)
250      ${RELOCATING+*(.rela.sbss.*)}
251      ${RELOCATING+*(.rel.gnu.linkonce.sb.*)}
252    }
253  .rel.bss     ${RELOCATING-0} :
254    {
255      *(.rel.bss)
256      ${RELOCATING+*(.rel.bss.*)}
257      ${RELOCATING+*(.rel.gnu.linkonce.b.*)}
258    }
259  .rela.bss    ${RELOCATING-0} :
260    {
261      *(.rela.bss)
262      ${RELOCATING+*(.rela.bss.*)}
263      ${RELOCATING+*(.rela.gnu.linkonce.b.*)}
264    }
265  .rel.stext		${RELOCATING-0} : { *(.rel.stest) }
266  .rela.stext		${RELOCATING-0} : { *(.rela.stest) }
267  .rel.etext		${RELOCATING-0} : { *(.rel.etest) }
268  .rela.etext		${RELOCATING-0} : { *(.rela.etest) }
269  .rel.sdata		${RELOCATING-0} : { *(.rel.sdata) }
270  .rela.sdata		${RELOCATING-0} : { *(.rela.sdata) }
271  .rel.edata		${RELOCATING-0} : { *(.rel.edata) }
272  .rela.edata		${RELOCATING-0} : { *(.rela.edata) }
273  .rel.eit_v		${RELOCATING-0} : { *(.rel.eit_v) }
274  .rela.eit_v		${RELOCATING-0} : { *(.rela.eit_v) }
275  .rel.ebss		${RELOCATING-0} : { *(.rel.ebss) }
276  .rela.ebss		${RELOCATING-0} : { *(.rela.ebss) }
277  .rel.srodata		${RELOCATING-0} : { *(.rel.srodata) }
278  .rela.srodata		${RELOCATING-0} : { *(.rela.srodata) }
279  .rel.erodata		${RELOCATING-0} : { *(.rel.erodata) }
280  .rela.erodata		${RELOCATING-0} : { *(.rela.erodata) }
281  .rel.got		${RELOCATING-0} : { *(.rel.got) }
282  .rela.got		${RELOCATING-0} : { *(.rela.got) }
283  .rel.ctors		${RELOCATING-0} : { *(.rel.ctors) }
284  .rela.ctors		${RELOCATING-0} : { *(.rela.ctors) }
285  .rel.dtors		${RELOCATING-0} : { *(.rel.dtors) }
286  .rela.dtors		${RELOCATING-0} : { *(.rela.dtors) }
287  .rel.init		${RELOCATING-0} : { *(.rel.init) }
288  .rela.init		${RELOCATING-0} : { *(.rela.init) }
289  .rel.fini		${RELOCATING-0} : { *(.rel.fini) }
290  .rela.fini		${RELOCATING-0} : { *(.rela.fini) }
291  .rel.plt		${RELOCATING-0} : { *(.rel.plt) }
292  .rela.plt		${RELOCATING-0} : { *(.rela.plt) }
293
294  /* Concatenate .page0 sections.  Put them in the page0 memory bank
295     unless we are creating a relocatable file.  */
296  .page0 :
297  {
298    *(.page0)
299  } ${RELOCATING+ > page0}
300
301  /* Start of text section.  */
302  .stext ${RELOCATING-0} :
303  {
304    *(.stext)
305  } ${RELOCATING+ > ${TEXT_MEMORY}}
306
307  .init	${RELOCATING-0} :
308  {
309    KEEP (*(SORT_NONE(.init)))
310  } ${RELOCATING+=${NOP-0}}
311
312  ${RELOCATING-${INSTALL_RELOC}}
313  ${RELOCATING-${FINISH_RELOC}}
314
315  .text ${RELOCATING-0}:
316  {
317    /* Put startup code at beginning so that _start keeps same address.  */
318    ${RELOCATING+${STARTUP_CODE}}
319
320    *(.text)
321    ${RELOCATING+*(.text.*)}
322    /* .gnu.warning sections are handled specially by elf.em.  */
323    *(.gnu.warning)
324    ${RELOCATING+*(.gnu.linkonce.t.*)}
325    ${RELOCATING+*(.tramp)}
326    ${RELOCATING+*(.tramp.*)}
327
328    ${RELOCATING+KEEP (*(SORT_NONE(.fini)))}
329    ${RELOCATING+${FINISH_CODE}}
330
331    ${RELOCATING+_etext = .;}
332    ${RELOCATING+PROVIDE (etext = .);}
333    ${RELOCATING+. = ALIGN(2);}
334  } ${RELOCATING+ > ${TEXT_MEMORY} =0xa7a7a7a7}
335
336  .eh_frame ${RELOCATING-0} :
337  {
338    KEEP (*(.eh_frame))
339  } ${RELOCATING+ > ${TEXT_MEMORY}}
340
341  .gcc_except_table ${RELOCATING-0} :
342  {
343    *(.gcc_except_table)
344  } ${RELOCATING+ > ${TEXT_MEMORY}}
345
346  .rodata  ${RELOCATING-0} :
347  {
348    *(.rodata)
349    ${RELOCATING+*(.rodata.*)}
350    ${RELOCATING+*(.gnu.linkonce.r*)}
351    ${RELOCATING+. = ALIGN(2);}
352  } ${RELOCATING+ > ${TEXT_MEMORY} =0xffffffff}
353
354  .rodata1 ${RELOCATING-0} :
355  {
356    *(.rodata1)
357    ${RELOCATING+. = ALIGN(2);}
358  } ${RELOCATING+ > ${TEXT_MEMORY} =0xffffffff}
359
360  /* Constructor and destructor tables are in ROM.  */
361  ${RELOCATING+${CTOR}}
362  ${RELOCATING+${DTOR}}
363
364  .jcr ${RELOCATING-0} :
365  {
366    KEEP (*(.jcr))
367  } ${RELOCATING+ > ${TEXT_MEMORY}}
368
369  /* Start of the data section image in ROM.  */
370  ${RELOCATING+__data_image = .;}
371  ${RELOCATING+PROVIDE (__data_image = .);}
372
373  /* All read-only sections that normally go in PROM must be above.
374     We construct the DATA image section in PROM at end of all these
375     read-only sections.  The data image must be copied at init time.
376     Refer to GNU ld, Section 3.6.8.2 Output Section LMA.  */
377  .data  ${RELOCATING-0} : ${RELOCATING+AT (__data_image)}
378  {
379    ${RELOCATING+__data_section_start = .;}
380    ${RELOCATING+PROVIDE (__data_section_start = .);}
381
382    ${RELOCATING+${DATA_START_SYMBOLS}}
383    ${RELOCATING+*(.sdata)}
384    *(.data)
385    ${RELOCATING+*(.data.*)}
386    ${RELOCATING+*(.data1)}
387    ${RELOCATING+*(.gnu.linkonce.d.*)}
388    ${CONSTRUCTING+CONSTRUCTORS}
389
390    ${RELOCATING+_edata  =  .;}
391    ${RELOCATING+PROVIDE (edata = .);}
392    ${RELOCATING+. = ALIGN(2);}
393  } ${RELOCATING+ > ${DATA_MEMORY} =0xffffffff}
394
395  ${RELOCATING+__data_section_size = SIZEOF(.data);}
396  ${RELOCATING+PROVIDE (__data_section_size = SIZEOF(.data));}
397  ${RELOCATING+__data_image_end = __data_image + __data_section_size;}
398
399  ${RELOCATING+${PRE_COMPUTE_DATA_SIZE}}
400
401  /* .install ${RELOCATING-0}:
402  {
403    . = _data_image_end;
404  } ${RELOCATING+ > ${TEXT_MEMORY}} */
405
406  /* Relocation for some bss and data sections.  */
407  ${RELOCATING-${BSS_DATA_RELOC}}
408  ${RELOCATING-${SOFT_REGS_RELOC}}
409
410  .bss ${RELOCATING-0} :
411  {
412    ${RELOCATING+__bss_start = .;}
413    ${RELOCATING+*(.softregs)}
414    ${RELOCATING+*(.sbss)}
415    ${RELOCATING+*(.scommon)}
416    ${RELOCATING+*(.dynbss)}
417    *(.bss)
418    ${RELOCATING+*(.bss.*)}
419    ${RELOCATING+*(.gnu.linkonce.b.*)}
420    ${RELOCATING+*(COMMON)}
421    ${RELOCATING+PROVIDE (_end = .);}
422  } ${RELOCATING+ > ${DATA_MEMORY}}
423  ${RELOCATING+__bss_size = SIZEOF(.bss);}
424  ${RELOCATING+PROVIDE (__bss_size = SIZEOF(.bss));}
425
426  .eeprom ${RELOCATING-0} :
427  {
428    *(.eeprom)
429    ${RELOCATING+*(.eeprom.*)}
430  } ${RELOCATING+ > ${EEPROM_MEMORY}}
431
432  ${RELOCATING+${VECTORS}}
433
434  /* Stabs debugging sections.  */
435  .stab		 0 : { *(.stab) }
436  .stabstr	 0 : { *(.stabstr) }
437  .stab.excl	 0 : { *(.stab.excl) }
438  .stab.exclstr	 0 : { *(.stab.exclstr) }
439  .stab.index	 0 : { *(.stab.index) }
440  .stab.indexstr 0 : { *(.stab.indexstr) }
441
442  .comment	 0 : { *(.comment) }
443
444  /* Treatment of DWARF debug section must be at end of the linker
445     script to avoid problems when there are undefined symbols. It's necessary
446     to avoid that the DWARF section is relocated before such undefined
447     symbols are found.  */
448EOF
449
450. $srcdir/scripttempl/DWARF.sc
451
452cat <<EOF
453}
454EOF
455