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 none)
9#	NO_SMALL_DATA - no .sbss/.sbss2/.sdata/.sdata2 sections if not
10#		empty.
11#	SMALL_DATA_CTOR - .ctors contains small data.
12#	SMALL_DATA_DTOR - .dtors contains small data.
13#	DATA_ADDR - if end-of-text-plus-one-page isn't right for data start
14#	INITIAL_READONLY_SECTIONS - at start of text segment
15#	OTHER_READONLY_SECTIONS - other than .text .init .rodata ...
16#		(e.g., .PARISC.milli)
17#	OTHER_TEXT_SECTIONS - these get put in .text when relocating
18#	INITIAL_READWRITE_SECTIONS - at start of data segment (after relro)
19#	OTHER_READWRITE_SECTIONS - other than .data .bss .ctors .sdata ...
20#		(e.g., .PARISC.global)
21#	OTHER_RELRO_SECTIONS - other than .data.rel.ro ...
22#		(e.g. PPC32 .fixup, .got[12])
23#	OTHER_RELRO_SECTIONS_2 - as above, but after .dynamic in text segment
24#	OTHER_BSS_SECTIONS - other than .bss .sbss ...
25#	ATTRS_SECTIONS - at the end
26#	OTHER_SECTIONS - at the end
27#	EXECUTABLE_SYMBOLS - symbols that must be defined for an
28#		executable (e.g., _DYNAMIC_LINK)
29#       TEXT_START_ADDR - the first byte of the text segment, after any
30#               headers.
31#       TEXT_BASE_ADDRESS - the first byte of the text segment.
32#	TEXT_START_SYMBOLS - symbols that appear at the start of the
33#		.text section.
34#	DATA_START_SYMBOLS - symbols that appear at the start of the
35#		.data section.
36#	DATA_END_SYMBOLS - symbols that appear at the end of the
37#		writeable data sections.
38#	OTHER_GOT_SYMBOLS - symbols defined just before .got.
39#	OTHER_GOT_SECTIONS - sections just after .got.
40#	OTHER_PLT_SECTIONS - sections just after .plt.
41#	OTHER_SDATA_SECTIONS - sections just after .sdata.
42#	OTHER_BSS_SYMBOLS - symbols that appear at the start of the
43#		.bss section besides __bss_start.
44#	PLT_NEXT_DATA - .plt next to data segment when .plt is in text segment.
45#	DATA_PLT - .plt should be in data segment, not text segment.
46#	PLT_BEFORE_GOT - .plt just before .got when .plt is in data segement.
47#	BSS_PLT - .plt should be in bss segment
48#	NO_REL_RELOCS - Don't include .rel.* sections in script
49#	NO_RELA_RELOCS - Don't include .rela.* sections in script
50#	NON_ALLOC_DYN - Place dynamic sections after data segment.
51#	TEXT_DYNAMIC - .dynamic in text segment, not data segment.
52#	EMBEDDED - whether this is for an embedded system.
53#	SHLIB_TEXT_START_ADDR - if set, add to SIZEOF_HEADERS to set
54#		start address of shared library.
55#	INPUT_FILES - INPUT command of files to always include
56#	WRITABLE_RODATA - if set, the .rodata section should be writable
57#	INIT_START, INIT_END -  statements just before and just after
58#	combination of .init sections.
59#	FINI_START, FINI_END - statements just before and just after
60#	combination of .fini sections.
61#	STACK_ADDR - start of a .stack section.
62#	OTHER_SYMBOLS - symbols to place right at the end of the script.
63#	ETEXT_NAME - name of a symbol for the end of the text section,
64#		normally etext.
65#	SEPARATE_CODE - if set, .text and similar sections containing
66#		actual machine instructions must be in wholly disjoint
67#		pages from any other data, including headers
68#	SEPARATE_GOTPLT - if set, .got.plt should be separate output section,
69#		so that .got can be in the RELRO area.  It should be set to
70#		the number of bytes in the beginning of .got.plt which can be
71#		in the RELRO area as well.
72#	USER_LABEL_PREFIX - prefix to add to user-visible symbols.
73#	RODATA_NAME, SDATA_NAME, SBSS_NAME, BSS_NAME - base parts of names
74#		for standard sections, without initial "." or suffixes.
75#
76# When adding sections, do note that the names of some sections are used
77# when specifying the start address of the next.
78#
79
80#  Many sections come in three flavours.  There is the 'real' section,
81#  like ".data".  Then there are the per-procedure or per-variable
82#  sections, generated by -ffunction-sections and -fdata-sections in GCC,
83#  and useful for --gc-sections, which for a variable "foo" might be
84#  ".data.foo".  Then there are the linkonce sections, for which the linker
85#  eliminates duplicates, which are named like ".gnu.linkonce.d.foo".
86#  The exact correspondences are:
87#
88#  Section	Linkonce section
89#  .text	.gnu.linkonce.t.foo
90#  .rodata	.gnu.linkonce.r.foo
91#  .data	.gnu.linkonce.d.foo
92#  .bss		.gnu.linkonce.b.foo
93#  .sdata	.gnu.linkonce.s.foo
94#  .sbss	.gnu.linkonce.sb.foo
95#  .sdata2	.gnu.linkonce.s2.foo
96#  .sbss2	.gnu.linkonce.sb2.foo
97#  .debug_info	.gnu.linkonce.wi.foo
98#  .tdata	.gnu.linkonce.td.foo
99#  .tbss	.gnu.linkonce.tb.foo
100#  .lrodata	.gnu.linkonce.lr.foo
101#  .ldata	.gnu.linkonce.l.foo
102#  .lbss	.gnu.linkonce.lb.foo
103#
104#  Each of these can also have corresponding .rel.* and .rela.* sections.
105
106test -z "$TEXT_START_SYMBOLS" && TEXT_START_SYMBOLS="PROVIDE_HIDDEN (__eprol = .);"
107test -z "$ENTRY" && ENTRY=_start
108if test -n "$NOP"; then
109  FILL="=$NOP"
110else
111  FILL=
112fi
113
114test -n "$CREATE_SHLIB$CREATE_PIE" && CREATE_PIC=" "
115test -z "$RODATA_NAME" && RODATA_NAME=rodata
116test -z "$SDATA_NAME" && SDATA_NAME=sdata
117test -z "$SBSS_NAME" && SBSS_NAME=sbss
118test -z "$BSS_NAME" && BSS_NAME=bss
119test -z "$ENTRY" && ENTRY=${USER_LABEL_PREFIX}_start
120test -z "${BIG_OUTPUT_FORMAT}" && BIG_OUTPUT_FORMAT=${OUTPUT_FORMAT}
121test -z "${LITTLE_OUTPUT_FORMAT}" && LITTLE_OUTPUT_FORMAT=${OUTPUT_FORMAT}
122if [ -z "$MACHINE" ]; then OUTPUT_ARCH=${ARCH}; else OUTPUT_ARCH=${ARCH}:${MACHINE}; fi
123test -z "${ELFSIZE}" && ELFSIZE=32
124test -z "${ALIGNMENT}" && ALIGNMENT="${ELFSIZE} / 8"
125test "$LD_FLAG" = "N" && DATA_ADDR=.
126test -z "${ETEXT_NAME}" && ETEXT_NAME=${USER_LABEL_PREFIX}etext
127test -n "$RELRO_NOW" && unset SEPARATE_GOTPLT
128test -z "$ATTRS_SECTIONS" && ATTRS_SECTIONS=".gnu.attributes 0 : { KEEP (*(.gnu.attributes)) }"
129if test -z "$DATA_SEGMENT_ALIGN"; then
130  test -n "$CREATE_SHLIB$CREATE_PIE" && test -n "$SHLIB_DATA_ADDR" && COMMONPAGESIZE=""
131  test -z "$CREATE_SHLIB$CREATE_PIE" && test -n "$DATA_ADDR" && COMMONPAGESIZE=""
132  DATA_SEGMENT_ALIGN="ALIGN(${SEGMENT_SIZE}) + (. & (${MAXPAGESIZE} - 1))"
133  DATA_SEGMENT_RELRO_END=""
134  DATA_SEGMENT_END=""
135  if test -n "${COMMONPAGESIZE}"; then
136    if test "${SEGMENT_SIZE}" != "${MAXPAGESIZE}"; then
137      DATA_SEGMENT_ALIGN="ALIGN (${SEGMENT_SIZE}) - ((${MAXPAGESIZE} - .) & (${MAXPAGESIZE} - 1)); . = DATA_SEGMENT_ALIGN (${MAXPAGESIZE}, ${COMMONPAGESIZE})"
138    else
139      DATA_SEGMENT_ALIGN="DATA_SEGMENT_ALIGN (${MAXPAGESIZE}, ${COMMONPAGESIZE})"
140    fi
141    DATA_SEGMENT_END=". = DATA_SEGMENT_END (.);"
142    DATA_SEGMENT_RELRO_END=". = DATA_SEGMENT_RELRO_END (${SEPARATE_GOTPLT-0}, .);"
143  fi
144fi
145if test -z "${INITIAL_READONLY_SECTIONS}${CREATE_SHLIB}"; then
146  INITIAL_READONLY_SECTIONS=".interp       ${RELOCATING-0} : { *(.interp) }"
147fi
148if test -z "$PLT"; then
149  IPLT=".iplt         ${RELOCATING-0} : { *(.iplt) }"
150  PLT=".plt          ${RELOCATING-0} : { *(.plt)${RELOCATING+${IREL_IN_PLT+ *(.iplt)}} }
151  ${IREL_IN_PLT-$IPLT}"
152fi
153test -n "${DATA_PLT-${BSS_PLT-text}}" && TEXT_PLT=
154if test -z "$GOT"; then
155  if test -z "$SEPARATE_GOTPLT"; then
156    GOT=".got          ${RELOCATING-0} : {${RELOCATING+ *(.got.plt) *(.igot.plt)} *(.got)${RELOCATING+ *(.igot)} }"
157  else
158    GOT=".got          ${RELOCATING-0} : { *(.got)${RELOCATING+ *(.igot)} }"
159    GOTPLT=".got.plt      ${RELOCATING-0} : { *(.got.plt)${RELOCATING+ *(.igot.plt)} }"
160  fi
161fi
162REL_IFUNC=".rel.ifunc    ${RELOCATING-0} : { *(.rel.ifunc) }"
163RELA_IFUNC=".rela.ifunc   ${RELOCATING-0} : { *(.rela.ifunc) }"
164REL_IPLT=".rel.iplt     ${RELOCATING-0} :
165    {
166      ${RELOCATING+${CREATE_PIC-PROVIDE_HIDDEN (${USER_LABEL_PREFIX}__rel_iplt_start = .);}}
167      *(.rel.iplt)
168      ${RELOCATING+${CREATE_PIC-PROVIDE_HIDDEN (${USER_LABEL_PREFIX}__rel_iplt_end = .);}}
169    }"
170RELA_IPLT=".rela.iplt    ${RELOCATING-0} :
171    {
172      ${RELOCATING+${CREATE_PIC-PROVIDE_HIDDEN (${USER_LABEL_PREFIX}__rela_iplt_start = .);}}
173      *(.rela.iplt)
174      ${RELOCATING+${CREATE_PIC-PROVIDE_HIDDEN (${USER_LABEL_PREFIX}__rela_iplt_end = .);}}
175    }"
176DYNAMIC=".dynamic      ${RELOCATING-0} : { *(.dynamic) }"
177RODATA=".${RODATA_NAME}       ${RELOCATING-0} : { *(.${RODATA_NAME}${RELOCATING+ .${RODATA_NAME}.* .gnu.linkonce.r.*}) }"
178DATARELRO=".data.rel.ro : { *(.data.rel.ro.local* .gnu.linkonce.d.rel.ro.local.*) *(.data.rel.ro .data.rel.ro.* .gnu.linkonce.d.rel.ro.*) }"
179DISCARDED="/DISCARD/ : { *(.note.GNU-stack) *(.gnu_debuglink) *(.gnu.lto_*) }"
180if test -z "${NO_SMALL_DATA}"; then
181  SBSS=".${SBSS_NAME}         ${RELOCATING-0} :
182  {
183    ${RELOCATING+${SBSS_START_SYMBOLS}}
184    ${CREATE_SHLIB+*(.${SBSS_NAME}2 .${SBSS_NAME}2.* .gnu.linkonce.sb2.*)}
185    ${RELOCATING+*(.dyn${SBSS_NAME})}
186    *(.${SBSS_NAME}${RELOCATING+ .${SBSS_NAME}.* .gnu.linkonce.sb.*})
187    ${RELOCATING+*(.scommon)}
188    ${RELOCATING+${SBSS_END_SYMBOLS}}
189  }"
190  SBSS2=".${SBSS_NAME}2        ${RELOCATING-0} : { *(.${SBSS_NAME}2${RELOCATING+ .${SBSS_NAME}2.* .gnu.linkonce.sb2.*}) }"
191  SDATA="/* We want the small data sections together, so single-instruction offsets
192     can access them all, and initialized data all before uninitialized, so
193     we can shorten the on-disk segment size.  */
194  .${SDATA_NAME}        ${RELOCATING-0} :
195  {
196    ${RELOCATING+${SDATA_START_SYMBOLS}}
197    ${CREATE_SHLIB+*(.${SDATA_NAME}2 .${SDATA_NAME}2.* .gnu.linkonce.s2.*)}
198    *(.${SDATA_NAME}${RELOCATING+ .${SDATA_NAME}.* .gnu.linkonce.s.*})
199  }"
200  SDATA2=".${SDATA_NAME}2       ${RELOCATING-0} :
201  {
202    ${RELOCATING+${SDATA2_START_SYMBOLS}}
203    *(.${SDATA_NAME}2${RELOCATING+ .${SDATA_NAME}2.* .gnu.linkonce.s2.*})
204  }"
205  REL_SDATA=".rel.${SDATA_NAME}    ${RELOCATING-0} : { *(.rel.${SDATA_NAME}${RELOCATING+ .rel.${SDATA_NAME}.* .rel.gnu.linkonce.s.*}) }
206  .rela.${SDATA_NAME}   ${RELOCATING-0} : { *(.rela.${SDATA_NAME}${RELOCATING+ .rela.${SDATA_NAME}.* .rela.gnu.linkonce.s.*}) }"
207  REL_SBSS=".rel.${SBSS_NAME}     ${RELOCATING-0} : { *(.rel.${SBSS_NAME}${RELOCATING+ .rel.${SBSS_NAME}.* .rel.gnu.linkonce.sb.*}) }
208  .rela.${SBSS_NAME}    ${RELOCATING-0} : { *(.rela.${SBSS_NAME}${RELOCATING+ .rela.${SBSS_NAME}.* .rela.gnu.linkonce.sb.*}) }"
209  REL_SDATA2=".rel.${SDATA_NAME}2   ${RELOCATING-0} : { *(.rel.${SDATA_NAME}2${RELOCATING+ .rel.${SDATA_NAME}2.* .rel.gnu.linkonce.s2.*}) }
210  .rela.${SDATA_NAME}2  ${RELOCATING-0} : { *(.rela.${SDATA_NAME}2${RELOCATING+ .rela.${SDATA_NAME}2.* .rela.gnu.linkonce.s2.*}) }"
211  REL_SBSS2=".rel.${SBSS_NAME}2    ${RELOCATING-0} : { *(.rel.${SBSS_NAME}2${RELOCATING+ .rel.${SBSS_NAME}2.* .rel.gnu.linkonce.sb2.*}) }
212  .rela.${SBSS_NAME}2   ${RELOCATING-0} : { *(.rela.${SBSS_NAME}2${RELOCATING+ .rela.${SBSS_NAME}2.* .rela.gnu.linkonce.sb2.*}) }"
213else
214  NO_SMALL_DATA=" "
215fi
216if test -z "${SDATA_GOT}${DATA_GOT}"; then
217  if test -n "${NO_SMALL_DATA}"; then
218    DATA_GOT=" "
219  fi
220fi
221if test -z "${SDATA_GOT}${DATA_GOT}"; then
222  if test -z "${NO_SMALL_DATA}"; then
223    SDATA_GOT=" "
224  fi
225fi
226test -n "$SEPARATE_GOTPLT" && SEPARATE_GOTPLT=" "
227test "${LARGE_SECTIONS}" = "yes" && REL_LARGE="
228  .rel.ldata    ${RELOCATING-0} : { *(.rel.ldata${RELOCATING+ .rel.ldata.* .rel.gnu.linkonce.l.*}) }
229  .rela.ldata   ${RELOCATING-0} : { *(.rela.ldata${RELOCATING+ .rela.ldata.* .rela.gnu.linkonce.l.*}) }
230  .rel.lbss     ${RELOCATING-0} : { *(.rel.lbss${RELOCATING+ .rel.lbss.* .rel.gnu.linkonce.lb.*}) }
231  .rela.lbss    ${RELOCATING-0} : { *(.rela.lbss${RELOCATING+ .rela.lbss.* .rela.gnu.linkonce.lb.*}) }
232  .rel.lrodata  ${RELOCATING-0} : { *(.rel.lrodata${RELOCATING+ .rel.lrodata.* .rel.gnu.linkonce.lr.*}) }
233  .rela.lrodata ${RELOCATING-0} : { *(.rela.lrodata${RELOCATING+ .rela.lrodata.* .rela.gnu.linkonce.lr.*}) }"
234test "${LARGE_SECTIONS}" = "yes" && LARGE_BSS="
235  .lbss ${RELOCATING-0} :
236  {
237    ${RELOCATING+*(.dynlbss)}
238    *(.lbss${RELOCATING+ .lbss.* .gnu.linkonce.lb.*})
239    ${RELOCATING+*(LARGE_COMMON)}
240  }"
241test "${LARGE_SECTIONS}" = "yes" && LARGE_SECTIONS="
242  .lrodata ${RELOCATING-0} ${RELOCATING+ALIGN(${MAXPAGESIZE}) + (. & (${MAXPAGESIZE} - 1))} :
243  {
244    *(.lrodata${RELOCATING+ .lrodata.* .gnu.linkonce.lr.*})
245  }
246  .ldata ${RELOCATING-0} ${RELOCATING+ALIGN(${MAXPAGESIZE}) + (. & (${MAXPAGESIZE} - 1))} :
247  {
248    *(.ldata${RELOCATING+ .ldata.* .gnu.linkonce.l.*})
249    ${RELOCATING+. = ALIGN(. != 0 ? ${ALIGNMENT} : 1);}
250  }"
251if test "${ENABLE_INITFINI_ARRAY}" = "yes"; then
252  SORT_INIT_ARRAY="KEEP (*(SORT_BY_INIT_PRIORITY(.init_array.*) SORT_BY_INIT_PRIORITY(.ctors.*)))"
253  SORT_FINI_ARRAY="KEEP (*(SORT_BY_INIT_PRIORITY(.fini_array.*) SORT_BY_INIT_PRIORITY(.dtors.*)))"
254  CTORS_IN_INIT_ARRAY="EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o $OTHER_EXCLUDE_FILES) .ctors"
255  DTORS_IN_FINI_ARRAY="EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o $OTHER_EXCLUDE_FILES) .dtors"
256else
257  SORT_INIT_ARRAY="KEEP (*(SORT(.init_array.*)))"
258  SORT_FINI_ARRAY="KEEP (*(SORT(.fini_array.*)))"
259  CTORS_IN_INIT_ARRAY=
260  DTORS_IN_FINI_ARRAY=
261fi
262PREINIT_ARRAY=".preinit_array    :
263  {
264    ${CREATE_SHLIB-PROVIDE_HIDDEN (${USER_LABEL_PREFIX}__preinit_array_start = .);}
265    KEEP (*(.preinit_array))
266    ${CREATE_SHLIB-PROVIDE_HIDDEN (${USER_LABEL_PREFIX}__preinit_array_end = .);}
267  }"
268INIT_ARRAY=".init_array    :
269  {
270    ${CREATE_SHLIB-PROVIDE_HIDDEN (${USER_LABEL_PREFIX}__init_array_start = .);}
271    ${SORT_INIT_ARRAY}
272    KEEP (*(.init_array ${CTORS_IN_INIT_ARRAY}))
273    ${CREATE_SHLIB-PROVIDE_HIDDEN (${USER_LABEL_PREFIX}__init_array_end = .);}
274  }"
275FINI_ARRAY=".fini_array    :
276  {
277    ${CREATE_SHLIB-PROVIDE_HIDDEN (${USER_LABEL_PREFIX}__fini_array_start = .);}
278    ${SORT_FINI_ARRAY}
279    KEEP (*(.fini_array ${DTORS_IN_FINI_ARRAY}))
280    ${CREATE_SHLIB-PROVIDE_HIDDEN (${USER_LABEL_PREFIX}__fini_array_end = .);}
281  }"
282CTOR=".ctors        ${CONSTRUCTING-0} :
283  {
284    ${CONSTRUCTING+${CTOR_START}}
285    /* gcc uses crtbegin.o to find the start of
286       the constructors, so we make sure it is
287       first.  Because this is a wildcard, it
288       doesn't matter if the user does not
289       actually link against crtbegin.o; the
290       linker won't look for a file to match a
291       wildcard.  The wildcard also means that it
292       doesn't matter which directory crtbegin.o
293       is in.  */
294
295    KEEP (*crtbegin.o(.ctors))
296    KEEP (*crtbegin?.o(.ctors))
297
298    /* We don't want to include the .ctor section from
299       the crtend.o file until after the sorted ctors.
300       The .ctor section from the crtend file contains the
301       end of ctors marker and it must be last */
302
303    KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o $OTHER_EXCLUDE_FILES) .ctors))
304    KEEP (*(SORT(.ctors.*)))
305    KEEP (*(.ctors))
306    ${CONSTRUCTING+${CTOR_END}}
307  }"
308DTOR=".dtors        ${CONSTRUCTING-0} :
309  {
310    ${CONSTRUCTING+${DTOR_START}}
311    KEEP (*crtbegin.o(.dtors))
312    KEEP (*crtbegin?.o(.dtors))
313    KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o $OTHER_EXCLUDE_FILES) .dtors))
314    KEEP (*(SORT(.dtors.*)))
315    KEEP (*(.dtors))
316    ${CONSTRUCTING+${DTOR_END}}
317  }"
318STACK=".stack        ${RELOCATING-0}${RELOCATING+${STACK_ADDR}} :
319  {
320    ${RELOCATING+${USER_LABEL_PREFIX}_stack = .;}
321    *(.stack)
322    ${RELOCATING+${STACK_SENTINEL}}
323  }"
324
325TEXT_START_ADDR="SEGMENT_START(\"text-segment\", ${TEXT_START_ADDR})"
326SHLIB_TEXT_START_ADDR="SEGMENT_START(\"text-segment\", ${SHLIB_TEXT_START_ADDR:-0})"
327
328# Don't bother with separate code segment when there are data sections
329# between .plt and .text.
330if test -z "$TINY_READONLY_SECTION"; then
331  case "$LD_FLAG" in
332    *textonly*)
333      SEPARATE_TEXT=" "
334      TEXT_SEGMENT_ALIGN=". = ALIGN(${MAXPAGESIZE});"
335      ;;
336  esac
337fi
338
339if [ -z "$SEPARATE_CODE" ]; then
340  SIZEOF_HEADERS_CODE=" + SIZEOF_HEADERS"
341else
342  SIZEOF_HEADERS_CODE=
343fi
344
345# If this is for an embedded system, don't add SIZEOF_HEADERS.
346if [ -z "$EMBEDDED" ]; then
347   test -z "${TEXT_BASE_ADDRESS}" && TEXT_BASE_ADDRESS="${TEXT_START_ADDR}${SIZEOF_HEADERS_CODE}"
348else
349   test -z "${TEXT_BASE_ADDRESS}" && TEXT_BASE_ADDRESS="${TEXT_START_ADDR}"
350fi
351
352cat <<EOF
353/* Copyright (C) 2014-2020 Free Software Foundation, Inc.
354
355   Copying and distribution of this script, with or without modification,
356   are permitted in any medium without royalty provided the copyright
357   notice and this notice are preserved.  */
358
359OUTPUT_FORMAT("${OUTPUT_FORMAT}", "${BIG_OUTPUT_FORMAT}",
360	      "${LITTLE_OUTPUT_FORMAT}")
361OUTPUT_ARCH(${OUTPUT_ARCH})
362${RELOCATING+ENTRY(${ENTRY})}
363
364${RELOCATING+${LIB_SEARCH_DIRS}}
365${RELOCATING+${CREATE_SHLIB-${EXECUTABLE_SYMBOLS}}}
366${RELOCATING+${INPUT_FILES}}
367${RELOCATING- /* For some reason, the Solaris linker makes bad executables
368  if gld -r is used and the intermediate file has sections starting
369  at non-zero addresses.  Could be a Solaris ld bug, could be a GNU ld
370  bug.  But for now assigning the zero vmas works.  */}
371
372SECTIONS
373{
374  ${RELOCATING+${SEPARATE_TEXT-/* Read-only sections, merged into text segment: */}}
375  ${CREATE_SHLIB-${CREATE_PIE-${RELOCATING+PROVIDE (__executable_start = ${TEXT_START_ADDR}); . = ${TEXT_BASE_ADDRESS};}}}
376  ${CREATE_SHLIB+${RELOCATING+. = ${SHLIB_TEXT_START_ADDR}${SIZEOF_HEADERS_CODE};}}
377  ${CREATE_PIE+${RELOCATING+PROVIDE (__executable_start = ${SHLIB_TEXT_START_ADDR}); . = ${SHLIB_TEXT_START_ADDR}${SIZEOF_HEADERS_CODE};}}
378EOF
379
380emit_early_ro()
381{
382  cat <<EOF
383  ${INITIAL_READONLY_SECTIONS}
384  .note.gnu.build-id ${RELOCATING-0}: { *(.note.gnu.build-id) }
385EOF
386}
387
388test -n "${SEPARATE_CODE}" || emit_early_ro
389
390test -n "${RELOCATING+0}" || unset NON_ALLOC_DYN
391test -z "${NON_ALLOC_DYN}" || TEXT_DYNAMIC=
392cat > ldscripts/dyntmp.$$ <<EOF
393  ${TEXT_DYNAMIC+${DYNAMIC}}
394  .hash         ${RELOCATING-0} : { *(.hash) }
395  .gnu.hash     ${RELOCATING-0} : { *(.gnu.hash) }
396  .dynsym       ${RELOCATING-0} : { *(.dynsym) }
397  .dynstr       ${RELOCATING-0} : { *(.dynstr) }
398  .gnu.version  ${RELOCATING-0} : { *(.gnu.version) }
399  .gnu.version_d ${RELOCATING-0}: { *(.gnu.version_d) }
400  .gnu.version_r ${RELOCATING-0}: { *(.gnu.version_r) }
401EOF
402
403if [ "x$COMBRELOC" = x ]; then
404  COMBRELOCCAT="cat >> ldscripts/dyntmp.$$"
405else
406  COMBRELOCCAT="cat > $COMBRELOC"
407fi
408eval $COMBRELOCCAT <<EOF
409  ${INITIAL_RELOC_SECTIONS}
410  .rel.init     ${RELOCATING-0} : { *(.rel.init) }
411  .rela.init    ${RELOCATING-0} : { *(.rela.init) }
412  .rel.text     ${RELOCATING-0} : { *(.rel.text${RELOCATING+ .rel.text.* .rel.gnu.linkonce.t.*}) }
413  .rela.text    ${RELOCATING-0} : { *(.rela.text${RELOCATING+ .rela.text.* .rela.gnu.linkonce.t.*}) }
414  .rel.fini     ${RELOCATING-0} : { *(.rel.fini) }
415  .rela.fini    ${RELOCATING-0} : { *(.rela.fini) }
416  .rel.${RODATA_NAME}   ${RELOCATING-0} : { *(.rel.${RODATA_NAME}${RELOCATING+ .rel.${RODATA_NAME}.* .rel.gnu.linkonce.r.*}) }
417  .rela.${RODATA_NAME}  ${RELOCATING-0} : { *(.rela.${RODATA_NAME}${RELOCATING+ .rela.${RODATA_NAME}.* .rela.gnu.linkonce.r.*}) }
418  ${OTHER_READONLY_RELOC_SECTIONS}
419  .rel.data.rel.ro ${RELOCATING-0} : { *(.rel.data.rel.ro${RELOCATING+ .rel.data.rel.ro.* .rel.gnu.linkonce.d.rel.ro.*}) }
420  .rela.data.rel.ro ${RELOCATING-0} : { *(.rela.data.rel.ro${RELOCATING+ .rela.data.rel.ro.* .rela.gnu.linkonce.d.rel.ro.*}) }
421  .rel.data     ${RELOCATING-0} : { *(.rel.data${RELOCATING+ .rel.data.* .rel.gnu.linkonce.d.*}) }
422  .rela.data    ${RELOCATING-0} : { *(.rela.data${RELOCATING+ .rela.data.* .rela.gnu.linkonce.d.*}) }
423  ${OTHER_READWRITE_RELOC_SECTIONS}
424  .rel.tdata	${RELOCATING-0} : { *(.rel.tdata${RELOCATING+ .rel.tdata.* .rel.gnu.linkonce.td.*}) }
425  .rela.tdata	${RELOCATING-0} : { *(.rela.tdata${RELOCATING+ .rela.tdata.* .rela.gnu.linkonce.td.*}) }
426  .rel.tbss	${RELOCATING-0} : { *(.rel.tbss${RELOCATING+ .rel.tbss.* .rel.gnu.linkonce.tb.*}) }
427  .rela.tbss	${RELOCATING-0} : { *(.rela.tbss${RELOCATING+ .rela.tbss.* .rela.gnu.linkonce.tb.*}) }
428  .rel.ctors    ${RELOCATING-0} : { *(.rel.ctors) }
429  .rela.ctors   ${RELOCATING-0} : { *(.rela.ctors) }
430  .rel.dtors    ${RELOCATING-0} : { *(.rel.dtors) }
431  .rela.dtors   ${RELOCATING-0} : { *(.rela.dtors) }
432  .rel.got      ${RELOCATING-0} : { *(.rel.got) }
433  .rela.got     ${RELOCATING-0} : { *(.rela.got) }
434  ${OTHER_GOT_RELOC_SECTIONS}
435  ${REL_SDATA}
436  ${REL_SBSS}
437  ${REL_SDATA2}
438  ${REL_SBSS2}
439  .rel.${BSS_NAME}      ${RELOCATING-0} : { *(.rel.${BSS_NAME}${RELOCATING+ .rel.${BSS_NAME}.* .rel.gnu.linkonce.b.*}) }
440  .rela.${BSS_NAME}     ${RELOCATING-0} : { *(.rela.${BSS_NAME}${RELOCATING+ .rela.${BSS_NAME}.* .rela.gnu.linkonce.b.*}) }
441  ${REL_LARGE}
442  ${IREL_IN_PLT+$REL_IFUNC}
443  ${IREL_IN_PLT+$RELA_IFUNC}
444  ${IREL_IN_PLT-$REL_IPLT}
445  ${IREL_IN_PLT-$RELA_IPLT}
446EOF
447
448if [ -n "$COMBRELOC" ]; then
449cat >> ldscripts/dyntmp.$$ <<EOF
450  .rel.dyn      ${RELOCATING-0} :
451    {
452EOF
453sed -e '/^[	 ]*[{}][	 ]*$/d;/:[	 ]*$/d;/\.rela\./d;/__rela_iplt_/d;s/^.*: { *\(.*\)}$/      \1/' $COMBRELOC >> ldscripts/dyntmp.$$
454cat >> ldscripts/dyntmp.$$ <<EOF
455    }
456  .rela.dyn     ${RELOCATING-0} :
457    {
458EOF
459sed -e '/^[	 ]*[{}][	 ]*$/d;/:[	 ]*$/d;/\.rel\./d;/__rel_iplt_/d;s/^.*: { *\(.*\)}/      \1/' $COMBRELOC >> ldscripts/dyntmp.$$
460cat >> ldscripts/dyntmp.$$ <<EOF
461    }
462EOF
463fi
464
465cat >> ldscripts/dyntmp.$$ <<EOF
466  .rel.plt      ${RELOCATING-0} :
467    {
468      *(.rel.plt)
469      ${IREL_IN_PLT+${RELOCATING+${CREATE_PIC-PROVIDE_HIDDEN (${USER_LABEL_PREFIX}__rel_iplt_start = .);}}}
470      ${IREL_IN_PLT+${RELOCATING+*(.rel.iplt)}}
471      ${IREL_IN_PLT+${RELOCATING+${CREATE_PIC-PROVIDE_HIDDEN (${USER_LABEL_PREFIX}__rel_iplt_end = .);}}}
472    }
473  .rela.plt     ${RELOCATING-0} :
474    {
475      *(.rela.plt)
476      ${IREL_IN_PLT+${RELOCATING+${CREATE_PIC-PROVIDE_HIDDEN (${USER_LABEL_PREFIX}__rela_iplt_start = .);}}}
477      ${IREL_IN_PLT+${RELOCATING+*(.rela.iplt)}}
478      ${IREL_IN_PLT+${RELOCATING+${CREATE_PIC-PROVIDE_HIDDEN (${USER_LABEL_PREFIX}__rela_iplt_end = .);}}}
479    }
480  ${OTHER_PLT_RELOC_SECTIONS}
481EOF
482
483emit_dyn()
484{
485  if test -z "${NO_REL_RELOCS}${NO_RELA_RELOCS}"; then
486    cat ldscripts/dyntmp.$$
487  else
488    if test -z "${NO_REL_RELOCS}"; then
489      sed -e '/^[	 ]*\.rela\.[^}]*$/,/}/d;/^[	 ]*\.rela\./d;/__rela_iplt_/d' ldscripts/dyntmp.$$
490    fi
491    if test -z "${NO_RELA_RELOCS}"; then
492      sed -e '/^[	 ]*\.rel\.[^}]*$/,/}/d;/^[	 ]*\.rel\./d;/__rel_iplt_/d' ldscripts/dyntmp.$$
493    fi
494  fi
495  rm -f ldscripts/dyntmp.$$
496}
497
498test -n "${NON_ALLOC_DYN}${SEPARATE_CODE}" || emit_dyn
499
500cat <<EOF
501  ${RELOCATING+${TEXT_SEGMENT_ALIGN}}
502
503  .init         ${RELOCATING-0}${RELOCATING+${INIT_ADDR}} :
504  {
505    ${RELOCATING+${INIT_START}}
506    KEEP (*(SORT_NONE(.init)))
507    ${RELOCATING+${INIT_END}}
508  } ${FILL}
509
510  ${TEXT_PLT+${PLT_NEXT_DATA-${PLT} ${OTHER_PLT_SECTIONS}}}
511  ${TINY_READONLY_SECTION}
512  .text         ${RELOCATING-0} :
513  {
514    ${RELOCATING+${TEXT_START_SYMBOLS}}
515    *(.text)
516    ${RELOCATING+*(.text.unlikely .text.*_unlikely .text.unlikely.*)}
517    ${RELOCATING+*(.text.exit .text.exit.*)}
518    ${RELOCATING+*(.text.startup .text.startup.*)}
519    ${RELOCATING+*(.text.hot .text.hot.*)}
520    ${RELOCATING+*(SORT(.text.sorted.*))}
521    *(.text .stub${RELOCATING+ .text.* .gnu.linkonce.t.*})
522    /* .gnu.warning sections are handled specially by elf.em.  */
523    *(.gnu.warning)
524    ${RELOCATING+${OTHER_TEXT_SECTIONS}}
525  } ${FILL}
526  .fini         ${RELOCATING-0}${RELOCATING+${FINI_ADDR}} :
527  {
528    ${RELOCATING+${FINI_START}}
529    KEEP (*(SORT_NONE(.fini)))
530    ${RELOCATING+${FINI_END}}
531  } ${FILL}
532  ${RELOCATING+PROVIDE (__${ETEXT_NAME} = .);}
533  ${RELOCATING+PROVIDE (_${ETEXT_NAME} = .);}
534  ${RELOCATING+PROVIDE (${ETEXT_NAME} = .);}
535  ${RELOCATING+${TEXT_SEGMENT_ALIGN}}
536EOF
537
538if test -n "${SEPARATE_CODE}${SEPARATE_TEXT}"; then
539  if test -n "${RODATA_ADDR}"; then
540    RODATA_ADDR="\
541SEGMENT_START(\"rodata-segment\", ${RODATA_ADDR}) + SIZEOF_HEADERS"
542  else
543    RODATA_ADDR="ALIGN(${SEGMENT_SIZE}) + (. & (${MAXPAGESIZE} - 1))"
544    RODATA_ADDR="SEGMENT_START(\"rodata-segment\", ${RODATA_ADDR})"
545  fi
546  if test -n "${SHLIB_RODATA_ADDR}"; then
547    SHLIB_RODATA_ADDR="\
548SEGMENT_START(\"rodata-segment\", ${SHLIB_RODATA_ADDR}) + SIZEOF_HEADERS"
549  else
550    SHLIB_RODATA_ADDR="ALIGN(${SEGMENT_SIZE}) + (. & (${MAXPAGESIZE} - 1))"
551    SHLIB_RODATA_ADDR="SEGMENT_START(\"rodata-segment\", ${SHLIB_RODATA_ADDR})"
552  fi
553  cat <<EOF
554  ${RELOCATING+/* Adjust the address for the rodata segment.  We want to adjust up to
555     the same address within the page on the next page up.  */
556  ${CREATE_SHLIB-${CREATE_PIE-. = ${RODATA_ADDR};}}
557  ${CREATE_SHLIB+. = ${SHLIB_RODATA_ADDR};}
558  ${CREATE_PIE+. = ${SHLIB_RODATA_ADDR};}}
559EOF
560  if test -n "${SEPARATE_CODE}"; then
561    emit_early_ro
562    emit_dyn
563  fi
564fi
565
566cat <<EOF
567  ${WRITABLE_RODATA-${RODATA}}
568  .${RODATA_NAME}1      ${RELOCATING-0} : { *(.${RODATA_NAME}1) }
569  ${CREATE_SHLIB-${SDATA2}}
570  ${CREATE_SHLIB-${SBSS2}}
571  ${OTHER_READONLY_SECTIONS}
572  .eh_frame_hdr ${RELOCATING-0} : { *(.eh_frame_hdr)${RELOCATING+ *(.eh_frame_entry .eh_frame_entry.*)} }
573  .eh_frame     ${RELOCATING-0} : ONLY_IF_RO { KEEP (*(.eh_frame))${RELOCATING+ *(.eh_frame.*)} }
574  .gcc_except_table ${RELOCATING-0} : ONLY_IF_RO { *(.gcc_except_table${RELOCATING+ .gcc_except_table.*}) }
575  .gnu_extab ${RELOCATING-0} : ONLY_IF_RO { *(.gnu_extab*) }
576  /* These sections are generated by the Sun/Oracle C++ compiler.  */
577  .exception_ranges ${RELOCATING-0} : ONLY_IF_RO { *(.exception_ranges${RELOCATING+*}) }
578  ${TEXT_PLT+${PLT_NEXT_DATA+${PLT} ${OTHER_PLT_SECTIONS}}}
579
580  ${RELOCATING+/* Adjust the address for the data segment.  We want to adjust up to
581     the same address within the page on the next page up.  */}
582  ${CREATE_SHLIB-${CREATE_PIE-${RELOCATING+. = ${DATA_ADDR-${DATA_SEGMENT_ALIGN}};}}}
583  ${CREATE_SHLIB+. = ${SHLIB_DATA_ADDR-${DATA_SEGMENT_ALIGN}};}
584  ${CREATE_PIE+. = ${SHLIB_DATA_ADDR-${DATA_SEGMENT_ALIGN}};}
585
586  /* Exception handling  */
587  .eh_frame     ${RELOCATING-0} : ONLY_IF_RW { KEEP (*(.eh_frame))${RELOCATING+ *(.eh_frame.*)} }
588  .gnu_extab    ${RELOCATING-0} : ONLY_IF_RW { *(.gnu_extab) }
589  .gcc_except_table ${RELOCATING-0} : ONLY_IF_RW { *(.gcc_except_table${RELOCATING+ .gcc_except_table.*}) }
590  .exception_ranges ${RELOCATING-0} : ONLY_IF_RW { *(.exception_ranges${RELOCATING+*}) }
591
592  /* Thread Local Storage sections  */
593  .tdata	${RELOCATING-0} :
594   {
595     ${RELOCATING+${CREATE_SHLIB-PROVIDE_HIDDEN (${USER_LABEL_PREFIX}__tdata_start = .);}}
596     *(.tdata${RELOCATING+ .tdata.* .gnu.linkonce.td.*})
597   }
598  .tbss		${RELOCATING-0} : { *(.tbss${RELOCATING+ .tbss.* .gnu.linkonce.tb.*})${RELOCATING+ *(.tcommon)} }
599
600  ${RELOCATING+${PREINIT_ARRAY}}
601  ${RELOCATING+${INIT_ARRAY}}
602  ${RELOCATING+${FINI_ARRAY}}
603  ${SMALL_DATA_CTOR-${RELOCATING+${CTOR}}}
604  ${SMALL_DATA_DTOR-${RELOCATING+${DTOR}}}
605  .jcr          ${RELOCATING-0} : { KEEP (*(.jcr)) }
606
607  ${RELOCATING+${DATARELRO}}
608  ${OTHER_RELRO_SECTIONS}
609  ${TEXT_DYNAMIC-${DYNAMIC}}
610  ${OTHER_RELRO_SECTIONS_2}
611  ${DATA_GOT+${RELRO_NOW+${DATA_PLT+${PLT_BEFORE_GOT+${PLT}}}}}
612  ${DATA_GOT+${RELRO_NOW+${GOT}}}
613  ${DATA_GOT+${RELRO_NOW+${GOTPLT}}}
614  ${DATA_GOT+${RELRO_NOW-${SEPARATE_GOTPLT+${GOT}}}}
615  ${RELOCATING+${DATA_SEGMENT_RELRO_END}}
616  ${INITIAL_READWRITE_SECTIONS}
617  ${DATA_SDATA+${SDATA}}
618  ${DATA_SDATA+${OTHER_SDATA_SECTIONS}}
619  ${DATA_SDATA+${SBSS}}
620  ${DATA_GOT+${RELRO_NOW-${DATA_PLT+${PLT_BEFORE_GOT+${PLT}}}}}
621  ${DATA_GOT+${RELRO_NOW-${SEPARATE_GOTPLT-${GOT}}}}
622  ${DATA_GOT+${RELRO_NOW-${GOTPLT}}}
623
624  ${DATA_PLT+${PLT_BEFORE_GOT-${PLT}}}
625
626  .data         ${RELOCATING-0} :
627  {
628    ${RELOCATING+${DATA_START_SYMBOLS}}
629    *(.data${RELOCATING+ .data.* .gnu.linkonce.d.*})
630    ${CONSTRUCTING+SORT(CONSTRUCTORS)}
631  }
632  .data1        ${RELOCATING-0} : { *(.data1) }
633  ${WRITABLE_RODATA+${RODATA}}
634  ${OTHER_READWRITE_SECTIONS}
635  ${SMALL_DATA_CTOR+${RELOCATING+${CTOR}}}
636  ${SMALL_DATA_DTOR+${RELOCATING+${DTOR}}}
637  ${SDATA_GOT+${DATA_PLT+${PLT_BEFORE_GOT+${PLT}}}}
638  ${SDATA_GOT+${RELOCATING+${OTHER_GOT_SYMBOLS+. = .; ${OTHER_GOT_SYMBOLS}}}}
639  ${SDATA_GOT+${GOT}}
640  ${SDATA_GOT+${OTHER_GOT_SECTIONS}}
641  ${DATA_SDATA-${SDATA}}
642  ${DATA_SDATA-${OTHER_SDATA_SECTIONS}}
643  ${RELOCATING+${DATA_END_SYMBOLS-${CREATE_SHLIB+PROVIDE (}${USER_LABEL_PREFIX}_edata = .${CREATE_SHLIB+)}; PROVIDE (${USER_LABEL_PREFIX}edata = .);}}
644  ${RELOCATING+. = .;}
645  ${RELOCATING+${CREATE_SHLIB+PROVIDE (}${USER_LABEL_PREFIX}__bss_start = .${CREATE_SHLIB+)};}
646  ${RELOCATING+${OTHER_BSS_SYMBOLS}}
647  ${DATA_SDATA-${SBSS}}
648  ${BSS_PLT+${PLT}}
649  .${BSS_NAME}          ${RELOCATING-0} :
650  {
651   ${RELOCATING+*(.dynbss)}
652   *(.${BSS_NAME}${RELOCATING+ .${BSS_NAME}.* .gnu.linkonce.b.*})
653   ${RELOCATING+*(COMMON)
654   /* Align here to ensure that the .bss section occupies space up to
655      _end.  Align after .bss to ensure correct alignment even if the
656      .bss section disappears because there are no input sections.
657      FIXME: Why do we need it? When there is no .bss section, we do not
658      pad the .data section.  */
659   . = ALIGN(. != 0 ? ${ALIGNMENT} : 1);}
660  }
661  ${OTHER_BSS_SECTIONS}
662  ${LARGE_BSS_AFTER_BSS+${LARGE_BSS}}
663  ${RELOCATING+${OTHER_BSS_END_SYMBOLS}}
664  ${RELOCATING+. = ALIGN(${ALIGNMENT});}
665EOF
666
667LARGE_DATA_ADDR=". = SEGMENT_START(\"ldata-segment\", ${LARGE_DATA_ADDR-.});"
668SHLIB_LARGE_DATA_ADDR=". = SEGMENT_START(\"ldata-segment\", ${SHLIB_LARGE_DATA_ADDR-.});"
669
670cat <<EOF
671  ${RELOCATING+${CREATE_SHLIB-${CREATE_PIE-${LARGE_DATA_ADDR}}}}
672  ${CREATE_SHLIB+${SHLIB_LARGE_DATA_ADDR}}
673  ${CREATE_PIE+${SHLIB_LARGE_DATA_ADDR}}
674  ${LARGE_SECTIONS}
675  ${LARGE_BSS_AFTER_BSS-${LARGE_BSS}}
676  ${RELOCATING+. = ALIGN(${ALIGNMENT});}
677  ${RELOCATING+${OTHER_END_SYMBOLS}}
678  ${RELOCATING+${END_SYMBOLS-${CREATE_SHLIB+PROVIDE (}${USER_LABEL_PREFIX}_end = .${CREATE_SHLIB+)}; PROVIDE (${USER_LABEL_PREFIX}end = .);}}
679  ${RELOCATING+${DATA_SEGMENT_END}}
680  ${TINY_DATA_SECTION}
681  ${TINY_BSS_SECTION}
682  ${STACK_ADDR+${STACK}}
683EOF
684
685test -z "${NON_ALLOC_DYN}" || emit_dyn
686
687cat <<EOF
688  /* Stabs debugging sections.  */
689  .stab          0 : { *(.stab) }
690  .stabstr       0 : { *(.stabstr) }
691  .stab.excl     0 : { *(.stab.excl) }
692  .stab.exclstr  0 : { *(.stab.exclstr) }
693  .stab.index    0 : { *(.stab.index) }
694  .stab.indexstr 0 : { *(.stab.indexstr) }
695
696  .comment       0 : { *(.comment) }
697
698  .gnu.build.attributes : { *(.gnu.build.attributes${RELOCATING+ .gnu.build.attributes.*}) }
699
700EOF
701
702. $srcdir/scripttempl/DWARF.sc
703
704cat <<EOF
705  ${ATTRS_SECTIONS}
706  ${OTHER_SECTIONS}
707  ${RELOCATING+${OTHER_SYMBOLS}}
708  ${RELOCATING+${DISCARDED}}
709}
710EOF
711