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