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