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