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