1# This shell script emits a C file. -*- C -*-
2# It does some substitutions.
3# This file is now misnamed, because it supports both 32 bit and 64 bit
4# ELF emulations.
5test -z "${ELFSIZE}" && ELFSIZE=32
6if [ -z "$MACHINE" ]; then
7  OUTPUT_ARCH=${ARCH}
8else
9  OUTPUT_ARCH=${ARCH}:${MACHINE}
10fi
11cat >e${EMULATION_NAME}.c <<EOF
12/* This file is is generated by a shell script.  DO NOT EDIT! */
13
14/* ${ELFSIZE} bit ELF emulation code for ${EMULATION_NAME}
15   Copyright 1991, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
16   2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
17   Written by Steve Chamberlain <sac@cygnus.com>
18   ELF support by Ian Lance Taylor <ian@cygnus.com>
19
20This file is part of GLD, the Gnu Linker.
21
22This program is free software; you can redistribute it and/or modify
23it under the terms of the GNU General Public License as published by
24the Free Software Foundation; either version 2 of the License, or
25(at your option) any later version.
26
27This program is distributed in the hope that it will be useful,
28but WITHOUT ANY WARRANTY; without even the implied warranty of
29MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30GNU General Public License for more details.
31
32You should have received a copy of the GNU General Public License
33along with this program; if not, write to the Free Software
34Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
35
36#define TARGET_IS_${EMULATION_NAME}
37
38#include "config.h"
39#include "bfd.h"
40#include "sysdep.h"
41#include "libiberty.h"
42#include "safe-ctype.h"
43#include "getopt.h"
44
45#include "bfdlink.h"
46
47#include "ld.h"
48#include "ldmain.h"
49#include "ldmisc.h"
50#include "ldexp.h"
51#include "ldlang.h"
52#include "ldfile.h"
53#include "ldemul.h"
54#include <ldgram.h>
55#include "elf/common.h"
56#include "elf-bfd.h"
57
58/* Declare functions used by various EXTRA_EM_FILEs.  */
59static void gld${EMULATION_NAME}_before_parse (void);
60static void gld${EMULATION_NAME}_after_open (void);
61static void gld${EMULATION_NAME}_before_allocation (void);
62static bfd_boolean gld${EMULATION_NAME}_place_orphan (asection *s);
63static void gld${EMULATION_NAME}_layout_sections_again (void);
64static void gld${EMULATION_NAME}_finish (void) ATTRIBUTE_UNUSED;
65
66EOF
67
68if [ "x${USE_LIBPATH}" = xyes ] ; then
69  case ${target} in
70    *-*-linux-* | *-*-k*bsd*-*)
71  cat >>e${EMULATION_NAME}.c <<EOF
72#ifdef HAVE_GLOB
73#include <glob.h>
74#endif
75EOF
76    ;;
77  esac
78fi
79
80case ${target} in
81  *-*-openbsd*)
82    cat >>e${EMULATION_NAME}.c <<EOF
83#include <sys/types.h>
84#include <dirent.h>
85
86/* Search a directory for a .so file.  */
87
88static char * gld${EMULATION_NAME}_search_dir_needed (const char *dirlist, const char *filename);
89
90static char * gld${EMULATION_NAME}_search_dir (const char *dirname,
91  const char *filename, int req_maj, int req_min);
92
93static char * gld${EMULATION_NAME}_split_lib_name (char *name, int *pmaj,
94  int *pmin);
95
96/* THIS FUNCTION MODIFIES THE name ARGUMENT string */
97static char *
98gld${EMULATION_NAME}_split_lib_name (name, pmaj, pmin)
99  char *name;
100  int *pmaj, *pmin;
101{
102  char*eptr, *lib = name;
103  char *s;
104  int found_so = 0;
105
106  *pmaj = -1;
107  *pmin = -1;
108
109  if (strncmp(lib, "lib", 3) == 0)
110    lib += 3;
111
112  s = lib;
113  while (found_so == 0)
114    {
115      s = strstr(s, ".so");
116
117      /* if .so not found, return not found, invalid lib name */
118      if (s == NULL)
119	{
120	  return NULL;
121	}
122
123      /* if .so is at end of string, fine return with pmaj/pmin -1 */
124      if (s[3] == '\0')
125	{
126	  *s = '\0';
127	  return lib;
128	}
129
130      if (s[3] == '.')
131	{
132	  *s = '\0';
133	  found_so = 1;
134	}
135      /* skip over the ".so" */
136      s += 3;
137    }
138
139
140  /* lib[name].so.[M].[N] */
141  /*  s          ^        */
142  s += 1;
143
144  /* lib[name].so.[M].[N] */
145  /*  s           ^       */
146  *pmaj = strtoul (s, &eptr, 10);
147
148  /* lib[name].so.[M]X... */
149  /*  eptr           ^    */
150  if (*eptr != '.' || s == eptr)
151    return NULL; /* invalid, must have minor */
152
153  s = eptr+1;
154
155  /* lib[name].so.[M].[N]  */
156  /*  s               ^    */
157  *pmin = strtoul (s, &eptr, 10);
158
159  /* lib[name].so.[M].[N]  */
160  /*  eptr               ^ */
161  if (*eptr != '\0' || s == eptr)
162    return NULL;  /* minor must be last field of library */
163
164  return lib;
165}
166
167static char *
168gld${EMULATION_NAME}_search_dir_needed (dirlist, filename)
169    const char *dirlist;
170    const char *filename;
171{
172  char *dlist, *dlist_alloc, *dir;
173  char *fnam, *fnam_alloc, *lib;
174  char *found = NULL;
175  int maj = -1, min = -1;
176
177  dlist_alloc = dlist = xstrdup(dirlist);
178  fnam_alloc = fnam = xstrdup(filename);
179
180  lib = gld${EMULATION_NAME}_split_lib_name(fnam, &maj, &min);
181
182  while (lib != NULL && found == NULL)
183    {
184      dir = strsep(&dlist, ":");
185      if (dir == NULL)
186	break;
187      if (*dir == '\0')
188	continue; /* skip dirlist of ...::... */
189      found = gld${EMULATION_NAME}_search_dir(dir, lib, maj, min);
190    }
191
192  free(dlist_alloc);
193  free(fnam_alloc);
194  return found;
195}
196
197
198static char *
199gld${EMULATION_NAME}_search_dir (dirname, filename, req_maj, req_min)
200     const char *dirname;
201     const char *filename;
202     int req_maj, req_min;
203{
204  const char *dot;
205  unsigned int len;
206  char *found;
207  int max_maj, max_min;
208  DIR *dir;
209  struct dirent *entry;
210  unsigned int dirnamelen;
211  char *full_path;
212  int statval;
213  struct stat st;
214
215  dot = strchr (filename, '.');
216  len = strlen (filename);
217  found = NULL;
218  max_maj = max_min = 0;
219
220  dir = opendir (dirname);
221  if (dir == NULL)
222    return NULL;
223  dirnamelen = strlen (dirname);
224
225  while ((entry = readdir (dir)) != NULL)
226    {
227      const char *s;
228      char *eptr, *eptr1;
229      int found_maj, found_min;
230
231      if (strncmp (entry->d_name, "lib", 3) != 0
232	  || strncmp (entry->d_name + 3, filename, len) != 0)
233	continue;
234
235      /* We accept libfoo.so without a version number, even though the
236         native linker does not.  This is more convenient for packages
237         which just generate .so files for shared libraries, as on ELF
238         systems.  */
239      if (strncmp (entry->d_name + 3 + len, ".so", 3) != 0)
240	continue;
241
242      if (entry->d_name[6 + len] == '\0')
243	;
244      else if (entry->d_name[6 + len] == '.'
245	       && ISDIGIT ((unsigned char) entry->d_name[7 + len]))
246	;
247      else
248	continue;
249
250      for (s = entry->d_name + 6 + len; *s != '\0'; s++)
251	if (*s != '.' && ! ISDIGIT ((unsigned char) *s))
252	  break;
253      if (*s != '\0')
254	continue;
255
256      /* We've found a .so file.  Work out the major and minor
257	 version numbers.  */
258      found_maj = -1;
259      found_min = -1;
260
261      /* do allow libN.so */
262      if (entry->d_name[6 + len] == '.') {
263	found_maj = strtoul (entry->d_name + 7 + len, &eptr, 10);
264
265	/* do not support libN.so. or libN.so.X */
266	if (*eptr != '.' || ((entry->d_name + 3 + len) == eptr))
267	  continue;
268
269	found_min = strtoul (eptr+1, &eptr1, 10);
270
271	/* do not support libN.so.X. or libN.so.X.Y.[anything] */
272	if (*eptr1 != '\0' || (eptr+1 == eptr1))
273	  continue;
274      }
275
276      /* Make sure the file really exists (ignore broken symlinks).  */
277      full_path = xmalloc (dirnamelen + 1 + strlen (entry->d_name) + 1);
278      sprintf (full_path, "%s/%s", dirname, entry->d_name);
279      statval = stat (full_path, &st);
280      free (full_path);
281      if (statval != 0)
282	continue;
283
284      /* We've found a match for the name we are searching for.  See
285	 if this is the version we should use.  */
286      if (((req_maj == -1) && (found == NULL
287	    || (found_maj > max_maj)
288	    || (found_maj == max_maj && (found_min > max_min))))
289	  || ((found_maj == req_maj) && (found_min >= req_min)
290	    && (found_min > max_min)))
291	{
292	  if (found != NULL)
293	    free (found);
294	  found = (char *) xmalloc (dirnamelen + strlen (entry->d_name) + 2);
295	  sprintf (found, "%s/%s", dirname, entry->d_name);
296	  max_maj = found_maj;
297	  max_min = found_min;
298	}
299    }
300
301  closedir (dir);
302
303  return found;
304}
305
306EOF
307    ;;
308esac
309cat >>e${EMULATION_NAME}.c <<EOF
310EOF
311# Import any needed special functions and/or overrides.
312#
313if test -n "$EXTRA_EM_FILE" ; then
314. ${srcdir}/emultempl/${EXTRA_EM_FILE}.em
315fi
316
317# Functions in this file can be overridden by setting the LDEMUL_* shell
318# variables.  If the name of the overriding function is the same as is
319# defined in this file, then don't output this file's version.
320# If a different overriding name is given then output the standard function
321# as presumably it is called from the overriding function.
322#
323if test x"$LDEMUL_BEFORE_PARSE" != xgld"$EMULATION_NAME"_before_parse; then
324cat >>e${EMULATION_NAME}.c <<EOF
325
326static void
327gld${EMULATION_NAME}_before_parse (void)
328{
329  ldfile_set_output_arch ("${OUTPUT_ARCH}", bfd_arch_`echo ${ARCH} | sed -e 's/:.*//'`);
330  config.dynamic_link = ${DYNAMIC_LINK-TRUE};
331  config.has_shared = `if test -n "$GENERATE_SHLIB_SCRIPT" ; then echo TRUE ; else echo FALSE ; fi`;
332}
333
334EOF
335fi
336
337if test x"$LDEMUL_RECOGNIZED_FILE" != xgld"${EMULATION_NAME}"_load_symbols; then
338cat >>e${EMULATION_NAME}.c <<EOF
339/* Handle as_needed DT_NEEDED.  */
340
341static bfd_boolean
342gld${EMULATION_NAME}_load_symbols (lang_input_statement_type *entry)
343{
344  int class = 0;
345
346  /* Tell the ELF linker that we don't want the output file to have a
347     DT_NEEDED entry for this file, unless it is used to resolve
348     references in a regular object.  */
349  if (entry->as_needed)
350    class = DYN_AS_NEEDED;
351
352  /* Tell the ELF linker that we don't want the output file to have a
353     DT_NEEDED entry for any dynamic library in DT_NEEDED tags from
354     this file at all.  */
355  if (!entry->add_needed)
356    class |= DYN_NO_ADD_NEEDED;
357
358  if (entry->just_syms_flag
359      && (bfd_get_file_flags (entry->the_bfd) & DYNAMIC) != 0)
360    einfo (_("%P%F: --just-symbols may not be used on DSO: %B\n"),
361	   entry->the_bfd);
362
363  if (!class
364      || (bfd_get_file_flags (entry->the_bfd) & DYNAMIC) == 0)
365    return FALSE;
366
367  bfd_elf_set_dyn_lib_class (entry->the_bfd, class);
368
369  /* Continue on with normal load_symbols processing.  */
370  return FALSE;
371}
372EOF
373fi
374
375cat >>e${EMULATION_NAME}.c <<EOF
376
377/* These variables are required to pass information back and forth
378   between after_open and check_needed and stat_needed and vercheck.  */
379
380static struct bfd_link_needed_list *global_needed;
381static struct stat global_stat;
382static lang_input_statement_type *global_found;
383static struct bfd_link_needed_list *global_vercheck_needed;
384static bfd_boolean global_vercheck_failed;
385
386
387/* On Linux, it's possible to have different versions of the same
388   shared library linked against different versions of libc.  The
389   dynamic linker somehow tags which libc version to use in
390   /etc/ld.so.cache, and, based on the libc that it sees in the
391   executable, chooses which version of the shared library to use.
392
393   We try to do a similar check here by checking whether this shared
394   library needs any other shared libraries which may conflict with
395   libraries we have already included in the link.  If it does, we
396   skip it, and try to find another shared library farther on down the
397   link path.
398
399   This is called via lang_for_each_input_file.
400   GLOBAL_VERCHECK_NEEDED is the list of objects needed by the object
401   which we are checking.  This sets GLOBAL_VERCHECK_FAILED if we find
402   a conflicting version.  */
403
404static void
405gld${EMULATION_NAME}_vercheck (lang_input_statement_type *s)
406{
407  const char *soname;
408  struct bfd_link_needed_list *l;
409
410  if (global_vercheck_failed)
411    return;
412  if (s->the_bfd == NULL
413      || (bfd_get_file_flags (s->the_bfd) & DYNAMIC) == 0)
414    return;
415
416  soname = bfd_elf_get_dt_soname (s->the_bfd);
417  if (soname == NULL)
418    soname = lbasename (bfd_get_filename (s->the_bfd));
419
420  for (l = global_vercheck_needed; l != NULL; l = l->next)
421    {
422      const char *suffix;
423
424      if (strcmp (soname, l->name) == 0)
425	{
426	  /* Probably can't happen, but it's an easy check.  */
427	  continue;
428	}
429
430      if (strchr (l->name, '/') != NULL)
431	continue;
432
433      suffix = strstr (l->name, ".so.");
434      if (suffix == NULL)
435	continue;
436
437      suffix += sizeof ".so." - 1;
438
439      if (strncmp (soname, l->name, suffix - l->name) == 0)
440	{
441	  /* Here we know that S is a dynamic object FOO.SO.VER1, and
442	     the object we are considering needs a dynamic object
443	     FOO.SO.VER2, and VER1 and VER2 are different.  This
444	     appears to be a version mismatch, so we tell the caller
445	     to try a different version of this library.  */
446	  global_vercheck_failed = TRUE;
447	  return;
448	}
449    }
450}
451
452
453/* See if an input file matches a DT_NEEDED entry by running stat on
454   the file.  */
455
456static void
457gld${EMULATION_NAME}_stat_needed (lang_input_statement_type *s)
458{
459  struct stat st;
460  const char *suffix;
461  const char *soname;
462
463  if (global_found != NULL)
464    return;
465  if (s->the_bfd == NULL)
466    return;
467
468  /* If this input file was an as-needed entry, and wasn't found to be
469     needed at the stage it was linked, then don't say we have loaded it.  */
470  if ((bfd_elf_get_dyn_lib_class (s->the_bfd) & DYN_AS_NEEDED) != 0)
471    return;
472
473  if (bfd_stat (s->the_bfd, &st) != 0)
474    {
475      einfo ("%P:%B: bfd_stat failed: %E\n", s->the_bfd);
476      return;
477    }
478
479  /* Some operating systems, e.g. Windows, do not provide a meaningful
480     st_ino; they always set it to zero.  (Windows does provide a
481     meaningful st_dev.)  Do not indicate a duplicate library in that
482     case.  While there is no guarantee that a system that provides
483     meaningful inode numbers will never set st_ino to zero, this is
484     merely an optimization, so we do not need to worry about false
485     negatives.  */
486  if (st.st_dev == global_stat.st_dev
487      && st.st_ino == global_stat.st_ino
488      && st.st_ino != 0)
489    {
490      global_found = s;
491      return;
492    }
493
494  /* We issue a warning if it looks like we are including two
495     different versions of the same shared library.  For example,
496     there may be a problem if -lc picks up libc.so.6 but some other
497     shared library has a DT_NEEDED entry of libc.so.5.  This is a
498     heuristic test, and it will only work if the name looks like
499     NAME.so.VERSION.  FIXME: Depending on file names is error-prone.
500     If we really want to issue warnings about mixing version numbers
501     of shared libraries, we need to find a better way.  */
502
503  if (strchr (global_needed->name, '/') != NULL)
504    return;
505  suffix = strstr (global_needed->name, ".so.");
506  if (suffix == NULL)
507    return;
508  suffix += sizeof ".so." - 1;
509
510  soname = bfd_elf_get_dt_soname (s->the_bfd);
511  if (soname == NULL)
512    soname = lbasename (s->filename);
513
514  if (strncmp (soname, global_needed->name, suffix - global_needed->name) == 0)
515    einfo ("%P: warning: %s, needed by %B, may conflict with %s\n",
516	   global_needed->name, global_needed->by, soname);
517}
518
519struct dt_needed
520{
521  bfd *by;
522  const char *name;
523};
524
525/* This function is called for each possible name for a dynamic object
526   named by a DT_NEEDED entry.  The FORCE parameter indicates whether
527   to skip the check for a conflicting version.  */
528
529static bfd_boolean
530gld${EMULATION_NAME}_try_needed (struct dt_needed *needed,
531				 int force)
532{
533  bfd *abfd;
534  const char *name = needed->name;
535  char *soname;
536  int class;
537
538  abfd = bfd_openr (name, bfd_get_target (output_bfd));
539  if (abfd == NULL)
540    return FALSE;
541  if (! bfd_check_format (abfd, bfd_object))
542    {
543      bfd_close (abfd);
544      return FALSE;
545    }
546  if ((bfd_get_file_flags (abfd) & DYNAMIC) == 0)
547    {
548      bfd_close (abfd);
549      return FALSE;
550    }
551
552  /* For DT_NEEDED, they have to match.  */
553  if (abfd->xvec != output_bfd->xvec)
554    {
555      bfd_close (abfd);
556      return FALSE;
557    }
558
559  /* Check whether this object would include any conflicting library
560     versions.  If FORCE is set, then we skip this check; we use this
561     the second time around, if we couldn't find any compatible
562     instance of the shared library.  */
563
564  if (! force)
565    {
566      struct bfd_link_needed_list *needed;
567
568      if (! bfd_elf_get_bfd_needed_list (abfd, &needed))
569	einfo ("%F%P:%B: bfd_elf_get_bfd_needed_list failed: %E\n", abfd);
570
571      if (needed != NULL)
572	{
573	  global_vercheck_needed = needed;
574	  global_vercheck_failed = FALSE;
575	  lang_for_each_input_file (gld${EMULATION_NAME}_vercheck);
576	  if (global_vercheck_failed)
577	    {
578	      bfd_close (abfd);
579	      /* Return FALSE to force the caller to move on to try
580		 another file on the search path.  */
581	      return FALSE;
582	    }
583
584	  /* But wait!  It gets much worse.  On Linux, if a shared
585	     library does not use libc at all, we are supposed to skip
586	     it the first time around in case we encounter a shared
587	     library later on with the same name which does use the
588	     version of libc that we want.  This is much too horrible
589	     to use on any system other than Linux.  */
590
591EOF
592case ${target} in
593  *-*-linux-* | *-*-k*bsd*-*)
594    cat >>e${EMULATION_NAME}.c <<EOF
595	  {
596	    struct bfd_link_needed_list *l;
597
598	    for (l = needed; l != NULL; l = l->next)
599	      if (strncmp (l->name, "libc.so", 7) == 0)
600		break;
601	    if (l == NULL)
602	      {
603		bfd_close (abfd);
604		return FALSE;
605	      }
606	  }
607
608EOF
609    ;;
610esac
611cat >>e${EMULATION_NAME}.c <<EOF
612	}
613    }
614
615  /* We've found a dynamic object matching the DT_NEEDED entry.  */
616
617  /* We have already checked that there is no other input file of the
618     same name.  We must now check again that we are not including the
619     same file twice.  We need to do this because on many systems
620     libc.so is a symlink to, e.g., libc.so.1.  The SONAME entry will
621     reference libc.so.1.  If we have already included libc.so, we
622     don't want to include libc.so.1 if they are the same file, and we
623     can only check that using stat.  */
624
625  if (bfd_stat (abfd, &global_stat) != 0)
626    einfo ("%F%P:%B: bfd_stat failed: %E\n", abfd);
627
628  /* First strip off everything before the last '/'.  */
629  soname = xstrdup (lbasename (abfd->filename));
630
631  if (trace_file_tries)
632    info_msg (_("found %s at %s\n"), soname, name);
633
634  global_found = NULL;
635  lang_for_each_input_file (gld${EMULATION_NAME}_stat_needed);
636  if (global_found != NULL)
637    {
638      /* Return TRUE to indicate that we found the file, even though
639	 we aren't going to do anything with it.  */
640      free (soname);
641      return TRUE;
642    }
643
644  /* Specify the soname to use.  */
645  bfd_elf_set_dt_needed_name (abfd, soname);
646
647  /* Tell the ELF linker that we don't want the output file to have a
648     DT_NEEDED entry for this file, unless it is used to resolve
649     references in a regular object.  */
650  class = DYN_DT_NEEDED;
651
652  /* Tell the ELF linker that we don't want the output file to have a
653     DT_NEEDED entry for this file at all if the entry is from a file
654     with DYN_NO_ADD_NEEDED.  */
655  if (needed->by != NULL
656      && (bfd_elf_get_dyn_lib_class (needed->by) & DYN_NO_ADD_NEEDED) != 0)
657    class |= DYN_NO_NEEDED | DYN_NO_ADD_NEEDED;
658
659  bfd_elf_set_dyn_lib_class (abfd, class);
660
661  /* Add this file into the symbol table.  */
662  if (! bfd_link_add_symbols (abfd, &link_info))
663    einfo ("%F%B: could not read symbols: %E\n", abfd);
664
665  return TRUE;
666}
667
668
669/* Search for a needed file in a path.  */
670
671static bfd_boolean
672gld${EMULATION_NAME}_search_needed (const char *path,
673				    struct dt_needed *n, int force)
674{
675  const char *s;
676  const char *name = n->name;
677  size_t len;
678  struct dt_needed needed;
679
680  if (name[0] == '/')
681    return gld${EMULATION_NAME}_try_needed (n, force);
682
683  if (path == NULL || *path == '\0')
684    return FALSE;
685
686  needed.by = n->by;
687EOF
688case ${target} in
689  *-*-openbsd*)
690    cat >>e${EMULATION_NAME}.c <<EOF
691
692  {
693    char *found;
694    if ((found = gld${EMULATION_NAME}_search_dir_needed(path, name)) != NULL) {
695      needed.name = found;
696      if (gld${EMULATION_NAME}_try_needed (&needed, force)) {
697	return TRUE;
698      }
699      free(found);
700    }
701  }
702
703EOF
704esac
705cat >>e${EMULATION_NAME}.c <<EOF
706  needed.name = n->name;
707
708  len = strlen (name);
709  while (1)
710    {
711      char *filename, *sset;
712
713      s = strchr (path, ':');
714      if (s == NULL)
715	s = path + strlen (path);
716
717      filename = (char *) xmalloc (s - path + len + 2);
718      if (s == path)
719	sset = filename;
720      else
721	{
722	  memcpy (filename, path, s - path);
723	  filename[s - path] = '/';
724	  sset = filename + (s - path) + 1;
725	}
726      strcpy (sset, name);
727
728      needed.name = filename;
729      if (gld${EMULATION_NAME}_try_needed (&needed, force))
730	return TRUE;
731
732      free (filename);
733
734      if (*s == '\0')
735	break;
736      path = s + 1;
737    }
738
739  return FALSE;
740}
741
742EOF
743if [ "x${USE_LIBPATH}" = xyes ] ; then
744  cat >>e${EMULATION_NAME}.c <<EOF
745
746/* Add the sysroot to every entry in a colon-separated path.  */
747
748static char *
749gld${EMULATION_NAME}_add_sysroot (const char *path)
750{
751  int len, colons, i;
752  char *ret, *p;
753
754  len = strlen (path);
755  colons = 0;
756  i = 0;
757  while (path[i])
758    if (path[i++] == ':')
759      colons++;
760
761  if (path[i])
762    colons++;
763
764  len = len + (colons + 1) * strlen (ld_sysroot);
765  ret = xmalloc (len + 1);
766  strcpy (ret, ld_sysroot);
767  p = ret + strlen (ret);
768  i = 0;
769  while (path[i])
770    if (path[i] == ':')
771      {
772	*p++ = path[i++];
773	strcpy (p, ld_sysroot);
774	p = p + strlen (p);
775      }
776    else
777      *p++ = path[i++];
778
779  *p = 0;
780  return ret;
781}
782
783EOF
784  case ${target} in
785    *-*-freebsd* | *-*-dragonfly*)
786      cat >>e${EMULATION_NAME}.c <<EOF
787/* Read the system search path the FreeBSD way rather than the Linux way.  */
788#ifdef HAVE_ELF_HINTS_H
789#include <elf-hints.h>
790#else
791#include "elf-hints-local.h"
792#endif
793
794static bfd_boolean
795gld${EMULATION_NAME}_check_ld_elf_hints (const char *name, int force)
796{
797  static bfd_boolean initialized;
798  static char *ld_elf_hints;
799  struct dt_needed needed;
800
801  if (!initialized)
802    {
803      FILE *f;
804      char *tmppath;
805
806      tmppath = concat (ld_sysroot, _PATH_ELF_HINTS, NULL);
807      f = fopen (tmppath, FOPEN_RB);
808      free (tmppath);
809      if (f != NULL)
810	{
811	  struct elfhints_hdr hdr;
812
813	  if (fread (&hdr, 1, sizeof (hdr), f) == sizeof (hdr)
814	      && hdr.magic == ELFHINTS_MAGIC
815	      && hdr.version == 1)
816	    {
817	      if (fseek (f, hdr.strtab + hdr.dirlist, SEEK_SET) != -1)
818		{
819		  char *b;
820
821		  b = xmalloc (hdr.dirlistlen + 1);
822		  if (fread (b, 1, hdr.dirlistlen + 1, f) ==
823		      hdr.dirlistlen + 1)
824		    ld_elf_hints = gld${EMULATION_NAME}_add_sysroot (b);
825
826		  free (b);
827		}
828	    }
829	  fclose (f);
830	}
831
832      initialized = TRUE;
833    }
834
835  if (ld_elf_hints == NULL)
836    return FALSE;
837
838  needed.by = NULL;
839  needed.name = name;
840  return gld${EMULATION_NAME}_search_needed (ld_elf_hints, & needed,
841					     force);
842}
843EOF
844    # FreeBSD
845    ;;
846
847    *-*-linux-* | *-*-k*bsd*-*)
848      cat >>e${EMULATION_NAME}.c <<EOF
849/* For a native linker, check the file /etc/ld.so.conf for directories
850   in which we may find shared libraries.  /etc/ld.so.conf is really
851   only meaningful on Linux.  */
852
853struct gld${EMULATION_NAME}_ld_so_conf
854{
855  char *path;
856  size_t len, alloc;
857};
858
859static bfd_boolean
860gld${EMULATION_NAME}_parse_ld_so_conf
861     (struct gld${EMULATION_NAME}_ld_so_conf *info, const char *filename);
862
863static void
864gld${EMULATION_NAME}_parse_ld_so_conf_include
865     (struct gld${EMULATION_NAME}_ld_so_conf *info, const char *filename,
866      const char *pattern)
867{
868  char *newp = NULL;
869#ifdef HAVE_GLOB
870  glob_t gl;
871#endif
872
873  if (pattern[0] != '/')
874    {
875      char *p = strrchr (filename, '/');
876      size_t patlen = strlen (pattern) + 1;
877
878      newp = xmalloc (p - filename + 1 + patlen);
879      memcpy (newp, filename, p - filename + 1);
880      memcpy (newp + (p - filename + 1), pattern, patlen);
881      pattern = newp;
882    }
883
884#ifdef HAVE_GLOB
885  if (glob (pattern, 0, NULL, &gl) == 0)
886    {
887      size_t i;
888
889      for (i = 0; i < gl.gl_pathc; ++i)
890	gld${EMULATION_NAME}_parse_ld_so_conf (info, gl.gl_pathv[i]);
891      globfree (&gl);
892    }
893#else
894  /* If we do not have glob, treat the pattern as a literal filename.  */
895  gld${EMULATION_NAME}_parse_ld_so_conf (info, pattern);
896#endif
897
898  if (newp)
899    free (newp);
900}
901
902static bfd_boolean
903gld${EMULATION_NAME}_parse_ld_so_conf
904     (struct gld${EMULATION_NAME}_ld_so_conf *info, const char *filename)
905{
906  FILE *f = fopen (filename, FOPEN_RT);
907  char *line;
908  size_t linelen;
909
910  if (f == NULL)
911    return FALSE;
912
913  linelen = 256;
914  line = xmalloc (linelen);
915  do
916    {
917      char *p = line, *q;
918
919      /* Normally this would use getline(3), but we need to be portable.  */
920      while ((q = fgets (p, linelen - (p - line), f)) != NULL
921	     && strlen (q) == linelen - (p - line) - 1
922	     && line[linelen - 2] != '\n')
923	{
924	  line = xrealloc (line, 2 * linelen);
925	  p = line + linelen - 1;
926	  linelen += linelen;
927	}
928
929      if (q == NULL && p == line)
930	break;
931
932      p = strchr (line, '\n');
933      if (p)
934	*p = '\0';
935
936      /* Because the file format does not know any form of quoting we
937	 can search forward for the next '#' character and if found
938	 make it terminating the line.  */
939      p = strchr (line, '#');
940      if (p)
941	*p = '\0';
942
943      /* Remove leading whitespace.  NUL is no whitespace character.  */
944      p = line;
945      while (*p == ' ' || *p == '\f' || *p == '\r' || *p == '\t' || *p == '\v')
946	++p;
947
948      /* If the line is blank it is ignored.  */
949      if (p[0] == '\0')
950	continue;
951
952      if (!strncmp (p, "include", 7) && (p[7] == ' ' || p[7] == '\t'))
953	{
954	  char *dir, c;
955	  p += 8;
956	  do
957	    {
958	      while (*p == ' ' || *p == '\t')
959		++p;
960
961	      if (*p == '\0')
962		break;
963
964	      dir = p;
965
966	      while (*p != ' ' && *p != '\t' && *p)
967		++p;
968
969	      c = *p;
970	      *p++ = '\0';
971	      if (dir[0] != '\0')
972		gld${EMULATION_NAME}_parse_ld_so_conf_include (info, filename,
973							       dir);
974	    }
975	  while (c != '\0');
976	}
977      else
978	{
979	  char *dir = p;
980	  while (*p && *p != '=' && *p != ' ' && *p != '\t' && *p != '\f'
981		 && *p != '\r' && *p != '\v')
982	    ++p;
983
984	  while (p != dir && p[-1] == '/')
985	    --p;
986	  if (info->path == NULL)
987	    {
988	      info->alloc = p - dir + 1 + 256;
989	      info->path = xmalloc (info->alloc);
990	      info->len = 0;
991	    }
992	  else
993	    {
994	      if (info->len + 1 + (p - dir) >= info->alloc)
995		{
996		  info->alloc += p - dir + 256;
997		  info->path = xrealloc (info->path, info->alloc);
998		}
999	      info->path[info->len++] = ':';
1000	    }
1001	  memcpy (info->path + info->len, dir, p - dir);
1002	  info->len += p - dir;
1003	  info->path[info->len] = '\0';
1004	}
1005    }
1006  while (! feof (f));
1007  free (line);
1008  fclose (f);
1009  return TRUE;
1010}
1011
1012static bfd_boolean
1013gld${EMULATION_NAME}_check_ld_so_conf (const char *name, int force)
1014{
1015  static bfd_boolean initialized;
1016  static char *ld_so_conf;
1017  struct dt_needed needed;
1018
1019  if (! initialized)
1020    {
1021      char *tmppath;
1022      struct gld${EMULATION_NAME}_ld_so_conf info;
1023
1024      info.path = NULL;
1025      info.len = info.alloc = 0;
1026      tmppath = concat (ld_sysroot, "${prefix}/etc/ld.so.conf", NULL);
1027      if (!gld${EMULATION_NAME}_parse_ld_so_conf (&info, tmppath))
1028	{
1029	  free (tmppath);
1030	  tmppath = concat (ld_sysroot, "/etc/ld.so.conf", NULL);
1031	  gld${EMULATION_NAME}_parse_ld_so_conf (&info, tmppath);
1032	}
1033      free (tmppath);
1034
1035      if (info.path)
1036	{
1037	  char *d = gld${EMULATION_NAME}_add_sysroot (info.path);
1038	  free (info.path);
1039	  ld_so_conf = d;
1040	}
1041      initialized = TRUE;
1042    }
1043
1044  if (ld_so_conf == NULL)
1045    return FALSE;
1046
1047
1048  needed.by = NULL;
1049  needed.name = name;
1050  return gld${EMULATION_NAME}_search_needed (ld_so_conf, &needed, force);
1051}
1052
1053EOF
1054    # Linux
1055    ;;
1056  esac
1057fi
1058cat >>e${EMULATION_NAME}.c <<EOF
1059
1060/* See if an input file matches a DT_NEEDED entry by name.  */
1061
1062static void
1063gld${EMULATION_NAME}_check_needed (lang_input_statement_type *s)
1064{
1065  const char *soname;
1066
1067  /* Stop looking if we've found a loaded lib.  */
1068  if (global_found != NULL
1069      && (bfd_elf_get_dyn_lib_class (global_found->the_bfd)
1070	  & DYN_AS_NEEDED) == 0)
1071    return;
1072
1073  if (s->filename == NULL || s->the_bfd == NULL)
1074    return;
1075
1076  /* Don't look for a second non-loaded as-needed lib.  */
1077  if (global_found != NULL
1078      && (bfd_elf_get_dyn_lib_class (s->the_bfd) & DYN_AS_NEEDED) != 0)
1079    return;
1080
1081  if (strcmp (s->filename, global_needed->name) == 0)
1082    {
1083      global_found = s;
1084      return;
1085    }
1086
1087  if (s->search_dirs_flag)
1088    {
1089      const char *f = strrchr (s->filename, '/');
1090      if (f != NULL
1091	  && strcmp (f + 1, global_needed->name) == 0)
1092	{
1093	  global_found = s;
1094	  return;
1095	}
1096    }
1097
1098  soname = bfd_elf_get_dt_soname (s->the_bfd);
1099  if (soname != NULL
1100      && strcmp (soname, global_needed->name) == 0)
1101    {
1102      global_found = s;
1103      return;
1104    }
1105}
1106
1107EOF
1108
1109if test x"$LDEMUL_AFTER_OPEN" != xgld"$EMULATION_NAME"_after_open; then
1110cat >>e${EMULATION_NAME}.c <<EOF
1111
1112static void
1113gld${EMULATION_NAME}_force_readonly(lang_input_statement_type *s)
1114{
1115  asection *sec;
1116
1117  if (s->the_bfd == NULL)
1118	  return;
1119
1120  sec = bfd_get_section_by_name (s->the_bfd, ".ctors");
1121  if (sec)
1122    sec->flags |= SEC_READONLY;
1123  sec = bfd_get_section_by_name (s->the_bfd, ".dtors");
1124  if (sec)
1125    sec->flags |= SEC_READONLY;
1126}
1127
1128/* This is called after all the input files have been opened.  */
1129
1130static void
1131gld${EMULATION_NAME}_after_open (void)
1132{
1133  struct bfd_link_needed_list *needed, *l;
1134
1135  /* We only need to worry about this when doing a final link.  */
1136  if (link_info.relocatable || !link_info.executable)
1137    return;
1138
1139  /* If we don't have a .dynamic section, we have no relocations, and
1140     we can make .got, .ctors and .dtors read-only.  This will make
1141     the segment containing those sections to be read-only in static
1142     executables.  */
1143  if (link_info.hash->type == bfd_link_elf_hash_table
1144      && !elf_hash_table (&link_info)->dynamic_sections_created)
1145    {
1146      bfd *dynobj = elf_hash_table (&link_info)->dynobj;
1147
1148      if (dynobj != NULL)
1149	{
1150	  asection *sec;
1151
1152	  sec = bfd_get_section_by_name (dynobj, ".got");
1153	  if (sec)
1154	    sec->flags |= SEC_READONLY;
1155	}
1156
1157      lang_for_each_input_file (gld${EMULATION_NAME}_force_readonly);
1158    }
1159
1160  /* Get the list of files which appear in DT_NEEDED entries in
1161     dynamic objects included in the link (often there will be none).
1162     For each such file, we want to track down the corresponding
1163     library, and include the symbol table in the link.  This is what
1164     the runtime dynamic linker will do.  Tracking the files down here
1165     permits one dynamic object to include another without requiring
1166     special action by the person doing the link.  Note that the
1167     needed list can actually grow while we are stepping through this
1168     loop.  */
1169  needed = bfd_elf_get_needed_list (output_bfd, &link_info);
1170  for (l = needed; l != NULL; l = l->next)
1171    {
1172      struct bfd_link_needed_list *ll;
1173      struct dt_needed n, nn;
1174      int force;
1175
1176      /* If the lib that needs this one was --as-needed and wasn't
1177	 found to be needed, then this lib isn't needed either.  */
1178      if (l->by != NULL
1179	  && (bfd_elf_get_dyn_lib_class (l->by) & DYN_AS_NEEDED) != 0)
1180	continue;
1181
1182      /* If we've already seen this file, skip it.  */
1183      for (ll = needed; ll != l; ll = ll->next)
1184	if ((ll->by == NULL
1185	     || (bfd_elf_get_dyn_lib_class (ll->by) & DYN_AS_NEEDED) == 0)
1186	    && strcmp (ll->name, l->name) == 0)
1187	  break;
1188      if (ll != l)
1189	continue;
1190
1191      /* See if this file was included in the link explicitly.  */
1192      global_needed = l;
1193      global_found = NULL;
1194      lang_for_each_input_file (gld${EMULATION_NAME}_check_needed);
1195      if (global_found != NULL
1196	  && (bfd_elf_get_dyn_lib_class (global_found->the_bfd)
1197	      & DYN_AS_NEEDED) == 0)
1198	continue;
1199
1200      n.by = l->by;
1201      n.name = l->name;
1202      nn.by = l->by;
1203      if (trace_file_tries)
1204	info_msg (_("%s needed by %B\n"), l->name, l->by);
1205
1206      /* As-needed libs specified on the command line (or linker script)
1207	 take priority over libs found in search dirs.  */
1208      if (global_found != NULL)
1209	{
1210	  nn.name = global_found->filename;
1211	  if (gld${EMULATION_NAME}_try_needed (&nn, TRUE))
1212	    continue;
1213	}
1214
1215      /* We need to find this file and include the symbol table.  We
1216	 want to search for the file in the same way that the dynamic
1217	 linker will search.  That means that we want to use
1218	 rpath_link, rpath, then the environment variable
1219	 LD_LIBRARY_PATH (native only), then the DT_RPATH/DT_RUNPATH
1220	 entries (native only), then the linker script LIB_SEARCH_DIRS.
1221	 We look at the -L arguments to build the search path.
1222
1223	 We search twice.  The first time, we skip objects which may
1224	 introduce version mismatches.  The second time, we force
1225	 their use.  See gld${EMULATION_NAME}_vercheck comment.  */
1226      for (force = 0; force < 2; force++)
1227	{
1228	  size_t len;
1229	  search_dirs_type *search;
1230EOF
1231if [ "x${NATIVE}" = xyes ] ; then
1232cat >>e${EMULATION_NAME}.c <<EOF
1233	  char *lib_path;
1234EOF
1235fi
1236if [ "x${USE_LIBPATH}" = xyes ] ; then
1237cat >>e${EMULATION_NAME}.c <<EOF
1238	  struct bfd_link_needed_list *rp;
1239	  int found;
1240EOF
1241fi
1242cat >>e${EMULATION_NAME}.c <<EOF
1243
1244	  if (gld${EMULATION_NAME}_search_needed (command_line.rpath_link,
1245						  &n, force))
1246	    break;
1247EOF
1248if [ "x${USE_LIBPATH}" = xyes ] ; then
1249cat >>e${EMULATION_NAME}.c <<EOF
1250	  if (gld${EMULATION_NAME}_search_needed (command_line.rpath,
1251						  &n, force))
1252	    break;
1253EOF
1254fi
1255if [ "x${NATIVE}" = xyes ] ; then
1256cat >>e${EMULATION_NAME}.c <<EOF
1257	  if (getenv ("LD_RUN_PATH") != NULL
1258	      && command_line.rpath_link == NULL
1259	      && command_line.rpath == NULL)
1260	    {
1261	      lib_path = getenv ("LD_RUN_PATH");
1262	      if (gld${EMULATION_NAME}_search_needed (lib_path, &n, force))
1263		break;
1264	    }
1265
1266	  len = strlen(search_head->name);
1267	  lib_path = xstrdup(search_head->name);
1268	  for (search = search_head->next; search != NULL;
1269	       search = search->next)
1270	    {
1271	      size_t nlen;
1272
1273	      nlen = strlen(search->name);
1274	      lib_path = xrealloc(lib_path, len + nlen + 2);
1275	      lib_path[len] = ':';
1276	      strcpy(lib_path + len + 1, search->name);
1277	      len += nlen + 1;
1278	    }
1279
1280	  if (gld${EMULATION_NAME}_search_needed (lib_path, &n, force))
1281	    {
1282	      free (lib_path);
1283	      break;
1284	    }
1285	  free (lib_path);
1286
1287	  lib_path = getenv ("LD_LIBRARY_PATH");
1288	  if (gld${EMULATION_NAME}_search_needed (lib_path, &n, force))
1289	    break;
1290EOF
1291fi
1292if [ "x${USE_LIBPATH}" = xyes ] ; then
1293cat >>e${EMULATION_NAME}.c <<EOF
1294	  found = 0;
1295	  rp = bfd_elf_get_runpath_list (output_bfd, &link_info);
1296	  for (; !found && rp != NULL; rp = rp->next)
1297	    {
1298	      char *tmpname = gld${EMULATION_NAME}_add_sysroot (rp->name);
1299	      found = (rp->by == l->by
1300		       && gld${EMULATION_NAME}_search_needed (tmpname,
1301							      &n,
1302							      force));
1303	      free (tmpname);
1304	    }
1305	  if (found)
1306	    break;
1307
1308EOF
1309fi
1310if [ "x${USE_LIBPATH}" = xyes ] ; then
1311  case ${target} in
1312    *-*-freebsd* | *-*-dragonfly*)
1313      cat >>e${EMULATION_NAME}.c <<EOF
1314	  if (gld${EMULATION_NAME}_check_ld_elf_hints (l->name, force))
1315	    break;
1316EOF
1317    # FreeBSD
1318    ;;
1319
1320    *-*-linux-* | *-*-k*bsd*-*)
1321    # Linux
1322      cat >>e${EMULATION_NAME}.c <<EOF
1323	  if (gld${EMULATION_NAME}_check_ld_so_conf (l->name, force))
1324	    break;
1325
1326EOF
1327    ;;
1328  esac
1329fi
1330cat >>e${EMULATION_NAME}.c <<EOF
1331	  len = strlen (l->name);
1332	  for (search = search_head; search != NULL; search = search->next)
1333	    {
1334	      char *filename;
1335
1336	      if (search->cmdline)
1337		continue;
1338	      filename = (char *) xmalloc (strlen (search->name) + len + 2);
1339	      sprintf (filename, "%s/%s", search->name, l->name);
1340	      nn.name = filename;
1341	      if (gld${EMULATION_NAME}_try_needed (&nn, force))
1342		break;
1343	      free (filename);
1344	    }
1345	  if (search != NULL)
1346	    break;
1347EOF
1348cat >>e${EMULATION_NAME}.c <<EOF
1349	}
1350
1351      if (force < 2)
1352	continue;
1353
1354      einfo ("%P: warning: %s, needed by %B, not found (try using -rpath or -rpath-link)\n",
1355	     l->name, l->by);
1356    }
1357}
1358
1359EOF
1360fi
1361
1362cat >>e${EMULATION_NAME}.c <<EOF
1363
1364/* Look through an expression for an assignment statement.  */
1365
1366static void
1367gld${EMULATION_NAME}_find_exp_assignment (etree_type *exp)
1368{
1369  bfd_boolean provide = FALSE;
1370
1371  switch (exp->type.node_class)
1372    {
1373    case etree_provide:
1374      provide = TRUE;
1375      /* Fall thru */
1376    case etree_assign:
1377      /* We call record_link_assignment even if the symbol is defined.
1378	 This is because if it is defined by a dynamic object, we
1379	 actually want to use the value defined by the linker script,
1380	 not the value from the dynamic object (because we are setting
1381	 symbols like etext).  If the symbol is defined by a regular
1382	 object, then, as it happens, calling record_link_assignment
1383	 will do no harm.  */
1384      if (strcmp (exp->assign.dst, ".") != 0)
1385	{
1386	  if (!bfd_elf_record_link_assignment (output_bfd, &link_info,
1387					       exp->assign.dst, provide,
1388					       exp->assign.hidden))
1389	    einfo ("%P%F: failed to record assignment to %s: %E\n",
1390		   exp->assign.dst);
1391	}
1392      gld${EMULATION_NAME}_find_exp_assignment (exp->assign.src);
1393      break;
1394
1395    case etree_binary:
1396      gld${EMULATION_NAME}_find_exp_assignment (exp->binary.lhs);
1397      gld${EMULATION_NAME}_find_exp_assignment (exp->binary.rhs);
1398      break;
1399
1400    case etree_trinary:
1401      gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.cond);
1402      gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.lhs);
1403      gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.rhs);
1404      break;
1405
1406    case etree_unary:
1407      gld${EMULATION_NAME}_find_exp_assignment (exp->unary.child);
1408      break;
1409
1410    default:
1411      break;
1412    }
1413}
1414
1415
1416/* This is called by the before_allocation routine via
1417   lang_for_each_statement.  It locates any assignment statements, and
1418   tells the ELF backend about them, in case they are assignments to
1419   symbols which are referred to by dynamic objects.  */
1420
1421static void
1422gld${EMULATION_NAME}_find_statement_assignment (lang_statement_union_type *s)
1423{
1424  if (s->header.type == lang_assignment_statement_enum)
1425    gld${EMULATION_NAME}_find_exp_assignment (s->assignment_statement.exp);
1426}
1427
1428EOF
1429
1430if test x"$LDEMUL_BEFORE_ALLOCATION" != xgld"$EMULATION_NAME"_before_allocation; then
1431  if test x"${ELF_INTERPRETER_NAME+set}" = xset; then
1432    ELF_INTERPRETER_SET_DEFAULT="
1433  if (sinterp != NULL)
1434    {
1435      sinterp->contents = (unsigned char *) ${ELF_INTERPRETER_NAME};
1436      sinterp->size = strlen ((char *) sinterp->contents) + 1;
1437    }
1438
1439"
1440  else
1441    ELF_INTERPRETER_SET_DEFAULT=
1442  fi
1443cat >>e${EMULATION_NAME}.c <<EOF
1444
1445/* This is called after the sections have been attached to output
1446   sections, but before any sizes or addresses have been set.  */
1447
1448static void
1449gld${EMULATION_NAME}_before_allocation (void)
1450{
1451  const char *rpath;
1452  asection *sinterp;
1453
1454  if (link_info.hash->type == bfd_link_elf_hash_table)
1455    _bfd_elf_tls_setup (output_bfd, &link_info);
1456
1457  /* If we are going to make any variable assignments, we need to let
1458     the ELF backend know about them in case the variables are
1459     referred to by dynamic objects.  */
1460  lang_for_each_statement (gld${EMULATION_NAME}_find_statement_assignment);
1461
1462  /* Let the ELF backend work out the sizes of any sections required
1463     by dynamic linking.  */
1464  rpath = command_line.rpath;
1465  if (rpath == NULL)
1466    rpath = (const char *) getenv ("LD_RUN_PATH");
1467  if (! (bfd_elf_size_dynamic_sections
1468	 (output_bfd, command_line.soname, rpath,
1469	  command_line.filter_shlib,
1470	  (const char * const *) command_line.auxiliary_filters,
1471	  &link_info, &sinterp, lang_elf_version_info)))
1472    einfo ("%P%F: failed to set dynamic section sizes: %E\n");
1473
1474${ELF_INTERPRETER_SET_DEFAULT}
1475  /* Let the user override the dynamic linker we are using.  */
1476  if (command_line.interpreter != NULL
1477      && sinterp != NULL)
1478    {
1479      sinterp->contents = (bfd_byte *) command_line.interpreter;
1480      sinterp->size = strlen (command_line.interpreter) + 1;
1481    }
1482
1483  /* Look for any sections named .gnu.warning.  As a GNU extensions,
1484     we treat such sections as containing warning messages.  We print
1485     out the warning message, and then zero out the section size so
1486     that it does not get copied into the output file.  */
1487
1488  {
1489    LANG_FOR_EACH_INPUT_STATEMENT (is)
1490      {
1491	asection *s;
1492	bfd_size_type sz;
1493	char *msg;
1494	bfd_boolean ret;
1495
1496	if (is->just_syms_flag)
1497	  continue;
1498
1499	s = bfd_get_section_by_name (is->the_bfd, ".gnu.warning");
1500	if (s == NULL)
1501	  continue;
1502
1503	sz = s->size;
1504	msg = xmalloc ((size_t) (sz + 1));
1505	if (! bfd_get_section_contents (is->the_bfd, s,	msg, (file_ptr) 0, sz))
1506	  einfo ("%F%B: Can't read contents of section .gnu.warning: %E\n",
1507		 is->the_bfd);
1508	msg[sz] = '\0';
1509	ret = link_info.callbacks->warning (&link_info, msg,
1510					    (const char *) NULL,
1511					    is->the_bfd, (asection *) NULL,
1512					    (bfd_vma) 0);
1513	ASSERT (ret);
1514	free (msg);
1515
1516	/* Clobber the section size, so that we don't waste copying the
1517	   warning into the output file.  */
1518	s->size = 0;
1519
1520	/* Also set SEC_EXCLUDE, so that symbols defined in the warning
1521	   section don't get copied to the output.  */
1522	s->flags |= SEC_EXCLUDE;
1523      }
1524  }
1525
1526  before_allocation_default ();
1527
1528  if (!bfd_elf_size_dynsym_hash_dynstr (output_bfd, &link_info))
1529    einfo ("%P%F: failed to set dynamic section sizes: %E\n");
1530}
1531
1532EOF
1533fi
1534
1535if test x"$LDEMUL_OPEN_DYNAMIC_ARCHIVE" != xgld"$EMULATION_NAME"_open_dynamic_archive; then
1536cat >>e${EMULATION_NAME}.c <<EOF
1537
1538/* Try to open a dynamic archive.  This is where we know that ELF
1539   dynamic libraries have an extension of .so (or .sl on oddball systems
1540   like hpux).  */
1541
1542static bfd_boolean
1543gld${EMULATION_NAME}_open_dynamic_archive
1544  (const char *arch __unused, search_dirs_type *search,
1545  lang_input_statement_type *entry)
1546{
1547  const char *filename;
1548  char *string;
1549
1550  if (! entry->is_archive)
1551    return FALSE;
1552
1553  filename = entry->filename;
1554
1555EOF
1556case ${target} in
1557  *-*-openbsd*)
1558    cat >>e${EMULATION_NAME}.c <<EOF
1559  string = gld${EMULATION_NAME}_search_dir(search->name, filename, -1, -1);
1560  if (string == NULL)
1561    return FALSE;
1562EOF
1563   ;;
1564  *)
1565    cat >>e${EMULATION_NAME}.c <<EOF
1566  /* This allocates a few bytes too many when EXTRA_SHLIB_EXTENSION
1567     is defined, but it does not seem worth the headache to optimize
1568     away those two bytes of space.  */
1569  string = (char *) xmalloc (strlen (search->name)
1570			     + strlen (filename)
1571			     + strlen (arch)
1572#ifdef EXTRA_SHLIB_EXTENSION
1573			     + strlen (EXTRA_SHLIB_EXTENSION)
1574#endif
1575			     + sizeof "/lib.so");
1576
1577  sprintf (string, "%s/lib%s%s.so", search->name, filename, arch);
1578
1579EOF
1580   ;;
1581esac
1582cat >>e${EMULATION_NAME}.c <<EOF
1583#ifdef EXTRA_SHLIB_EXTENSION
1584  /* Try the .so extension first.  If that fails build a new filename
1585     using EXTRA_SHLIB_EXTENSION.  */
1586  if (! ldfile_try_open_bfd (string, entry))
1587    sprintf (string, "%s/lib%s%s%s", search->name,
1588	     filename, arch, EXTRA_SHLIB_EXTENSION);
1589#endif
1590
1591  if (! ldfile_try_open_bfd (string, entry))
1592    {
1593      free (string);
1594      return FALSE;
1595    }
1596
1597  entry->filename = string;
1598
1599  /* We have found a dynamic object to include in the link.  The ELF
1600     backend linker will create a DT_NEEDED entry in the .dynamic
1601     section naming this file.  If this file includes a DT_SONAME
1602     entry, it will be used.  Otherwise, the ELF linker will just use
1603     the name of the file.  For an archive found by searching, like
1604     this one, the DT_NEEDED entry should consist of just the name of
1605     the file, without the path information used to find it.  Note
1606     that we only need to do this if we have a dynamic object; an
1607     archive will never be referenced by a DT_NEEDED entry.
1608
1609     FIXME: This approach--using bfd_elf_set_dt_needed_name--is not
1610     very pretty.  I haven't been able to think of anything that is
1611     pretty, though.  */
1612  if (bfd_check_format (entry->the_bfd, bfd_object)
1613      && (entry->the_bfd->flags & DYNAMIC) != 0)
1614    {
1615      ASSERT (entry->is_archive && entry->search_dirs_flag);
1616
1617      /* Rather than duplicating the logic above.  Just use the
1618	 filename we recorded earlier.  */
1619
1620      filename = lbasename (entry->filename);
1621      bfd_elf_set_dt_needed_name (entry->the_bfd, filename);
1622    }
1623
1624  return TRUE;
1625}
1626
1627EOF
1628fi
1629
1630if test x"$LDEMUL_PLACE_ORPHAN" != xgld"$EMULATION_NAME"_place_orphan; then
1631cat >>e${EMULATION_NAME}.c <<EOF
1632
1633/* A variant of lang_output_section_find used by place_orphan.  */
1634
1635static lang_output_section_statement_type *
1636output_rel_find (asection *sec, int isdyn)
1637{
1638  lang_output_section_statement_type *lookup;
1639  lang_output_section_statement_type *last = NULL;
1640  lang_output_section_statement_type *last_alloc = NULL;
1641  lang_output_section_statement_type *last_rel = NULL;
1642  lang_output_section_statement_type *last_rel_alloc = NULL;
1643  int rela = sec->name[4] == 'a';
1644
1645  for (lookup = &lang_output_section_statement.head->output_section_statement;
1646       lookup != NULL;
1647       lookup = lookup->next)
1648    {
1649      if (lookup->constraint != -1
1650	  && strncmp (".rel", lookup->name, 4) == 0)
1651	{
1652	  int lookrela = lookup->name[4] == 'a';
1653
1654	  /* .rel.dyn must come before all other reloc sections, to suit
1655	     GNU ld.so.  */
1656	  if (isdyn)
1657	    break;
1658
1659	  /* Don't place after .rel.plt as doing so results in wrong
1660	     dynamic tags.  */
1661	  if (strcmp (".plt", lookup->name + 4 + lookrela) == 0)
1662	    break;
1663
1664	  if (rela == lookrela || last_rel == NULL)
1665	    last_rel = lookup;
1666	  if ((rela == lookrela || last_rel_alloc == NULL)
1667	      && lookup->bfd_section != NULL
1668	      && (lookup->bfd_section->flags & SEC_ALLOC) != 0)
1669	    last_rel_alloc = lookup;
1670	}
1671
1672      last = lookup;
1673      if (lookup->bfd_section != NULL
1674	  && (lookup->bfd_section->flags & SEC_ALLOC) != 0)
1675	last_alloc = lookup;
1676    }
1677
1678  if (last_rel_alloc)
1679    return last_rel_alloc;
1680
1681  if (last_rel)
1682    return last_rel;
1683
1684  if (last_alloc)
1685    return last_alloc;
1686
1687  return last;
1688}
1689
1690/* Place an orphan section.  We use this to put random SHF_ALLOC
1691   sections in the right segment.  */
1692
1693static bfd_boolean
1694gld${EMULATION_NAME}_place_orphan (asection *s)
1695{
1696  static struct orphan_save hold[] =
1697    {
1698      { ".text",
1699	SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE,
1700	0, 0, 0, 0 },
1701      { ".rodata",
1702	SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA,
1703	0, 0, 0, 0 },
1704      { ".data",
1705	SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_DATA,
1706	0, 0, 0, 0 },
1707      { ".bss",
1708	SEC_ALLOC,
1709	0, 0, 0, 0 },
1710      { 0,
1711	SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA,
1712	0, 0, 0, 0 },
1713      { ".interp",
1714	SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA,
1715	0, 0, 0, 0 },
1716      { ".sdata",
1717	SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_DATA | SEC_SMALL_DATA,
1718	0, 0, 0, 0 }
1719    };
1720  enum orphan_save_index
1721    {
1722      orphan_text = 0,
1723      orphan_rodata,
1724      orphan_data,
1725      orphan_bss,
1726      orphan_rel,
1727      orphan_interp,
1728      orphan_sdata
1729    };
1730  static int orphan_init_done = 0;
1731  struct orphan_save *place;
1732  const char *secname;
1733  lang_output_section_statement_type *after;
1734  lang_output_section_statement_type *os;
1735  int isdyn = 0;
1736  int iself = s->owner->xvec->flavour == bfd_target_elf_flavour;
1737  unsigned int sh_type = iself ? elf_section_type (s) : SHT_NULL;
1738
1739  secname = bfd_get_section_name (s->owner, s);
1740
1741  if (! link_info.relocatable
1742      && link_info.combreloc
1743      && (s->flags & SEC_ALLOC))
1744    {
1745      if (iself)
1746	switch (sh_type)
1747	  {
1748	  case SHT_RELA:
1749	    secname = ".rela.dyn";
1750	    isdyn = 1;
1751	    break;
1752	  case SHT_REL:
1753	    secname = ".rel.dyn";
1754	    isdyn = 1;
1755	    break;
1756	  default:
1757	    break;
1758	  }
1759      else if (strncmp (secname, ".rel", 4) == 0)
1760	{
1761	  secname = secname[4] == 'a' ? ".rela.dyn" : ".rel.dyn";
1762	  isdyn = 1;
1763	}
1764    }
1765
1766  if (isdyn || (!config.unique_orphan_sections && !unique_section_p (s)))
1767    {
1768      /* Look through the script to see where to place this section.  */
1769      os = lang_output_section_find (secname);
1770
1771      if (os != NULL
1772	  && (os->bfd_section == NULL
1773	      || os->bfd_section->flags == 0
1774	      || (_bfd_elf_match_sections_by_type (output_bfd,
1775						   os->bfd_section,
1776						   s->owner, s)
1777		  && ((s->flags ^ os->bfd_section->flags)
1778		      & (SEC_LOAD | SEC_ALLOC)) == 0)))
1779	{
1780	  /* We already have an output section statement with this
1781	     name, and its bfd section, if any, has compatible flags.
1782	     If the section already exists but does not have any flags
1783	     set, then it has been created by the linker, probably as a
1784	     result of a --section-start command line switch.  */
1785	  lang_add_section (&os->children, s, os);
1786	  return TRUE;
1787	}
1788    }
1789
1790  if (!orphan_init_done)
1791    {
1792      struct orphan_save *ho;
1793      for (ho = hold; ho < hold + sizeof (hold) / sizeof (hold[0]); ++ho)
1794	if (ho->name != NULL)
1795	  {
1796	    ho->os = lang_output_section_find (ho->name);
1797	    if (ho->os != NULL && ho->os->flags == 0)
1798	      ho->os->flags = ho->flags;
1799	  }
1800      orphan_init_done = 1;
1801    }
1802
1803  /* If this is a final link, then always put .gnu.warning.SYMBOL
1804     sections into the .text section to get them out of the way.  */
1805  if (link_info.executable
1806      && ! link_info.relocatable
1807      && strncmp (secname, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0
1808      && hold[orphan_text].os != NULL)
1809    {
1810      lang_add_section (&hold[orphan_text].os->children, s,
1811			hold[orphan_text].os);
1812      return TRUE;
1813    }
1814
1815  /* Decide which segment the section should go in based on the
1816     section name and section flags.  We put loadable .note sections
1817     right after the .interp section, so that the PT_NOTE segment is
1818     stored right after the program headers where the OS can read it
1819     in the first page.  */
1820
1821  place = NULL;
1822  if ((s->flags & SEC_ALLOC) == 0)
1823    ;
1824  else if ((s->flags & SEC_LOAD) != 0
1825	   && ((iself && sh_type == SHT_NOTE)
1826	       || (!iself && strncmp (secname, ".note", 5) == 0)))
1827    place = &hold[orphan_interp];
1828  else if ((s->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
1829    place = &hold[orphan_bss];
1830  else if ((s->flags & SEC_SMALL_DATA) != 0)
1831    place = &hold[orphan_sdata];
1832  else if ((s->flags & SEC_READONLY) == 0)
1833    place = &hold[orphan_data];
1834  else if (((iself && (sh_type == SHT_RELA || sh_type == SHT_REL))
1835	    || (!iself && strncmp (secname, ".rel", 4) == 0))
1836	   && (s->flags & SEC_LOAD) != 0)
1837    place = &hold[orphan_rel];
1838  else if ((s->flags & SEC_CODE) == 0)
1839    place = &hold[orphan_rodata];
1840  else
1841    place = &hold[orphan_text];
1842
1843  after = NULL;
1844  if (place != NULL)
1845    {
1846      if (place->os == NULL)
1847	{
1848	  if (place->name != NULL)
1849	    place->os = lang_output_section_find (place->name);
1850	  else
1851	    place->os = output_rel_find (s, isdyn);
1852	}
1853      after = place->os;
1854      if (after == NULL)
1855	after = lang_output_section_find_by_flags
1856	  (s, &place->os, _bfd_elf_match_sections_by_type);
1857      if (after == NULL)
1858	/* *ABS* is always the first output section statement.  */
1859	after = &lang_output_section_statement.head->output_section_statement;
1860    }
1861
1862  /* Choose a unique name for the section.  This will be needed if the
1863     same section name appears in the input file with different
1864     loadable or allocatable characteristics.  */
1865  if (bfd_get_section_by_name (output_bfd, secname) != NULL)
1866    {
1867      static int count = 1;
1868      secname = bfd_get_unique_section_name (output_bfd, secname, &count);
1869      if (secname == NULL)
1870	einfo ("%F%P: place_orphan failed: %E\n");
1871    }
1872
1873  lang_insert_orphan (s, secname, after, place, NULL, NULL);
1874
1875  return TRUE;
1876}
1877EOF
1878fi
1879
1880if test x"$LDEMUL_FINISH" != xgld"$EMULATION_NAME"_finish; then
1881cat >>e${EMULATION_NAME}.c <<EOF
1882
1883static void
1884gld${EMULATION_NAME}_layout_sections_again (void)
1885{
1886  lang_reset_memory_regions ();
1887
1888  /* Resize the sections.  */
1889  lang_size_sections (NULL, TRUE);
1890
1891  /* Redo special stuff.  */
1892  ldemul_after_allocation ();
1893
1894  /* Do the assignments again.  */
1895  lang_do_assignments ();
1896}
1897
1898static void
1899gld${EMULATION_NAME}_finish (void)
1900{
1901  if (bfd_elf_discard_info (output_bfd, &link_info))
1902    gld${EMULATION_NAME}_layout_sections_again ();
1903
1904  finish_default ();
1905}
1906EOF
1907fi
1908
1909if test x"$LDEMUL_GET_SCRIPT" != xgld"$EMULATION_NAME"_get_script; then
1910cat >>e${EMULATION_NAME}.c <<EOF
1911
1912static char *
1913gld${EMULATION_NAME}_get_script (int *isfile)
1914EOF
1915
1916if test -n "$COMPILE_IN"
1917then
1918# Scripts compiled in.
1919
1920# sed commands to quote an ld script as a C string.
1921sc="-f stringify.sed"
1922
1923cat >>e${EMULATION_NAME}.c <<EOF
1924{
1925  *isfile = 0;
1926
1927  if (link_info.relocatable && config.build_constructors)
1928    return
1929EOF
1930sed $sc ldscripts/${EMULATION_NAME}.xu			>> e${EMULATION_NAME}.c
1931echo '  ; else if (link_info.relocatable) return'	>> e${EMULATION_NAME}.c
1932sed $sc ldscripts/${EMULATION_NAME}.xr			>> e${EMULATION_NAME}.c
1933echo '  ; else if (!config.text_read_only) return'	>> e${EMULATION_NAME}.c
1934sed $sc ldscripts/${EMULATION_NAME}.xbn			>> e${EMULATION_NAME}.c
1935if cmp -s ldscripts/${EMULATION_NAME}.x ldscripts/${EMULATION_NAME}.xn; then : ; else
1936echo '  ; else if (!config.magic_demand_paged) return'	>> e${EMULATION_NAME}.c
1937sed $sc ldscripts/${EMULATION_NAME}.xn			>> e${EMULATION_NAME}.c
1938fi
1939if test -n "$GENERATE_PIE_SCRIPT" ; then
1940if test -n "$GENERATE_COMBRELOC_SCRIPT" ; then
1941echo '  ; else if (link_info.pie && link_info.combreloc' >> e${EMULATION_NAME}.c
1942echo '             && link_info.relro) return' >> e${EMULATION_NAME}.c
1943sed $sc ldscripts/${EMULATION_NAME}.xdw			>> e${EMULATION_NAME}.c
1944echo '  ; else if (link_info.pie && link_info.combreloc && config.data_bss_contig == TRUE) return' >> e${EMULATION_NAME}.c
1945sed $sc ldscripts/${EMULATION_NAME}.xdcz                >> e${EMULATION_NAME}.c
1946echo '  ; else if (link_info.pie && link_info.combreloc) return' >> e${EMULATION_NAME}.c
1947sed $sc ldscripts/${EMULATION_NAME}.xdc			>> e${EMULATION_NAME}.c
1948fi
1949echo '  ; else if (link_info.pie && config.data_bss_contig == TRUE) return' >> e${EMULATION_NAME}.c
1950sed $sc ldscripts/${EMULATION_NAME}.xdz                >> e${EMULATION_NAME}.c
1951echo '  ; else if (link_info.pie) return'		>> e${EMULATION_NAME}.c
1952sed $sc ldscripts/${EMULATION_NAME}.xd			>> e${EMULATION_NAME}.c
1953fi
1954if test -n "$GENERATE_SHLIB_SCRIPT" ; then
1955if test -n "$GENERATE_COMBRELOC_SCRIPT" ; then
1956echo '  ; else if (link_info.shared && link_info.combreloc' >> e${EMULATION_NAME}.c
1957echo '             && link_info.relro) return' >> e${EMULATION_NAME}.c
1958sed $sc ldscripts/${EMULATION_NAME}.xsw			>> e${EMULATION_NAME}.c
1959echo '  ; else if (link_info.shared && link_info.combreloc) return' >> e${EMULATION_NAME}.c
1960sed $sc ldscripts/${EMULATION_NAME}.xsc			>> e${EMULATION_NAME}.c
1961fi
1962echo '  ; else if (link_info.shared) return'		>> e${EMULATION_NAME}.c
1963sed $sc ldscripts/${EMULATION_NAME}.xs			>> e${EMULATION_NAME}.c
1964fi
1965echo '  ; else if (config.data_bss_contig == TRUE) return' >> e${EMULATION_NAME}.c
1966sed $sc ldscripts/${EMULATION_NAME}.xz                 >> e${EMULATION_NAME}.c
1967if test -n "$GENERATE_COMBRELOC_SCRIPT" ; then
1968echo '  ; else if (link_info.combreloc && link_info.relro) return' >> e${EMULATION_NAME}.c
1969sed $sc ldscripts/${EMULATION_NAME}.xw			>> e${EMULATION_NAME}.c
1970echo '  ; else if (link_info.combreloc) return'		>> e${EMULATION_NAME}.c
1971sed $sc ldscripts/${EMULATION_NAME}.xc			>> e${EMULATION_NAME}.c
1972fi
1973echo '  ; else return'					>> e${EMULATION_NAME}.c
1974sed $sc ldscripts/${EMULATION_NAME}.x			>> e${EMULATION_NAME}.c
1975echo '; }'						>> e${EMULATION_NAME}.c
1976
1977else
1978# Scripts read from the filesystem.
1979
1980cat >>e${EMULATION_NAME}.c <<EOF
1981{
1982  *isfile = 1;
1983
1984  if (link_info.relocatable && config.build_constructors)
1985    return "ldscripts/${EMULATION_NAME}.xu";
1986  else if (link_info.relocatable)
1987    return "ldscripts/${EMULATION_NAME}.xr";
1988  else if (!config.text_read_only)
1989    return "ldscripts/${EMULATION_NAME}.xbn";
1990EOF
1991if cmp -s ldscripts/${EMULATION_NAME}.x ldscripts/${EMULATION_NAME}.xn; then :
1992else
1993cat >>e${EMULATION_NAME}.c <<EOF
1994  else if (!config.magic_demand_paged)
1995    return "ldscripts/${EMULATION_NAME}.xn";
1996EOF
1997fi
1998if test -n "$GENERATE_PIE_SCRIPT" ; then
1999if test -n "$GENERATE_COMBRELOC_SCRIPT" ; then
2000cat >>e${EMULATION_NAME}.c <<EOF
2001  else if (link_info.pie && link_info.combreloc && link_info.relro)
2002    return "ldscripts/${EMULATION_NAME}.xdw";
2003  else if (link_info.pie && link_info.combreloc && config.data_bss_contig == TRUE)
2004    return "ldscripts/${EMULATION_NAME}.xdcz";
2005  else if (link_info.pie && link_info.combreloc)
2006    return "ldscripts/${EMULATION_NAME}.xdc";
2007EOF
2008fi
2009cat >>e${EMULATION_NAME}.c <<EOF
2010  else if (link_info.pie && config.data_bss_contig == TRUE)
2011    return "ldscripts/${EMULATION_NAME}.xdz";
2012  else if (link_info.pie)
2013    return "ldscripts/${EMULATION_NAME}.xd";
2014EOF
2015fi
2016if test -n "$GENERATE_SHLIB_SCRIPT" ; then
2017if test -n "$GENERATE_COMBRELOC_SCRIPT" ; then
2018cat >>e${EMULATION_NAME}.c <<EOF
2019  else if (link_info.shared && link_info.combreloc && link_info.relro)
2020    return "ldscripts/${EMULATION_NAME}.xsw";
2021  else if (link_info.shared && link_info.combreloc)
2022    return "ldscripts/${EMULATION_NAME}.xsc";
2023EOF
2024fi
2025cat >>e${EMULATION_NAME}.c <<EOF
2026  else if (link_info.shared)
2027    return "ldscripts/${EMULATION_NAME}.xs";
2028EOF
2029fi
2030cat >>e${EMULATION_NAME}.c <<EOF
2031  else if (config.data_bss_contig == TRUE)
2032    return "ldscripts/${EMULATION_NAME}.xz";
2033EOF
2034if test -n "$GENERATE_COMBRELOC_SCRIPT" ; then
2035cat >>e${EMULATION_NAME}.c <<EOF
2036  else if (link_info.combreloc && link_info.relro)
2037    return "ldscripts/${EMULATION_NAME}.xw";
2038  else if (link_info.combreloc)
2039    return "ldscripts/${EMULATION_NAME}.xc";
2040EOF
2041fi
2042cat >>e${EMULATION_NAME}.c <<EOF
2043  else
2044    return "ldscripts/${EMULATION_NAME}.x";
2045}
2046
2047EOF
2048fi
2049fi
2050
2051if test -n "$PARSE_AND_LIST_ARGS_CASES" -o x"$GENERATE_SHLIB_SCRIPT" = xyes; then
2052
2053if test -n "$PARSE_AND_LIST_PROLOGUE" ; then
2054cat >>e${EMULATION_NAME}.c <<EOF
2055 $PARSE_AND_LIST_PROLOGUE
2056EOF
2057fi
2058
2059cat >>e${EMULATION_NAME}.c <<EOF
2060
2061#define OPTION_DISABLE_NEW_DTAGS	(400)
2062#define OPTION_ENABLE_NEW_DTAGS		(OPTION_DISABLE_NEW_DTAGS + 1)
2063#define OPTION_GROUP			(OPTION_ENABLE_NEW_DTAGS + 1)
2064#define OPTION_EH_FRAME_HDR		(OPTION_GROUP + 1)
2065#define OPTION_EXCLUDE_LIBS		(OPTION_EH_FRAME_HDR + 1)
2066
2067static void
2068gld${EMULATION_NAME}_add_options
2069  (int ns, char **shortopts, int nl, struct option **longopts,
2070   int nrl ATTRIBUTE_UNUSED, struct option **really_longopts ATTRIBUTE_UNUSED)
2071{
2072  static const char xtra_short[] = "${PARSE_AND_LIST_SHORTOPTS}z:";
2073  static const struct option xtra_long[] = {
2074EOF
2075
2076if test x"$GENERATE_SHLIB_SCRIPT" = xyes; then
2077cat >>e${EMULATION_NAME}.c <<EOF
2078    {"disable-new-dtags", no_argument, NULL, OPTION_DISABLE_NEW_DTAGS},
2079    {"enable-new-dtags", no_argument, NULL, OPTION_ENABLE_NEW_DTAGS},
2080    {"eh-frame-hdr", no_argument, NULL, OPTION_EH_FRAME_HDR},
2081    {"exclude-libs", required_argument, NULL, OPTION_EXCLUDE_LIBS},
2082    {"Bgroup", no_argument, NULL, OPTION_GROUP},
2083EOF
2084fi
2085
2086if test -n "$PARSE_AND_LIST_LONGOPTS" ; then
2087cat >>e${EMULATION_NAME}.c <<EOF
2088    $PARSE_AND_LIST_LONGOPTS
2089EOF
2090fi
2091
2092cat >>e${EMULATION_NAME}.c <<EOF
2093    {NULL, no_argument, NULL, 0}
2094  };
2095
2096  *shortopts = (char *) xrealloc (*shortopts, ns + sizeof (xtra_short));
2097  memcpy (*shortopts + ns, &xtra_short, sizeof (xtra_short));
2098  *longopts = (struct option *)
2099    xrealloc (*longopts, nl * sizeof (struct option) + sizeof (xtra_long));
2100  memcpy (*longopts + nl, &xtra_long, sizeof (xtra_long));
2101}
2102
2103static bfd_boolean
2104gld${EMULATION_NAME}_handle_option (int optc)
2105{
2106  switch (optc)
2107    {
2108    default:
2109      return FALSE;
2110
2111EOF
2112
2113if test x"$GENERATE_SHLIB_SCRIPT" = xyes; then
2114cat >>e${EMULATION_NAME}.c <<EOF
2115    case OPTION_DISABLE_NEW_DTAGS:
2116      link_info.new_dtags = FALSE;
2117      break;
2118
2119    case OPTION_ENABLE_NEW_DTAGS:
2120      link_info.new_dtags = TRUE;
2121      break;
2122
2123    case OPTION_EH_FRAME_HDR:
2124      link_info.eh_frame_hdr = TRUE;
2125      break;
2126
2127    case OPTION_GROUP:
2128      link_info.flags_1 |= (bfd_vma) DF_1_GROUP;
2129      /* Groups must be self-contained.  */
2130      link_info.unresolved_syms_in_objects = RM_GENERATE_ERROR;
2131      link_info.unresolved_syms_in_shared_libs = RM_GENERATE_ERROR;
2132      break;
2133
2134    case OPTION_EXCLUDE_LIBS:
2135      add_excluded_libs (optarg);
2136      break;
2137
2138    case 'z':
2139      if (strcmp (optarg, "initfirst") == 0)
2140	link_info.flags_1 |= (bfd_vma) DF_1_INITFIRST;
2141      else if (strcmp (optarg, "interpose") == 0)
2142	link_info.flags_1 |= (bfd_vma) DF_1_INTERPOSE;
2143      else if (strcmp (optarg, "loadfltr") == 0)
2144	link_info.flags_1 |= (bfd_vma) DF_1_LOADFLTR;
2145      else if (strcmp (optarg, "nodefaultlib") == 0)
2146	link_info.flags_1 |= (bfd_vma) DF_1_NODEFLIB;
2147      else if (strcmp (optarg, "nodelete") == 0)
2148	link_info.flags_1 |= (bfd_vma) DF_1_NODELETE;
2149      else if (strcmp (optarg, "nodlopen") == 0)
2150	link_info.flags_1 |= (bfd_vma) DF_1_NOOPEN;
2151      else if (strcmp (optarg, "nodump") == 0)
2152	link_info.flags_1 |= (bfd_vma) DF_1_NODUMP;
2153      else if (strcmp (optarg, "now") == 0)
2154	{
2155	  link_info.flags |= (bfd_vma) DF_BIND_NOW;
2156	  link_info.flags_1 |= (bfd_vma) DF_1_NOW;
2157	}
2158      else if (strcmp (optarg, "origin") == 0)
2159	{
2160	  link_info.flags |= (bfd_vma) DF_ORIGIN;
2161	  link_info.flags_1 |= (bfd_vma) DF_1_ORIGIN;
2162	}
2163      else if (strcmp (optarg, "defs") == 0)
2164	link_info.unresolved_syms_in_objects = RM_GENERATE_ERROR;
2165      else if (strcmp (optarg, "muldefs") == 0)
2166	link_info.allow_multiple_definition = TRUE;
2167      else if (strcmp (optarg, "combreloc") == 0)
2168	link_info.combreloc = TRUE;
2169      else if (strcmp (optarg, "nocombreloc") == 0)
2170	link_info.combreloc = FALSE;
2171      else if (strcmp (optarg, "nocopyreloc") == 0)
2172	link_info.nocopyreloc = TRUE;
2173      else if (strcmp (optarg, "execstack") == 0)
2174	{
2175	  link_info.execstack = TRUE;
2176	  link_info.noexecstack = FALSE;
2177	}
2178      else if (strcmp (optarg, "noexecstack") == 0)
2179	{
2180	  link_info.noexecstack = TRUE;
2181	  link_info.execstack = FALSE;
2182	}
2183      else if (strcmp (optarg, "relro") == 0)
2184	link_info.relro = TRUE;
2185      else if (strcmp (optarg, "norelro") == 0)
2186	link_info.relro = FALSE;
2187      else if (strcmp (optarg, "wxneeded") == 0)
2188	link_info.wxneeded = TRUE;
2189      else if (strcmp (optarg, "notext") == 0)
2190	link_info.allow_textrel = TRUE;
2191      else if (strcmp (optarg, "text") == 0)
2192	link_info.allow_textrel = FALSE;
2193      /* What about the other Solaris -z options? FIXME.  */
2194      break;
2195EOF
2196fi
2197
2198if test -n "$PARSE_AND_LIST_ARGS_CASES" ; then
2199cat >>e${EMULATION_NAME}.c <<EOF
2200 $PARSE_AND_LIST_ARGS_CASES
2201EOF
2202fi
2203
2204cat >>e${EMULATION_NAME}.c <<EOF
2205    }
2206
2207  return TRUE;
2208}
2209
2210EOF
2211
2212if test x"$LDEMUL_LIST_OPTIONS" != xgld"$EMULATION_NAME"_list_options; then
2213cat >>e${EMULATION_NAME}.c <<EOF
2214
2215static void
2216gld${EMULATION_NAME}_list_options (FILE * file)
2217{
2218EOF
2219
2220if test x"$GENERATE_SHLIB_SCRIPT" = xyes; then
2221cat >>e${EMULATION_NAME}.c <<EOF
2222  fprintf (file, _("  -Bgroup\t\tSelects group name lookup rules for DSO\n"));
2223  fprintf (file, _("  --disable-new-dtags\tDisable new dynamic tags\n"));
2224  fprintf (file, _("  --enable-new-dtags\tEnable new dynamic tags\n"));
2225  fprintf (file, _("  --eh-frame-hdr\tCreate .eh_frame_hdr section\n"));
2226  fprintf (file, _("  -z combreloc\t\tMerge dynamic relocs into one section and sort\n"));
2227  fprintf (file, _("  -z defs\t\tReport unresolved symbols in object files.\n"));
2228  fprintf (file, _("  -z execstack\t\tMark executable as requiring executable stack\n"));
2229  fprintf (file, _("  -z initfirst\t\tMark DSO to be initialized first at runtime\n"));
2230  fprintf (file, _("  -z interpose\t\tMark object to interpose all DSOs but executable\n"));
2231  fprintf (file, _("  -z loadfltr\t\tMark object requiring immediate process\n"));
2232  fprintf (file, _("  -z muldefs\t\tAllow multiple definitions\n"));
2233  fprintf (file, _("  -z nocombreloc\tDon't merge dynamic relocs into one section\n"));
2234  fprintf (file, _("  -z nocopyreloc\tDon't create copy relocs\n"));
2235  fprintf (file, _("  -z nodefaultlib\tMark object not to use default search paths\n"));
2236  fprintf (file, _("  -z nodelete\t\tMark DSO non-deletable at runtime\n"));
2237  fprintf (file, _("  -z nodlopen\t\tMark DSO not available to dlopen\n"));
2238  fprintf (file, _("  -z nodump\t\tMark DSO not available to dldump\n"));
2239  fprintf (file, _("  -z noexecstack\tMark executable as not requiring executable stack\n"));
2240  fprintf (file, _("  -z norelro\t\tDon't create RELRO program header\n"));
2241  fprintf (file, _("  -z now\t\tMark object non-lazy runtime binding\n"));
2242  fprintf (file, _("  -z origin\t\tMark object requiring immediate \$ORIGIN processing\n\t\t\t  at runtime\n"));
2243  fprintf (file, _("  -z relro\t\tCreate RELRO program header\n"));
2244  fprintf (file, _("  -z KEYWORD\t\tIgnored for Solaris compatibility\n"));
2245EOF
2246fi
2247
2248if test -n "$PARSE_AND_LIST_OPTIONS" ; then
2249cat >>e${EMULATION_NAME}.c <<EOF
2250 $PARSE_AND_LIST_OPTIONS
2251EOF
2252fi
2253
2254cat >>e${EMULATION_NAME}.c <<EOF
2255}
2256EOF
2257
2258if test -n "$PARSE_AND_LIST_EPILOGUE" ; then
2259cat >>e${EMULATION_NAME}.c <<EOF
2260 $PARSE_AND_LIST_EPILOGUE
2261EOF
2262fi
2263fi
2264else
2265cat >>e${EMULATION_NAME}.c <<EOF
2266#define gld${EMULATION_NAME}_add_options NULL
2267#define gld${EMULATION_NAME}_handle_option NULL
2268EOF
2269if test x"$LDEMUL_LIST_OPTIONS" != xgld"$EMULATION_NAME"_list_options; then
2270cat >>e${EMULATION_NAME}.c <<EOF
2271#define gld${EMULATION_NAME}_list_options NULL
2272EOF
2273fi
2274fi
2275
2276cat >>e${EMULATION_NAME}.c <<EOF
2277
2278struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
2279{
2280  ${LDEMUL_BEFORE_PARSE-gld${EMULATION_NAME}_before_parse},
2281  ${LDEMUL_SYSLIB-syslib_default},
2282  ${LDEMUL_HLL-hll_default},
2283  ${LDEMUL_AFTER_PARSE-after_parse_default},
2284  ${LDEMUL_AFTER_OPEN-gld${EMULATION_NAME}_after_open},
2285  ${LDEMUL_AFTER_ALLOCATION-after_allocation_default},
2286  ${LDEMUL_SET_OUTPUT_ARCH-set_output_arch_default},
2287  ${LDEMUL_CHOOSE_TARGET-ldemul_default_target},
2288  ${LDEMUL_BEFORE_ALLOCATION-gld${EMULATION_NAME}_before_allocation},
2289  ${LDEMUL_GET_SCRIPT-gld${EMULATION_NAME}_get_script},
2290  "${EMULATION_NAME}",
2291  "${OUTPUT_FORMAT}",
2292  ${LDEMUL_FINISH-gld${EMULATION_NAME}_finish},
2293  ${LDEMUL_CREATE_OUTPUT_SECTION_STATEMENTS-NULL},
2294  ${LDEMUL_OPEN_DYNAMIC_ARCHIVE-gld${EMULATION_NAME}_open_dynamic_archive},
2295  ${LDEMUL_PLACE_ORPHAN-gld${EMULATION_NAME}_place_orphan},
2296  ${LDEMUL_SET_SYMBOLS-NULL},
2297  ${LDEMUL_PARSE_ARGS-NULL},
2298  gld${EMULATION_NAME}_add_options,
2299  gld${EMULATION_NAME}_handle_option,
2300  ${LDEMUL_UNRECOGNIZED_FILE-NULL},
2301  ${LDEMUL_LIST_OPTIONS-gld${EMULATION_NAME}_list_options},
2302  ${LDEMUL_RECOGNIZED_FILE-gld${EMULATION_NAME}_load_symbols},
2303  ${LDEMUL_FIND_POTENTIAL_LIBRARIES-NULL},
2304  ${LDEMUL_NEW_VERS_PATTERN-NULL}
2305};
2306EOF
2307