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