xref: /netbsd-src/external/gpl3/binutils/dist/ld/emultempl/elf.em (revision 4439cfd0acf9c7dc90625e5cd83b2317a9ab8967)
1# This shell script emits a C file. -*- C -*-
2# It does some substitutions.
3test -z "${ELFSIZE}" && ELFSIZE=32
4if [ -z "$MACHINE" ]; then
5  OUTPUT_ARCH=${ARCH}
6else
7  OUTPUT_ARCH=${ARCH}:${MACHINE}
8fi
9fragment <<EOF
10/* This file is is generated by a shell script.  DO NOT EDIT! */
11
12/* ${ELFSIZE} bit ELF emulation code for ${EMULATION_NAME}
13   Copyright (C) 1991-2024 Free Software Foundation, Inc.
14   Written by Steve Chamberlain <sac@cygnus.com>
15   ELF support by Ian Lance Taylor <ian@cygnus.com>
16
17   This file is part of the GNU Binutils.
18
19   This program is free software; you can redistribute it and/or modify
20   it under the terms of the GNU General Public License as published by
21   the Free Software Foundation; either version 3 of the License, or
22   (at your option) any later version.
23
24   This program is distributed in the hope that it will be useful,
25   but WITHOUT ANY WARRANTY; without even the implied warranty of
26   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27   GNU General Public License for more details.
28
29   You should have received a copy of the GNU General Public License
30   along with this program; if not, write to the Free Software
31   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
32   MA 02110-1301, USA.  */
33
34#define TARGET_IS_${EMULATION_NAME}
35
36#include "sysdep.h"
37#include "bfd.h"
38#include "libiberty.h"
39#include "getopt.h"
40#include "bfdlink.h"
41#include "ctf-api.h"
42#include "ld.h"
43#include "ldmain.h"
44#include "ldmisc.h"
45#include "ldexp.h"
46#include "ldlang.h"
47#include "ldfile.h"
48#include "ldemul.h"
49#include <ldgram.h>
50#include "elf-bfd.h"
51#include "ldelf.h"
52#include "ldelfgen.h"
53
54/* Declare functions used by various EXTRA_EM_FILEs.  */
55static void gld${EMULATION_NAME}_before_parse (void);
56static void gld${EMULATION_NAME}_before_plugin_all_symbols_read
57  (void);
58static void gld${EMULATION_NAME}_after_open (void);
59static void gld${EMULATION_NAME}_before_allocation (void);
60static void gld${EMULATION_NAME}_after_allocation (void);
61EOF
62
63# Import any needed special functions and/or overrides.
64#
65source_em ${srcdir}/emultempl/elf-generic.em
66if test -n "$EXTRA_EM_FILE" ; then
67  source_em ${srcdir}/emultempl/${EXTRA_EM_FILE}.em
68fi
69
70# Functions in this file can be overridden by setting the LDEMUL_* shell
71# variables.  If the name of the overriding function is the same as is
72# defined in this file, then don't output this file's version.
73# If a different overriding name is given then output the standard function
74# as presumably it is called from the overriding function.
75#
76if test x"$LDEMUL_BEFORE_PARSE" != xgld"$EMULATION_NAME"_before_parse; then
77fragment <<EOF
78
79static void
80gld${EMULATION_NAME}_before_parse (void)
81{
82  ldfile_set_output_arch ("${OUTPUT_ARCH}", bfd_arch_`echo ${ARCH} | sed -e 's/:.*//'`);
83  input_flags.dynamic = ${DYNAMIC_LINK-true};
84  /* XXX For NetBSD, ----copy-dt-needed-entries by default.  */
85  input_flags.add_DT_NEEDED_for_dynamic = true;
86  config.has_shared = `if test -n "$GENERATE_SHLIB_SCRIPT" ; then echo true ; else echo false ; fi`;
87  config.separate_code = `if test "x${SEPARATE_CODE}" = xyes ; then echo true ; else echo false ; fi`;
88  link_info.check_relocs_after_open_input = true;
89EOF
90if test -n "$COMMONPAGESIZE"; then
91fragment <<EOF
92  link_info.relro = DEFAULT_LD_Z_RELRO;
93EOF
94fi
95fragment <<EOF
96  link_info.separate_code = DEFAULT_LD_Z_SEPARATE_CODE;
97  link_info.warn_execstack = DEFAULT_LD_WARN_EXECSTACK;
98  link_info.no_warn_rwx_segments = ! DEFAULT_LD_WARN_RWX_SEGMENTS;
99  link_info.default_execstack = DEFAULT_LD_EXECSTACK;
100  link_info.error_execstack = DEFAULT_LD_ERROR_EXECSTACK;
101  link_info.warn_is_error_for_rwx_segments = DEFAULT_LD_ERROR_RWX_SEGMENTS;
102}
103
104EOF
105fi
106
107fragment <<EOF
108
109/* These variables are used to implement target options */
110
111static char *audit; /* colon (typically) separated list of libs */
112static char *depaudit; /* colon (typically) separated list of libs */
113
114EOF
115
116if test x"$LDEMUL_AFTER_OPEN" != xgld"$EMULATION_NAME"_after_open; then
117
118  IS_LINUX_TARGET=false
119  IS_FREEBSD_TARGET=false
120  case ${target} in
121    *-*-linux-* | *-*-k*bsd*-* | *-*-gnu*)
122      IS_LINUX_TARGET=true ;;
123    *-*-freebsd* | *-*-dragonfly*)
124      IS_FREEBSD_TARGET=true ;;
125  esac
126  IS_LIBPATH=false
127  if test "x${USE_LIBPATH}" = xyes; then
128    IS_LIBPATH=true
129  fi
130  IS_NATIVE=false
131  if test "x${NATIVE}" = xyes; then
132    IS_NATIVE=true
133  fi
134
135fragment <<EOF
136
137/* This is called before calling plugin 'all symbols read' hook.  */
138
139static void
140gld${EMULATION_NAME}_before_plugin_all_symbols_read (void)
141{
142  ldelf_before_plugin_all_symbols_read ($IS_LIBPATH, $IS_NATIVE,
143				        $IS_LINUX_TARGET,
144					$IS_FREEBSD_TARGET,
145					$ELFSIZE, "$prefix");
146}
147
148/* This is called after all the input files have been opened.  */
149
150static void
151gld${EMULATION_NAME}_after_open (void)
152{
153  ldelf_after_open ($IS_LIBPATH, $IS_NATIVE,
154		    $IS_LINUX_TARGET, $IS_FREEBSD_TARGET, $ELFSIZE, "$prefix");
155}
156
157EOF
158fi
159
160if test x"$LDEMUL_BEFORE_ALLOCATION" != xgld"$EMULATION_NAME"_before_allocation; then
161  if test x"${ELF_INTERPRETER_NAME}" = x; then
162    ELF_INTERPRETER_NAME=NULL
163  fi
164fragment <<EOF
165
166/* This is called after the sections have been attached to output
167   sections, but before any sizes or addresses have been set.  */
168
169static void
170gld${EMULATION_NAME}_before_allocation (void)
171{
172  ldelf_before_allocation (audit, depaudit, ${ELF_INTERPRETER_NAME});
173}
174
175EOF
176fi
177
178if test x"$LDEMUL_AFTER_ALLOCATION" != xgld"$EMULATION_NAME"_after_allocation; then
179fragment <<EOF
180
181static void
182gld${EMULATION_NAME}_after_allocation (void)
183{
184  int need_layout = bfd_elf_discard_info (link_info.output_bfd, &link_info);
185
186  if (need_layout < 0)
187    einfo (_("%X%P: .eh_frame/.stab edit: %E\n"));
188  else
189    ldelf_map_segments (need_layout);
190}
191EOF
192fi
193
194if test x"$LDEMUL_GET_SCRIPT" != xgld"$EMULATION_NAME"_get_script; then
195fragment <<EOF
196
197static char *
198gld${EMULATION_NAME}_get_script (int *isfile)
199EOF
200
201if test x"$COMPILE_IN" = xyes
202then
203# Scripts compiled in.
204
205# sed commands to quote an ld script as a C string.
206sc="-f ${srcdir}/emultempl/stringify.sed"
207
208fragment <<EOF
209{
210  *isfile = 0;
211
212  if (bfd_link_relocatable (&link_info) && config.build_constructors)
213    return
214EOF
215sed $sc ldscripts/${EMULATION_NAME}.xu			>> e${EMULATION_NAME}.c
216echo '  ; else if (bfd_link_relocatable (&link_info)) return' >> e${EMULATION_NAME}.c
217sed $sc ldscripts/${EMULATION_NAME}.xr			>> e${EMULATION_NAME}.c
218echo '  ; else if (!config.text_read_only) return'	>> e${EMULATION_NAME}.c
219sed $sc ldscripts/${EMULATION_NAME}.xbn			>> e${EMULATION_NAME}.c
220if cmp -s ldscripts/${EMULATION_NAME}.x ldscripts/${EMULATION_NAME}.xn; then : ; else
221echo '  ; else if (!config.magic_demand_paged) return'	>> e${EMULATION_NAME}.c
222sed $sc ldscripts/${EMULATION_NAME}.xn			>> e${EMULATION_NAME}.c
223fi
224if test -n "$GENERATE_PIE_SCRIPT" ; then
225if test -n "$GENERATE_COMBRELOC_SCRIPT" ; then
226echo '  ; else if (bfd_link_pie (&link_info)'		>> e${EMULATION_NAME}.c
227echo '             && link_info.combreloc'		>> e${EMULATION_NAME}.c
228echo '             && link_info.separate_code'		>> e${EMULATION_NAME}.c
229echo '             && (link_info.flags & DF_BIND_NOW)) return' >> e${EMULATION_NAME}.c
230sed $sc ldscripts/${EMULATION_NAME}.xdwe		>> e${EMULATION_NAME}.c
231echo '  ; else if (bfd_link_pie (&link_info)'		>> e${EMULATION_NAME}.c
232echo '             && link_info.combreloc'		>> e${EMULATION_NAME}.c
233echo '             && link_info.relro'			>> e${EMULATION_NAME}.c
234echo '             && (link_info.flags & DF_BIND_NOW)) return' >> e${EMULATION_NAME}.c
235sed $sc ldscripts/${EMULATION_NAME}.xdw			>> e${EMULATION_NAME}.c
236if test -n "$GENERATE_RELRO_SCRIPT" ; then
237echo '  ; else if (bfd_link_pie (&link_info)'          >> e${EMULATION_NAME}.c
238echo '             && link_info.combreloc'             >> e${EMULATION_NAME}.c
239echo '             && link_info.separate_code'         >> e${EMULATION_NAME}.c
240echo '             && link_info.relro) return'         >> e${EMULATION_NAME}.c
241sed $sc ldscripts/${EMULATION_NAME}.xdceo              >> e${EMULATION_NAME}.c
242fi
243echo '  ; else if (bfd_link_pie (&link_info)'		>> e${EMULATION_NAME}.c
244echo '             && link_info.separate_code'		>> e${EMULATION_NAME}.c
245echo '             && link_info.combreloc) return'	>> e${EMULATION_NAME}.c
246sed $sc ldscripts/${EMULATION_NAME}.xdce		>> e${EMULATION_NAME}.c
247if test -n "$GENERATE_RELRO_SCRIPT" ; then
248echo '  ; else if (bfd_link_pie (&link_info)'          >> e${EMULATION_NAME}.c
249echo '             && link_info.combreloc'             >> e${EMULATION_NAME}.c
250echo '             && link_info.relro) return'         >> e${EMULATION_NAME}.c
251sed $sc ldscripts/${EMULATION_NAME}.xdco               >> e${EMULATION_NAME}.c
252fi
253echo '  ; else if (bfd_link_pie (&link_info)'		>> e${EMULATION_NAME}.c
254echo '             && link_info.combreloc) return'	>> e${EMULATION_NAME}.c
255sed $sc ldscripts/${EMULATION_NAME}.xdc			>> e${EMULATION_NAME}.c
256if test -n "$GENERATE_RELRO_SCRIPT" ; then
257echo '  ; else if (bfd_link_pie (&link_info)'          >> e${EMULATION_NAME}.c
258echo '             && link_info.separate_code'         >> e${EMULATION_NAME}.c
259echo '             && link_info.relro) return'         >> e${EMULATION_NAME}.c
260sed $sc ldscripts/${EMULATION_NAME}.xdeo               >> e${EMULATION_NAME}.c
261fi
262fi
263echo '  ; else if (bfd_link_pie (&link_info)'		>> e${EMULATION_NAME}.c
264echo '             && link_info.separate_code) return'	>> e${EMULATION_NAME}.c
265sed $sc ldscripts/${EMULATION_NAME}.xde			>> e${EMULATION_NAME}.c
266if test -n "$GENERATE_RELRO_SCRIPT" ; then
267echo '  ; else if (bfd_link_pie (&link_info)'          >> e${EMULATION_NAME}.c
268echo '             && link_info.relro) return'         >> e${EMULATION_NAME}.c
269sed $sc ldscripts/${EMULATION_NAME}.xdo                >> e${EMULATION_NAME}.c
270fi
271echo '  ; else if (bfd_link_pie (&link_info)) return'	>> e${EMULATION_NAME}.c
272sed $sc ldscripts/${EMULATION_NAME}.xd			>> e${EMULATION_NAME}.c
273fi
274if test -n "$GENERATE_SHLIB_SCRIPT" ; then
275if test -n "$GENERATE_COMBRELOC_SCRIPT" ; then
276echo '  ; else if (bfd_link_dll (&link_info)'		>> e${EMULATION_NAME}.c
277echo '             && link_info.combreloc'		>> e${EMULATION_NAME}.c
278echo '             && link_info.separate_code'		>> e${EMULATION_NAME}.c
279echo '             && (link_info.flags & DF_BIND_NOW)) return' >> e${EMULATION_NAME}.c
280sed $sc ldscripts/${EMULATION_NAME}.xswe		>> e${EMULATION_NAME}.c
281echo '  ; else if (bfd_link_dll (&link_info)'		>> e${EMULATION_NAME}.c
282echo '             && link_info.combreloc'		>> e${EMULATION_NAME}.c
283echo '             && link_info.relro'			>> e${EMULATION_NAME}.c
284echo '             && (link_info.flags & DF_BIND_NOW)) return' >> e${EMULATION_NAME}.c
285sed $sc ldscripts/${EMULATION_NAME}.xsw			>> e${EMULATION_NAME}.c
286if test -n "$GENERATE_RELRO_SCRIPT" ; then
287echo '  ; else if (bfd_link_dll (&link_info)'          >> e${EMULATION_NAME}.c
288echo '             && link_info.combreloc'             >> e${EMULATION_NAME}.c
289echo '             && link_info.separate_code'         >> e${EMULATION_NAME}.c
290echo '             && link_info.relro) return'         >> e${EMULATION_NAME}.c
291sed $sc ldscripts/${EMULATION_NAME}.xsceo              >> e${EMULATION_NAME}.c
292fi
293echo '  ; else if (bfd_link_dll (&link_info)'		>> e${EMULATION_NAME}.c
294echo '             && link_info.combreloc'		>> e${EMULATION_NAME}.c
295echo '             && link_info.separate_code) return'	>> e${EMULATION_NAME}.c
296sed $sc ldscripts/${EMULATION_NAME}.xsce			>> e${EMULATION_NAME}.c
297if test -n "$GENERATE_RELRO_SCRIPT" ; then
298echo '  ; else if (bfd_link_dll (&link_info)'          >> e${EMULATION_NAME}.c
299echo '             && link_info.combreloc'             >> e${EMULATION_NAME}.c
300echo '             && link_info.relro) return'         >> e${EMULATION_NAME}.c
301sed $sc ldscripts/${EMULATION_NAME}.xsco               >> e${EMULATION_NAME}.c
302fi
303echo '  ; else if (bfd_link_dll (&link_info)'		>> e${EMULATION_NAME}.c
304echo '             && link_info.combreloc) return'	>> e${EMULATION_NAME}.c
305sed $sc ldscripts/${EMULATION_NAME}.xsc			>> e${EMULATION_NAME}.c
306if test -n "$GENERATE_RELRO_SCRIPT" ; then
307echo '  ; else if (bfd_link_dll (&link_info)'          >> e${EMULATION_NAME}.c
308echo '             && link_info.separate_code'         >> e${EMULATION_NAME}.c
309echo '             && link_info.relro) return'         >> e${EMULATION_NAME}.c
310sed $sc ldscripts/${EMULATION_NAME}.xseo               >> e${EMULATION_NAME}.c
311fi
312fi
313echo '  ; else if (bfd_link_dll (&link_info)'		>> e${EMULATION_NAME}.c
314echo '             && link_info.separate_code) return'	>> e${EMULATION_NAME}.c
315sed $sc ldscripts/${EMULATION_NAME}.xse			>> e${EMULATION_NAME}.c
316if test -n "$GENERATE_RELRO_SCRIPT" ; then
317echo '  ; else if (bfd_link_dll (&link_info)'          >> e${EMULATION_NAME}.c
318echo '             && link_info.relro) return'         >> e${EMULATION_NAME}.c
319sed $sc ldscripts/${EMULATION_NAME}.xso               >> e${EMULATION_NAME}.c
320fi
321echo '  ; else if (bfd_link_dll (&link_info)) return'	>> e${EMULATION_NAME}.c
322sed $sc ldscripts/${EMULATION_NAME}.xs			>> e${EMULATION_NAME}.c
323fi
324if test -n "$GENERATE_COMBRELOC_SCRIPT" ; then
325echo '  ; else if (link_info.combreloc'			>> e${EMULATION_NAME}.c
326echo '             && link_info.separate_code'		>> e${EMULATION_NAME}.c
327echo '             && (link_info.flags & DF_BIND_NOW)) return' >> e${EMULATION_NAME}.c
328sed $sc ldscripts/${EMULATION_NAME}.xwe			>> e${EMULATION_NAME}.c
329echo '  ; else if (link_info.combreloc'			>> e${EMULATION_NAME}.c
330echo '             && link_info.relro'			>> e${EMULATION_NAME}.c
331echo '             && (link_info.flags & DF_BIND_NOW)) return' >> e${EMULATION_NAME}.c
332sed $sc ldscripts/${EMULATION_NAME}.xw			>> e${EMULATION_NAME}.c
333if test -n "$GENERATE_RELRO_SCRIPT" ; then
334echo '  ; else if (link_info.combreloc'                 >> e${EMULATION_NAME}.c
335echo '             && link_info.separate_code'		>> e${EMULATION_NAME}.c
336echo '             && link_info.relro) return'          >> e${EMULATION_NAME}.c
337sed $sc ldscripts/${EMULATION_NAME}.xceo                >> e${EMULATION_NAME}.c
338fi
339echo '  ; else if (link_info.combreloc'			>> e${EMULATION_NAME}.c
340echo '             && link_info.separate_code) return'	>> e${EMULATION_NAME}.c
341sed $sc ldscripts/${EMULATION_NAME}.xce			>> e${EMULATION_NAME}.c
342if test -n "$GENERATE_RELRO_SCRIPT" ; then
343echo '  ; else if (link_info.combreloc'                 >> e${EMULATION_NAME}.c
344echo '             && link_info.relro) return'          >> e${EMULATION_NAME}.c
345sed $sc ldscripts/${EMULATION_NAME}.xco                 >> e${EMULATION_NAME}.c
346fi
347echo '  ; else if (link_info.combreloc) return'		>> e${EMULATION_NAME}.c
348sed $sc ldscripts/${EMULATION_NAME}.xc			>> e${EMULATION_NAME}.c
349fi
350if test -n "$GENERATE_RELRO_SCRIPT" ; then
351echo '  ; else if (link_info.separate_code'             >> e${EMULATION_NAME}.c
352echo '             && link_info.relro) return'          >> e${EMULATION_NAME}.c
353sed $sc ldscripts/${EMULATION_NAME}.xeo                 >> e${EMULATION_NAME}.c
354fi
355echo '  ; else if (link_info.separate_code) return'     >> e${EMULATION_NAME}.c
356sed $sc ldscripts/${EMULATION_NAME}.xe			>> e${EMULATION_NAME}.c
357if test -n "$GENERATE_RELRO_SCRIPT" ; then
358echo '  ; else if (link_info.relro) return'             >> e${EMULATION_NAME}.c
359sed $sc ldscripts/${EMULATION_NAME}.xo                  >> e${EMULATION_NAME}.c
360fi
361echo '  ; else return'					>> e${EMULATION_NAME}.c
362sed $sc ldscripts/${EMULATION_NAME}.x			>> e${EMULATION_NAME}.c
363echo '; }'						>> e${EMULATION_NAME}.c
364
365else
366# Scripts read from the filesystem.
367
368fragment <<EOF
369{
370  *isfile = 1;
371
372  if (bfd_link_relocatable (&link_info) && config.build_constructors)
373    return "ldscripts/${EMULATION_NAME}.xu";
374  else if (bfd_link_relocatable (&link_info))
375    return "ldscripts/${EMULATION_NAME}.xr";
376  else if (!config.text_read_only)
377    return "ldscripts/${EMULATION_NAME}.xbn";
378EOF
379if cmp -s ldscripts/${EMULATION_NAME}.x ldscripts/${EMULATION_NAME}.xn; then :
380else
381fragment <<EOF
382  else if (!config.magic_demand_paged)
383    return "ldscripts/${EMULATION_NAME}.xn";
384EOF
385fi
386if test -n "$GENERATE_PIE_SCRIPT" ; then
387if test -n "$GENERATE_COMBRELOC_SCRIPT" ; then
388fragment <<EOF
389  else if (bfd_link_pie (&link_info)
390	   && link_info.combreloc
391	   && link_info.relro
392	   && (link_info.flags & DF_BIND_NOW))
393    {
394      if (link_info.separate_code)
395	return "ldscripts/${EMULATION_NAME}.xdwe";
396      else
397	return "ldscripts/${EMULATION_NAME}.xdw";
398    }
399EOF
400if test -n "$GENERATE_RELRO_SCRIPT" ; then
401fragment <<EOF
402  else if (bfd_link_pie (&link_info)
403	   && link_info.combreloc
404	   && link_info.relro)
405    {
406      if (link_info.separate_code)
407	return "ldscripts/${EMULATION_NAME}.xdceo";
408      else
409	return "ldscripts/${EMULATION_NAME}.xdco";
410    }
411EOF
412fi
413fragment <<EOF
414  else if (bfd_link_pie (&link_info)
415	   && link_info.combreloc)
416    {
417      if (link_info.separate_code)
418	return "ldscripts/${EMULATION_NAME}.xdce";
419      else
420	return "ldscripts/${EMULATION_NAME}.xdc";
421    }
422EOF
423fi
424if test -n "$GENERATE_RELRO_SCRIPT" ; then
425fragment <<EOF
426  else if (bfd_link_pie (&link_info)
427	   && link_info.relro)
428    {
429      if (link_info.separate_code)
430	return "ldscripts/${EMULATION_NAME}.xdeo";
431      else
432	return "ldscripts/${EMULATION_NAME}.xdo";
433    }
434EOF
435fi
436fragment <<EOF
437  else if (bfd_link_pie (&link_info))
438    {
439      if (link_info.separate_code)
440	return "ldscripts/${EMULATION_NAME}.xde";
441      else
442	return "ldscripts/${EMULATION_NAME}.xd";
443    }
444EOF
445fi
446if test -n "$GENERATE_SHLIB_SCRIPT" ; then
447if test -n "$GENERATE_COMBRELOC_SCRIPT" ; then
448fragment <<EOF
449  else if (bfd_link_dll (&link_info) && link_info.combreloc
450	   && link_info.relro && (link_info.flags & DF_BIND_NOW))
451    {
452      if (link_info.separate_code)
453	return "ldscripts/${EMULATION_NAME}.xswe";
454      else
455	return "ldscripts/${EMULATION_NAME}.xsw";
456    }
457EOF
458if test -n "$GENERATE_RELRO_SCRIPT" ; then
459fragment <<EOF
460  else if (bfd_link_dll (&link_info)
461	   && link_info.combreloc
462	   && link_info.relro)
463    {
464      if (link_info.separate_code)
465	return "ldscripts/${EMULATION_NAME}.xsceo";
466      else
467	return "ldscripts/${EMULATION_NAME}.xsco";
468    }
469EOF
470fi
471fragment <<EOF
472  else if (bfd_link_dll (&link_info) && link_info.combreloc)
473    {
474      if (link_info.separate_code)
475	return "ldscripts/${EMULATION_NAME}.xsce";
476      else
477	return "ldscripts/${EMULATION_NAME}.xsc";
478    }
479EOF
480fi
481if test -n "$GENERATE_RELRO_SCRIPT" ; then
482fragment <<EOF
483  else if (bfd_link_dll (&link_info)
484	   && link_info.relro)
485    {
486      if (link_info.separate_code)
487	return "ldscripts/${EMULATION_NAME}.xseo";
488      else
489	return "ldscripts/${EMULATION_NAME}.xso";
490    }
491EOF
492fi
493fragment <<EOF
494  else if (bfd_link_dll (&link_info))
495    {
496      if (link_info.separate_code)
497	return "ldscripts/${EMULATION_NAME}.xse";
498      else
499	return "ldscripts/${EMULATION_NAME}.xs";
500    }
501EOF
502fi
503if test -n "$GENERATE_COMBRELOC_SCRIPT" ; then
504fragment <<EOF
505  else if (link_info.combreloc && link_info.relro
506	   && (link_info.flags & DF_BIND_NOW))
507    {
508      if (link_info.separate_code)
509	return "ldscripts/${EMULATION_NAME}.xwe";
510      else
511	return "ldscripts/${EMULATION_NAME}.xw";
512    }
513EOF
514if test -n "$GENERATE_RELRO_SCRIPT" ; then
515fragment <<EOF
516  else if (link_info.combreloc
517	   && link_info.relro)
518    {
519      if (link_info.separate_code)
520	return "ldscripts/${EMULATION_NAME}.xceo";
521      else
522	return "ldscripts/${EMULATION_NAME}.xco";
523    }
524EOF
525fi
526fragment <<EOF
527  else if (link_info.combreloc)
528    {
529      if (link_info.separate_code)
530	return "ldscripts/${EMULATION_NAME}.xce";
531      else
532	return "ldscripts/${EMULATION_NAME}.xc";
533    }
534EOF
535fi
536if test -n "$GENERATE_RELRO_SCRIPT" ; then
537fragment <<EOF
538  else if (link_info.relro)
539    {
540      if (link_info.separate_code)
541	return "ldscripts/${EMULATION_NAME}.xeo";
542      else
543	return "ldscripts/${EMULATION_NAME}.xo";
544    }
545EOF
546fi
547fragment <<EOF
548  else
549    {
550      if (link_info.separate_code)
551	return "ldscripts/${EMULATION_NAME}.xe";
552      else
553	return "ldscripts/${EMULATION_NAME}.x";
554    }
555}
556
557EOF
558fi
559fi
560
561if test -n "$PARSE_AND_LIST_PROLOGUE" ; then
562fragment <<EOF
563 $PARSE_AND_LIST_PROLOGUE
564EOF
565fi
566
567fragment <<EOF
568
569enum elf_options
570{
571  OPTION_DISABLE_NEW_DTAGS = 400,
572  OPTION_ENABLE_NEW_DTAGS,
573  OPTION_GROUP,
574  OPTION_EH_FRAME_HDR,
575  OPTION_NO_EH_FRAME_HDR,
576  OPTION_EXCLUDE_LIBS,
577  OPTION_HASH_STYLE,
578  OPTION_BUILD_ID,
579  OPTION_PACKAGE_METADATA,
580  OPTION_AUDIT,
581  OPTION_COMPRESS_DEBUG
582};
583
584static void
585gld${EMULATION_NAME}_add_options
586  (int ns, char **shortopts, int nl, struct option **longopts,
587   int nrl ATTRIBUTE_UNUSED, struct option **really_longopts ATTRIBUTE_UNUSED)
588{
589EOF
590if test x"$GENERATE_SHLIB_SCRIPT" = xyes; then
591fragment <<EOF
592  static const char xtra_short[] = "${PARSE_AND_LIST_SHORTOPTS}z:P:";
593EOF
594else
595fragment <<EOF
596  static const char xtra_short[] = "${PARSE_AND_LIST_SHORTOPTS}z:";
597EOF
598fi
599fragment <<EOF
600  static const struct option xtra_long[] = {
601EOF
602if test x"$GENERATE_SHLIB_SCRIPT" = xyes; then
603fragment <<EOF
604    {"audit", required_argument, NULL, OPTION_AUDIT},
605    {"Bgroup", no_argument, NULL, OPTION_GROUP},
606EOF
607fi
608fragment <<EOF
609    {"build-id", optional_argument, NULL, OPTION_BUILD_ID},
610    {"package-metadata", optional_argument, NULL, OPTION_PACKAGE_METADATA},
611    {"compress-debug-sections", required_argument, NULL, OPTION_COMPRESS_DEBUG},
612EOF
613if test x"$GENERATE_SHLIB_SCRIPT" = xyes; then
614fragment <<EOF
615    {"depaudit", required_argument, NULL, 'P'},
616    {"disable-new-dtags", no_argument, NULL, OPTION_DISABLE_NEW_DTAGS},
617    {"enable-new-dtags", no_argument, NULL, OPTION_ENABLE_NEW_DTAGS},
618    {"eh-frame-hdr", no_argument, NULL, OPTION_EH_FRAME_HDR},
619    {"no-eh-frame-hdr", no_argument, NULL, OPTION_NO_EH_FRAME_HDR},
620    {"exclude-libs", required_argument, NULL, OPTION_EXCLUDE_LIBS},
621    {"hash-style", required_argument, NULL, OPTION_HASH_STYLE},
622EOF
623fi
624if test -n "$PARSE_AND_LIST_LONGOPTS" ; then
625fragment <<EOF
626    $PARSE_AND_LIST_LONGOPTS
627EOF
628fi
629fragment <<EOF
630    {NULL, no_argument, NULL, 0}
631  };
632
633  *shortopts = (char *) xrealloc (*shortopts, ns + sizeof (xtra_short));
634  memcpy (*shortopts + ns, &xtra_short, sizeof (xtra_short));
635  *longopts = (struct option *)
636    xrealloc (*longopts, nl * sizeof (struct option) + sizeof (xtra_long));
637  memcpy (*longopts + nl, &xtra_long, sizeof (xtra_long));
638}
639
640#define DEFAULT_BUILD_ID_STYLE	"sha1"
641
642static bool
643gld${EMULATION_NAME}_handle_option (int optc)
644{
645  switch (optc)
646    {
647    default:
648      return false;
649
650    case OPTION_BUILD_ID:
651      free ((char *) ldelf_emit_note_gnu_build_id);
652      ldelf_emit_note_gnu_build_id = NULL;
653      if (optarg == NULL)
654	optarg = DEFAULT_BUILD_ID_STYLE;
655      if (strcmp (optarg, "none"))
656	ldelf_emit_note_gnu_build_id = xstrdup (optarg);
657      break;
658
659    case OPTION_PACKAGE_METADATA:
660      free ((char *) ldelf_emit_note_fdo_package_metadata);
661      ldelf_emit_note_fdo_package_metadata = NULL;
662      if (optarg != NULL && strlen(optarg) > 0)
663	ldelf_emit_note_fdo_package_metadata = xstrdup (optarg);
664      break;
665
666    case OPTION_COMPRESS_DEBUG:
667      config.compress_debug = bfd_get_compression_algorithm (optarg);
668      if (strcasecmp (optarg, "zstd") == 0)
669	{
670#ifndef HAVE_ZSTD
671	  if (config.compress_debug == COMPRESS_DEBUG_ZSTD)
672	    einfo (_ ("%F%P: --compress-debug-sections=zstd: ld is not built "
673		  "with zstd support\n"));
674#endif
675	}
676      if (config.compress_debug == COMPRESS_UNKNOWN)
677	einfo (_("%F%P: invalid --compress-debug-sections option: \`%s'\n"),
678	       optarg);
679      break;
680EOF
681
682if test x"$GENERATE_SHLIB_SCRIPT" = xyes; then
683fragment <<EOF
684    case OPTION_AUDIT:
685	ldelf_append_to_separated_string (&audit, optarg);
686	break;
687
688    case 'P':
689	ldelf_append_to_separated_string (&depaudit, optarg);
690	break;
691
692    case OPTION_DISABLE_NEW_DTAGS:
693      link_info.new_dtags = false;
694      break;
695
696    case OPTION_ENABLE_NEW_DTAGS:
697      link_info.new_dtags = true;
698      break;
699
700    case OPTION_EH_FRAME_HDR:
701      link_info.eh_frame_hdr_type = DWARF2_EH_HDR;
702      break;
703
704    case OPTION_NO_EH_FRAME_HDR:
705      link_info.eh_frame_hdr_type = 0;
706      break;
707
708    case OPTION_GROUP:
709      link_info.flags_1 |= (bfd_vma) DF_1_GROUP;
710      /* Groups must be self-contained.  */
711      link_info.unresolved_syms_in_objects = RM_DIAGNOSE;
712      link_info.unresolved_syms_in_shared_libs = RM_DIAGNOSE;
713      break;
714
715    case OPTION_EXCLUDE_LIBS:
716      add_excluded_libs (optarg);
717      break;
718
719    case OPTION_HASH_STYLE:
720      link_info.emit_hash = false;
721      link_info.emit_gnu_hash = false;
722      if (strcmp (optarg, "sysv") == 0)
723	link_info.emit_hash = true;
724      else if (strcmp (optarg, "gnu") == 0)
725	link_info.emit_gnu_hash = true;
726      else if (strcmp (optarg, "both") == 0)
727	{
728	  link_info.emit_hash = true;
729	  link_info.emit_gnu_hash = true;
730	}
731      else
732	einfo (_("%F%P: invalid hash style \`%s'\n"), optarg);
733      break;
734
735EOF
736fi
737fragment <<EOF
738    case 'z':
739      if (strcmp (optarg, "defs") == 0)
740	link_info.unresolved_syms_in_objects = RM_DIAGNOSE;
741      else if (strcmp (optarg, "undefs") == 0)
742	link_info.unresolved_syms_in_objects = RM_IGNORE;
743      else if (strcmp (optarg, "muldefs") == 0)
744	link_info.allow_multiple_definition = true;
745      else if (startswith (optarg, "max-page-size="))
746	{
747	  char *end;
748
749	  link_info.maxpagesize = strtoul (optarg + 14, &end, 0);
750	  if (*end
751	      || (link_info.maxpagesize & (link_info.maxpagesize - 1)) != 0)
752	    einfo (_("%F%P: invalid maximum page size \`%s'\n"),
753		   optarg + 14);
754	  link_info.maxpagesize_is_set = true;
755	}
756      else if (startswith (optarg, "common-page-size="))
757	{
758	  char *end;
759	  link_info.commonpagesize = strtoul (optarg + 17, &end, 0);
760	  if (*end
761	      || (link_info.commonpagesize & (link_info.commonpagesize - 1)) != 0)
762	    einfo (_("%F%P: invalid common page size \`%s'\n"),
763		   optarg + 17);
764	  link_info.commonpagesize_is_set = true;
765	}
766      else if (startswith (optarg, "stack-size="))
767	{
768	  char *end;
769	  link_info.stacksize = strtoul (optarg + 11, &end, 0);
770	  if (*end || link_info.stacksize < 0)
771	    einfo (_("%F%P: invalid stack size \`%s'\n"), optarg + 11);
772	  if (!link_info.stacksize)
773	    /* Use -1 for explicit no-stack, because zero means
774	       'default'.   */
775	    link_info.stacksize = -1;
776	}
777      else if (strcmp (optarg, "execstack") == 0)
778	{
779	  link_info.execstack = true;
780	  link_info.noexecstack = false;
781	}
782      else if (strcmp (optarg, "noexecstack") == 0)
783	{
784	  link_info.noexecstack = true;
785	  link_info.execstack = false;
786	}
787      else if (strcmp (optarg, "unique-symbol") == 0)
788	link_info.unique_symbol = true;
789      else if (strcmp (optarg, "nounique-symbol") == 0)
790	link_info.unique_symbol = false;
791      else if (strcmp (optarg, "globalaudit") == 0)
792	{
793	  link_info.flags_1 |= DF_1_GLOBAUDIT;
794	}
795      else if (startswith (optarg, "start-stop-gc"))
796	link_info.start_stop_gc = true;
797      else if (startswith (optarg, "nostart-stop-gc"))
798	link_info.start_stop_gc = false;
799      else if (startswith (optarg, "start-stop-visibility="))
800	{
801	  if (strcmp (optarg, "start-stop-visibility=default") == 0)
802	    link_info.start_stop_visibility = STV_DEFAULT;
803	  else if (strcmp (optarg, "start-stop-visibility=internal") == 0)
804	    link_info.start_stop_visibility = STV_INTERNAL;
805	  else if (strcmp (optarg, "start-stop-visibility=hidden") == 0)
806	    link_info.start_stop_visibility = STV_HIDDEN;
807	  else if (strcmp (optarg, "start-stop-visibility=protected") == 0)
808	    link_info.start_stop_visibility = STV_PROTECTED;
809	  else
810	    einfo (_("%F%P: invalid visibility in \`-z %s'; "
811		     "must be default, internal, hidden, or protected"),
812		   optarg);
813	}
814      else if (strcmp (optarg, "sectionheader") == 0)
815	config.no_section_header = false;
816      else if (strcmp (optarg, "nosectionheader") == 0)
817	config.no_section_header = true;
818EOF
819
820if test x"$GENERATE_SHLIB_SCRIPT" = xyes; then
821fragment <<EOF
822      else if (strcmp (optarg, "global") == 0)
823	link_info.flags_1 |= (bfd_vma) DF_1_GLOBAL;
824      else if (strcmp (optarg, "initfirst") == 0)
825	link_info.flags_1 |= (bfd_vma) DF_1_INITFIRST;
826      else if (strcmp (optarg, "interpose") == 0)
827	link_info.flags_1 |= (bfd_vma) DF_1_INTERPOSE;
828      else if (strcmp (optarg, "loadfltr") == 0)
829	link_info.flags_1 |= (bfd_vma) DF_1_LOADFLTR;
830      else if (strcmp (optarg, "nodefaultlib") == 0)
831	link_info.flags_1 |= (bfd_vma) DF_1_NODEFLIB;
832      else if (strcmp (optarg, "nodelete") == 0)
833	link_info.flags_1 |= (bfd_vma) DF_1_NODELETE;
834      else if (strcmp (optarg, "nodlopen") == 0)
835	link_info.flags_1 |= (bfd_vma) DF_1_NOOPEN;
836      else if (strcmp (optarg, "nodump") == 0)
837	link_info.flags_1 |= (bfd_vma) DF_1_NODUMP;
838      else if (strcmp (optarg, "now") == 0)
839	{
840	  link_info.flags |= (bfd_vma) DF_BIND_NOW;
841	  link_info.flags_1 |= (bfd_vma) DF_1_NOW;
842	}
843      else if (strcmp (optarg, "lazy") == 0)
844	{
845	  link_info.flags &= ~(bfd_vma) DF_BIND_NOW;
846	  link_info.flags_1 &= ~(bfd_vma) DF_1_NOW;
847	}
848      else if (strcmp (optarg, "origin") == 0)
849	{
850	  link_info.flags |= (bfd_vma) DF_ORIGIN;
851	  link_info.flags_1 |= (bfd_vma) DF_1_ORIGIN;
852	}
853      else if (strcmp (optarg, "unique") == 0)
854	link_info.gnu_flags_1 |= (bfd_vma) DF_GNU_1_UNIQUE;
855      else if (strcmp (optarg, "nounique") == 0)
856	link_info.gnu_flags_1 &= ~(bfd_vma) DF_GNU_1_UNIQUE;
857      else if (strcmp (optarg, "combreloc") == 0)
858	link_info.combreloc = true;
859      else if (strcmp (optarg, "nocombreloc") == 0)
860	link_info.combreloc = false;
861      else if (strcmp (optarg, "nocopyreloc") == 0)
862	link_info.nocopyreloc = true;
863EOF
864if test -n "$COMMONPAGESIZE"; then
865fragment <<EOF
866      else if (strcmp (optarg, "relro") == 0)
867	link_info.relro = true;
868      else if (strcmp (optarg, "norelro") == 0)
869	link_info.relro = false;
870EOF
871fi
872fragment <<EOF
873      else if (strcmp (optarg, "separate-code") == 0)
874	link_info.separate_code = true;
875      else if (strcmp (optarg, "noseparate-code") == 0)
876	link_info.separate_code = false;
877      else if (strcmp (optarg, "common") == 0)
878	link_info.elf_stt_common = elf_stt_common;
879      else if (strcmp (optarg, "nocommon") == 0)
880	link_info.elf_stt_common = no_elf_stt_common;
881      else if (strcmp (optarg, "text") == 0)
882	link_info.textrel_check = textrel_check_error;
883      else if (strcmp (optarg, "notext") == 0)
884	link_info.textrel_check = textrel_check_none;
885      else if (strcmp (optarg, "textoff") == 0)
886	link_info.textrel_check = textrel_check_none;
887EOF
888fi
889
890if test -n "$PARSE_AND_LIST_ARGS_CASE_Z" ; then
891fragment <<EOF
892 $PARSE_AND_LIST_ARGS_CASE_Z
893EOF
894fi
895
896fragment <<EOF
897      else
898	einfo (_("%P: warning: -z %s ignored\n"), optarg);
899      break;
900EOF
901
902if test -n "$PARSE_AND_LIST_ARGS_CASES" ; then
903fragment <<EOF
904 $PARSE_AND_LIST_ARGS_CASES
905EOF
906fi
907
908fragment <<EOF
909    }
910
911  return true;
912}
913
914EOF
915
916if test x"$LDEMUL_LIST_OPTIONS" != xgld"$EMULATION_NAME"_list_options; then
917gld_list_options="gld${EMULATION_NAME}_list_options"
918if test -n "$PARSE_AND_LIST_OPTIONS"; then
919fragment <<EOF
920
921static void
922gld${EMULATION_NAME}_list_options (FILE * file)
923{
924EOF
925
926if test -n "$PARSE_AND_LIST_OPTIONS" ; then
927fragment <<EOF
928 $PARSE_AND_LIST_OPTIONS
929EOF
930fi
931
932fragment <<EOF
933}
934EOF
935else
936  gld_list_options="NULL"
937fi
938
939if test -n "$PARSE_AND_LIST_EPILOGUE" ; then
940fragment <<EOF
941 $PARSE_AND_LIST_EPILOGUE
942EOF
943fi
944fi
945
946LDEMUL_AFTER_PARSE=${LDEMUL_AFTER_PARSE-ldelf_after_parse}
947LDEMUL_BEFORE_PLUGIN_ALL_SYMBOLS_READ=${LDEMUL_BEFORE_PLUGIN_ALL_SYMBOLS_READ-gld${EMULATION_NAME}_before_plugin_all_symbols_read}
948LDEMUL_AFTER_OPEN=${LDEMUL_AFTER_OPEN-gld${EMULATION_NAME}_after_open}
949LDEMUL_BEFORE_PLACE_ORPHANS=${LDEMUL_BEFORE_PLACE_ORPHANS-ldelf_before_place_orphans}
950LDEMUL_AFTER_ALLOCATION=${LDEMUL_AFTER_ALLOCATION-gld${EMULATION_NAME}_after_allocation}
951LDEMUL_SET_OUTPUT_ARCH=${LDEMUL_SET_OUTPUT_ARCH-ldelf_set_output_arch}
952LDEMUL_BEFORE_ALLOCATION=${LDEMUL_BEFORE_ALLOCATION-gld${EMULATION_NAME}_before_allocation}
953LDEMUL_OPEN_DYNAMIC_ARCHIVE=${LDEMUL_OPEN_DYNAMIC_ARCHIVE-ldelf_open_dynamic_archive}
954LDEMUL_PLACE_ORPHAN=${LDEMUL_PLACE_ORPHAN-ldelf_place_orphan}
955LDEMUL_ADD_OPTIONS=gld${EMULATION_NAME}_add_options
956LDEMUL_HANDLE_OPTION=gld${EMULATION_NAME}_handle_option
957LDEMUL_LIST_OPTIONS=${LDEMUL_LIST_OPTIONS-${gld_list_options}}
958LDEMUL_RECOGNIZED_FILE=${LDEMUL_RECOGNIZED_FILE-ldelf_load_symbols}
959
960source_em ${srcdir}/emultempl/emulation.em
961