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