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