1# Copyright (C) 2014-2021 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#	INITIAL_READONLY_SECTIONS - at start of data segment
11#	OTHER_READONLY_SECTIONS - other than .text .init .rodata ...
12#		(e.g., .PARISC.milli)
13#	OTHER_TEXT_SECTIONS - these get put in .text when relocating
14#	OTHER_READWRITE_SECTIONS - other than .data .bss .ctors .sdata ...
15#		(e.g., .PARISC.global)
16#	OTHER_SECTIONS - at the end
17#	EXECUTABLE_SYMBOLS - symbols that must be defined for an
18#		executable (e.g., _DYNAMIC_LINK)
19#	TEXT_START_SYMBOLS - symbols that appear at the start of the
20#		.text section.
21#	DATA_START_SYMBOLS - symbols that appear at the start of the
22#		.data section.
23#	OTHER_GOT_SYMBOLS - symbols defined just before .got.
24#	OTHER_GOT_SECTIONS - sections just after .got.
25#	OTHER_SDATA_SECTIONS - sections just after .sdata.
26#	OTHER_BSS_SYMBOLS - symbols that appear at the start of the
27#		.bss section besides __bss_start.
28#	DATA_PLT - .plt should be in data segment, not text segment.
29#	BSS_PLT - .plt should be in bss segment
30#	TEXT_DYNAMIC - .dynamic in text segment, not data segment.
31#	EMBEDDED - whether this is for an embedded system.
32#	SHLIB_TEXT_START_ADDR - if set, add to SIZEOF_HEADERS to set
33#		start address of shared library.
34#	INPUT_FILES - INPUT command of files to always include
35#	WRITABLE_RODATA - if set, the .rodata section should be writable
36#	INIT_START, INIT_END -  statements just before and just after
37#	combination of .init sections.
38#	FINI_START, FINI_END - statements just before and just after
39#	combination of .fini sections.
40#
41# When adding sections, do note that the names of some sections are used
42# when specifying the start address of the next.
43#
44
45#  Many sections come in three flavours.  There is the 'real' section,
46#  like ".data".  Then there are the per-procedure or per-variable
47#  sections, generated by -ffunction-sections and -fdata-sections in GCC,
48#  and useful for --gc-sections, which for a variable "foo" might be
49#  ".data.foo".  Then there are the linkonce sections, for which the linker
50#  eliminates duplicates, which are named like ".gnu.linkonce.d.foo".
51#  The exact correspondences are:
52#
53#  Section	Linkonce section
54#  .text	.gnu.linkonce.t.foo
55#  .rodata	.gnu.linkonce.r.foo
56#  .data	.gnu.linkonce.d.foo
57#  .bss		.gnu.linkonce.b.foo
58#  .sdata	.gnu.linkonce.s.foo
59#  .sbss	.gnu.linkonce.sb.foo
60#  .sdata2	.gnu.linkonce.s2.foo
61#  .sbss2	.gnu.linkonce.sb2.foo
62#
63#  Each of these can also have corresponding .rel.* and .rela.* sections.
64
65test -z "$ENTRY" && ENTRY=_start
66test -z "${BIG_OUTPUT_FORMAT}" && BIG_OUTPUT_FORMAT=${OUTPUT_FORMAT}
67test -z "${LITTLE_OUTPUT_FORMAT}" && LITTLE_OUTPUT_FORMAT=${OUTPUT_FORMAT}
68if [ -z "$MACHINE" ]; then OUTPUT_ARCH=${ARCH}; else OUTPUT_ARCH=${ARCH}:${MACHINE}; fi
69test -z "${ELFSIZE}" && ELFSIZE=32
70test -z "${ALIGNMENT}" && ALIGNMENT="${ELFSIZE} / 8"
71test "$LD_FLAG" = "N" && DATA_ADDR=.
72INTERP=".interp   ${RELOCATING-0} : { *(.interp) }"
73PLT=".plt    ${RELOCATING-0} : { *(.plt)	}"
74DYNAMIC=".dynamic     ${RELOCATING-0} : { *(.dynamic) }"
75RODATA=".rodata ${RELOCATING-0} : { *(.rodata) ${RELOCATING+*(.rodata.*)} ${RELOCATING+*(.gnu.linkonce.r.*)} }"
76SBSS2=".sbss2 ${RELOCATING-0} : { *(.sbss2) ${RELOCATING+*(.sbss2.*)} ${RELOCATING+*(.gnu.linkonce.sb2.*)} }"
77SDATA2=".sdata2 ${RELOCATING-0} : { *(.sdata2) ${RELOCATING+*(.sdata2.*)} ${RELOCATING+*(.gnu.linkonce.s2.*)} }"
78CTOR=".ctors ${CONSTRUCTING-0} :
79  {
80    ${CONSTRUCTING+${CTOR_START}}
81    /* gcc uses crtbegin.o to find the start of
82       the constructors, so we make sure it is
83       first.  Because this is a wildcard, it
84       doesn't matter if the user does not
85       actually link against crtbegin.o; the
86       linker won't look for a file to match a
87       wildcard.  The wildcard also means that it
88       doesn't matter which directory crtbegin.o
89       is in.  */
90
91    KEEP (*crtbegin.o(.ctors))
92    KEEP (*crtbegin?.o(.ctors))
93
94    /* We don't want to include the .ctor section from
95       the crtend.o file until after the sorted ctors.
96       The .ctor section from the crtend file contains the
97       end of ctors marker and it must be last */
98
99    KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o $OTHER_EXCLUDE_FILES) .ctors))
100    KEEP (*(SORT(.ctors.*)))
101    KEEP (*(.ctors))
102    ${CONSTRUCTING+${CTOR_END}}
103  }"
104
105DTOR=" .dtors       ${CONSTRUCTING-0} :
106  {
107    ${CONSTRUCTING+${DTOR_START}}
108    KEEP (*crtbegin.o(.dtors))
109    KEEP (*crtbegin?.o(.dtors))
110    KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o $OTHER_EXCLUDE_FILES) .dtors))
111    KEEP (*(SORT(.dtors.*)))
112    KEEP (*(.dtors))
113    ${CONSTRUCTING+${DTOR_END}}
114  }"
115
116# If this is for an embedded system, don't add SIZEOF_HEADERS.
117if [ -z "$EMBEDDED" ]; then
118   test -z "${TEXT_BASE_ADDRESS}" && TEXT_BASE_ADDRESS="${TEXT_START_ADDR} + SIZEOF_HEADERS"
119else
120   test -z "${TEXT_BASE_ADDRESS}" && TEXT_BASE_ADDRESS="${TEXT_START_ADDR}"
121fi
122
123cat <<EOF
124/* Copyright (C) 2014-2021 Free Software Foundation, Inc.
125
126   Copying and distribution of this script, with or without modification,
127   are permitted in any medium without royalty provided the copyright
128   notice and this notice are preserved.  */
129
130OUTPUT_FORMAT("${OUTPUT_FORMAT}", "${BIG_OUTPUT_FORMAT}",
131	      "${LITTLE_OUTPUT_FORMAT}")
132OUTPUT_ARCH(${OUTPUT_ARCH})
133${RELOCATING+ENTRY(${ENTRY})}
134
135${RELOCATING+${LIB_SEARCH_DIRS}}
136${RELOCATING+/* Do we need any of these for elf?
137   __DYNAMIC = 0; ${STACKZERO+${STACKZERO}} ${SHLIB_PATH+${SHLIB_PATH}}  */}
138${RELOCATING+${EXECUTABLE_SYMBOLS}}
139${RELOCATING+${INPUT_FILES}}
140${RELOCATING- /* For some reason, the Solaris linker makes bad executables
141  if gld -r is used and the intermediate file has sections starting
142  at non-zero addresses.  Could be a Solaris ld bug, could be a GNU ld
143  bug.  But for now assigning the zero vmas works.  */}
144
145SECTIONS
146{
147  ${CREATE_SHLIB-${RELOCATING+. = ${TEXT_BASE_ADDRESS};}}
148  ${CREATE_SHLIB+${RELOCATING+. = ${SHLIB_TEXT_START_ADDR:-0} + SIZEOF_HEADERS;}}
149  ${CREATE_SHLIB-${INTERP}}
150  ${TEXT_DYNAMIC+${DYNAMIC}}
151
152EOF
153if [ "x$COMBRELOC" = x ]; then
154  COMBRELOCCAT=cat
155else
156  COMBRELOCCAT="cat > $COMBRELOC"
157fi
158eval $COMBRELOCCAT <<EOF
159  .rel.init    ${RELOCATING-0} : { *(.rel.init)	}
160  .rela.init   ${RELOCATING-0} : { *(.rela.init)	}
161  .rel.text    ${RELOCATING-0} :
162    {
163      *(.rel.text)
164      ${RELOCATING+*(.rel.text.*)}
165      ${RELOCATING+*(.rel.gnu.linkonce.t.*)}
166    }
167  .rela.text   ${RELOCATING-0} :
168    {
169      *(.rela.text)
170      ${RELOCATING+*(.rela.text.*)}
171      ${RELOCATING+*(.rela.gnu.linkonce.t.*)}
172    }
173  .rel.fini    ${RELOCATING-0} : { *(.rel.fini)	}
174  .rela.fini   ${RELOCATING-0} : { *(.rela.fini)	}
175  .rel.rodata  ${RELOCATING-0} :
176    {
177      *(.rel.rodata)
178      ${RELOCATING+*(.rel.rodata.*)}
179      ${RELOCATING+*(.rel.gnu.linkonce.r.*)}
180    }
181  .rela.rodata ${RELOCATING-0} :
182    {
183      *(.rela.rodata)
184      ${RELOCATING+*(.rela.rodata.*)}
185      ${RELOCATING+*(.rela.gnu.linkonce.r.*)}
186    }
187  ${OTHER_READONLY_RELOC_SECTIONS}
188  .rel.data    ${RELOCATING-0} :
189    {
190      *(.rel.data)
191      ${RELOCATING+*(.rel.data.*)}
192      ${RELOCATING+*(.rel.gnu.linkonce.d.*)}
193    }
194  .rela.data   ${RELOCATING-0} :
195    {
196      *(.rela.data)
197      ${RELOCATING+*(.rela.data.*)}
198      ${RELOCATING+*(.rela.gnu.linkonce.d.*)}
199    }
200  .rel.ctors   ${RELOCATING-0} : { *(.rel.ctors)	}
201  .rela.ctors  ${RELOCATING-0} : { *(.rela.ctors)	}
202  .rel.dtors   ${RELOCATING-0} : { *(.rel.dtors)	}
203  .rela.dtors  ${RELOCATING-0} : { *(.rela.dtors)	}
204  .rel.got     ${RELOCATING-0} : { *(.rel.got)		}
205  .rela.got    ${RELOCATING-0} : { *(.rela.got)		}
206  ${OTHER_GOT_RELOC_SECTIONS}
207  .rel.sdata   ${RELOCATING-0} :
208    {
209      *(.rel.sdata)
210      ${RELOCATING+*(.rel.sdata.*)}
211      ${RELOCATING+*(.rel.gnu.linkonce.s.*)}
212    }
213  .rela.sdata   ${RELOCATING-0} :
214    {
215      *(.rela.sdata)
216      ${RELOCATING+*(.rela.sdata.*)}
217      ${RELOCATING+*(.rela.gnu.linkonce.s.*)}
218    }
219  .rel.sbss    ${RELOCATING-0} :
220    {
221      *(.rel.sbss)
222      ${RELOCATING+*(.rel.sbss.*)}
223      ${RELOCATING+*(.rel.gnu.linkonce.sb.*)}
224    }
225  .rela.sbss   ${RELOCATING-0} :
226    {
227      *(.rela.sbss)
228      ${RELOCATING+*(.rela.sbss.*)}
229      ${RELOCATING+*(.rela.gnu.linkonce.sb.*)}
230    }
231  .rel.sdata2  ${RELOCATING-0} :
232    {
233      *(.rel.sdata2)
234      ${RELOCATING+*(.rel.sdata2.*)}
235      ${RELOCATING+*(.rel.gnu.linkonce.s2.*)}
236    }
237  .rela.sdata2 ${RELOCATING-0} :
238    {
239      *(.rela.sdata2)
240      ${RELOCATING+*(.rela.sdata2.*)}
241      ${RELOCATING+*(.rela.gnu.linkonce.s2.*)}
242    }
243  .rel.sbss2   ${RELOCATING-0} :
244    {
245      *(.rel.sbss2)
246      ${RELOCATING+*(.rel.sbss2.*)}
247      ${RELOCATING+*(.rel.gnu.linkonce.sb2.*)}
248    }
249  .rela.sbss2  ${RELOCATING-0} :
250    {
251      *(.rela.sbss2)
252      ${RELOCATING+*(.rela.sbss2.*)}
253      ${RELOCATING+*(.rela.gnu.linkonce.sb2.*)}
254    }
255  .rel.bss     ${RELOCATING-0} :
256    {
257      *(.rel.bss)
258      ${RELOCATING+*(.rel.bss.*)}
259      ${RELOCATING+*(.rel.gnu.linkonce.b.*)}
260    }
261  .rela.bss    ${RELOCATING-0} :
262    {
263      *(.rela.bss)
264      ${RELOCATING+*(.rela.bss.*)}
265      ${RELOCATING+*(.rela.gnu.linkonce.b.*)}
266    }
267EOF
268if [ -n "$COMBRELOC" ]; then
269cat <<EOF
270  .rel.dyn	 :
271    {
272EOF
273sed -e '/^[	 ]*[{}][	 ]*$/d;/:[	 ]*$/d;/\.rela\./d;s/^.*: { *\(.*\)}$/      \1/' $COMBRELOC
274cat <<EOF
275    }
276  .rela.dyn	 :
277    {
278EOF
279sed -e '/^[	 ]*[{}][	 ]*$/d;/:[	 ]*$/d;/\.rel\./d;s/^.*: { *\(.*\)}/      \1/' $COMBRELOC
280cat <<EOF
281    }
282EOF
283fi
284cat <<EOF
285  .rel.plt     ${RELOCATING-0} : { *(.rel.plt)		}
286  .rela.plt    ${RELOCATING-0} : { *(.rela.plt)		}
287  ${OTHER_PLT_RELOC_SECTIONS}
288
289  .init        ${RELOCATING-0} :
290  {
291    ${RELOCATING+${INIT_START}}
292    KEEP (*(SORT_NONE(.init)))
293    ${RELOCATING+${INIT_END}}
294  } =${NOP-0}
295
296  ${DATA_PLT-${BSS_PLT-${PLT}}}
297  .text    ${RELOCATING-0} :
298  {
299    ${RELOCATING+${TEXT_START_SYMBOLS}}
300    *(.text)
301    ${RELOCATING+*(.text.*)}
302    *(.stub)
303    /* .gnu.warning sections are handled specially by elf.em.  */
304    *(.gnu.warning)
305    ${RELOCATING+*(.gnu.linkonce.t.*)}
306    ${RELOCATING+${OTHER_TEXT_SECTIONS}}
307  } =${NOP-0}
308  .fini    ${RELOCATING-0} :
309  {
310    ${RELOCATING+${FINI_START}}
311    KEEP (*(SORT_NONE(.fini)))
312    ${RELOCATING+${FINI_END}}
313  } =${NOP-0}
314  ${RELOCATING+PROVIDE (__etext = .);}
315  ${RELOCATING+PROVIDE (_etext = .);}
316  ${RELOCATING+PROVIDE (etext = .);}
317
318  /* Adjust the address for the data segment.  We want to adjust up to
319     the same address within the page on the next page up.  */
320  ${CREATE_SHLIB-${RELOCATING+. = ${DATA_ADDR-ALIGN(${MAXPAGESIZE}) + (. & (${MAXPAGESIZE} - 1))};}}
321  ${CREATE_SHLIB+${RELOCATING+. = ${SHLIB_DATA_ADDR-ALIGN(${MAXPAGESIZE}) + (. & (${MAXPAGESIZE} - 1))};}}
322
323  .data  ${RELOCATING-0} :
324  {
325    ${RELOCATING+${DATA_START_SYMBOLS}}
326    *(.data)
327    ${RELOCATING+*(.data.*)}
328    ${RELOCATING+*(.gnu.linkonce.d.*)}
329    ${CONSTRUCTING+SORT(CONSTRUCTORS)}
330  }
331  .data1 ${RELOCATING-0} : { *(.data1) }
332  .eh_frame ${RELOCATING-0} :
333  {
334    ${RELOCATING+PROVIDE (__eh_frame_begin = .);}
335    *(.eh_frame)
336    ${RELOCATING+LONG (0);}
337    ${RELOCATING+PROVIDE (__eh_frame_end = .);}
338  }
339  .gcc_except_table : { *(.gcc_except_table) }
340  ${INITIAL_READONLY_SECTIONS}
341  .hash        ${RELOCATING-0} : { *(.hash)		}
342  .dynsym      ${RELOCATING-0} : { *(.dynsym)		}
343  .dynstr      ${RELOCATING-0} : { *(.dynstr)		}
344  .gnu.version ${RELOCATING-0} : { *(.gnu.version)	}
345  .gnu.version_d ${RELOCATING-0} : { *(.gnu.version_d)	}
346  .gnu.version_r ${RELOCATING-0} : { *(.gnu.version_r)	}
347  ${RODATA}
348  .rodata1 ${RELOCATING-0} : { *(.rodata1) }
349  ${CREATE_SHLIB-${SDATA2}}
350  ${CREATE_SHLIB-${SBSS2}}
351  ${RELOCATING+${OTHER_READONLY_SECTIONS}}
352  ${RELOCATING+${OTHER_READWRITE_SECTIONS}}
353  ${TEXT_DYNAMIC-${DYNAMIC}}
354  ${RELOCATING+${CTOR}}
355  ${RELOCATING+${DTOR}}
356  .jcr : { KEEP (*(.jcr)) }
357  ${DATA_PLT+${PLT}}
358  ${RELOCATING+${OTHER_GOT_SYMBOLS}}
359  .got		${RELOCATING-0} : {${RELOCATING+ *(.got.plt)} *(.got) }
360  ${RELOCATING+${OTHER_GOT_SECTIONS}}
361  ${CREATE_SHLIB+${SDATA2}}
362  ${CREATE_SHLIB+${SBSS2}}
363  /* We want the small data sections together, so single-instruction offsets
364     can access them all, and initialized data all before uninitialized, so
365     we can shorten the on-disk segment size.  */
366  .sdata   ${RELOCATING-0} :
367  {
368    ${RELOCATING+${SDATA_START_SYMBOLS}}
369    *(.sdata)
370    ${RELOCATING+*(.sdata.*)}
371    ${RELOCATING+*(.gnu.linkonce.s.*)}
372  }
373  ${RELOCATING+${OTHER_SDATA_SECTIONS}}
374  ${RELOCATING+_edata = .;}
375  ${RELOCATING+PROVIDE (edata = .);}
376  ${RELOCATING+__bss_start = .;}
377  ${RELOCATING+${OTHER_BSS_SYMBOLS}}
378  .sbss    ${RELOCATING-0} :
379  {
380    ${RELOCATING+PROVIDE (__sbss_start = .);}
381    ${RELOCATING+PROVIDE (___sbss_start = .);}
382    ${RELOCATING+*(.dynsbss)}
383    *(.sbss)
384    ${RELOCATING+*(.sbss.*)}
385    ${RELOCATING+*(.gnu.linkonce.sb.*)}
386    ${RELOCATING+*(.scommon)}
387    ${RELOCATING+PROVIDE (__sbss_end = .);}
388    ${RELOCATING+PROVIDE (___sbss_end = .);}
389  }
390  ${BSS_PLT+${PLT}}
391  .bss     ${RELOCATING-0} :
392  {
393   ${RELOCATING+*(.dynbss)}
394   *(.bss)
395   ${RELOCATING+*(.bss.*)}
396   ${RELOCATING+*(.gnu.linkonce.b.*)}
397   ${RELOCATING+*(COMMON)
398   /* Align here to ensure that the .bss section occupies space up to
399      _end.  Align after .bss to ensure correct alignment even if the
400      .bss section disappears because there are no input sections.  */
401   . = ALIGN(${ALIGNMENT});}
402  }
403  ${RELOCATING+${OTHER_BSS_END_SYMBOLS}}
404  ${RELOCATING+. = ALIGN(${ALIGNMENT});}
405  ${RELOCATING+${OTHER_END_SYMBOLS}}
406  ${RELOCATING+_end = .;}
407  ${RELOCATING+PROVIDE (end = .);}
408
409  /* Stabs debugging sections.  */
410  .stab 0 : { *(.stab) }
411  .stabstr 0 : { *(.stabstr) }
412  .stab.excl 0 : { *(.stab.excl) }
413  .stab.exclstr 0 : { *(.stab.exclstr) }
414  .stab.index 0 : { *(.stab.index) }
415  .stab.indexstr 0 : { *(.stab.indexstr) }
416
417  .comment 0 : { *(.comment) }
418
419EOF
420
421. $srcdir/scripttempl/DWARF.sc
422
423cat <<EOF
424  ${RELOCATING+${OTHER_RELOCATING_SECTIONS}}
425
426  /* These must appear regardless of ${RELOCATING}.  */
427  ${OTHER_SECTIONS}
428}
429EOF
430