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