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