Add support for PE startup files that want to define their own constructor and desctr...
[external/binutils.git] / ld / scripttempl / pep.sc
1 # Linker script for PE.
2 #
3 # Copyright (C) 2014-2017 Free Software Foundation, Inc.
4
5 # Copying and distribution of this file, with or without modification,
6 # are permitted in any medium without royalty provided the copyright
7 # notice and this notice are preserved.
8
9 if test -z "${RELOCATEABLE_OUTPUT_FORMAT}"; then
10   RELOCATEABLE_OUTPUT_FORMAT=${OUTPUT_FORMAT}
11 fi
12
13 # We can't easily and portably get an unquoted $ in a shell
14 # substitution, so we do this instead.
15 # Sorting of the .foo$* sections is required by the definition of
16 # grouped sections in PE.
17 # Sorting of the file names in R_IDATA is required by the
18 # current implementation of dlltool (this could probably be changed to
19 # use grouped sections instead).
20 if test "${RELOCATING}"; then
21   R_TEXT='*(SORT(.text$*))'
22   if test "x$LD_FLAG" = "xauto_import" ; then
23     R_DATA='*(SORT(.data$*))
24             *(.rdata)
25             *(SORT(.rdata$*))'
26     R_RDATA=''
27   else
28     R_DATA='*(SORT(.data$*))'
29     R_RDATA='*(.rdata)
30              *(SORT(.rdata$*))'
31   fi
32   R_IDATA234='
33     KEEP (SORT(*)(.idata$2))
34     KEEP (SORT(*)(.idata$3))
35     /* These zeroes mark the end of the import list.  */
36     LONG (0); LONG (0); LONG (0); LONG (0); LONG (0);
37     KEEP (SORT(*)(.idata$4))'
38   R_IDATA5='SORT(*)(.idata$5)'
39   R_IDATA67='
40     KEEP (SORT(*)(.idata$6))
41     KEEP (SORT(*)(.idata$7))'
42   R_CRT_XC='KEEP (*(SORT(.CRT$XC*)))  /* C initialization */'
43   R_CRT_XI='KEEP (*(SORT(.CRT$XI*)))  /* C++ initialization */'
44   R_CRT_XL='KEEP (*(SORT(.CRT$XL*)))  /* TLS callbacks */'
45   R_CRT_XP='KEEP (*(SORT(.CRT$XP*)))  /* Pre-termination */'
46   R_CRT_XT='KEEP (*(SORT(.CRT$XT*)))  /* Termination */'
47   R_TLS='
48     KEEP (*(.tls$AAA))
49     KEEP (*(.tls))
50     KEEP (*(.tls$))
51     KEEP (*(SORT(.tls$*)))
52     KEEP (*(.tls$ZZZ))'
53   R_RSRC='
54     KEEP (*(.rsrc))
55     KEEP (*(.rsrc$*))'
56 else
57   R_TEXT=
58   R_DATA=
59   R_RDATA='*(.rdata)'
60   R_IDATA234=
61   R_IDATA5=
62   R_IDATA67=
63   R_CRT_XC=
64   R_CRT_XI=
65   R_CRT_XL=
66   R_CRT_XP=
67   R_CRT_XT=
68   R_TLS='*(.tls)'
69   R_RSRC='*(.rsrc)'
70 fi
71
72 cat <<EOF
73 /* Copyright (C) 2014-2017 Free Software Foundation, Inc.
74
75    Copying and distribution of this script, with or without modification,
76    are permitted in any medium without royalty provided the copyright
77    notice and this notice are preserved.  */
78
79 ${RELOCATING+OUTPUT_FORMAT(${OUTPUT_FORMAT})}
80 ${RELOCATING-OUTPUT_FORMAT(${RELOCATEABLE_OUTPUT_FORMAT})}
81 ${OUTPUT_ARCH+OUTPUT_ARCH(${OUTPUT_ARCH})}
82
83 ${LIB_SEARCH_DIRS}
84
85 SECTIONS
86 {
87   ${RELOCATING+/* Make the virtual address and file offset synced if the alignment is}
88   ${RELOCATING+   lower than the target page size. */}
89   ${RELOCATING+. = SIZEOF_HEADERS;}
90   ${RELOCATING+. = ALIGN(__section_alignment__);}
91   .text ${RELOCATING+ __image_base__ + ( __section_alignment__ < ${TARGET_PAGE_SIZE} ? . : __section_alignment__ )} :
92   {
93     ${RELOCATING+ KEEP(*(.init))}
94     *(.text)
95     ${R_TEXT}
96     ${RELOCATING+ *(.text.*)}
97     ${RELOCATING+ *(.gnu.linkonce.t.*)}
98     ${RELOCATING+*(.glue_7t)}
99     ${RELOCATING+*(.glue_7)}
100     ${CONSTRUCTING+. = ALIGN(8);}
101     ${CONSTRUCTING+
102        PROVIDE(___CTOR_LIST__ = .);
103        PROVIDE(__CTOR_LIST__ = .);
104        LONG (-1); LONG (-1);
105        KEEP (*(.ctors));
106        KEEP (*(.ctor));
107        KEEP (*(SORT_BY_NAME(.ctors.*)));
108        LONG (0); LONG (0);
109      }
110     ${CONSTRUCTING+
111        PROVIDE(___DTOR_LIST__ = .);
112        PROVIDE(__DTOR_LIST__ = .);
113        LONG (-1); LONG (-1);
114        KEEP (*(.dtors));
115        KEEP (*(.dtor));
116        KEEP (*(SORT_BY_NAME(.dtors.*)));
117        LONG (0); LONG (0);
118      }
119     ${RELOCATING+ KEEP (*(.fini))}
120     /* ??? Why is .gcc_exc here?  */
121     ${RELOCATING+ *(.gcc_exc)}
122     ${RELOCATING+PROVIDE (etext = .);}
123     ${RELOCATING+ KEEP (*(.gcc_except_table))}
124   }
125
126   /* The Cygwin32 library uses a section to avoid copying certain data
127      on fork.  This used to be named ".data$nocopy".  The linker used
128      to include this between __data_start__ and __data_end__, but that
129      breaks building the cygwin32 dll.  Instead, we name the section
130      ".data_cygwin_nocopy" and explicitly include it after __data_end__. */
131
132   .data ${RELOCATING+BLOCK(__section_alignment__)} :
133   {
134     ${RELOCATING+__data_start__ = . ;}
135     *(.data)
136     ${RELOCATING+*(.data2)}
137     ${R_DATA}
138     KEEP(*(.jcr))
139     ${RELOCATING+__data_end__ = . ;}
140     ${RELOCATING+*(.data_cygwin_nocopy)}
141   }
142
143   .rdata ${RELOCATING+BLOCK(__section_alignment__)} :
144   {
145     ${R_RDATA}
146     ${RELOCATING+__rt_psrelocs_start = .;}
147     ${RELOCATING+KEEP(*(.rdata_runtime_pseudo_reloc))}
148     ${RELOCATING+__rt_psrelocs_end = .;}
149   }
150   ${RELOCATING+__rt_psrelocs_size = __rt_psrelocs_end - __rt_psrelocs_start;}
151   ${RELOCATING+___RUNTIME_PSEUDO_RELOC_LIST_END__ = .;}
152   ${RELOCATING+__RUNTIME_PSEUDO_RELOC_LIST_END__ = .;}
153   ${RELOCATING+___RUNTIME_PSEUDO_RELOC_LIST__ = . - __rt_psrelocs_size;}
154   ${RELOCATING+__RUNTIME_PSEUDO_RELOC_LIST__ = . - __rt_psrelocs_size;}
155
156   .eh_frame ${RELOCATING+BLOCK(__section_alignment__)} :
157   {
158     KEEP (*(.eh_frame${RELOCATING+*}))
159   }
160
161   .pdata ${RELOCATING+BLOCK(__section_alignment__)} :
162   {
163     KEEP(*(.pdata${RELOCATING+*}))
164   }
165
166   .xdata ${RELOCATING+BLOCK(__section_alignment__)} :
167   {
168     KEEP(*(.xdata${RELOCATING+*}))
169   }
170
171   .bss ${RELOCATING+BLOCK(__section_alignment__)} :
172   {
173     ${RELOCATING+__bss_start__ = . ;}
174     *(.bss)
175     *(COMMON)
176     ${RELOCATING+__bss_end__ = . ;}
177   }
178
179   .edata ${RELOCATING+BLOCK(__section_alignment__)} :
180   {
181     *(.edata)
182   }
183
184   /DISCARD/ :
185   {
186     *(.debug\$S)
187     *(.debug\$T)
188     *(.debug\$F)
189     *(.drectve)
190     ${RELOCATING+ *(.note.GNU-stack)}
191     ${RELOCATING+ *(.gnu.lto_*)}
192   }
193
194   .idata ${RELOCATING+BLOCK(__section_alignment__)} :
195   {
196     /* This cannot currently be handled with grouped sections.
197         See pep.em:sort_sections.  */
198     ${R_IDATA234}
199     ${RELOCATING+__IAT_start__ = .;}
200     ${R_IDATA5}
201     ${RELOCATING+__IAT_end__ = .;}
202     ${R_IDATA67}
203   }
204   .CRT ${RELOCATING+BLOCK(__section_alignment__)} :
205   {                                     
206     ${RELOCATING+___crt_xc_start__ = . ;}
207     ${R_CRT_XC}
208     ${RELOCATING+___crt_xc_end__ = . ;}
209     ${RELOCATING+___crt_xi_start__ = . ;}
210     ${R_CRT_XI}
211     ${RELOCATING+___crt_xi_end__ = . ;}
212     ${RELOCATING+___crt_xl_start__ = . ;}
213     ${R_CRT_XL}
214     /* ___crt_xl_end__ is defined in the TLS Directory support code */
215     ${RELOCATING+___crt_xp_start__ = . ;}
216     ${R_CRT_XP}
217     ${RELOCATING+___crt_xp_end__ = . ;}
218     ${RELOCATING+___crt_xt_start__ = . ;}
219     ${R_CRT_XT}
220     ${RELOCATING+___crt_xt_end__ = . ;}
221   }
222
223   /* Windows TLS expects .tls\$AAA to be at the start and .tls\$ZZZ to be
224      at the end of the .tls section.  This is important because _tls_start MUST
225      be at the beginning of the section to enable SECREL32 relocations with TLS
226      data.  */
227   .tls ${RELOCATING+BLOCK(__section_alignment__)} :
228   {                                     
229     ${RELOCATING+___tls_start__ = . ;}
230     ${R_TLS}
231     ${RELOCATING+___tls_end__ = . ;}
232   }
233
234   .endjunk ${RELOCATING+BLOCK(__section_alignment__)} :
235   {
236     /* end is deprecated, don't use it */
237     ${RELOCATING+PROVIDE (end = .);}
238     ${RELOCATING+PROVIDE ( _end = .);}
239     ${RELOCATING+ __end__ = .;}
240   }
241
242   .rsrc ${RELOCATING+BLOCK(__section_alignment__)} : SUBALIGN(4)
243   {
244     ${R_RSRC}
245   }
246
247   .reloc ${RELOCATING+BLOCK(__section_alignment__)} :
248   {
249     *(.reloc)
250   }
251
252   .stab ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} :
253   {
254     *(.stab)
255   }
256
257   .stabstr ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} :
258   {
259     *(.stabstr)
260   }
261
262   /* DWARF debug sections.
263      Symbols in the DWARF debugging sections are relative to the beginning
264      of the section.  Unlike other targets that fake this by putting the
265      section VMA at 0, the PE format will not allow it.  */
266
267   /* DWARF 1.1 and DWARF 2.  */
268   .debug_aranges ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} :
269   {
270     *(.debug_aranges)
271   }
272   .zdebug_aranges ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} :
273   {
274     *(.zdebug_aranges)
275   }
276
277   .debug_pubnames ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} :
278   {
279     *(.debug_pubnames)
280   }
281   .zdebug_pubnames ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} :
282   {
283     *(.zdebug_pubnames)
284   }
285
286   .debug_pubtypes ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} :
287   {
288     *(.debug_pubtypes)
289   }
290   .zdebug_pubtypes ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} :
291   {
292     *(.zdebug_pubtypes)
293   }
294
295   /* DWARF 2.  */
296   .debug_info ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} :
297   {
298     *(.debug_info${RELOCATING+ .gnu.linkonce.wi.*})
299   }
300   .zdebug_info ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} :
301   {
302     *(.zdebug_info${RELOCATING+ .zdebug.gnu.linkonce.wi.*})
303   }
304
305   .debug_abbrev ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} :
306   {
307     *(.debug_abbrev)
308   }
309   .zdebug_abbrev ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} :
310   {
311     *(.zdebug_abbrev)
312   }
313
314   .debug_line ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} :
315   {
316     *(.debug_line)
317   }
318   .zdebug_line ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} :
319   {
320     *(.zdebug_line)
321   }
322
323   .debug_frame ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} :
324   {
325     *(.debug_frame)
326   }
327   .zdebug_frame ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} :
328   {
329     *(.zdebug_frame)
330   }
331
332   .debug_str ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} :
333   {
334     *(.debug_str)
335   }
336   .zdebug_str ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} :
337   {
338     *(.zdebug_str)
339   }
340
341   .debug_loc ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} :
342   {
343     *(.debug_loc)
344   }
345   .zdebug_loc ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} :
346   {
347     *(.zdebug_loc)
348   }
349
350   .debug_macinfo ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} :
351   {
352     *(.debug_macinfo)
353   }
354   .zdebug_macinfo ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} :
355   {
356     *(.zdebug_macinfo)
357   }
358
359   /* SGI/MIPS DWARF 2 extensions.  */
360   .debug_weaknames ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} :
361   {
362     *(.debug_weaknames)
363   }
364   .zdebug_weaknames ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} :
365   {
366     *(.zdebug_weaknames)
367   }
368
369   .debug_funcnames ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} :
370   {
371     *(.debug_funcnames)
372   }
373   .zdebug_funcnames ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} :
374   {
375     *(.zdebug_funcnames)
376   }
377
378   .debug_typenames ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} :
379   {
380     *(.debug_typenames)
381   }
382   .zdebug_typenames ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} :
383   {
384     *(.zdebug_typenames)
385   }
386
387   .debug_varnames ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} :
388   {
389     *(.debug_varnames)
390   }
391   .zdebug_varnames ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} :
392   {
393     *(.zdebug_varnames)
394   }
395
396   .debug_macro ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} :
397   {
398     *(.debug_macro)
399   }
400   .zdebug_macro ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} :
401   {
402     *(.zdebug_macro)
403   }
404
405   /* DWARF 3.  */
406   .debug_ranges ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} :
407   {
408     *(.debug_ranges)
409   }
410   .zdebug_ranges ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} :
411   {
412     *(.zdebug_ranges)
413   }
414
415   /* DWARF 4.  */
416   .debug_types ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} :
417   {
418     *(.debug_types${RELOCATING+ .gnu.linkonce.wt.*})
419   }
420   .zdebug_types ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} :
421   {
422     *(.zdebug_types${RELOCATING+ .zdebug.gnu.linkonce.wt.*})
423   }
424
425   /* For Go and Rust.  */
426   .debug_gdb_scripts ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} :
427   {
428     *(.debug_gdb_scripts)
429   }
430   .zdebug_gdb_scripts ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} :
431   {
432     *(.zdebug_gdb_scripts)
433   }
434 }
435 EOF