elf32.em revision 104847
1# $FreeBSD: head/contrib/binutils/ld/emultempl/elf32.em 104847 2002-10-11 06:15:47Z obrien $
2
3
4# This shell script emits a C file. -*- C -*-
5# It does some substitutions.
6# This file is now misnamed, because it supports both 32 bit and 64 bit
7# ELF emulations.
8test -z "${ELFSIZE}" && ELFSIZE=32
9if [ -z "$MACHINE" ]; then
10  OUTPUT_ARCH=${ARCH}
11else
12  OUTPUT_ARCH=${ARCH}:${MACHINE}
13fi
14cat >e${EMULATION_NAME}.c <<EOF
15/* This file is is generated by a shell script.  DO NOT EDIT! */
16
17/* ${ELFSIZE} bit ELF emulation code for ${EMULATION_NAME}
18   Copyright 1991, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
19   2002 Free Software Foundation, Inc.
20   Written by Steve Chamberlain <sac@cygnus.com>
21   ELF support by Ian Lance Taylor <ian@cygnus.com>
22
23This file is part of GLD, the Gnu Linker.
24
25This program is free software; you can redistribute it and/or modify
26it under the terms of the GNU General Public License as published by
27the Free Software Foundation; either version 2 of the License, or
28(at your option) any later version.
29
30This program is distributed in the hope that it will be useful,
31but WITHOUT ANY WARRANTY; without even the implied warranty of
32MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33GNU General Public License for more details.
34
35You should have received a copy of the GNU General Public License
36along with this program; if not, write to the Free Software
37Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
38
39#define TARGET_IS_${EMULATION_NAME}
40
41#include "bfd.h"
42#include "sysdep.h"
43#include "libiberty.h"
44#include "safe-ctype.h"
45
46#include "bfdlink.h"
47
48#include "ld.h"
49#include "ldmain.h"
50#include "ldmisc.h"
51#include "ldexp.h"
52#include "ldlang.h"
53#include "ldfile.h"
54#include "ldemul.h"
55#include "ldgram.h"
56#include "elf/common.h"
57
58static void gld${EMULATION_NAME}_before_parse
59  PARAMS ((void));
60static void gld${EMULATION_NAME}_vercheck
61  PARAMS ((lang_input_statement_type *));
62static void gld${EMULATION_NAME}_stat_needed
63  PARAMS ((lang_input_statement_type *));
64static boolean gld${EMULATION_NAME}_try_needed
65  PARAMS ((const char *, int));
66static boolean gld${EMULATION_NAME}_search_needed
67  PARAMS ((const char *, const char *, int));
68static void gld${EMULATION_NAME}_check_needed
69  PARAMS ((lang_input_statement_type *));
70static void gld${EMULATION_NAME}_after_open
71  PARAMS ((void));
72static void gld${EMULATION_NAME}_find_exp_assignment
73  PARAMS ((etree_type *));
74static void gld${EMULATION_NAME}_find_statement_assignment
75  PARAMS ((lang_statement_union_type *));
76static void gld${EMULATION_NAME}_before_allocation
77  PARAMS ((void));
78static boolean gld${EMULATION_NAME}_open_dynamic_archive
79  PARAMS ((const char *, search_dirs_type *, lang_input_statement_type *));
80static lang_output_section_statement_type *output_rel_find
81  PARAMS ((void));
82static asection *output_prev_sec_find
83  PARAMS ((lang_output_section_statement_type *));
84static boolean gld${EMULATION_NAME}_place_orphan
85  PARAMS ((lang_input_statement_type *, asection *));
86static void gld${EMULATION_NAME}_finish
87  PARAMS ((void));
88static char *gld${EMULATION_NAME}_get_script
89  PARAMS ((int *isfile));
90
91EOF
92
93# Import any needed special functions and/or overrides.
94#
95if test -n "$EXTRA_EM_FILE" ; then
96. ${srcdir}/emultempl/${EXTRA_EM_FILE}.em
97fi
98
99# Functions in this file can be overridden by setting the LDEMUL_* shell
100# variables.  If the name of the overriding function is the same as is
101# defined in this file, then don't output this file's version.
102# If a different overriding name is given then output the standard function
103# as presumably it is called from the overriding function.
104#
105if test x"$LDEMUL_BEFORE_PARSE" != xgld"$EMULATION_NAME"_before_parse; then
106cat >>e${EMULATION_NAME}.c <<EOF
107
108static void
109gld${EMULATION_NAME}_before_parse ()
110{
111  const bfd_arch_info_type *arch = bfd_scan_arch ("${OUTPUT_ARCH}");
112  if (arch)
113    {
114      ldfile_output_architecture = arch->arch;
115      ldfile_output_machine = arch->mach;
116      ldfile_output_machine_name = arch->printable_name;
117    }
118  else
119    ldfile_output_architecture = bfd_arch_`echo ${ARCH} | sed -e 's/:.*//'`;
120  config.dynamic_link = ${DYNAMIC_LINK-true};
121  config.has_shared = `if test -n "$GENERATE_SHLIB_SCRIPT" ; then echo true ; else echo false ; fi`;
122}
123
124EOF
125fi
126
127cat >>e${EMULATION_NAME}.c <<EOF
128
129/* These variables are required to pass information back and forth
130   between after_open and check_needed and stat_needed and vercheck.  */
131
132static struct bfd_link_needed_list *global_needed;
133static struct stat global_stat;
134static boolean global_found;
135static struct bfd_link_needed_list *global_vercheck_needed;
136static boolean global_vercheck_failed;
137
138
139/* On Linux, it's possible to have different versions of the same
140   shared library linked against different versions of libc.  The
141   dynamic linker somehow tags which libc version to use in
142   /etc/ld.so.cache, and, based on the libc that it sees in the
143   executable, chooses which version of the shared library to use.
144
145   We try to do a similar check here by checking whether this shared
146   library needs any other shared libraries which may conflict with
147   libraries we have already included in the link.  If it does, we
148   skip it, and try to find another shared library farther on down the
149   link path.
150
151   This is called via lang_for_each_input_file.
152   GLOBAL_VERCHECK_NEEDED is the list of objects needed by the object
153   which we are checking.  This sets GLOBAL_VERCHECK_FAILED if we find
154   a conflicting version.  */
155
156static void
157gld${EMULATION_NAME}_vercheck (s)
158     lang_input_statement_type *s;
159{
160  const char *soname;
161  struct bfd_link_needed_list *l;
162
163  if (global_vercheck_failed)
164    return;
165  if (s->the_bfd == NULL
166      || (bfd_get_file_flags (s->the_bfd) & DYNAMIC) == 0)
167    return;
168
169  soname = bfd_elf_get_dt_soname (s->the_bfd);
170  if (soname == NULL)
171    soname = lbasename (bfd_get_filename (s->the_bfd));
172
173  for (l = global_vercheck_needed; l != NULL; l = l->next)
174    {
175      const char *suffix;
176
177      if (strcmp (soname, l->name) == 0)
178	{
179	  /* Probably can't happen, but it's an easy check.  */
180	  continue;
181	}
182
183      if (strchr (l->name, '/') != NULL)
184	continue;
185
186      suffix = strstr (l->name, ".so.");
187      if (suffix == NULL)
188	continue;
189
190      suffix += sizeof ".so." - 1;
191
192      if (strncmp (soname, l->name, suffix - l->name) == 0)
193	{
194	  /* Here we know that S is a dynamic object FOO.SO.VER1, and
195             the object we are considering needs a dynamic object
196             FOO.SO.VER2, and VER1 and VER2 are different.  This
197             appears to be a version mismatch, so we tell the caller
198             to try a different version of this library.  */
199	  global_vercheck_failed = true;
200	  return;
201	}
202    }
203}
204
205
206/* See if an input file matches a DT_NEEDED entry by running stat on
207   the file.  */
208
209static void
210gld${EMULATION_NAME}_stat_needed (s)
211     lang_input_statement_type *s;
212{
213  struct stat st;
214  const char *suffix;
215  const char *soname;
216
217  if (global_found)
218    return;
219  if (s->the_bfd == NULL)
220    return;
221
222  if (bfd_stat (s->the_bfd, &st) != 0)
223    {
224      einfo ("%P:%B: bfd_stat failed: %E\n", s->the_bfd);
225      return;
226    }
227
228  if (st.st_dev == global_stat.st_dev
229      && st.st_ino == global_stat.st_ino)
230    {
231      global_found = true;
232      return;
233    }
234
235  /* We issue a warning if it looks like we are including two
236     different versions of the same shared library.  For example,
237     there may be a problem if -lc picks up libc.so.6 but some other
238     shared library has a DT_NEEDED entry of libc.so.5.  This is a
239     heuristic test, and it will only work if the name looks like
240     NAME.so.VERSION.  FIXME: Depending on file names is error-prone.
241     If we really want to issue warnings about mixing version numbers
242     of shared libraries, we need to find a better way.  */
243
244  if (strchr (global_needed->name, '/') != NULL)
245    return;
246  suffix = strstr (global_needed->name, ".so.");
247  if (suffix == NULL)
248    return;
249  suffix += sizeof ".so." - 1;
250
251  soname = bfd_elf_get_dt_soname (s->the_bfd);
252  if (soname == NULL)
253    soname = lbasename (s->filename);
254
255  if (strncmp (soname, global_needed->name, suffix - global_needed->name) == 0)
256    einfo ("%P: warning: %s, needed by %B, may conflict with %s\n",
257	   global_needed->name, global_needed->by, soname);
258}
259
260
261/* This function is called for each possible name for a dynamic object
262   named by a DT_NEEDED entry.  The FORCE parameter indicates whether
263   to skip the check for a conflicting version.  */
264
265static boolean
266gld${EMULATION_NAME}_try_needed (name, force)
267     const char *name;
268     int force;
269{
270  bfd *abfd;
271  const char *soname;
272
273  abfd = bfd_openr (name, bfd_get_target (output_bfd));
274  if (abfd == NULL)
275    return false;
276  if (! bfd_check_format (abfd, bfd_object))
277    {
278      bfd_close (abfd);
279      return false;
280    }
281  if ((bfd_get_file_flags (abfd) & DYNAMIC) == 0)
282    {
283      bfd_close (abfd);
284      return false;
285    }
286
287  /* For DT_NEEDED, they have to match.  */
288  if (abfd->xvec != output_bfd->xvec)
289    {
290      bfd_close (abfd);
291      return false;
292    }
293
294  /* Check whether this object would include any conflicting library
295     versions.  If FORCE is set, then we skip this check; we use this
296     the second time around, if we couldn't find any compatible
297     instance of the shared library.  */
298
299  if (! force)
300    {
301      struct bfd_link_needed_list *needed;
302
303      if (! bfd_elf_get_bfd_needed_list (abfd, &needed))
304	einfo ("%F%P:%B: bfd_elf_get_bfd_needed_list failed: %E\n", abfd);
305
306      if (needed != NULL)
307	{
308	  global_vercheck_needed = needed;
309	  global_vercheck_failed = false;
310	  lang_for_each_input_file (gld${EMULATION_NAME}_vercheck);
311	  if (global_vercheck_failed)
312	    {
313	      bfd_close (abfd);
314	      /* Return false to force the caller to move on to try
315                 another file on the search path.  */
316	      return false;
317	    }
318
319	  /* But wait!  It gets much worse.  On Linux, if a shared
320             library does not use libc at all, we are supposed to skip
321             it the first time around in case we encounter a shared
322             library later on with the same name which does use the
323             version of libc that we want.  This is much too horrible
324             to use on any system other than Linux.  */
325
326EOF
327case ${target} in
328  *-*-linux-gnu*)
329    cat >>e${EMULATION_NAME}.c <<EOF
330	  {
331	    struct bfd_link_needed_list *l;
332
333	    for (l = needed; l != NULL; l = l->next)
334	      if (strncmp (l->name, "libc.so", 7) == 0)
335		break;
336	    if (l == NULL)
337	      {
338		bfd_close (abfd);
339		return false;
340	      }
341	  }
342
343EOF
344    ;;
345esac
346cat >>e${EMULATION_NAME}.c <<EOF
347	}
348    }
349
350  /* We've found a dynamic object matching the DT_NEEDED entry.  */
351
352  /* We have already checked that there is no other input file of the
353     same name.  We must now check again that we are not including the
354     same file twice.  We need to do this because on many systems
355     libc.so is a symlink to, e.g., libc.so.1.  The SONAME entry will
356     reference libc.so.1.  If we have already included libc.so, we
357     don't want to include libc.so.1 if they are the same file, and we
358     can only check that using stat.  */
359
360  if (bfd_stat (abfd, &global_stat) != 0)
361    einfo ("%F%P:%B: bfd_stat failed: %E\n", abfd);
362
363  /* First strip off everything before the last '/'.  */
364  soname = lbasename (abfd->filename);
365
366  if (trace_file_tries)
367    info_msg (_("found %s at %s\n"), soname, name);
368
369  global_found = false;
370  lang_for_each_input_file (gld${EMULATION_NAME}_stat_needed);
371  if (global_found)
372    {
373      /* Return true to indicate that we found the file, even though
374         we aren't going to do anything with it.  */
375      return true;
376    }
377
378  /* Tell the ELF backend that we don't want the output file to have a
379     DT_NEEDED entry for this file.  */
380  bfd_elf_set_dt_needed_name (abfd, "");
381
382  /* Tell the ELF backend that the output file needs a DT_NEEDED
383     entry for this file if it is used to resolve the reference in
384     a regular object.  */
385  bfd_elf_set_dt_needed_soname (abfd, soname);
386
387  /* Add this file into the symbol table.  */
388  if (! bfd_link_add_symbols (abfd, &link_info))
389    einfo ("%F%B: could not read symbols: %E\n", abfd);
390
391  return true;
392}
393
394
395/* Search for a needed file in a path.  */
396
397static boolean
398gld${EMULATION_NAME}_search_needed (path, name, force)
399     const char *path;
400     const char *name;
401     int force;
402{
403  const char *s;
404  size_t len;
405
406  if (name[0] == '/')
407    return gld${EMULATION_NAME}_try_needed (name, force);
408
409  if (path == NULL || *path == '\0')
410    return false;
411  len = strlen (name);
412  while (1)
413    {
414      char *filename, *sset;
415
416      s = strchr (path, ':');
417      if (s == NULL)
418	s = path + strlen (path);
419
420      filename = (char *) xmalloc (s - path + len + 2);
421      if (s == path)
422	sset = filename;
423      else
424	{
425	  memcpy (filename, path, s - path);
426	  filename[s - path] = '/';
427	  sset = filename + (s - path) + 1;
428	}
429      strcpy (sset, name);
430
431      if (gld${EMULATION_NAME}_try_needed (filename, force))
432	return true;
433
434      free (filename);
435
436      if (*s == '\0')
437	break;
438      path = s + 1;
439    }
440
441  return false;
442}
443
444EOF
445if [ "x${host}" = "x${target}" ] ; then
446  case " ${EMULATION_LIBPATH} " in
447  *" ${EMULATION_NAME} "*)
448    case ${target} in
449      *-*-freebsd*)
450	cat >>e${EMULATION_NAME}.c <<EOF
451/*
452 * Read the system search path the FreeBSD way rather than like Linux.
453 */
454#include <elf-hints.h>
455
456static boolean gld${EMULATION_NAME}_check_ld_elf_hints
457  PARAMS ((const char *, int));
458
459static boolean
460gld${EMULATION_NAME}_check_ld_elf_hints (name, force)
461     const char *name;
462     int force;
463{
464  static boolean initialized;
465  static char *ld_elf_hints;
466
467  if (! initialized)
468    {
469      FILE *f;
470
471      f = fopen (_PATH_ELF_HINTS, FOPEN_RB);
472      if (f != NULL)
473	{
474	  struct elfhints_hdr hdr;
475
476	  if (fread(&hdr, 1, sizeof(hdr), f) == sizeof(hdr) &&
477	      hdr.magic == ELFHINTS_MAGIC &&
478	      hdr.version == 1)
479	    {
480	      if (fseek(f, hdr.strtab + hdr.dirlist, SEEK_SET) != -1)
481		{
482		  char *b;
483
484		  b = (char *) xmalloc (hdr.dirlistlen + 1);
485		  if (fread(b, 1, hdr.dirlistlen + 1, f) !=
486		      hdr.dirlistlen + 1)
487		    {
488		      free(b);
489		    }
490		  else
491		    {
492		      ld_elf_hints = b;
493		    }
494		}
495	    }
496	  fclose (f);
497	}
498
499      initialized = true;
500    }
501
502  if (ld_elf_hints == NULL)
503    return false;
504
505  return gld${EMULATION_NAME}_search_needed (ld_elf_hints, name, force);
506}
507EOF
508	# FreeBSD
509	;;
510
511      *-*-linux-gnu*)
512	cat >>e${EMULATION_NAME}.c <<EOF
513
514/* For a native linker, check the file /etc/ld.so.conf for directories
515   in which we may find shared libraries.  /etc/ld.so.conf is really
516   only meaningful on Linux.  */
517
518static boolean gld${EMULATION_NAME}_check_ld_so_conf
519  PARAMS ((const char *, int));
520
521static boolean
522gld${EMULATION_NAME}_check_ld_so_conf (name, force)
523     const char *name;
524     int force;
525{
526  static boolean initialized;
527  static char *ld_so_conf;
528
529  if (! initialized)
530    {
531      FILE *f;
532
533      f = fopen ("/etc/ld.so.conf", FOPEN_RT);
534      if (f != NULL)
535	{
536	  char *b;
537	  size_t len, alloc;
538	  int c;
539
540	  len = 0;
541	  alloc = 100;
542	  b = (char *) xmalloc (alloc);
543
544	  while ((c = getc (f)) != EOF)
545	    {
546	      if (len + 1 >= alloc)
547		{
548		  alloc *= 2;
549		  b = (char *) xrealloc (b, alloc);
550		}
551	      if (c != ':'
552		  && c != ' '
553		  && c != '\t'
554		  && c != '\n'
555		  && c != ',')
556		{
557		  b[len] = c;
558		  ++len;
559		}
560	      else
561		{
562		  if (len > 0 && b[len - 1] != ':')
563		    {
564		      b[len] = ':';
565		      ++len;
566		    }
567		}
568	    }
569
570	  if (len > 0 && b[len - 1] == ':')
571	    --len;
572
573	  if (len > 0)
574	    b[len] = '\0';
575	  else
576	    {
577	      free (b);
578	      b = NULL;
579	    }
580
581	  fclose (f);
582
583	  ld_so_conf = b;
584	}
585
586      initialized = true;
587    }
588
589  if (ld_so_conf == NULL)
590    return false;
591
592  return gld${EMULATION_NAME}_search_needed (ld_so_conf, name, force);
593}
594
595EOF
596	# Linux
597	;;
598    esac
599  esac
600fi
601cat >>e${EMULATION_NAME}.c <<EOF
602
603/* See if an input file matches a DT_NEEDED entry by name.  */
604
605static void
606gld${EMULATION_NAME}_check_needed (s)
607     lang_input_statement_type *s;
608{
609  if (global_found)
610    return;
611
612  if (s->filename != NULL)
613    {
614      const char *f;
615
616      if (strcmp (s->filename, global_needed->name) == 0)
617	{
618	  global_found = true;
619	  return;
620	}
621
622      if (s->search_dirs_flag)
623	{
624	  f = strrchr (s->filename, '/');
625	  if (f != NULL
626	      && strcmp (f + 1, global_needed->name) == 0)
627	    {
628	      global_found = true;
629	      return;
630	    }
631	}
632    }
633
634  if (s->the_bfd != NULL)
635    {
636      const char *soname;
637
638      soname = bfd_elf_get_dt_soname (s->the_bfd);
639      if (soname != NULL
640	  && strcmp (soname, global_needed->name) == 0)
641	{
642	  global_found = true;
643	  return;
644	}
645    }
646}
647
648EOF
649
650if test x"$LDEMUL_AFTER_OPEN" != xgld"$EMULATION_NAME"_after_open; then
651cat >>e${EMULATION_NAME}.c <<EOF
652
653/* This is called after all the input files have been opened.  */
654
655static void
656gld${EMULATION_NAME}_after_open ()
657{
658  struct bfd_link_needed_list *needed, *l;
659
660  /* We only need to worry about this when doing a final link.  */
661  if (link_info.relocateable || link_info.shared)
662    return;
663
664  /* Get the list of files which appear in DT_NEEDED entries in
665     dynamic objects included in the link (often there will be none).
666     For each such file, we want to track down the corresponding
667     library, and include the symbol table in the link.  This is what
668     the runtime dynamic linker will do.  Tracking the files down here
669     permits one dynamic object to include another without requiring
670     special action by the person doing the link.  Note that the
671     needed list can actually grow while we are stepping through this
672     loop.  */
673  needed = bfd_elf_get_needed_list (output_bfd, &link_info);
674  for (l = needed; l != NULL; l = l->next)
675    {
676      struct bfd_link_needed_list *ll;
677      int force;
678
679      /* If we've already seen this file, skip it.  */
680      for (ll = needed; ll != l; ll = ll->next)
681	if (strcmp (ll->name, l->name) == 0)
682	  break;
683      if (ll != l)
684	continue;
685
686      /* See if this file was included in the link explicitly.  */
687      global_needed = l;
688      global_found = false;
689      lang_for_each_input_file (gld${EMULATION_NAME}_check_needed);
690      if (global_found)
691	continue;
692
693      if (trace_file_tries)
694	info_msg (_("%s needed by %B\n"), l->name, l->by);
695
696      /* We need to find this file and include the symbol table.  We
697	 want to search for the file in the same way that the dynamic
698	 linker will search.  That means that we want to use
699	 rpath_link, rpath, then the environment variable
700	 LD_LIBRARY_PATH (native only), then the DT_RPATH/DT_RUNPATH
701	 entries (native only), then the linker script LIB_SEARCH_DIRS.
702	 We do not search using the -L arguments.
703
704	 We search twice.  The first time, we skip objects which may
705	 introduce version mismatches.  The second time, we force
706	 their use.  See gld${EMULATION_NAME}_vercheck comment.  */
707      for (force = 0; force < 2; force++)
708	{
709	  size_t len;
710	  search_dirs_type *search;
711EOF
712if [ "x${host}" = "x${target}" ] ; then
713  case " ${EMULATION_LIBPATH} " in
714  *" ${EMULATION_NAME} "*)
715cat >>e${EMULATION_NAME}.c <<EOF
716	  const char *lib_path;
717	  struct bfd_link_needed_list *rp;
718	  int found;
719EOF
720  ;;
721  esac
722fi
723cat >>e${EMULATION_NAME}.c <<EOF
724
725	  if (gld${EMULATION_NAME}_search_needed (command_line.rpath_link,
726						  l->name, force))
727	    break;
728EOF
729if [ "x${host}" = "x${target}" ] ; then
730  case " ${EMULATION_LIBPATH} " in
731  *" ${EMULATION_NAME} "*)
732cat >>e${EMULATION_NAME}.c <<EOF
733	  if (gld${EMULATION_NAME}_search_needed (command_line.rpath,
734						  l->name, force))
735	    break;
736	  if (command_line.rpath_link == NULL
737	      && command_line.rpath == NULL)
738	    {
739	      lib_path = (const char *) getenv ("LD_RUN_PATH");
740	      if (gld${EMULATION_NAME}_search_needed (lib_path, l->name,
741						      force))
742		break;
743	    }
744	  lib_path = (const char *) getenv ("LD_LIBRARY_PATH");
745	  if (gld${EMULATION_NAME}_search_needed (lib_path, l->name, force))
746	    break;
747
748	  found = 0;
749	  rp = bfd_elf_get_runpath_list (output_bfd, &link_info);
750	  for (; !found && rp != NULL; rp = rp->next)
751	    {
752	      found = (rp->by == l->by
753		       && gld${EMULATION_NAME}_search_needed (rp->name,
754							      l->name,
755							      force));
756	    }
757	  if (found)
758	    break;
759
760EOF
761  ;;
762  esac
763fi
764cat >>e${EMULATION_NAME}.c <<EOF
765	  len = strlen (l->name);
766	  for (search = search_head; search != NULL; search = search->next)
767	    {
768	      char *filename;
769
770	      if (search->cmdline)
771		continue;
772	      filename = (char *) xmalloc (strlen (search->name) + len + 2);
773	      sprintf (filename, "%s/%s", search->name, l->name);
774	      if (gld${EMULATION_NAME}_try_needed (filename, force))
775		break;
776	      free (filename);
777	    }
778	  if (search != NULL)
779	    break;
780EOF
781if [ "x${host}" = "x${target}" ] ; then
782  case " ${EMULATION_LIBPATH} " in
783  *" ${EMULATION_NAME} "*)
784    case ${target} in
785      *-*-freebsd*)
786	cat >>e${EMULATION_NAME}.c <<EOF
787	  if (gld${EMULATION_NAME}_check_ld_elf_hints (l->name, force))
788	    break;
789EOF
790        ;;
791      *-*-linux-gnu*)
792	cat >>e${EMULATION_NAME}.c <<EOF
793	  if (gld${EMULATION_NAME}_check_ld_so_conf (l->name, force))
794	    break;
795EOF
796	# Linux
797        ;;
798    esac
799  ;;
800  esac
801fi
802cat >>e${EMULATION_NAME}.c <<EOF
803	}
804
805      if (force < 2)
806	continue;
807
808      einfo ("%P: warning: %s, needed by %B, not found (try using -rpath or -rpath-link)\n",
809	     l->name, l->by);
810    }
811}
812
813EOF
814fi
815
816cat >>e${EMULATION_NAME}.c <<EOF
817
818/* Look through an expression for an assignment statement.  */
819
820static void
821gld${EMULATION_NAME}_find_exp_assignment (exp)
822     etree_type *exp;
823{
824  struct bfd_link_hash_entry *h;
825
826  switch (exp->type.node_class)
827    {
828    case etree_provide:
829      h = bfd_link_hash_lookup (link_info.hash, exp->assign.dst,
830				false, false, false);
831      if (h == NULL)
832	break;
833
834      /* We call record_link_assignment even if the symbol is defined.
835	 This is because if it is defined by a dynamic object, we
836	 actually want to use the value defined by the linker script,
837	 not the value from the dynamic object (because we are setting
838	 symbols like etext).  If the symbol is defined by a regular
839	 object, then, as it happens, calling record_link_assignment
840	 will do no harm.  */
841
842      /* Fall through.  */
843    case etree_assign:
844      if (strcmp (exp->assign.dst, ".") != 0)
845	{
846	  if (! (bfd_elf${ELFSIZE}_record_link_assignment
847		 (output_bfd, &link_info, exp->assign.dst,
848		  exp->type.node_class == etree_provide ? true : false)))
849	    einfo ("%P%F: failed to record assignment to %s: %E\n",
850		   exp->assign.dst);
851	}
852      gld${EMULATION_NAME}_find_exp_assignment (exp->assign.src);
853      break;
854
855    case etree_binary:
856      gld${EMULATION_NAME}_find_exp_assignment (exp->binary.lhs);
857      gld${EMULATION_NAME}_find_exp_assignment (exp->binary.rhs);
858      break;
859
860    case etree_trinary:
861      gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.cond);
862      gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.lhs);
863      gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.rhs);
864      break;
865
866    case etree_unary:
867      gld${EMULATION_NAME}_find_exp_assignment (exp->unary.child);
868      break;
869
870    default:
871      break;
872    }
873}
874
875
876/* This is called by the before_allocation routine via
877   lang_for_each_statement.  It locates any assignment statements, and
878   tells the ELF backend about them, in case they are assignments to
879   symbols which are referred to by dynamic objects.  */
880
881static void
882gld${EMULATION_NAME}_find_statement_assignment (s)
883     lang_statement_union_type *s;
884{
885  if (s->header.type == lang_assignment_statement_enum)
886    gld${EMULATION_NAME}_find_exp_assignment (s->assignment_statement.exp);
887}
888
889EOF
890
891if test x"$LDEMUL_BEFORE_ALLOCATION" != xgld"$EMULATION_NAME"_before_allocation; then
892  if test x"${ELF_INTERPRETER_NAME+set}" = xset; then
893    ELF_INTERPRETER_SET_DEFAULT="
894  if (sinterp != NULL)
895    {
896      sinterp->contents = ${ELF_INTERPRETER_NAME};
897      sinterp->_raw_size = strlen (sinterp->contents) + 1;
898    }
899
900"
901  else
902    ELF_INTERPRETER_SET_DEFAULT=
903  fi
904cat >>e${EMULATION_NAME}.c <<EOF
905
906/* This is called after the sections have been attached to output
907   sections, but before any sizes or addresses have been set.  */
908
909static void
910gld${EMULATION_NAME}_before_allocation ()
911{
912  const char *rpath;
913  asection *sinterp;
914
915  /* If we are going to make any variable assignments, we need to let
916     the ELF backend know about them in case the variables are
917     referred to by dynamic objects.  */
918  lang_for_each_statement (gld${EMULATION_NAME}_find_statement_assignment);
919
920  /* Let the ELF backend work out the sizes of any sections required
921     by dynamic linking.  */
922  rpath = command_line.rpath;
923  if (rpath == NULL)
924    rpath = (const char *) getenv ("LD_RUN_PATH");
925  if (! (bfd_elf${ELFSIZE}_size_dynamic_sections
926         (output_bfd, command_line.soname, rpath,
927	  command_line.filter_shlib,
928	  (const char * const *) command_line.auxiliary_filters,
929	  &link_info, &sinterp, lang_elf_version_info)))
930    einfo ("%P%F: failed to set dynamic section sizes: %E\n");
931${ELF_INTERPRETER_SET_DEFAULT}
932  /* Let the user override the dynamic linker we are using.  */
933  if (command_line.interpreter != NULL
934      && sinterp != NULL)
935    {
936      sinterp->contents = (bfd_byte *) command_line.interpreter;
937      sinterp->_raw_size = strlen (command_line.interpreter) + 1;
938    }
939
940  /* Look for any sections named .gnu.warning.  As a GNU extensions,
941     we treat such sections as containing warning messages.  We print
942     out the warning message, and then zero out the section size so
943     that it does not get copied into the output file.  */
944
945  {
946    LANG_FOR_EACH_INPUT_STATEMENT (is)
947      {
948	asection *s;
949	bfd_size_type sz;
950	char *msg;
951	boolean ret;
952
953	if (is->just_syms_flag)
954	  continue;
955
956	s = bfd_get_section_by_name (is->the_bfd, ".gnu.warning");
957	if (s == NULL)
958	  continue;
959
960	sz = bfd_section_size (is->the_bfd, s);
961	msg = xmalloc ((size_t) sz + 1);
962	if (! bfd_get_section_contents (is->the_bfd, s, msg, (file_ptr) 0, sz))
963	  einfo ("%F%B: Can't read contents of section .gnu.warning: %E\n",
964		 is->the_bfd);
965	msg[sz] = '\0';
966	ret = link_info.callbacks->warning (&link_info, msg,
967					    (const char *) NULL,
968					    is->the_bfd, (asection *) NULL,
969					    (bfd_vma) 0);
970	ASSERT (ret);
971	free (msg);
972
973	/* Clobber the section size, so that we don't waste copying the
974	   warning into the output file.  */
975	s->_raw_size = 0;
976      }
977  }
978}
979
980EOF
981fi
982
983if test x"$LDEMUL_OPEN_DYNAMIC_ARCHIVE" != xgld"$EMULATION_NAME"_open_dynamic_archive; then
984cat >>e${EMULATION_NAME}.c <<EOF
985
986/* Try to open a dynamic archive.  This is where we know that ELF
987   dynamic libraries have an extension of .so (or .sl on oddball systems
988   like hpux).  */
989
990static boolean
991gld${EMULATION_NAME}_open_dynamic_archive (arch, search, entry)
992     const char *arch;
993     search_dirs_type *search;
994     lang_input_statement_type *entry;
995{
996  const char *filename;
997  char *string;
998
999  if (! entry->is_archive)
1000    return false;
1001
1002  filename = entry->filename;
1003
1004  /* This allocates a few bytes too many when EXTRA_SHLIB_EXTENSION
1005     is defined, but it does not seem worth the headache to optimize
1006     away those two bytes of space.  */
1007  string = (char *) xmalloc (strlen (search->name)
1008			     + strlen (filename)
1009			     + strlen (arch)
1010#ifdef EXTRA_SHLIB_EXTENSION
1011			     + strlen (EXTRA_SHLIB_EXTENSION)
1012#endif
1013			     + sizeof "/lib.so");
1014
1015  sprintf (string, "%s/lib%s%s.so", search->name, filename, arch);
1016
1017#ifdef EXTRA_SHLIB_EXTENSION
1018  /* Try the .so extension first.  If that fails build a new filename
1019     using EXTRA_SHLIB_EXTENSION.  */
1020  if (! ldfile_try_open_bfd (string, entry))
1021    sprintf (string, "%s/lib%s%s%s", search->name,
1022	     filename, arch, EXTRA_SHLIB_EXTENSION);
1023#endif
1024
1025  if (! ldfile_try_open_bfd (string, entry))
1026    {
1027      free (string);
1028      return false;
1029    }
1030
1031  entry->filename = string;
1032
1033  /* We have found a dynamic object to include in the link.  The ELF
1034     backend linker will create a DT_NEEDED entry in the .dynamic
1035     section naming this file.  If this file includes a DT_SONAME
1036     entry, it will be used.  Otherwise, the ELF linker will just use
1037     the name of the file.  For an archive found by searching, like
1038     this one, the DT_NEEDED entry should consist of just the name of
1039     the file, without the path information used to find it.  Note
1040     that we only need to do this if we have a dynamic object; an
1041     archive will never be referenced by a DT_NEEDED entry.
1042
1043     FIXME: This approach--using bfd_elf_set_dt_needed_name--is not
1044     very pretty.  I haven't been able to think of anything that is
1045     pretty, though.  */
1046  if (bfd_check_format (entry->the_bfd, bfd_object)
1047      && (entry->the_bfd->flags & DYNAMIC) != 0)
1048    {
1049      ASSERT (entry->is_archive && entry->search_dirs_flag);
1050
1051      /* Rather than duplicating the logic above.  Just use the
1052	 filename we recorded earlier.  */
1053
1054      filename = lbasename (entry->filename);
1055      bfd_elf_set_dt_needed_name (entry->the_bfd, filename);
1056    }
1057
1058  return true;
1059}
1060
1061EOF
1062fi
1063
1064if test x"$LDEMUL_PLACE_ORPHAN" != xgld"$EMULATION_NAME"_place_orphan; then
1065cat >>e${EMULATION_NAME}.c <<EOF
1066
1067/* A variant of lang_output_section_find.  Used by place_orphan.  */
1068
1069static lang_output_section_statement_type *
1070output_rel_find ()
1071{
1072  lang_statement_union_type *u;
1073  lang_output_section_statement_type *lookup;
1074
1075  for (u = lang_output_section_statement.head;
1076       u != (lang_statement_union_type *) NULL;
1077       u = lookup->next)
1078    {
1079      lookup = &u->output_section_statement;
1080      if (strncmp (".rel", lookup->name, 4) == 0
1081	  && lookup->bfd_section != NULL
1082	  && (lookup->bfd_section->flags & SEC_ALLOC) != 0)
1083	{
1084	  return lookup;
1085	}
1086    }
1087  return (lang_output_section_statement_type *) NULL;
1088}
1089
1090/* Find the last output section before given output statement.
1091   Used by place_orphan.  */
1092
1093static asection *
1094output_prev_sec_find (os)
1095     lang_output_section_statement_type *os;
1096{
1097  asection *s = (asection *) NULL;
1098  lang_statement_union_type *u;
1099  lang_output_section_statement_type *lookup;
1100
1101  for (u = lang_output_section_statement.head;
1102       u != (lang_statement_union_type *) NULL;
1103       u = lookup->next)
1104    {
1105      lookup = &u->output_section_statement;
1106      if (lookup == os)
1107	return s;
1108
1109      if (lookup->bfd_section != NULL && lookup->bfd_section->owner != NULL)
1110	s = lookup->bfd_section;
1111    }
1112
1113  return NULL;
1114}
1115
1116/* Place an orphan section.  We use this to put random SHF_ALLOC
1117   sections in the right segment.  */
1118
1119struct orphan_save {
1120  lang_output_section_statement_type *os;
1121  asection **section;
1122  lang_statement_union_type **stmt;
1123};
1124
1125static boolean
1126gld${EMULATION_NAME}_place_orphan (file, s)
1127     lang_input_statement_type *file;
1128     asection *s;
1129{
1130  static struct orphan_save hold_text;
1131  static struct orphan_save hold_rodata;
1132  static struct orphan_save hold_data;
1133  static struct orphan_save hold_bss;
1134  static struct orphan_save hold_rel;
1135  static struct orphan_save hold_interp;
1136  static struct orphan_save hold_sdata;
1137  static int count = 1;
1138  struct orphan_save *place;
1139  lang_statement_list_type *old;
1140  lang_statement_list_type add;
1141  etree_type *address;
1142  const char *secname;
1143  const char *outsecname;
1144  const char *ps = NULL;
1145  lang_output_section_statement_type *os;
1146
1147  secname = bfd_get_section_name (s->owner, s);
1148
1149  if (! config.unique_orphan_sections && ! unique_section_p (secname))
1150    {
1151      /* Look through the script to see where to place this section.  */
1152      os = lang_output_section_find (secname);
1153
1154      if (os != NULL
1155	  && (os->bfd_section == NULL
1156	      || ((s->flags ^ os->bfd_section->flags)
1157		  & (SEC_LOAD | SEC_ALLOC)) == 0))
1158	{
1159	  /* We already have an output section statement with this
1160	     name, and its bfd section, if any, has compatible flags.  */
1161	  lang_add_section (&os->children, s, os, file);
1162	  return true;
1163	}
1164    }
1165
1166  if (hold_text.os == NULL)
1167    hold_text.os = lang_output_section_find (".text");
1168
1169  /* If this is a final link, then always put .gnu.warning.SYMBOL
1170     sections into the .text section to get them out of the way.  */
1171  if (! link_info.shared
1172      && ! link_info.relocateable
1173      && strncmp (secname, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0
1174      && hold_text.os != NULL)
1175    {
1176      lang_add_section (&hold_text.os->children, s, hold_text.os, file);
1177      return true;
1178    }
1179
1180  /* Decide which segment the section should go in based on the
1181     section name and section flags.  We put loadable .note sections
1182     right after the .interp section, so that the PT_NOTE segment is
1183     stored right after the program headers where the OS can read it
1184     in the first page.  */
1185#define HAVE_SECTION(hold, name) \
1186(hold.os != NULL || (hold.os = lang_output_section_find (name)) != NULL)
1187
1188  if ((s->flags & SEC_EXCLUDE) != 0 && !link_info.relocateable)
1189    {
1190      if (s->output_section == NULL)
1191	s->output_section = bfd_abs_section_ptr;
1192      return true;
1193    }
1194
1195  place = NULL;
1196  if ((s->flags & SEC_ALLOC) == 0)
1197    ;
1198  else if ((s->flags & SEC_LOAD) != 0
1199	   && strncmp (secname, ".note", 5) == 0
1200	   && HAVE_SECTION (hold_interp, ".interp"))
1201    place = &hold_interp;
1202  else if ((s->flags & SEC_HAS_CONTENTS) == 0
1203	   && HAVE_SECTION (hold_bss, ".bss"))
1204    place = &hold_bss;
1205  else if ((s->flags & SEC_SMALL_DATA) != 0
1206	   && HAVE_SECTION (hold_sdata, ".sdata"))
1207    place = &hold_sdata;
1208  else if ((s->flags & SEC_READONLY) == 0
1209	   && HAVE_SECTION (hold_data, ".data"))
1210    place = &hold_data;
1211  else if (strncmp (secname, ".rel", 4) == 0
1212	   && (hold_rel.os != NULL
1213	       || (hold_rel.os = output_rel_find ()) != NULL))
1214    {
1215      if (! link_info.relocateable && link_info.combreloc)
1216	{
1217	  if (strncmp (secname, ".rela", 5) == 0)
1218	    os = lang_output_section_find (".rela.dyn");
1219	  else
1220	    os = lang_output_section_find (".rel.dyn");
1221
1222	  if (os != NULL
1223	      && os->bfd_section != NULL
1224	      && ((s->flags ^ os->bfd_section->flags)
1225		  & (SEC_LOAD | SEC_ALLOC)) == 0)
1226	    {
1227	      lang_add_section (&os->children, s, os, file);
1228	      return true;
1229	    }
1230	}
1231      place = &hold_rel;
1232    }
1233  else if ((s->flags & (SEC_CODE | SEC_READONLY)) == SEC_READONLY
1234	   && HAVE_SECTION (hold_rodata, ".rodata"))
1235    place = &hold_rodata;
1236  else if ((s->flags & (SEC_CODE | SEC_READONLY)) == (SEC_CODE | SEC_READONLY)
1237	   && hold_text.os != NULL)
1238    place = &hold_text;
1239
1240#undef HAVE_SECTION
1241
1242  /* Choose a unique name for the section.  This will be needed if the
1243     same section name appears in the input file with different
1244     loadable or allocatable characteristics.  */
1245  outsecname = secname;
1246  if (bfd_get_section_by_name (output_bfd, outsecname) != NULL)
1247    {
1248      outsecname = bfd_get_unique_section_name (output_bfd,
1249						outsecname,
1250						&count);
1251      if (outsecname == NULL)
1252	einfo ("%F%P: place_orphan failed: %E\n");
1253    }
1254
1255  /* Start building a list of statements for this section.
1256     First save the current statement pointer.  */
1257  old = stat_ptr;
1258
1259  /* If we have found an appropriate place for the output section
1260     statements for this orphan, add them to our own private list,
1261     inserting them later into the global statement list.  */
1262  if (place != NULL)
1263    {
1264      stat_ptr = &add;
1265      lang_list_init (stat_ptr);
1266    }
1267
1268  if (config.build_constructors)
1269    {
1270      /* If the name of the section is representable in C, then create
1271	 symbols to mark the start and the end of the section.  */
1272      for (ps = outsecname; *ps != '\0'; ps++)
1273	if (! ISALNUM (*ps) && *ps != '_')
1274	  break;
1275      if (*ps == '\0')
1276	{
1277	  char *symname;
1278	  etree_type *e_align;
1279
1280	  symname = (char *) xmalloc (ps - outsecname + sizeof "__start_");
1281	  sprintf (symname, "__start_%s", outsecname);
1282	  e_align = exp_unop (ALIGN_K,
1283			      exp_intop ((bfd_vma) 1 << s->alignment_power));
1284	  lang_add_assignment (exp_assop ('=', symname, e_align));
1285	}
1286    }
1287
1288  if (link_info.relocateable || (s->flags & (SEC_LOAD | SEC_ALLOC)) == 0)
1289    address = exp_intop ((bfd_vma) 0);
1290  else
1291    address = NULL;
1292
1293  os = lang_enter_output_section_statement (outsecname, address, 0,
1294					    (bfd_vma) 0,
1295					    (etree_type *) NULL,
1296					    (etree_type *) NULL,
1297					    (etree_type *) NULL);
1298
1299  lang_add_section (&os->children, s, os, file);
1300
1301  lang_leave_output_section_statement
1302    ((bfd_vma) 0, "*default*",
1303     (struct lang_output_section_phdr_list *) NULL, NULL);
1304
1305  if (config.build_constructors && *ps == '\0')
1306    {
1307      char *symname;
1308
1309      /* lang_leave_ouput_section_statement resets stat_ptr.  Put
1310	 stat_ptr back where we want it.  */
1311      if (place != NULL)
1312	stat_ptr = &add;
1313
1314      symname = (char *) xmalloc (ps - outsecname + sizeof "__stop_");
1315      sprintf (symname, "__stop_%s", outsecname);
1316      lang_add_assignment (exp_assop ('=', symname,
1317				      exp_nameop (NAME, ".")));
1318    }
1319
1320  /* Restore the global list pointer.  */
1321  stat_ptr = old;
1322
1323  if (place != NULL && os->bfd_section != NULL)
1324    {
1325      asection *snew, **pps;
1326
1327      snew = os->bfd_section;
1328
1329      /* Shuffle the bfd section list to make the output file look
1330	 neater.  This is really only cosmetic.  */
1331      if (place->section == NULL)
1332	{
1333	  asection *bfd_section = place->os->bfd_section;
1334
1335	  /* If the output statement hasn't been used to place
1336	     any input sections (and thus doesn't have an output
1337	     bfd_section), look for the closest prior output statement
1338	     having an output section.  */
1339	  if (bfd_section == NULL)
1340	    bfd_section = output_prev_sec_find (place->os);
1341
1342	  if (bfd_section != NULL && bfd_section != snew)
1343	    place->section = &bfd_section->next;
1344	}
1345
1346      if (place->section != NULL)
1347	{
1348	  /* Unlink the section.  */
1349	  for (pps = &output_bfd->sections; *pps != snew; pps = &(*pps)->next)
1350	    ;
1351	  bfd_section_list_remove (output_bfd, pps);
1352
1353	  /* Now tack it on to the "place->os" section list.  */
1354	  bfd_section_list_insert (output_bfd, place->section, snew);
1355	}
1356
1357      /* Save the end of this list.  Further ophans of this type will
1358	 follow the one we've just added.  */
1359      place->section = &snew->next;
1360
1361      /* The following is non-cosmetic.  We try to put the output
1362	 statements in some sort of reasonable order here, because
1363	 they determine the final load addresses of the orphan
1364	 sections.  In addition, placing output statements in the
1365	 wrong order may require extra segments.  For instance,
1366	 given a typical situation of all read-only sections placed
1367	 in one segment and following that a segment containing all
1368	 the read-write sections, we wouldn't want to place an orphan
1369	 read/write section before or amongst the read-only ones.  */
1370      if (add.head != NULL)
1371	{
1372	  if (place->stmt == NULL)
1373	    {
1374	      /* Put the new statement list right at the head.  */
1375	      *add.tail = place->os->header.next;
1376	      place->os->header.next = add.head;
1377	    }
1378	  else
1379	    {
1380	      /* Put it after the last orphan statement we added.  */
1381	      *add.tail = *place->stmt;
1382	      *place->stmt = add.head;
1383	    }
1384
1385	  /* Fix the global list pointer if we happened to tack our
1386	     new list at the tail.  */
1387	  if (*old->tail == add.head)
1388	    old->tail = add.tail;
1389
1390	  /* Save the end of this list.  */
1391	  place->stmt = add.tail;
1392	}
1393    }
1394
1395  return true;
1396}
1397EOF
1398fi
1399
1400if test x"$LDEMUL_FINISH" != xgld"$EMULATION_NAME"_finish; then
1401cat >>e${EMULATION_NAME}.c <<EOF
1402
1403static void
1404gld${EMULATION_NAME}_finish ()
1405{
1406  if (bfd_elf${ELFSIZE}_discard_info (output_bfd, &link_info))
1407    {
1408      lang_reset_memory_regions ();
1409
1410      /* Resize the sections.  */
1411      lang_size_sections (stat_ptr->head, abs_output_section,
1412			  &stat_ptr->head, 0, (bfd_vma) 0, NULL);
1413
1414      /* Redo special stuff.  */
1415      ldemul_after_allocation ();
1416
1417      /* Do the assignments again.  */
1418      lang_do_assignments (stat_ptr->head, abs_output_section,
1419			   (fill_type *) 0, (bfd_vma) 0);
1420    }
1421}
1422EOF
1423fi
1424
1425if test x"$LDEMUL_GET_SCRIPT" != xgld"$EMULATION_NAME"_get_script; then
1426cat >>e${EMULATION_NAME}.c <<EOF
1427
1428static char *
1429gld${EMULATION_NAME}_get_script (isfile)
1430     int *isfile;
1431EOF
1432
1433if test -n "$COMPILE_IN"
1434then
1435# Scripts compiled in.
1436
1437# sed commands to quote an ld script as a C string.
1438sc="-f stringify.sed"
1439
1440cat >>e${EMULATION_NAME}.c <<EOF
1441{
1442  *isfile = 0;
1443
1444  if (link_info.relocateable == true && config.build_constructors == true)
1445    return
1446EOF
1447sed $sc ldscripts/${EMULATION_NAME}.xu                     >> e${EMULATION_NAME}.c
1448echo '  ; else if (link_info.relocateable == true) return' >> e${EMULATION_NAME}.c
1449sed $sc ldscripts/${EMULATION_NAME}.xr                     >> e${EMULATION_NAME}.c
1450echo '  ; else if (!config.text_read_only) return'         >> e${EMULATION_NAME}.c
1451sed $sc ldscripts/${EMULATION_NAME}.xbn                    >> e${EMULATION_NAME}.c
1452if cmp -s ldscripts/${EMULATION_NAME}.x ldscripts/${EMULATION_NAME}.xn; then : ; else
1453echo '  ; else if (!config.magic_demand_paged) return'     >> e${EMULATION_NAME}.c
1454sed $sc ldscripts/${EMULATION_NAME}.xn                     >> e${EMULATION_NAME}.c
1455fi
1456if test -n "$GENERATE_SHLIB_SCRIPT" ; then
1457if test -n "$GENERATE_COMBRELOC_SCRIPT" ; then
1458echo '  ; else if (link_info.shared && link_info.combreloc) return' >> e${EMULATION_NAME}.c
1459sed $sc ldscripts/${EMULATION_NAME}.xsc                    >> e${EMULATION_NAME}.c
1460fi
1461echo '  ; else if (link_info.shared) return'		   >> e${EMULATION_NAME}.c
1462sed $sc ldscripts/${EMULATION_NAME}.xs                     >> e${EMULATION_NAME}.c
1463fi
1464if test -n "$GENERATE_COMBRELOC_SCRIPT" ; then
1465echo '  ; else if (link_info.combreloc) return'            >> e${EMULATION_NAME}.c
1466sed $sc ldscripts/${EMULATION_NAME}.xc                     >> e${EMULATION_NAME}.c
1467fi
1468echo '  ; else return'                                     >> e${EMULATION_NAME}.c
1469sed $sc ldscripts/${EMULATION_NAME}.x                      >> e${EMULATION_NAME}.c
1470echo '; }'                                                 >> e${EMULATION_NAME}.c
1471
1472else
1473# Scripts read from the filesystem.
1474
1475cat >>e${EMULATION_NAME}.c <<EOF
1476{
1477  *isfile = 1;
1478
1479  if (link_info.relocateable == true && config.build_constructors == true)
1480    return "ldscripts/${EMULATION_NAME}.xu";
1481  else if (link_info.relocateable == true)
1482    return "ldscripts/${EMULATION_NAME}.xr";
1483  else if (!config.text_read_only)
1484    return "ldscripts/${EMULATION_NAME}.xbn";
1485  else if (!config.magic_demand_paged)
1486    return "ldscripts/${EMULATION_NAME}.xn";
1487  else if (link_info.shared)
1488    return "ldscripts/${EMULATION_NAME}.xs";
1489  else
1490    return "ldscripts/${EMULATION_NAME}.x";
1491}
1492
1493EOF
1494fi
1495fi
1496
1497if test -n "$PARSE_AND_LIST_ARGS_CASES" -o x"$GENERATE_SHLIB_SCRIPT" = xyes; then
1498
1499if test x"$LDEMUL_PARSE_ARGS" != xgld"$EMULATION_NAME"_parse_args; then
1500
1501if test -n "$PARSE_AND_LIST_PROLOGUE" ; then
1502cat >>e${EMULATION_NAME}.c <<EOF
1503 $PARSE_AND_LIST_PROLOGUE
1504EOF
1505fi
1506
1507cat >>e${EMULATION_NAME}.c <<EOF
1508
1509#include "getopt.h"
1510
1511#define OPTION_DISABLE_NEW_DTAGS	(400)
1512#define OPTION_ENABLE_NEW_DTAGS		(OPTION_DISABLE_NEW_DTAGS + 1)
1513#define OPTION_GROUP			(OPTION_ENABLE_NEW_DTAGS + 1)
1514#define OPTION_EH_FRAME_HDR		(OPTION_GROUP + 1)
1515
1516static struct option longopts[] =
1517{
1518EOF
1519
1520if test x"$GENERATE_SHLIB_SCRIPT" = xyes; then
1521cat >>e${EMULATION_NAME}.c <<EOF
1522  /* getopt allows abbreviations, so we do this to stop it from
1523     treating -d/-e as abbreviations for these options. */
1524  {"disable-new-dtags", no_argument, NULL, OPTION_DISABLE_NEW_DTAGS},
1525  {"disable-new-dtags", no_argument, NULL, OPTION_DISABLE_NEW_DTAGS},
1526  {"enable-new-dtags", no_argument, NULL, OPTION_ENABLE_NEW_DTAGS},
1527  {"enable-new-dtags", no_argument, NULL, OPTION_ENABLE_NEW_DTAGS},
1528  {"eh-frame-hdr", no_argument, NULL, OPTION_EH_FRAME_HDR},
1529  {"Bgroup", no_argument, NULL, OPTION_GROUP},
1530  {"Bgroup", no_argument, NULL, OPTION_GROUP},
1531EOF
1532fi
1533
1534if test -n "$PARSE_AND_LIST_LONGOPTS" ; then
1535cat >>e${EMULATION_NAME}.c <<EOF
1536 $PARSE_AND_LIST_LONGOPTS
1537EOF
1538fi
1539
1540cat >>e${EMULATION_NAME}.c <<EOF
1541  {NULL, no_argument, NULL, 0}
1542};
1543
1544
1545static int gld${EMULATION_NAME}_parse_args PARAMS ((int, char **));
1546
1547static int
1548gld${EMULATION_NAME}_parse_args (argc, argv)
1549     int argc;
1550     char ** argv;
1551{
1552  int longind;
1553  int optc;
1554  static int prevoptind = -1;
1555  int prevopterr = opterr;
1556  int wanterror;
1557
1558  if (prevoptind != optind)
1559    opterr = 0;
1560
1561  wanterror = opterr;
1562  prevoptind = optind;
1563
1564  optc = getopt_long_only (argc, argv,
1565			   "-${PARSE_AND_LIST_SHORTOPTS}z:", longopts,
1566			   &longind);
1567  opterr = prevopterr;
1568
1569  switch (optc)
1570    {
1571    default:
1572      if (wanterror)
1573	xexit (1);
1574      optind = prevoptind;
1575      return 0;
1576
1577EOF
1578
1579if test x"$GENERATE_SHLIB_SCRIPT" = xyes; then
1580cat >>e${EMULATION_NAME}.c <<EOF
1581    case OPTION_DISABLE_NEW_DTAGS:
1582      link_info.new_dtags = false;
1583      break;
1584
1585    case OPTION_ENABLE_NEW_DTAGS:
1586      link_info.new_dtags = true;
1587      break;
1588
1589    case OPTION_EH_FRAME_HDR:
1590      link_info.eh_frame_hdr = true;
1591      break;
1592
1593    case OPTION_GROUP:
1594      link_info.flags_1 |= (bfd_vma) DF_1_GROUP;
1595      /* Groups must be self-contained.  */
1596      link_info.no_undefined = true;
1597      break;
1598
1599    case 'z':
1600      if (strcmp (optarg, "initfirst") == 0)
1601	link_info.flags_1 |= (bfd_vma) DF_1_INITFIRST;
1602      else if (strcmp (optarg, "interpose") == 0)
1603	link_info.flags_1 |= (bfd_vma) DF_1_INTERPOSE;
1604      else if (strcmp (optarg, "loadfltr") == 0)
1605	link_info.flags_1 |= (bfd_vma) DF_1_LOADFLTR;
1606      else if (strcmp (optarg, "nodefaultlib") == 0)
1607	link_info.flags_1 |= (bfd_vma) DF_1_NODEFLIB;
1608      else if (strcmp (optarg, "nodelete") == 0)
1609	link_info.flags_1 |= (bfd_vma) DF_1_NODELETE;
1610      else if (strcmp (optarg, "nodlopen") == 0)
1611	link_info.flags_1 |= (bfd_vma) DF_1_NOOPEN;
1612      else if (strcmp (optarg, "nodump") == 0)
1613	link_info.flags_1 |= (bfd_vma) DF_1_NODUMP;
1614      else if (strcmp (optarg, "now") == 0)
1615	{
1616	  link_info.flags |= (bfd_vma) DF_BIND_NOW;
1617	  link_info.flags_1 |= (bfd_vma) DF_1_NOW;
1618	}
1619      else if (strcmp (optarg, "origin") == 0)
1620	{
1621	  link_info.flags |= (bfd_vma) DF_ORIGIN;
1622	  link_info.flags_1 |= (bfd_vma) DF_1_ORIGIN;
1623	}
1624      else if (strcmp (optarg, "defs") == 0)
1625	link_info.no_undefined = true;
1626      else if (strcmp (optarg, "muldefs") == 0)
1627	link_info.allow_multiple_definition = true;
1628      else if (strcmp (optarg, "combreloc") == 0)
1629	link_info.combreloc = true;
1630      else if (strcmp (optarg, "nocombreloc") == 0)
1631	link_info.combreloc = false;
1632      else if (strcmp (optarg, "nocopyreloc") == 0)
1633        link_info.nocopyreloc = true;
1634      /* What about the other Solaris -z options? FIXME.  */
1635      break;
1636EOF
1637fi
1638
1639if test -n "$PARSE_AND_LIST_ARGS_CASES" ; then
1640cat >>e${EMULATION_NAME}.c <<EOF
1641 $PARSE_AND_LIST_ARGS_CASES
1642EOF
1643fi
1644
1645cat >>e${EMULATION_NAME}.c <<EOF
1646    }
1647
1648  return 1;
1649}
1650
1651EOF
1652fi
1653
1654if test x"$LDEMUL_LIST_OPTIONS" != xgld"$EMULATION_NAME"_list_options; then
1655cat >>e${EMULATION_NAME}.c <<EOF
1656
1657static void gld${EMULATION_NAME}_list_options PARAMS ((FILE * file));
1658
1659static void
1660gld${EMULATION_NAME}_list_options (file)
1661     FILE * file;
1662{
1663EOF
1664
1665if test x"$GENERATE_SHLIB_SCRIPT" = xyes; then
1666cat >>e${EMULATION_NAME}.c <<EOF
1667  fprintf (file, _("  -Bgroup\t\tSelects group name lookup rules for DSO\n"));
1668  fprintf (file, _("  --disable-new-dtags\tDisable new dynamic tags\n"));
1669  fprintf (file, _("  --enable-new-dtags\tEnable new dynamic tags\n"));
1670  fprintf (file, _("  --eh-frame-hdr\tCreate .eh_frame_hdr section\n"));
1671  fprintf (file, _("  -z combreloc\t\tMerge dynamic relocs into one section and sort\n"));
1672  fprintf (file, _("  -z defs\t\tDisallows undefined symbols\n"));
1673  fprintf (file, _("  -z initfirst\t\tMark DSO to be initialized first at runtime\n"));
1674  fprintf (file, _("  -z interpose\t\tMark object to interpose all DSOs but executable\n"));
1675  fprintf (file, _("  -z loadfltr\t\tMark object requiring immediate process\n"));
1676  fprintf (file, _("  -z muldefs\t\tAllow multiple definitions\n"));
1677  fprintf (file, _("  -z nocombreloc\tDon't merge dynamic relocs into one section\n"));
1678  fprintf (file, _("  -z nocopyreloc\tDon't create copy relocs\n"));
1679  fprintf (file, _("  -z nodefaultlib\tMark object not to use default search paths\n"));
1680  fprintf (file, _("  -z nodelete\t\tMark DSO non-deletable at runtime\n"));
1681  fprintf (file, _("  -z nodlopen\t\tMark DSO not available to dlopen\n"));
1682  fprintf (file, _("  -z nodump\t\tMark DSO not available to dldump\n"));
1683  fprintf (file, _("  -z now\t\tMark object non-lazy runtime binding\n"));
1684  fprintf (file, _("  -z origin\t\tMark object requiring immediate \$ORIGIN processing\n\t\t\t  at runtime\n"));
1685  fprintf (file, _("  -z KEYWORD\t\tIgnored for Solaris compatibility\n"));
1686EOF
1687fi
1688
1689if test -n "$PARSE_AND_LIST_OPTIONS" ; then
1690cat >>e${EMULATION_NAME}.c <<EOF
1691 $PARSE_AND_LIST_OPTIONS
1692EOF
1693fi
1694
1695cat >>e${EMULATION_NAME}.c <<EOF
1696}
1697EOF
1698
1699if test -n "$PARSE_AND_LIST_EPILOGUE" ; then
1700cat >>e${EMULATION_NAME}.c <<EOF
1701 $PARSE_AND_LIST_EPILOGUE
1702EOF
1703fi
1704fi
1705else
1706if test x"$LDEMUL_PARSE_ARGS" != xgld"$EMULATION_NAME"_parse_args; then
1707cat >>e${EMULATION_NAME}.c <<EOF
1708#define gld${EMULATION_NAME}_parse_args   NULL
1709EOF
1710fi
1711if test x"$LDEMUL_LIST_OPTIONS" != xgld"$EMULATION_NAME"_list_options; then
1712cat >>e${EMULATION_NAME}.c <<EOF
1713#define gld${EMULATION_NAME}_list_options NULL
1714EOF
1715fi
1716fi
1717
1718cat >>e${EMULATION_NAME}.c <<EOF
1719
1720struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
1721{
1722  ${LDEMUL_BEFORE_PARSE-gld${EMULATION_NAME}_before_parse},
1723  ${LDEMUL_SYSLIB-syslib_default},
1724  ${LDEMUL_HLL-hll_default},
1725  ${LDEMUL_AFTER_PARSE-after_parse_default},
1726  ${LDEMUL_AFTER_OPEN-gld${EMULATION_NAME}_after_open},
1727  ${LDEMUL_AFTER_ALLOCATION-after_allocation_default},
1728  ${LDEMUL_SET_OUTPUT_ARCH-set_output_arch_default},
1729  ${LDEMUL_CHOOSE_TARGET-ldemul_default_target},
1730  ${LDEMUL_BEFORE_ALLOCATION-gld${EMULATION_NAME}_before_allocation},
1731  ${LDEMUL_GET_SCRIPT-gld${EMULATION_NAME}_get_script},
1732  "${EMULATION_NAME}",
1733  "${OUTPUT_FORMAT}",
1734  ${LDEMUL_FINISH-gld${EMULATION_NAME}_finish},
1735  ${LDEMUL_CREATE_OUTPUT_SECTION_STATEMENTS-NULL},
1736  ${LDEMUL_OPEN_DYNAMIC_ARCHIVE-gld${EMULATION_NAME}_open_dynamic_archive},
1737  ${LDEMUL_PLACE_ORPHAN-gld${EMULATION_NAME}_place_orphan},
1738  ${LDEMUL_SET_SYMBOLS-NULL},
1739  ${LDEMUL_PARSE_ARGS-gld${EMULATION_NAME}_parse_args},
1740  ${LDEMUL_UNRECOGNIZED_FILE-NULL},
1741  ${LDEMUL_LIST_OPTIONS-gld${EMULATION_NAME}_list_options},
1742  ${LDEMUL_RECOGNIZED_FILE-NULL},
1743  ${LDEMUL_FIND_POTENTIAL_LIBRARIES-NULL},
1744  ${LDEMUL_NEW_VERS_PATTERN-NULL}
1745};
1746EOF
1747