Fix: Segmentation fault caused by npd in objdump
[binutils-gdb.git] / ld / emultempl / elf.em
blob1c5030d5e1ccb64dc1fa0942f35f54e1b234be80
1 # This shell script emits a C file. -*- C -*-
2 # It does some substitutions.
3 test -z "${ELFSIZE}" && ELFSIZE=32
4 if [ -z "$MACHINE" ]; then
5   OUTPUT_ARCH=${ARCH}
6 else
7   OUTPUT_ARCH=${ARCH}:${MACHINE}
8 fi
9 fragment <<EOF
10 /* This file is is generated by a shell script.  DO NOT EDIT! */
12 /* ${ELFSIZE} bit ELF emulation code for ${EMULATION_NAME}
13    Copyright (C) 1991-2023 Free Software Foundation, Inc.
14    Written by Steve Chamberlain <sac@cygnus.com>
15    ELF support by Ian Lance Taylor <ian@cygnus.com>
17    This file is part of the GNU Binutils.
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.
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.
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.  */
34 #define TARGET_IS_${EMULATION_NAME}
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"
54 /* Declare functions used by various EXTRA_EM_FILEs.  */
55 static void gld${EMULATION_NAME}_before_parse (void);
56 static void gld${EMULATION_NAME}_before_plugin_all_symbols_read
57   (void);
58 static void gld${EMULATION_NAME}_after_open (void);
59 static void gld${EMULATION_NAME}_before_allocation (void);
60 static void gld${EMULATION_NAME}_after_allocation (void);
61 EOF
63 # Import any needed special functions and/or overrides.
65 source_em ${srcdir}/emultempl/elf-generic.em
66 if test -n "$EXTRA_EM_FILE" ; then
67   source_em ${srcdir}/emultempl/${EXTRA_EM_FILE}.em
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.
76 if test x"$LDEMUL_BEFORE_PARSE" != xgld"$EMULATION_NAME"_before_parse; then
77 fragment <<EOF
79 static void
80 gld${EMULATION_NAME}_before_parse (void)
82   ldfile_set_output_arch ("${OUTPUT_ARCH}", bfd_arch_`echo ${ARCH} | sed -e 's/:.*//'`);
83   input_flags.dynamic = ${DYNAMIC_LINK-true};
84   config.has_shared = `if test -n "$GENERATE_SHLIB_SCRIPT" ; then echo true ; else echo false ; fi`;
85   config.separate_code = `if test "x${SEPARATE_CODE}" = xyes ; then echo true ; else echo false ; fi`;
86   link_info.check_relocs_after_open_input = true;
87 EOF
88 if test -n "$COMMONPAGESIZE"; then
89 fragment <<EOF
90   link_info.relro = DEFAULT_LD_Z_RELRO;
91 EOF
93 fragment <<EOF
94   link_info.separate_code = DEFAULT_LD_Z_SEPARATE_CODE;
95   link_info.warn_execstack = DEFAULT_LD_WARN_EXECSTACK;
96   link_info.no_warn_rwx_segments = ! DEFAULT_LD_WARN_RWX_SEGMENTS;
97   link_info.default_execstack = DEFAULT_LD_EXECSTACK;
103 fragment <<EOF
105 /* These variables are used to implement target options */
107 static char *audit; /* colon (typically) separated list of libs */
108 static char *depaudit; /* colon (typically) separated list of libs */
112 if test x"$LDEMUL_AFTER_OPEN" != xgld"$EMULATION_NAME"_after_open; then
114   IS_LINUX_TARGET=false
115   IS_FREEBSD_TARGET=false
116   case ${target} in
117     *-*-linux-* | *-*-k*bsd*-* | *-*-gnu*)
118       IS_LINUX_TARGET=true ;;
119     *-*-freebsd* | *-*-dragonfly*)
120       IS_FREEBSD_TARGET=true ;;
121   esac
122   IS_LIBPATH=false
123   if test "x${USE_LIBPATH}" = xyes; then
124     IS_LIBPATH=true
125   fi
126   IS_NATIVE=false
127   if test "x${NATIVE}" = xyes; then
128     IS_NATIVE=true
129   fi
131 fragment <<EOF
133 /* This is called before calling plugin 'all symbols read' hook.  */
135 static void
136 gld${EMULATION_NAME}_before_plugin_all_symbols_read (void)
138   ldelf_before_plugin_all_symbols_read ($IS_LIBPATH, $IS_NATIVE,
139                                         $IS_LINUX_TARGET,
140                                         $IS_FREEBSD_TARGET,
141                                         $ELFSIZE, "$prefix");
144 /* This is called after all the input files have been opened.  */
146 static void
147 gld${EMULATION_NAME}_after_open (void)
149   ldelf_after_open ($IS_LIBPATH, $IS_NATIVE,
150                     $IS_LINUX_TARGET, $IS_FREEBSD_TARGET, $ELFSIZE, "$prefix");
156 if test x"$LDEMUL_BEFORE_ALLOCATION" != xgld"$EMULATION_NAME"_before_allocation; then
157   if test x"${ELF_INTERPRETER_NAME}" = x; then
158     ELF_INTERPRETER_NAME=NULL
159   fi
160 fragment <<EOF
162 /* This is called after the sections have been attached to output
163    sections, but before any sizes or addresses have been set.  */
165 static void
166 gld${EMULATION_NAME}_before_allocation (void)
168   ldelf_before_allocation (audit, depaudit, ${ELF_INTERPRETER_NAME});
174 if test x"$LDEMUL_AFTER_ALLOCATION" != xgld"$EMULATION_NAME"_after_allocation; then
175 fragment <<EOF
177 static void
178 gld${EMULATION_NAME}_after_allocation (void)
180   int need_layout = bfd_elf_discard_info (link_info.output_bfd, &link_info);
182   if (need_layout < 0)
183     einfo (_("%X%P: .eh_frame/.stab edit: %E\n"));
184   else
185     ldelf_map_segments (need_layout);
190 if test x"$LDEMUL_GET_SCRIPT" != xgld"$EMULATION_NAME"_get_script; then
191 fragment <<EOF
193 static char *
194 gld${EMULATION_NAME}_get_script (int *isfile)
197 if test x"$COMPILE_IN" = xyes
198 then
199 # Scripts compiled in.
201 # sed commands to quote an ld script as a C string.
202 sc="-f ${srcdir}/emultempl/stringify.sed"
204 fragment <<EOF
206   *isfile = 0;
208   if (bfd_link_relocatable (&link_info) && config.build_constructors)
209     return
211 sed $sc ldscripts/${EMULATION_NAME}.xu                  >> e${EMULATION_NAME}.c
212 echo '  ; else if (bfd_link_relocatable (&link_info)) return' >> e${EMULATION_NAME}.c
213 sed $sc ldscripts/${EMULATION_NAME}.xr                  >> e${EMULATION_NAME}.c
214 echo '  ; else if (!config.text_read_only) return'      >> e${EMULATION_NAME}.c
215 sed $sc ldscripts/${EMULATION_NAME}.xbn                 >> e${EMULATION_NAME}.c
216 if cmp -s ldscripts/${EMULATION_NAME}.x ldscripts/${EMULATION_NAME}.xn; then : ; else
217 echo '  ; else if (!config.magic_demand_paged) return'  >> e${EMULATION_NAME}.c
218 sed $sc ldscripts/${EMULATION_NAME}.xn                  >> e${EMULATION_NAME}.c
220 if test -n "$GENERATE_PIE_SCRIPT" ; then
221 if test -n "$GENERATE_COMBRELOC_SCRIPT" ; then
222 echo '  ; else if (bfd_link_pie (&link_info)'           >> e${EMULATION_NAME}.c
223 echo '             && link_info.combreloc'              >> e${EMULATION_NAME}.c
224 echo '             && link_info.separate_code'          >> e${EMULATION_NAME}.c
225 echo '             && (link_info.flags & DF_BIND_NOW)) return' >> e${EMULATION_NAME}.c
226 sed $sc ldscripts/${EMULATION_NAME}.xdwe                >> e${EMULATION_NAME}.c
227 echo '  ; else if (bfd_link_pie (&link_info)'           >> e${EMULATION_NAME}.c
228 echo '             && link_info.combreloc'              >> e${EMULATION_NAME}.c
229 echo '             && link_info.relro'                  >> e${EMULATION_NAME}.c
230 echo '             && (link_info.flags & DF_BIND_NOW)) return' >> e${EMULATION_NAME}.c
231 sed $sc ldscripts/${EMULATION_NAME}.xdw                 >> e${EMULATION_NAME}.c
232 if test -n "$GENERATE_RELRO_SCRIPT" ; then
233 echo '  ; else if (bfd_link_pie (&link_info)'          >> e${EMULATION_NAME}.c
234 echo '             && link_info.combreloc'             >> e${EMULATION_NAME}.c
235 echo '             && link_info.separate_code'         >> e${EMULATION_NAME}.c
236 echo '             && link_info.relro) return'         >> e${EMULATION_NAME}.c
237 sed $sc ldscripts/${EMULATION_NAME}.xdceo              >> e${EMULATION_NAME}.c
239 echo '  ; else if (bfd_link_pie (&link_info)'           >> e${EMULATION_NAME}.c
240 echo '             && link_info.separate_code'          >> e${EMULATION_NAME}.c
241 echo '             && link_info.combreloc) return'      >> e${EMULATION_NAME}.c
242 sed $sc ldscripts/${EMULATION_NAME}.xdce                >> e${EMULATION_NAME}.c
243 if test -n "$GENERATE_RELRO_SCRIPT" ; then
244 echo '  ; else if (bfd_link_pie (&link_info)'          >> e${EMULATION_NAME}.c
245 echo '             && link_info.combreloc'             >> e${EMULATION_NAME}.c
246 echo '             && link_info.relro) return'         >> e${EMULATION_NAME}.c
247 sed $sc ldscripts/${EMULATION_NAME}.xdco               >> e${EMULATION_NAME}.c
249 echo '  ; else if (bfd_link_pie (&link_info)'           >> e${EMULATION_NAME}.c
250 echo '             && link_info.combreloc) return'      >> e${EMULATION_NAME}.c
251 sed $sc ldscripts/${EMULATION_NAME}.xdc                 >> e${EMULATION_NAME}.c
252 if test -n "$GENERATE_RELRO_SCRIPT" ; then
253 echo '  ; else if (bfd_link_pie (&link_info)'          >> e${EMULATION_NAME}.c
254 echo '             && link_info.separate_code'         >> e${EMULATION_NAME}.c
255 echo '             && link_info.relro) return'         >> e${EMULATION_NAME}.c
256 sed $sc ldscripts/${EMULATION_NAME}.xdeo               >> e${EMULATION_NAME}.c
259 echo '  ; else if (bfd_link_pie (&link_info)'           >> e${EMULATION_NAME}.c
260 echo '             && link_info.separate_code) return'  >> e${EMULATION_NAME}.c
261 sed $sc ldscripts/${EMULATION_NAME}.xde                 >> e${EMULATION_NAME}.c
262 if test -n "$GENERATE_RELRO_SCRIPT" ; then
263 echo '  ; else if (bfd_link_pie (&link_info)'          >> e${EMULATION_NAME}.c
264 echo '             && link_info.relro) return'         >> e${EMULATION_NAME}.c
265 sed $sc ldscripts/${EMULATION_NAME}.xdo                >> e${EMULATION_NAME}.c
267 echo '  ; else if (bfd_link_pie (&link_info)) return'   >> e${EMULATION_NAME}.c
268 sed $sc ldscripts/${EMULATION_NAME}.xd                  >> e${EMULATION_NAME}.c
270 if test -n "$GENERATE_SHLIB_SCRIPT" ; then
271 if test -n "$GENERATE_COMBRELOC_SCRIPT" ; then
272 echo '  ; else if (bfd_link_dll (&link_info)'           >> e${EMULATION_NAME}.c
273 echo '             && link_info.combreloc'              >> e${EMULATION_NAME}.c
274 echo '             && link_info.separate_code'          >> e${EMULATION_NAME}.c
275 echo '             && (link_info.flags & DF_BIND_NOW)) return' >> e${EMULATION_NAME}.c
276 sed $sc ldscripts/${EMULATION_NAME}.xswe                >> e${EMULATION_NAME}.c
277 echo '  ; else if (bfd_link_dll (&link_info)'           >> e${EMULATION_NAME}.c
278 echo '             && link_info.combreloc'              >> e${EMULATION_NAME}.c
279 echo '             && link_info.relro'                  >> e${EMULATION_NAME}.c
280 echo '             && (link_info.flags & DF_BIND_NOW)) return' >> e${EMULATION_NAME}.c
281 sed $sc ldscripts/${EMULATION_NAME}.xsw                 >> e${EMULATION_NAME}.c
282 if test -n "$GENERATE_RELRO_SCRIPT" ; then
283 echo '  ; else if (bfd_link_dll (&link_info)'          >> e${EMULATION_NAME}.c
284 echo '             && link_info.combreloc'             >> e${EMULATION_NAME}.c
285 echo '             && link_info.separate_code'         >> e${EMULATION_NAME}.c
286 echo '             && link_info.relro) return'         >> e${EMULATION_NAME}.c
287 sed $sc ldscripts/${EMULATION_NAME}.xsceo              >> e${EMULATION_NAME}.c
289 echo '  ; else if (bfd_link_dll (&link_info)'           >> e${EMULATION_NAME}.c
290 echo '             && link_info.combreloc'              >> e${EMULATION_NAME}.c
291 echo '             && link_info.separate_code) return'  >> e${EMULATION_NAME}.c
292 sed $sc ldscripts/${EMULATION_NAME}.xsce                        >> e${EMULATION_NAME}.c
293 if test -n "$GENERATE_RELRO_SCRIPT" ; then
294 echo '  ; else if (bfd_link_dll (&link_info)'          >> e${EMULATION_NAME}.c
295 echo '             && link_info.combreloc'             >> e${EMULATION_NAME}.c
296 echo '             && link_info.relro) return'         >> e${EMULATION_NAME}.c
297 sed $sc ldscripts/${EMULATION_NAME}.xsco               >> e${EMULATION_NAME}.c
299 echo '  ; else if (bfd_link_dll (&link_info)'           >> e${EMULATION_NAME}.c
300 echo '             && link_info.combreloc) return'      >> e${EMULATION_NAME}.c
301 sed $sc ldscripts/${EMULATION_NAME}.xsc                 >> e${EMULATION_NAME}.c
302 if test -n "$GENERATE_RELRO_SCRIPT" ; then
303 echo '  ; else if (bfd_link_dll (&link_info)'          >> e${EMULATION_NAME}.c
304 echo '             && link_info.separate_code'         >> e${EMULATION_NAME}.c
305 echo '             && link_info.relro) return'         >> e${EMULATION_NAME}.c
306 sed $sc ldscripts/${EMULATION_NAME}.xseo               >> e${EMULATION_NAME}.c
309 echo '  ; else if (bfd_link_dll (&link_info)'           >> e${EMULATION_NAME}.c
310 echo '             && link_info.separate_code) return'  >> e${EMULATION_NAME}.c
311 sed $sc ldscripts/${EMULATION_NAME}.xse                 >> e${EMULATION_NAME}.c
312 if test -n "$GENERATE_RELRO_SCRIPT" ; then
313 echo '  ; else if (bfd_link_dll (&link_info)'          >> e${EMULATION_NAME}.c
314 echo '             && link_info.relro) return'         >> e${EMULATION_NAME}.c
315 sed $sc ldscripts/${EMULATION_NAME}.xso               >> e${EMULATION_NAME}.c
317 echo '  ; else if (bfd_link_dll (&link_info)) return'   >> e${EMULATION_NAME}.c
318 sed $sc ldscripts/${EMULATION_NAME}.xs                  >> e${EMULATION_NAME}.c
320 if test -n "$GENERATE_COMBRELOC_SCRIPT" ; then
321 echo '  ; else if (link_info.combreloc'                 >> e${EMULATION_NAME}.c
322 echo '             && link_info.separate_code'          >> e${EMULATION_NAME}.c
323 echo '             && (link_info.flags & DF_BIND_NOW)) return' >> e${EMULATION_NAME}.c
324 sed $sc ldscripts/${EMULATION_NAME}.xwe                 >> e${EMULATION_NAME}.c
325 echo '  ; else if (link_info.combreloc'                 >> e${EMULATION_NAME}.c
326 echo '             && link_info.relro'                  >> e${EMULATION_NAME}.c
327 echo '             && (link_info.flags & DF_BIND_NOW)) return' >> e${EMULATION_NAME}.c
328 sed $sc ldscripts/${EMULATION_NAME}.xw                  >> e${EMULATION_NAME}.c
329 if test -n "$GENERATE_RELRO_SCRIPT" ; then
330 echo '  ; else if (link_info.combreloc'                 >> e${EMULATION_NAME}.c
331 echo '             && link_info.separate_code'          >> e${EMULATION_NAME}.c
332 echo '             && link_info.relro) return'          >> e${EMULATION_NAME}.c
333 sed $sc ldscripts/${EMULATION_NAME}.xceo                >> e${EMULATION_NAME}.c
335 echo '  ; else if (link_info.combreloc'                 >> e${EMULATION_NAME}.c
336 echo '             && link_info.separate_code) return'  >> e${EMULATION_NAME}.c
337 sed $sc ldscripts/${EMULATION_NAME}.xce                 >> e${EMULATION_NAME}.c
338 if test -n "$GENERATE_RELRO_SCRIPT" ; then
339 echo '  ; else if (link_info.combreloc'                 >> e${EMULATION_NAME}.c
340 echo '             && link_info.relro) return'          >> e${EMULATION_NAME}.c
341 sed $sc ldscripts/${EMULATION_NAME}.xco                 >> e${EMULATION_NAME}.c
343 echo '  ; else if (link_info.combreloc) return'         >> e${EMULATION_NAME}.c
344 sed $sc ldscripts/${EMULATION_NAME}.xc                  >> e${EMULATION_NAME}.c
346 if test -n "$GENERATE_RELRO_SCRIPT" ; then
347 echo '  ; else if (link_info.separate_code'             >> e${EMULATION_NAME}.c
348 echo '             && link_info.relro) return'          >> e${EMULATION_NAME}.c
349 sed $sc ldscripts/${EMULATION_NAME}.xeo                 >> e${EMULATION_NAME}.c
351 echo '  ; else if (link_info.separate_code) return'     >> e${EMULATION_NAME}.c
352 sed $sc ldscripts/${EMULATION_NAME}.xe                  >> e${EMULATION_NAME}.c
353 if test -n "$GENERATE_RELRO_SCRIPT" ; then
354 echo '  ; else if (link_info.relro) return'             >> e${EMULATION_NAME}.c
355 sed $sc ldscripts/${EMULATION_NAME}.xo                  >> e${EMULATION_NAME}.c
357 echo '  ; else return'                                  >> e${EMULATION_NAME}.c
358 sed $sc ldscripts/${EMULATION_NAME}.x                   >> e${EMULATION_NAME}.c
359 echo '; }'                                              >> e${EMULATION_NAME}.c
361 else
362 # Scripts read from the filesystem.
364 fragment <<EOF
366   *isfile = 1;
368   if (bfd_link_relocatable (&link_info) && config.build_constructors)
369     return "ldscripts/${EMULATION_NAME}.xu";
370   else if (bfd_link_relocatable (&link_info))
371     return "ldscripts/${EMULATION_NAME}.xr";
372   else if (!config.text_read_only)
373     return "ldscripts/${EMULATION_NAME}.xbn";
375 if cmp -s ldscripts/${EMULATION_NAME}.x ldscripts/${EMULATION_NAME}.xn; then :
376 else
377 fragment <<EOF
378   else if (!config.magic_demand_paged)
379     return "ldscripts/${EMULATION_NAME}.xn";
382 if test -n "$GENERATE_PIE_SCRIPT" ; then
383 if test -n "$GENERATE_COMBRELOC_SCRIPT" ; then
384 fragment <<EOF
385   else if (bfd_link_pie (&link_info)
386            && link_info.combreloc
387            && link_info.relro
388            && (link_info.flags & DF_BIND_NOW))
389     {
390       if (link_info.separate_code)
391         return "ldscripts/${EMULATION_NAME}.xdwe";
392       else
393         return "ldscripts/${EMULATION_NAME}.xdw";
394     }
396 if test -n "$GENERATE_RELRO_SCRIPT" ; then
397 fragment <<EOF
398   else if (bfd_link_pie (&link_info)
399            && link_info.combreloc
400            && link_info.relro)
401     {
402       if (link_info.separate_code)
403         return "ldscripts/${EMULATION_NAME}.xdceo";
404       else
405         return "ldscripts/${EMULATION_NAME}.xdco";
406     }
409 fragment <<EOF
410   else if (bfd_link_pie (&link_info)
411            && link_info.combreloc)
412     {
413       if (link_info.separate_code)
414         return "ldscripts/${EMULATION_NAME}.xdce";
415       else
416         return "ldscripts/${EMULATION_NAME}.xdc";
417     }
420 if test -n "$GENERATE_RELRO_SCRIPT" ; then
421 fragment <<EOF
422   else if (bfd_link_pie (&link_info)
423            && link_info.relro)
424     {
425       if (link_info.separate_code)
426         return "ldscripts/${EMULATION_NAME}.xdeo";
427       else
428         return "ldscripts/${EMULATION_NAME}.xdo";
429     }
432 fragment <<EOF
433   else if (bfd_link_pie (&link_info))
434     {
435       if (link_info.separate_code)
436         return "ldscripts/${EMULATION_NAME}.xde";
437       else
438         return "ldscripts/${EMULATION_NAME}.xd";
439     }
442 if test -n "$GENERATE_SHLIB_SCRIPT" ; then
443 if test -n "$GENERATE_COMBRELOC_SCRIPT" ; then
444 fragment <<EOF
445   else if (bfd_link_dll (&link_info) && link_info.combreloc
446            && link_info.relro && (link_info.flags & DF_BIND_NOW))
447     {
448       if (link_info.separate_code)
449         return "ldscripts/${EMULATION_NAME}.xswe";
450       else
451         return "ldscripts/${EMULATION_NAME}.xsw";
452     }
454 if test -n "$GENERATE_RELRO_SCRIPT" ; then
455 fragment <<EOF
456   else if (bfd_link_dll (&link_info)
457            && link_info.combreloc
458            && link_info.relro)
459     {
460       if (link_info.separate_code)
461         return "ldscripts/${EMULATION_NAME}.xsceo";
462       else
463         return "ldscripts/${EMULATION_NAME}.xsco";
464     }
467 fragment <<EOF
468   else if (bfd_link_dll (&link_info) && link_info.combreloc)
469     {
470       if (link_info.separate_code)
471         return "ldscripts/${EMULATION_NAME}.xsce";
472       else
473         return "ldscripts/${EMULATION_NAME}.xsc";
474     }
477 if test -n "$GENERATE_RELRO_SCRIPT" ; then
478 fragment <<EOF
479   else if (bfd_link_dll (&link_info)
480            && link_info.relro)
481     {
482       if (link_info.separate_code)
483         return "ldscripts/${EMULATION_NAME}.xseo";
484       else
485         return "ldscripts/${EMULATION_NAME}.xso";
486     }
489 fragment <<EOF
490   else if (bfd_link_dll (&link_info))
491     {
492       if (link_info.separate_code)
493         return "ldscripts/${EMULATION_NAME}.xse";
494       else
495         return "ldscripts/${EMULATION_NAME}.xs";
496     }
499 if test -n "$GENERATE_COMBRELOC_SCRIPT" ; then
500 fragment <<EOF
501   else if (link_info.combreloc && link_info.relro
502            && (link_info.flags & DF_BIND_NOW))
503     {
504       if (link_info.separate_code)
505         return "ldscripts/${EMULATION_NAME}.xwe";
506       else
507         return "ldscripts/${EMULATION_NAME}.xw";
508     }
510 if test -n "$GENERATE_RELRO_SCRIPT" ; then
511 fragment <<EOF
512   else if (link_info.combreloc
513            && link_info.relro)
514     {
515       if (link_info.separate_code)
516         return "ldscripts/${EMULATION_NAME}.xceo";
517       else
518         return "ldscripts/${EMULATION_NAME}.xco";
519     }
522 fragment <<EOF
523   else if (link_info.combreloc)
524     {
525       if (link_info.separate_code)
526         return "ldscripts/${EMULATION_NAME}.xce";
527       else
528         return "ldscripts/${EMULATION_NAME}.xc";
529     }
532 if test -n "$GENERATE_RELRO_SCRIPT" ; then
533 fragment <<EOF
534   else if (link_info.relro)
535     {
536       if (link_info.separate_code)
537         return "ldscripts/${EMULATION_NAME}.xeo";
538       else
539         return "ldscripts/${EMULATION_NAME}.xo";
540     }
543 fragment <<EOF
544   else
545     {
546       if (link_info.separate_code)
547         return "ldscripts/${EMULATION_NAME}.xe";
548       else
549         return "ldscripts/${EMULATION_NAME}.x";
550     }
557 if test -n "$PARSE_AND_LIST_PROLOGUE" ; then
558 fragment <<EOF
559  $PARSE_AND_LIST_PROLOGUE
563 fragment <<EOF
565 enum elf_options
567   OPTION_DISABLE_NEW_DTAGS = 400,
568   OPTION_ENABLE_NEW_DTAGS,
569   OPTION_GROUP,
570   OPTION_EH_FRAME_HDR,
571   OPTION_NO_EH_FRAME_HDR,
572   OPTION_EXCLUDE_LIBS,
573   OPTION_HASH_STYLE,
574   OPTION_BUILD_ID,
575   OPTION_PACKAGE_METADATA,
576   OPTION_AUDIT,
577   OPTION_COMPRESS_DEBUG
580 static void
581 gld${EMULATION_NAME}_add_options
582   (int ns, char **shortopts, int nl, struct option **longopts,
583    int nrl ATTRIBUTE_UNUSED, struct option **really_longopts ATTRIBUTE_UNUSED)
586 if test x"$GENERATE_SHLIB_SCRIPT" = xyes; then
587 fragment <<EOF
588   static const char xtra_short[] = "${PARSE_AND_LIST_SHORTOPTS}z:P:";
590 else
591 fragment <<EOF
592   static const char xtra_short[] = "${PARSE_AND_LIST_SHORTOPTS}z:";
595 fragment <<EOF
596   static const struct option xtra_long[] = {
598 if test x"$GENERATE_SHLIB_SCRIPT" = xyes; then
599 fragment <<EOF
600     {"audit", required_argument, NULL, OPTION_AUDIT},
601     {"Bgroup", no_argument, NULL, OPTION_GROUP},
604 fragment <<EOF
605     {"build-id", optional_argument, NULL, OPTION_BUILD_ID},
606     {"package-metadata", optional_argument, NULL, OPTION_PACKAGE_METADATA},
607     {"compress-debug-sections", required_argument, NULL, OPTION_COMPRESS_DEBUG},
609 if test x"$GENERATE_SHLIB_SCRIPT" = xyes; then
610 fragment <<EOF
611     {"depaudit", required_argument, NULL, 'P'},
612     {"disable-new-dtags", no_argument, NULL, OPTION_DISABLE_NEW_DTAGS},
613     {"enable-new-dtags", no_argument, NULL, OPTION_ENABLE_NEW_DTAGS},
614     {"eh-frame-hdr", no_argument, NULL, OPTION_EH_FRAME_HDR},
615     {"no-eh-frame-hdr", no_argument, NULL, OPTION_NO_EH_FRAME_HDR},
616     {"exclude-libs", required_argument, NULL, OPTION_EXCLUDE_LIBS},
617     {"hash-style", required_argument, NULL, OPTION_HASH_STYLE},
620 if test -n "$PARSE_AND_LIST_LONGOPTS" ; then
621 fragment <<EOF
622     $PARSE_AND_LIST_LONGOPTS
625 fragment <<EOF
626     {NULL, no_argument, NULL, 0}
627   };
629   *shortopts = (char *) xrealloc (*shortopts, ns + sizeof (xtra_short));
630   memcpy (*shortopts + ns, &xtra_short, sizeof (xtra_short));
631   *longopts = (struct option *)
632     xrealloc (*longopts, nl * sizeof (struct option) + sizeof (xtra_long));
633   memcpy (*longopts + nl, &xtra_long, sizeof (xtra_long));
636 #define DEFAULT_BUILD_ID_STYLE  "sha1"
638 static bool
639 gld${EMULATION_NAME}_handle_option (int optc)
641   switch (optc)
642     {
643     default:
644       return false;
646     case OPTION_BUILD_ID:
647       free ((char *) ldelf_emit_note_gnu_build_id);
648       ldelf_emit_note_gnu_build_id = NULL;
649       if (optarg == NULL)
650         optarg = DEFAULT_BUILD_ID_STYLE;
651       if (strcmp (optarg, "none"))
652         ldelf_emit_note_gnu_build_id = xstrdup (optarg);
653       break;
655     case OPTION_PACKAGE_METADATA:
656       free ((char *) ldelf_emit_note_fdo_package_metadata);
657       ldelf_emit_note_fdo_package_metadata = NULL;
658       if (optarg != NULL && strlen(optarg) > 0)
659         ldelf_emit_note_fdo_package_metadata = xstrdup (optarg);
660       break;
662     case OPTION_COMPRESS_DEBUG:
663       config.compress_debug = bfd_get_compression_algorithm (optarg);
664       if (strcasecmp (optarg, "zstd") == 0)
665         {
666 #ifndef HAVE_ZSTD
667           if (config.compress_debug == COMPRESS_DEBUG_ZSTD)
668             einfo (_ ("%F%P: --compress-debug-sections=zstd: ld is not built "
669                   "with zstd support\n"));
670 #endif
671         }
672       if (config.compress_debug == COMPRESS_UNKNOWN)
673         einfo (_("%F%P: invalid --compress-debug-sections option: \`%s'\n"),
674                optarg);
675       break;
678 if test x"$GENERATE_SHLIB_SCRIPT" = xyes; then
679 fragment <<EOF
680     case OPTION_AUDIT:
681         ldelf_append_to_separated_string (&audit, optarg);
682         break;
684     case 'P':
685         ldelf_append_to_separated_string (&depaudit, optarg);
686         break;
688     case OPTION_DISABLE_NEW_DTAGS:
689       link_info.new_dtags = false;
690       break;
692     case OPTION_ENABLE_NEW_DTAGS:
693       link_info.new_dtags = true;
694       break;
696     case OPTION_EH_FRAME_HDR:
697       link_info.eh_frame_hdr_type = DWARF2_EH_HDR;
698       break;
700     case OPTION_NO_EH_FRAME_HDR:
701       link_info.eh_frame_hdr_type = 0;
702       break;
704     case OPTION_GROUP:
705       link_info.flags_1 |= (bfd_vma) DF_1_GROUP;
706       /* Groups must be self-contained.  */
707       link_info.unresolved_syms_in_objects = RM_DIAGNOSE;
708       link_info.unresolved_syms_in_shared_libs = RM_DIAGNOSE;
709       break;
711     case OPTION_EXCLUDE_LIBS:
712       add_excluded_libs (optarg);
713       break;
715     case OPTION_HASH_STYLE:
716       link_info.emit_hash = false;
717       link_info.emit_gnu_hash = false;
718       if (strcmp (optarg, "sysv") == 0)
719         link_info.emit_hash = true;
720       else if (strcmp (optarg, "gnu") == 0)
721         link_info.emit_gnu_hash = true;
722       else if (strcmp (optarg, "both") == 0)
723         {
724           link_info.emit_hash = true;
725           link_info.emit_gnu_hash = true;
726         }
727       else
728         einfo (_("%F%P: invalid hash style \`%s'\n"), optarg);
729       break;
733 fragment <<EOF
734     case 'z':
735       if (strcmp (optarg, "defs") == 0)
736         link_info.unresolved_syms_in_objects = RM_DIAGNOSE;
737       else if (strcmp (optarg, "undefs") == 0)
738         link_info.unresolved_syms_in_objects = RM_IGNORE;
739       else if (strcmp (optarg, "muldefs") == 0)
740         link_info.allow_multiple_definition = true;
741       else if (startswith (optarg, "max-page-size="))
742         {
743           char *end;
745           link_info.maxpagesize = strtoul (optarg + 14, &end, 0);
746           if (*end
747               || (link_info.maxpagesize & (link_info.maxpagesize - 1)) != 0)
748             einfo (_("%F%P: invalid maximum page size \`%s'\n"),
749                    optarg + 14);
750           link_info.maxpagesize_is_set = true;
751         }
752       else if (startswith (optarg, "common-page-size="))
753         {
754           char *end;
755           link_info.commonpagesize = strtoul (optarg + 17, &end, 0);
756           if (*end
757               || (link_info.commonpagesize & (link_info.commonpagesize - 1)) != 0)
758             einfo (_("%F%P: invalid common page size \`%s'\n"),
759                    optarg + 17);
760           link_info.commonpagesize_is_set = true;
761         }
762       else if (startswith (optarg, "stack-size="))
763         {
764           char *end;
765           link_info.stacksize = strtoul (optarg + 11, &end, 0);
766           if (*end || link_info.stacksize < 0)
767             einfo (_("%F%P: invalid stack size \`%s'\n"), optarg + 11);
768           if (!link_info.stacksize)
769             /* Use -1 for explicit no-stack, because zero means
770                'default'.   */
771             link_info.stacksize = -1;
772         }
773       else if (strcmp (optarg, "execstack") == 0)
774         {
775           link_info.execstack = true;
776           link_info.noexecstack = false;
777         }
778       else if (strcmp (optarg, "noexecstack") == 0)
779         {
780           link_info.noexecstack = true;
781           link_info.execstack = false;
782         }
783       else if (strcmp (optarg, "unique-symbol") == 0)
784         link_info.unique_symbol = true;
785       else if (strcmp (optarg, "nounique-symbol") == 0)
786         link_info.unique_symbol = false;
787       else if (strcmp (optarg, "globalaudit") == 0)
788         {
789           link_info.flags_1 |= DF_1_GLOBAUDIT;
790         }
791       else if (startswith (optarg, "start-stop-gc"))
792         link_info.start_stop_gc = true;
793       else if (startswith (optarg, "nostart-stop-gc"))
794         link_info.start_stop_gc = false;
795       else if (startswith (optarg, "start-stop-visibility="))
796         {
797           if (strcmp (optarg, "start-stop-visibility=default") == 0)
798             link_info.start_stop_visibility = STV_DEFAULT;
799           else if (strcmp (optarg, "start-stop-visibility=internal") == 0)
800             link_info.start_stop_visibility = STV_INTERNAL;
801           else if (strcmp (optarg, "start-stop-visibility=hidden") == 0)
802             link_info.start_stop_visibility = STV_HIDDEN;
803           else if (strcmp (optarg, "start-stop-visibility=protected") == 0)
804             link_info.start_stop_visibility = STV_PROTECTED;
805           else
806             einfo (_("%F%P: invalid visibility in \`-z %s'; "
807                      "must be default, internal, hidden, or protected"),
808                    optarg);
809         }
810       else if (strcmp (optarg, "sectionheader") == 0)
811         config.no_section_header = false;
812       else if (strcmp (optarg, "nosectionheader") == 0)
813         config.no_section_header = true;
816 if test x"$GENERATE_SHLIB_SCRIPT" = xyes; then
817 fragment <<EOF
818       else if (strcmp (optarg, "global") == 0)
819         link_info.flags_1 |= (bfd_vma) DF_1_GLOBAL;
820       else if (strcmp (optarg, "initfirst") == 0)
821         link_info.flags_1 |= (bfd_vma) DF_1_INITFIRST;
822       else if (strcmp (optarg, "interpose") == 0)
823         link_info.flags_1 |= (bfd_vma) DF_1_INTERPOSE;
824       else if (strcmp (optarg, "loadfltr") == 0)
825         link_info.flags_1 |= (bfd_vma) DF_1_LOADFLTR;
826       else if (strcmp (optarg, "nodefaultlib") == 0)
827         link_info.flags_1 |= (bfd_vma) DF_1_NODEFLIB;
828       else if (strcmp (optarg, "nodelete") == 0)
829         link_info.flags_1 |= (bfd_vma) DF_1_NODELETE;
830       else if (strcmp (optarg, "nodlopen") == 0)
831         link_info.flags_1 |= (bfd_vma) DF_1_NOOPEN;
832       else if (strcmp (optarg, "nodump") == 0)
833         link_info.flags_1 |= (bfd_vma) DF_1_NODUMP;
834       else if (strcmp (optarg, "now") == 0)
835         {
836           link_info.flags |= (bfd_vma) DF_BIND_NOW;
837           link_info.flags_1 |= (bfd_vma) DF_1_NOW;
838         }
839       else if (strcmp (optarg, "lazy") == 0)
840         {
841           link_info.flags &= ~(bfd_vma) DF_BIND_NOW;
842           link_info.flags_1 &= ~(bfd_vma) DF_1_NOW;
843         }
844       else if (strcmp (optarg, "origin") == 0)
845         {
846           link_info.flags |= (bfd_vma) DF_ORIGIN;
847           link_info.flags_1 |= (bfd_vma) DF_1_ORIGIN;
848         }
849       else if (strcmp (optarg, "unique") == 0)
850         link_info.gnu_flags_1 |= (bfd_vma) DF_GNU_1_UNIQUE;
851       else if (strcmp (optarg, "nounique") == 0)
852         link_info.gnu_flags_1 &= ~(bfd_vma) DF_GNU_1_UNIQUE;
853       else if (strcmp (optarg, "combreloc") == 0)
854         link_info.combreloc = true;
855       else if (strcmp (optarg, "nocombreloc") == 0)
856         link_info.combreloc = false;
857       else if (strcmp (optarg, "nocopyreloc") == 0)
858         link_info.nocopyreloc = true;
860 if test -n "$COMMONPAGESIZE"; then
861 fragment <<EOF
862       else if (strcmp (optarg, "relro") == 0)
863         link_info.relro = true;
864       else if (strcmp (optarg, "norelro") == 0)
865         link_info.relro = false;
868 fragment <<EOF
869       else if (strcmp (optarg, "separate-code") == 0)
870         link_info.separate_code = true;
871       else if (strcmp (optarg, "noseparate-code") == 0)
872         link_info.separate_code = false;
873       else if (strcmp (optarg, "common") == 0)
874         link_info.elf_stt_common = elf_stt_common;
875       else if (strcmp (optarg, "nocommon") == 0)
876         link_info.elf_stt_common = no_elf_stt_common;
877       else if (strcmp (optarg, "text") == 0)
878         link_info.textrel_check = textrel_check_error;
879       else if (strcmp (optarg, "notext") == 0)
880         link_info.textrel_check = textrel_check_none;
881       else if (strcmp (optarg, "textoff") == 0)
882         link_info.textrel_check = textrel_check_none;
886 if test -n "$PARSE_AND_LIST_ARGS_CASE_Z" ; then
887 fragment <<EOF
888  $PARSE_AND_LIST_ARGS_CASE_Z
892 fragment <<EOF
893       else
894         einfo (_("%P: warning: -z %s ignored\n"), optarg);
895       break;
898 if test -n "$PARSE_AND_LIST_ARGS_CASES" ; then
899 fragment <<EOF
900  $PARSE_AND_LIST_ARGS_CASES
904 fragment <<EOF
905     }
907   return true;
912 if test x"$LDEMUL_LIST_OPTIONS" != xgld"$EMULATION_NAME"_list_options; then
913 gld_list_options="gld${EMULATION_NAME}_list_options"
914 if test -n "$PARSE_AND_LIST_OPTIONS"; then
915 fragment <<EOF
917 static void
918 gld${EMULATION_NAME}_list_options (FILE * file)
922 if test -n "$PARSE_AND_LIST_OPTIONS" ; then
923 fragment <<EOF
924  $PARSE_AND_LIST_OPTIONS
928 fragment <<EOF
931 else
932   gld_list_options="NULL"
935 if test -n "$PARSE_AND_LIST_EPILOGUE" ; then
936 fragment <<EOF
937  $PARSE_AND_LIST_EPILOGUE
942 LDEMUL_AFTER_PARSE=${LDEMUL_AFTER_PARSE-ldelf_after_parse}
943 LDEMUL_BEFORE_PLUGIN_ALL_SYMBOLS_READ=${LDEMUL_BEFORE_PLUGIN_ALL_SYMBOLS_READ-gld${EMULATION_NAME}_before_plugin_all_symbols_read}
944 LDEMUL_AFTER_OPEN=${LDEMUL_AFTER_OPEN-gld${EMULATION_NAME}_after_open}
945 LDEMUL_BEFORE_PLACE_ORPHANS=${LDEMUL_BEFORE_PLACE_ORPHANS-ldelf_before_place_orphans}
946 LDEMUL_AFTER_ALLOCATION=${LDEMUL_AFTER_ALLOCATION-gld${EMULATION_NAME}_after_allocation}
947 LDEMUL_SET_OUTPUT_ARCH=${LDEMUL_SET_OUTPUT_ARCH-ldelf_set_output_arch}
948 LDEMUL_BEFORE_ALLOCATION=${LDEMUL_BEFORE_ALLOCATION-gld${EMULATION_NAME}_before_allocation}
949 LDEMUL_OPEN_DYNAMIC_ARCHIVE=${LDEMUL_OPEN_DYNAMIC_ARCHIVE-ldelf_open_dynamic_archive}
950 LDEMUL_PLACE_ORPHAN=${LDEMUL_PLACE_ORPHAN-ldelf_place_orphan}
951 LDEMUL_ADD_OPTIONS=gld${EMULATION_NAME}_add_options
952 LDEMUL_HANDLE_OPTION=gld${EMULATION_NAME}_handle_option
953 LDEMUL_LIST_OPTIONS=${LDEMUL_LIST_OPTIONS-${gld_list_options}}
954 LDEMUL_RECOGNIZED_FILE=${LDEMUL_RECOGNIZED_FILE-ldelf_load_symbols}
956 source_em ${srcdir}/emultempl/emulation.em