elf32.em revision 76232
1# $FreeBSD: head/contrib/binutils/ld/emultempl/elf32.em 76232 2001-05-03 01:49:58Z obrien $
2# This shell script emits a C file. -*- C -*-
3# It does some substitutions.
4# This file is now misnamed, because it supports both 32 bit and 64 bit
5# ELF emulations.
6test -z "${ELFSIZE}" && ELFSIZE=32
7cat >e${EMULATION_NAME}.c <<EOF
8/* This file is is generated by a shell script.  DO NOT EDIT! */
9
10/* ${ELFSIZE} bit ELF emulation code for ${EMULATION_NAME}
11   Copyright (C) 1991, 93, 94, 95, 96, 97, 98, 99, 2000
12   Free Software Foundation, Inc.
13   Written by Steve Chamberlain <sac@cygnus.com>
14   ELF support by Ian Lance Taylor <ian@cygnus.com>
15
16This file is part of GLD, the Gnu Linker.
17
18This program is free software; you can redistribute it and/or modify
19it under the terms of the GNU General Public License as published by
20the Free Software Foundation; either version 2 of the License, or
21(at your option) any later version.
22
23This program is distributed in the hope that it will be useful,
24but WITHOUT ANY WARRANTY; without even the implied warranty of
25MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26GNU General Public License for more details.
27
28You should have received a copy of the GNU General Public License
29along with this program; if not, write to the Free Software
30Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
31
32#define TARGET_IS_${EMULATION_NAME}
33
34#include "bfd.h"
35#include "sysdep.h"
36
37#include <ctype.h>
38
39#include "bfdlink.h"
40
41#include "ld.h"
42#include "ldmain.h"
43#include "ldemul.h"
44#include "ldfile.h"
45#include "ldmisc.h"
46#include "ldexp.h"
47#include "ldlang.h"
48#include "ldgram.h"
49
50static void gld${EMULATION_NAME}_before_parse PARAMS ((void));
51static boolean gld${EMULATION_NAME}_open_dynamic_archive
52  PARAMS ((const char *, search_dirs_type *, lang_input_statement_type *));
53static void gld${EMULATION_NAME}_after_open PARAMS ((void));
54static void gld${EMULATION_NAME}_check_needed
55  PARAMS ((lang_input_statement_type *));
56static void gld${EMULATION_NAME}_stat_needed
57  PARAMS ((lang_input_statement_type *));
58static boolean gld${EMULATION_NAME}_search_needed
59  PARAMS ((const char *, const char *, int));
60static boolean gld${EMULATION_NAME}_try_needed PARAMS ((const char *, int));
61static void gld${EMULATION_NAME}_vercheck
62  PARAMS ((lang_input_statement_type *));
63static void gld${EMULATION_NAME}_before_allocation PARAMS ((void));
64static void gld${EMULATION_NAME}_find_statement_assignment
65  PARAMS ((lang_statement_union_type *));
66static void gld${EMULATION_NAME}_find_exp_assignment PARAMS ((etree_type *));
67static lang_output_section_statement_type *output_rel_find PARAMS ((void));
68static boolean gld${EMULATION_NAME}_place_orphan
69  PARAMS ((lang_input_statement_type *, asection *));
70static char *gld${EMULATION_NAME}_get_script PARAMS ((int *isfile));
71
72static void
73gld${EMULATION_NAME}_before_parse()
74{
75  ldfile_output_architecture = bfd_arch_`echo ${ARCH} | sed -e 's/:.*//'`;
76  config.dynamic_link = ${DYNAMIC_LINK-true};
77  config.has_shared = `if test -n "$GENERATE_SHLIB_SCRIPT" ; then echo true ; else echo false ; fi`;
78}
79
80/* Try to open a dynamic archive.  This is where we know that ELF
81   dynamic libraries have an extension of .so (or .sl on oddball systems
82   like hpux).  */
83
84static boolean
85gld${EMULATION_NAME}_open_dynamic_archive (arch, search, entry)
86     const char *arch;
87     search_dirs_type *search;
88     lang_input_statement_type *entry;
89{
90  const char *filename;
91  char *string;
92
93  if (! entry->is_archive)
94    return false;
95
96  filename = entry->filename;
97
98  /* This allocates a few bytes too many when EXTRA_SHLIB_EXTENSION
99     is defined, but it does not seem worth the headache to optimize
100     away those two bytes of space.  */
101  string = (char *) xmalloc (strlen (search->name)
102			     + strlen (filename)
103			     + strlen (arch)
104#ifdef EXTRA_SHLIB_EXTENSION
105			     + strlen (EXTRA_SHLIB_EXTENSION)
106#endif
107			     + sizeof "/lib.so");
108
109  sprintf (string, "%s/lib%s%s.so", search->name, filename, arch);
110
111#ifdef EXTRA_SHLIB_EXTENSION
112  /* Try the .so extension first.  If that fails build a new filename
113     using EXTRA_SHLIB_EXTENSION.  */
114  if (! ldfile_try_open_bfd (string, entry))
115    sprintf (string, "%s/lib%s%s%s", search->name,
116	     filename, arch, EXTRA_SHLIB_EXTENSION);
117#endif
118
119  if (! ldfile_try_open_bfd (string, entry))
120    {
121      free (string);
122      return false;
123    }
124
125  entry->filename = string;
126
127  /* We have found a dynamic object to include in the link.  The ELF
128     backend linker will create a DT_NEEDED entry in the .dynamic
129     section naming this file.  If this file includes a DT_SONAME
130     entry, it will be used.  Otherwise, the ELF linker will just use
131     the name of the file.  For an archive found by searching, like
132     this one, the DT_NEEDED entry should consist of just the name of
133     the file, without the path information used to find it.  Note
134     that we only need to do this if we have a dynamic object; an
135     archive will never be referenced by a DT_NEEDED entry.
136
137     FIXME: This approach--using bfd_elf_set_dt_needed_name--is not
138     very pretty.  I haven't been able to think of anything that is
139     pretty, though.  */
140  if (bfd_check_format (entry->the_bfd, bfd_object)
141      && (entry->the_bfd->flags & DYNAMIC) != 0)
142    {
143      char *needed_name;
144
145      ASSERT (entry->is_archive && entry->search_dirs_flag);
146
147      /* Rather than duplicating the logic above.  Just use the
148	 filename we recorded earlier.
149
150	 First strip off everything before the last '/'.  */
151      filename = strrchr (entry->filename, '/');
152      filename++;
153
154      needed_name = (char *) xmalloc (strlen (filename) + 1);
155      strcpy (needed_name, filename);
156      bfd_elf_set_dt_needed_name (entry->the_bfd, needed_name);
157    }
158
159  return true;
160}
161
162EOF
163if [ "x${host}" = "x${target}" ] ; then
164  case " ${EMULATION_LIBPATH} " in
165  *" ${EMULATION_NAME} "*)
166    case ${target} in
167      *-*-freebsd*)
168	cat >>e${EMULATION_NAME}.c <<EOF
169/*
170 * Read the system search path the FreeBSD way rather than like Linux.
171 */
172#include <elf-hints.h>
173
174static boolean gld${EMULATION_NAME}_check_ld_elf_hints
175  PARAMS ((const char *, int));
176
177static boolean
178gld${EMULATION_NAME}_check_ld_elf_hints (name, force)
179     const char *name;
180     int force;
181{
182  static boolean initialized;
183  static char *ld_elf_hints;
184
185  if (! initialized)
186    {
187      FILE *f;
188
189      f = fopen (_PATH_ELF_HINTS, FOPEN_RB);
190      if (f != NULL)
191	{
192	  struct elfhints_hdr hdr;
193
194	  if (fread(&hdr, 1, sizeof(hdr), f) == sizeof(hdr) &&
195	      hdr.magic == ELFHINTS_MAGIC &&
196	      hdr.version == 1)
197	    {
198	      if (fseek(f, hdr.strtab + hdr.dirlist, SEEK_SET) != -1)
199		{
200		  char *b;
201
202		  b = (char *) xmalloc (hdr.dirlistlen + 1);
203		  if (fread(b, 1, hdr.dirlistlen + 1, f) !=
204		      hdr.dirlistlen + 1)
205		    {
206		      free(b);
207		    }
208		  else
209		    {
210		      ld_elf_hints = b;
211		    }
212		}
213	    }
214	  fclose (f);
215	}
216
217      initialized = true;
218    }
219
220  if (ld_elf_hints == NULL)
221    return false;
222
223  return gld${EMULATION_NAME}_search_needed (ld_elf_hints, name, force);
224}
225EOF
226	;;
227
228      *linux*|*Linux*|*LINUX*)
229	cat >>e${EMULATION_NAME}.c <<EOF
230
231/* For a native linker, check the file /etc/ld.so.conf for directories
232   in which we may find shared libraries.  /etc/ld.so.conf is really
233   only meaningful on Linux.  */
234
235static boolean gld${EMULATION_NAME}_check_ld_so_conf
236  PARAMS ((const char *, int));
237
238static boolean
239gld${EMULATION_NAME}_check_ld_so_conf (name, force)
240     const char *name;
241     int force;
242{
243  static boolean initialized;
244  static char *ld_so_conf;
245
246  if (! initialized)
247    {
248      FILE *f;
249
250      f = fopen ("/etc/ld.so.conf", FOPEN_RT);
251      if (f != NULL)
252	{
253	  char *b;
254	  size_t len, alloc;
255	  int c;
256
257	  len = 0;
258	  alloc = 100;
259	  b = (char *) xmalloc (alloc);
260
261	  while ((c = getc (f)) != EOF)
262	    {
263	      if (len + 1 >= alloc)
264		{
265		  alloc *= 2;
266		  b = (char *) xrealloc (b, alloc);
267		}
268	      if (c != ':'
269		  && c != ' '
270		  && c != '\t'
271		  && c != '\n'
272		  && c != ',')
273		{
274		  b[len] = c;
275		  ++len;
276		}
277	      else
278		{
279		  if (len > 0 && b[len - 1] != ':')
280		    {
281		      b[len] = ':';
282		      ++len;
283		    }
284		}
285	    }
286
287	  if (len > 0 && b[len - 1] == ':')
288	    --len;
289
290	  if (len > 0)
291	    b[len] = '\0';
292	  else
293	    {
294	      free (b);
295	      b = NULL;
296	    }
297
298	  fclose (f);
299
300	  ld_so_conf = b;
301	}
302
303      initialized = true;
304    }
305
306  if (ld_so_conf == NULL)
307    return false;
308
309  return gld${EMULATION_NAME}_search_needed (ld_so_conf, name, force);
310}
311
312EOF
313	;;
314    esac
315  esac
316fi
317cat >>e${EMULATION_NAME}.c <<EOF
318
319/* These variables are required to pass information back and forth
320   between after_open and check_needed and stat_needed and vercheck.  */
321
322static struct bfd_link_needed_list *global_needed;
323static struct stat global_stat;
324static boolean global_found;
325static struct bfd_link_needed_list *global_vercheck_needed;
326static boolean global_vercheck_failed;
327
328/* This is called after all the input files have been opened.  */
329
330static void
331gld${EMULATION_NAME}_after_open ()
332{
333  struct bfd_link_needed_list *needed, *l;
334
335  /* We only need to worry about this when doing a final link.  */
336  if (link_info.relocateable || link_info.shared)
337    return;
338
339  /* Get the list of files which appear in DT_NEEDED entries in
340     dynamic objects included in the link (often there will be none).
341     For each such file, we want to track down the corresponding
342     library, and include the symbol table in the link.  This is what
343     the runtime dynamic linker will do.  Tracking the files down here
344     permits one dynamic object to include another without requiring
345     special action by the person doing the link.  Note that the
346     needed list can actually grow while we are stepping through this
347     loop.  */
348  needed = bfd_elf_get_needed_list (output_bfd, &link_info);
349  for (l = needed; l != NULL; l = l->next)
350    {
351      struct bfd_link_needed_list *ll;
352      int force;
353
354      /* If we've already seen this file, skip it.  */
355      for (ll = needed; ll != l; ll = ll->next)
356	if (strcmp (ll->name, l->name) == 0)
357	  break;
358      if (ll != l)
359	continue;
360
361      /* See if this file was included in the link explicitly.  */
362      global_needed = l;
363      global_found = false;
364      lang_for_each_input_file (gld${EMULATION_NAME}_check_needed);
365      if (global_found)
366	continue;
367
368      /* We need to find this file and include the symbol table.  We
369	 want to search for the file in the same way that the dynamic
370	 linker will search.  That means that we want to use
371	 rpath_link, rpath, then the environment variable
372	 LD_LIBRARY_PATH (native only), then the linker script
373	 LIB_SEARCH_DIRS.  We do not search using the -L arguments.
374
375	 We search twice.  The first time, we skip objects which may
376	 introduce version mismatches.  The second time, we force
377	 their use.  See gld${EMULATION_NAME}_vercheck comment.  */
378      for (force = 0; force < 2; force++)
379	{
380	  const char *lib_path;
381	  size_t len;
382	  search_dirs_type *search;
383
384	  if (gld${EMULATION_NAME}_search_needed (command_line.rpath_link,
385						  l->name, force))
386	    break;
387	  if (gld${EMULATION_NAME}_search_needed (command_line.rpath,
388						  l->name, force))
389	    break;
390	  if (command_line.rpath_link == NULL
391	      && command_line.rpath == NULL)
392	    {
393	      lib_path = (const char *) getenv ("LD_RUN_PATH");
394	      if (gld${EMULATION_NAME}_search_needed (lib_path, l->name,
395						      force))
396		break;
397	    }
398EOF
399if [ "x${host}" = "x${target}" ] ; then
400  case " ${EMULATION_LIBPATH} " in
401  *" ${EMULATION_NAME} "*)
402cat >>e${EMULATION_NAME}.c <<EOF
403	  lib_path = (const char *) getenv ("LD_LIBRARY_PATH");
404	  if (gld${EMULATION_NAME}_search_needed (lib_path, l->name, force))
405	    break;
406EOF
407  ;;
408  esac
409fi
410cat >>e${EMULATION_NAME}.c <<EOF
411	  len = strlen (l->name);
412	  for (search = search_head; search != NULL; search = search->next)
413	    {
414	      char *filename;
415
416	      if (search->cmdline)
417		continue;
418	      filename = (char *) xmalloc (strlen (search->name) + len + 2);
419	      sprintf (filename, "%s/%s", search->name, l->name);
420	      if (gld${EMULATION_NAME}_try_needed (filename, force))
421		break;
422	      free (filename);
423	    }
424	  if (search != NULL)
425	    break;
426EOF
427if [ "x${host}" = "x${target}" ] ; then
428  case " ${EMULATION_LIBPATH} " in
429  *" ${EMULATION_NAME} "*)
430    case ${target} in
431      *-*-freebsd*)
432	cat >>e${EMULATION_NAME}.c <<EOF
433	  if (gld${EMULATION_NAME}_check_ld_elf_hints (l->name, force))
434	    break;
435EOF
436        ;;
437      *linux*|*Linux*|*LINUX*)
438	cat >>e${EMULATION_NAME}.c <<EOF
439	  if (gld${EMULATION_NAME}_check_ld_so_conf (l->name, force))
440	    break;
441EOF
442        ;;
443    esac
444  ;;
445  esac
446fi
447cat >>e${EMULATION_NAME}.c <<EOF
448	}
449
450      if (force < 2)
451	continue;
452
453      einfo ("%P: warning: %s, needed by %B, not found (try using -rpath or -rpath-link)\n",
454	     l->name, l->by);
455    }
456}
457
458/* Search for a needed file in a path.  */
459
460static boolean
461gld${EMULATION_NAME}_search_needed (path, name, force)
462     const char *path;
463     const char *name;
464     int force;
465{
466  const char *s;
467  size_t len;
468
469  if (path == NULL || *path == '\0')
470    return false;
471  len = strlen (name);
472  while (1)
473    {
474      char *filename, *sset;
475
476      s = strchr (path, ':');
477      if (s == NULL)
478	s = path + strlen (path);
479
480      filename = (char *) xmalloc (s - path + len + 2);
481      if (s == path)
482	sset = filename;
483      else
484	{
485	  memcpy (filename, path, s - path);
486	  filename[s - path] = '/';
487	  sset = filename + (s - path) + 1;
488	}
489      strcpy (sset, name);
490
491      if (gld${EMULATION_NAME}_try_needed (filename, force))
492	return true;
493
494      free (filename);
495
496      if (*s == '\0')
497	break;
498      path = s + 1;
499    }
500
501  return false;	  
502}
503
504/* This function is called for each possible name for a dynamic object
505   named by a DT_NEEDED entry.  The FORCE parameter indicates whether
506   to skip the check for a conflicting version.  */
507
508static boolean
509gld${EMULATION_NAME}_try_needed (name, force)
510     const char *name;
511     int force;
512{
513  bfd *abfd;
514
515  abfd = bfd_openr (name, bfd_get_target (output_bfd));
516  if (abfd == NULL)
517    return false;
518  if (! bfd_check_format (abfd, bfd_object))
519    {
520      (void) bfd_close (abfd);
521      return false;
522    }
523  if ((bfd_get_file_flags (abfd) & DYNAMIC) == 0)
524    {
525      (void) bfd_close (abfd);
526      return false;
527    }
528
529  /* Check whether this object would include any conflicting library
530     versions.  If FORCE is set, then we skip this check; we use this
531     the second time around, if we couldn't find any compatible
532     instance of the shared library.  */
533
534  if (! force)
535    {
536      struct bfd_link_needed_list *needed;
537
538      if (! bfd_elf_get_bfd_needed_list (abfd, &needed))
539	einfo ("%F%P:%B: bfd_elf_get_bfd_needed_list failed: %E\n", abfd);
540
541      if (needed != NULL)
542	{
543	  global_vercheck_needed = needed;
544	  global_vercheck_failed = false;
545	  lang_for_each_input_file (gld${EMULATION_NAME}_vercheck);
546	  if (global_vercheck_failed)
547	    {
548	      (void) bfd_close (abfd);
549	      /* Return false to force the caller to move on to try
550                 another file on the search path.  */
551	      return false;
552	    }
553
554	  /* But wait!  It gets much worse.  On Linux, if a shared
555             library does not use libc at all, we are supposed to skip
556             it the first time around in case we encounter a shared
557             library later on with the same name which does use the
558             version of libc that we want.  This is much too horrible
559             to use on any system other than Linux.  */
560
561EOF
562case ${target} in
563  *-*-linux-gnu*)
564    cat >>e${EMULATION_NAME}.c <<EOF
565	  {
566	    struct bfd_link_needed_list *l;
567
568	    for (l = needed; l != NULL; l = l->next)
569	      if (strncmp (l->name, "libc.so", 7) == 0)
570		break;
571	    if (l == NULL)
572	      {
573		(void) bfd_close (abfd);
574		return false;
575	      }
576	  }
577
578EOF
579    ;;
580esac
581cat >>e${EMULATION_NAME}.c <<EOF
582	}
583    }
584
585  /* We've found a dynamic object matching the DT_NEEDED entry.  */
586
587  /* We have already checked that there is no other input file of the
588     same name.  We must now check again that we are not including the
589     same file twice.  We need to do this because on many systems
590     libc.so is a symlink to, e.g., libc.so.1.  The SONAME entry will
591     reference libc.so.1.  If we have already included libc.so, we
592     don't want to include libc.so.1 if they are the same file, and we
593     can only check that using stat.  */
594
595  if (bfd_stat (abfd, &global_stat) != 0)
596    einfo ("%F%P:%B: bfd_stat failed: %E\n", abfd);
597  global_found = false;
598  lang_for_each_input_file (gld${EMULATION_NAME}_stat_needed);
599  if (global_found)
600    {
601      /* Return true to indicate that we found the file, even though
602         we aren't going to do anything with it.  */
603      return true;
604    }
605
606  /* Tell the ELF backend that don't want the output file to have a
607     DT_NEEDED entry for this file.  */
608  bfd_elf_set_dt_needed_name (abfd, "");
609
610  /* Add this file into the symbol table.  */
611  if (! bfd_link_add_symbols (abfd, &link_info))
612    einfo ("%F%B: could not read symbols: %E\n", abfd);
613
614  return true;
615}
616
617/* See if an input file matches a DT_NEEDED entry by name.  */
618
619static void
620gld${EMULATION_NAME}_check_needed (s)
621     lang_input_statement_type *s;
622{
623  if (global_found)
624    return;
625
626  if (s->filename != NULL
627      && strcmp (s->filename, global_needed->name) == 0)
628    {
629      global_found = true;
630      return;
631    }
632
633  if (s->the_bfd != NULL)
634    {
635      const char *soname;
636
637      soname = bfd_elf_get_dt_soname (s->the_bfd);
638      if (soname != NULL
639	  && strcmp (soname, global_needed->name) == 0)
640	{
641	  global_found = true;
642	  return;
643	}
644    }
645	  
646  if (s->search_dirs_flag
647      && s->filename != NULL
648      && strchr (global_needed->name, '/') == NULL)
649    {
650      const char *f;
651
652      f = strrchr (s->filename, '/');
653      if (f != NULL
654	  && strcmp (f + 1, global_needed->name) == 0)
655	{
656	  global_found = true;
657	  return;
658	}
659    }
660}
661
662/* See if an input file matches a DT_NEEDED entry by running stat on
663   the file.  */
664
665static void
666gld${EMULATION_NAME}_stat_needed (s)
667     lang_input_statement_type *s;
668{
669  struct stat st;
670  const char *suffix;
671  const char *soname;
672  const char *f;
673
674  if (global_found)
675    return;
676  if (s->the_bfd == NULL)
677    return;
678
679  if (bfd_stat (s->the_bfd, &st) != 0)
680    {
681      einfo ("%P:%B: bfd_stat failed: %E\n", s->the_bfd);
682      return;
683    }
684
685  if (st.st_dev == global_stat.st_dev
686      && st.st_ino == global_stat.st_ino)
687    {
688      global_found = true;
689      return;
690    }
691
692  /* We issue a warning if it looks like we are including two
693     different versions of the same shared library.  For example,
694     there may be a problem if -lc picks up libc.so.6 but some other
695     shared library has a DT_NEEDED entry of libc.so.5.  This is a
696     hueristic test, and it will only work if the name looks like
697     NAME.so.VERSION.  FIXME: Depending on file names is error-prone.
698     If we really want to issue warnings about mixing version numbers
699     of shared libraries, we need to find a better way.  */
700
701  if (strchr (global_needed->name, '/') != NULL)
702    return;
703  suffix = strstr (global_needed->name, ".so.");
704  if (suffix == NULL)
705    return;
706  suffix += sizeof ".so." - 1;
707
708  soname = bfd_elf_get_dt_soname (s->the_bfd);
709  if (soname == NULL)
710    soname = s->filename;
711
712  f = strrchr (soname, '/');
713  if (f != NULL)
714    ++f;
715  else
716    f = soname;
717
718  if (strncmp (f, global_needed->name, suffix - global_needed->name) == 0)
719    einfo ("%P: warning: %s, needed by %B, may conflict with %s\n",
720	   global_needed->name, global_needed->by, f);
721}
722
723/* On Linux, it's possible to have different versions of the same
724   shared library linked against different versions of libc.  The
725   dynamic linker somehow tags which libc version to use in
726   /etc/ld.so.cache, and, based on the libc that it sees in the
727   executable, chooses which version of the shared library to use.
728
729   We try to do a similar check here by checking whether this shared
730   library needs any other shared libraries which may conflict with
731   libraries we have already included in the link.  If it does, we
732   skip it, and try to find another shared library farther on down the
733   link path.
734
735   This is called via lang_for_each_input_file.
736   GLOBAL_VERCHECK_NEEDED is the list of objects needed by the object
737   which we ar checking.  This sets GLOBAL_VERCHECK_FAILED if we find
738   a conflicting version.  */
739
740static void
741gld${EMULATION_NAME}_vercheck (s)
742     lang_input_statement_type *s;
743{
744  const char *soname, *f;
745  struct bfd_link_needed_list *l;
746
747  if (global_vercheck_failed)
748    return;
749  if (s->the_bfd == NULL
750      || (bfd_get_file_flags (s->the_bfd) & DYNAMIC) == 0)
751    return;
752
753  soname = bfd_elf_get_dt_soname (s->the_bfd);
754  if (soname == NULL)
755    soname = bfd_get_filename (s->the_bfd);
756
757  f = strrchr (soname, '/');
758  if (f != NULL)
759    ++f;
760  else
761    f = soname;
762
763  for (l = global_vercheck_needed; l != NULL; l = l->next)
764    {
765      const char *suffix;
766
767      if (strcmp (f, l->name) == 0)
768	{
769	  /* Probably can't happen, but it's an easy check.  */
770	  continue;
771	}
772
773      if (strchr (l->name, '/') != NULL)
774	continue;
775
776      suffix = strstr (l->name, ".so.");
777      if (suffix == NULL)
778	continue;
779
780      suffix += sizeof ".so." - 1;
781
782      if (strncmp (f, l->name, suffix - l->name) == 0)
783	{
784	  /* Here we know that S is a dynamic object FOO.SO.VER1, and
785             the object we are considering needs a dynamic object
786             FOO.SO.VER2, and VER1 and VER2 are different.  This
787             appears to be a version mismatch, so we tell the caller
788             to try a different version of this library.  */
789	  global_vercheck_failed = true;
790	  return;
791	}
792    }
793}
794
795/* This is called after the sections have been attached to output
796   sections, but before any sizes or addresses have been set.  */
797
798static void
799gld${EMULATION_NAME}_before_allocation ()
800{
801  const char *rpath;
802  asection *sinterp;
803
804  /* If we are going to make any variable assignments, we need to let
805     the ELF backend know about them in case the variables are
806     referred to by dynamic objects.  */
807  lang_for_each_statement (gld${EMULATION_NAME}_find_statement_assignment);
808
809  /* Let the ELF backend work out the sizes of any sections required
810     by dynamic linking.  */
811  rpath = command_line.rpath;
812  if (rpath == NULL)
813    rpath = (const char *) getenv ("LD_RUN_PATH");
814  if (! (bfd_elf${ELFSIZE}_size_dynamic_sections
815         (output_bfd, command_line.soname, rpath,
816	  command_line.export_dynamic, command_line.filter_shlib,
817	  (const char * const *) command_line.auxiliary_filters,
818	  &link_info, &sinterp, lang_elf_version_info)))
819    einfo ("%P%F: failed to set dynamic section sizes: %E\n");
820
821  /* Let the user override the dynamic linker we are using.  */
822  if (command_line.interpreter != NULL
823      && sinterp != NULL)
824    {
825      sinterp->contents = (bfd_byte *) command_line.interpreter;
826      sinterp->_raw_size = strlen (command_line.interpreter) + 1;
827    }
828
829  /* Look for any sections named .gnu.warning.  As a GNU extensions,
830     we treat such sections as containing warning messages.  We print
831     out the warning message, and then zero out the section size so
832     that it does not get copied into the output file.  */
833
834  {
835    LANG_FOR_EACH_INPUT_STATEMENT (is)
836      {
837	asection *s;
838	bfd_size_type sz;
839	char *msg;
840	boolean ret;
841
842	if (is->just_syms_flag)
843	  continue;
844
845	s = bfd_get_section_by_name (is->the_bfd, ".gnu.warning");
846	if (s == NULL)
847	  continue;
848
849	sz = bfd_section_size (is->the_bfd, s);
850	msg = xmalloc ((size_t) sz + 1);
851	if (! bfd_get_section_contents (is->the_bfd, s, msg, (file_ptr) 0, sz))
852	  einfo ("%F%B: Can't read contents of section .gnu.warning: %E\n",
853		 is->the_bfd);
854	msg[sz] = '\0';
855	ret = link_info.callbacks->warning (&link_info, msg,
856					    (const char *) NULL,
857					    is->the_bfd, (asection *) NULL,
858					    (bfd_vma) 0);
859	ASSERT (ret);
860	free (msg);
861
862	/* Clobber the section size, so that we don't waste copying the
863	   warning into the output file.  */
864	s->_raw_size = 0;
865      }
866  }
867}
868
869/* This is called by the before_allocation routine via
870   lang_for_each_statement.  It locates any assignment statements, and
871   tells the ELF backend about them, in case they are assignments to
872   symbols which are referred to by dynamic objects.  */
873
874static void
875gld${EMULATION_NAME}_find_statement_assignment (s)
876     lang_statement_union_type *s;
877{
878  if (s->header.type == lang_assignment_statement_enum)
879    gld${EMULATION_NAME}_find_exp_assignment (s->assignment_statement.exp);
880}
881
882/* Look through an expression for an assignment statement.  */
883
884static void
885gld${EMULATION_NAME}_find_exp_assignment (exp)
886     etree_type *exp;
887{
888  struct bfd_link_hash_entry *h;
889
890  switch (exp->type.node_class)
891    {
892    case etree_provide:
893      h = bfd_link_hash_lookup (link_info.hash, exp->assign.dst,
894				false, false, false);
895      if (h == NULL)
896	break;
897
898      /* We call record_link_assignment even if the symbol is defined.
899	 This is because if it is defined by a dynamic object, we
900	 actually want to use the value defined by the linker script,
901	 not the value from the dynamic object (because we are setting
902	 symbols like etext).  If the symbol is defined by a regular
903	 object, then, as it happens, calling record_link_assignment
904	 will do no harm.  */
905
906      /* Fall through.  */
907    case etree_assign:
908      if (strcmp (exp->assign.dst, ".") != 0)
909	{
910	  if (! (bfd_elf${ELFSIZE}_record_link_assignment
911		 (output_bfd, &link_info, exp->assign.dst,
912		  exp->type.node_class == etree_provide ? true : false)))
913	    einfo ("%P%F: failed to record assignment to %s: %E\n",
914		   exp->assign.dst);
915	}
916      gld${EMULATION_NAME}_find_exp_assignment (exp->assign.src);
917      break;
918
919    case etree_binary:
920      gld${EMULATION_NAME}_find_exp_assignment (exp->binary.lhs);
921      gld${EMULATION_NAME}_find_exp_assignment (exp->binary.rhs);
922      break;
923
924    case etree_trinary:
925      gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.cond);
926      gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.lhs);
927      gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.rhs);
928      break;
929
930    case etree_unary:
931      gld${EMULATION_NAME}_find_exp_assignment (exp->unary.child);
932      break;
933
934    default:
935      break;
936    }
937}
938
939/* A variant of lang_output_section_find.  Used by place_orphan.  */
940
941static lang_output_section_statement_type *
942output_rel_find ()
943{
944  lang_statement_union_type *u;
945  lang_output_section_statement_type *lookup;
946
947  for (u = lang_output_section_statement.head;
948       u != (lang_statement_union_type *) NULL;
949       u = lookup->next)
950    {
951      lookup = &u->output_section_statement;
952      if (strncmp (".rel", lookup->name, 4) == 0
953	  && lookup->bfd_section != NULL
954	  && (lookup->bfd_section->flags & SEC_ALLOC) != 0)
955	{
956	  return lookup;
957	}
958    }
959  return (lang_output_section_statement_type *) NULL;
960}
961
962/* Place an orphan section.  We use this to put random SHF_ALLOC
963   sections in the right segment.  */
964
965struct orphan_save {
966  lang_output_section_statement_type *os;
967  asection **section;
968  lang_statement_union_type **stmt;
969};
970
971static boolean
972gld${EMULATION_NAME}_place_orphan (file, s)
973     lang_input_statement_type *file;
974     asection *s;
975{
976  static struct orphan_save hold_text;
977  static struct orphan_save hold_rodata;
978  static struct orphan_save hold_data;
979  static struct orphan_save hold_bss;
980  static struct orphan_save hold_rel;
981  static struct orphan_save hold_interp;
982  struct orphan_save *place;
983  lang_statement_list_type *old;
984  lang_statement_list_type add;
985  etree_type *address;
986  const char *secname;
987  const char *outsecname;
988  const char *ps = NULL;
989  lang_output_section_statement_type *os;
990
991  secname = bfd_get_section_name (s->owner, s);
992
993  /* Look through the script to see where to place this section.  */ 
994  os = lang_output_section_find (secname);
995
996  if (os != NULL
997      && os->bfd_section != NULL
998      && ((s->flags ^ os->bfd_section->flags) & (SEC_LOAD | SEC_ALLOC)) == 0)
999    {
1000      /* We have already placed a section with this name.  */
1001      wild_doit (&os->children, s, os, file);
1002      return true;
1003    }
1004
1005  if (hold_text.os == NULL)
1006    hold_text.os = lang_output_section_find (".text");
1007
1008  /* If this is a final link, then always put .gnu.warning.SYMBOL
1009     sections into the .text section to get them out of the way.  */
1010  if (! link_info.shared
1011      && ! link_info.relocateable
1012      && strncmp (secname, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0
1013      && hold_text.os != NULL)
1014    {
1015      wild_doit (&hold_text.os->children, s, hold_text.os, file);
1016      return true;
1017    }
1018
1019  /* Decide which segment the section should go in based on the
1020     section name and section flags.  We put loadable .note sections
1021     right after the .interp section, so that the PT_NOTE segment is
1022     stored right after the program headers where the OS can read it
1023     in the first page.  */
1024#define HAVE_SECTION(hold, name) \
1025(hold.os != NULL || (hold.os = lang_output_section_find (name)) != NULL)
1026
1027  if (s->flags & SEC_EXCLUDE)
1028    return false;
1029
1030  place = NULL;
1031  if ((s->flags & SEC_ALLOC) == 0)
1032    ;
1033  else if ((s->flags & SEC_LOAD) != 0
1034	   && strncmp (secname, ".note", 4) == 0
1035	   && HAVE_SECTION (hold_interp, ".interp"))
1036    place = &hold_interp;
1037  else if ((s->flags & SEC_HAS_CONTENTS) == 0
1038	   && HAVE_SECTION (hold_bss, ".bss"))
1039    place = &hold_bss;
1040  else if ((s->flags & SEC_READONLY) == 0
1041	   && HAVE_SECTION (hold_data, ".data"))
1042    place = &hold_data;
1043  else if (strncmp (secname, ".rel", 4) == 0
1044	   && (hold_rel.os != NULL
1045	       || (hold_rel.os = output_rel_find ()) != NULL))
1046    place = &hold_rel;
1047  else if ((s->flags & (SEC_CODE | SEC_READONLY)) == SEC_READONLY
1048	   && HAVE_SECTION (hold_rodata, ".rodata"))
1049    place = &hold_rodata;
1050  else if ((s->flags & (SEC_CODE | SEC_READONLY)) == (SEC_CODE | SEC_READONLY)
1051	   && hold_text.os != NULL)
1052    place = &hold_text;
1053
1054#undef HAVE_SECTION
1055
1056  /* Choose a unique name for the section.  This will be needed if the
1057     same section name appears in the input file with different
1058     loadable or allocateable characteristics.  */
1059  outsecname = secname;
1060  if (bfd_get_section_by_name (output_bfd, outsecname) != NULL)
1061    {
1062      unsigned int len;
1063      char *newname;
1064      unsigned int i;
1065
1066      len = strlen (outsecname);
1067      newname = xmalloc (len + 5);
1068      strcpy (newname, outsecname);
1069      i = 0;
1070      do
1071	{
1072	  sprintf (newname + len, "%d", i);
1073	  ++i;
1074	}
1075      while (bfd_get_section_by_name (output_bfd, newname) != NULL);
1076
1077      outsecname = newname;
1078    }
1079
1080  /* Start building a list of statements for this section.
1081     First save the current statement pointer.  */
1082  old = stat_ptr;
1083
1084  /* If we have found an appropriate place for the output section
1085     statements for this orphan, add them to our own private list,
1086     inserting them later into the global statement list.  */
1087  if (place != NULL)
1088    {
1089      stat_ptr = &add;
1090      lang_list_init (stat_ptr);
1091    }
1092
1093  if (config.build_constructors)
1094    {
1095      /* If the name of the section is representable in C, then create
1096	 symbols to mark the start and the end of the section.  */
1097      for (ps = outsecname; *ps != '\0'; ps++)
1098	if (! isalnum ((unsigned char) *ps) && *ps != '_')
1099	  break;
1100      if (*ps == '\0')
1101	{
1102	  char *symname;
1103	  etree_type *e_align;
1104
1105	  symname = (char *) xmalloc (ps - outsecname + sizeof "__start_");
1106	  sprintf (symname, "__start_%s", outsecname);
1107	  e_align = exp_unop (ALIGN_K,
1108			      exp_intop ((bfd_vma) 1 << s->alignment_power));
1109	  lang_add_assignment (exp_assop ('=', symname, e_align));
1110	}
1111    }
1112
1113  if (link_info.relocateable || (s->flags & (SEC_LOAD | SEC_ALLOC)) == 0)
1114    address = exp_intop ((bfd_vma) 0);
1115  else
1116    address = NULL;
1117
1118  os = lang_enter_output_section_statement (outsecname, address, 0,
1119					    (bfd_vma) 0,
1120					    (etree_type *) NULL,
1121					    (etree_type *) NULL,
1122					    (etree_type *) NULL);
1123
1124  wild_doit (&os->children, s, os, file);
1125
1126  lang_leave_output_section_statement
1127    ((bfd_vma) 0, "*default*",
1128     (struct lang_output_section_phdr_list *) NULL, "*default*");
1129
1130  if (config.build_constructors && *ps == '\0')
1131    {
1132      char *symname;
1133
1134      /* lang_leave_ouput_section_statement resets stat_ptr.  Put
1135	 stat_ptr back where we want it.  */
1136      if (place != NULL)
1137	stat_ptr = &add;
1138
1139      symname = (char *) xmalloc (ps - outsecname + sizeof "__stop_");
1140      sprintf (symname, "__stop_%s", outsecname);
1141      lang_add_assignment (exp_assop ('=', symname,
1142				      exp_nameop (NAME, ".")));
1143    }
1144
1145  /* Restore the global list pointer.  */
1146  stat_ptr = old;
1147
1148  if (place != NULL)
1149    {
1150      asection *snew, **pps;
1151
1152      snew = os->bfd_section;
1153      if (place->section != NULL
1154	  || (place->os->bfd_section != NULL
1155	      && place->os->bfd_section != snew))
1156	{
1157	  /* Shuffle the section to make the output file look neater.
1158	     This is really only cosmetic.  */
1159	  if (place->section == NULL)
1160	    {
1161#if 0
1162	      /* Finding the end of the list is a little tricky.  We
1163		 make a wild stab at it by comparing section flags.  */
1164	      flagword first_flags = place->os->bfd_section->flags;
1165	      for (pps = &place->os->bfd_section->next;
1166		   *pps != NULL && (*pps)->flags == first_flags;
1167		   pps = &(*pps)->next)
1168		;
1169	      place->section = pps;
1170#else
1171	      /* Put orphans after the first section on the list.  */
1172	      place->section = &place->os->bfd_section->next;
1173#endif
1174	    }
1175
1176	  /*  Unlink the section.  */
1177	  for (pps = &output_bfd->sections; *pps != snew; pps = &(*pps)->next)
1178	    ;
1179	  *pps = snew->next;
1180
1181	  /* Now tack it on to the "place->os" section list.  */
1182	  snew->next = *place->section;
1183	  *place->section = snew;
1184	}
1185      place->section = &snew->next;	/* Save the end of this list.  */
1186
1187      if (add.head != NULL)
1188	{
1189	  /* We try to put the output statements in some sort of
1190	     reasonable order here, because they determine the final
1191	     load addresses of the orphan sections.  */
1192	  if (place->stmt == NULL)
1193	    {
1194	      /* Put the new statement list right at the head.  */
1195	      *add.tail = place->os->header.next;
1196	      place->os->header.next = add.head;
1197	    }
1198	  else
1199	    {
1200	      /* Put it after the last orphan statement we added.  */
1201	      *add.tail = *place->stmt;
1202	      *place->stmt = add.head;
1203	    }
1204
1205	  /* Fix the global list pointer if we happened to tack our
1206	     new list at the tail.  */
1207	  if (*old->tail == add.head)
1208	    old->tail = add.tail;
1209
1210	  /* Save the end of this list.  */
1211	  place->stmt = add.tail;
1212	}
1213    }
1214
1215  return true;
1216}
1217
1218static char *
1219gld${EMULATION_NAME}_get_script(isfile)
1220     int *isfile;
1221EOF
1222
1223if test -n "$COMPILE_IN"
1224then
1225# Scripts compiled in.
1226
1227# sed commands to quote an ld script as a C string.
1228sc="-f stringify.sed"
1229
1230cat >>e${EMULATION_NAME}.c <<EOF
1231{			     
1232  *isfile = 0;
1233
1234  if (link_info.relocateable == true && config.build_constructors == true)
1235    return
1236EOF
1237sed $sc ldscripts/${EMULATION_NAME}.xu                     >> e${EMULATION_NAME}.c
1238echo '  ; else if (link_info.relocateable == true) return' >> e${EMULATION_NAME}.c
1239sed $sc ldscripts/${EMULATION_NAME}.xr                     >> e${EMULATION_NAME}.c
1240echo '  ; else if (!config.text_read_only) return'         >> e${EMULATION_NAME}.c
1241sed $sc ldscripts/${EMULATION_NAME}.xbn                    >> e${EMULATION_NAME}.c
1242echo '  ; else if (!config.magic_demand_paged) return'     >> e${EMULATION_NAME}.c
1243sed $sc ldscripts/${EMULATION_NAME}.xn                     >> e${EMULATION_NAME}.c
1244
1245if test -n "$GENERATE_SHLIB_SCRIPT" ; then
1246echo '  ; else if (link_info.shared) return'		   >> e${EMULATION_NAME}.c
1247sed $sc ldscripts/${EMULATION_NAME}.xs                     >> e${EMULATION_NAME}.c
1248fi
1249
1250echo '  ; else return'                                     >> e${EMULATION_NAME}.c
1251sed $sc ldscripts/${EMULATION_NAME}.x                      >> e${EMULATION_NAME}.c
1252echo '; }'                                                 >> e${EMULATION_NAME}.c
1253
1254else
1255# Scripts read from the filesystem.
1256
1257cat >>e${EMULATION_NAME}.c <<EOF
1258{			     
1259  *isfile = 1;
1260
1261  if (link_info.relocateable == true && config.build_constructors == true)
1262    return "ldscripts/${EMULATION_NAME}.xu";
1263  else if (link_info.relocateable == true)
1264    return "ldscripts/${EMULATION_NAME}.xr";
1265  else if (!config.text_read_only)
1266    return "ldscripts/${EMULATION_NAME}.xbn";
1267  else if (!config.magic_demand_paged)
1268    return "ldscripts/${EMULATION_NAME}.xn";
1269  else if (link_info.shared)
1270    return "ldscripts/${EMULATION_NAME}.xs";
1271  else
1272    return "ldscripts/${EMULATION_NAME}.x";
1273}
1274EOF
1275
1276fi
1277
1278if test -n "$PARSE_AND_LIST_ARGS" ; then
1279cat >>e${EMULATION_NAME}.c <<EOF
1280static int  gld_${EMULATION_NAME}_parse_args PARAMS ((int, char **));
1281static void gld_${EMULATION_NAME}_list_options PARAMS ((FILE * file));
1282
1283 $PARSE_AND_LIST_ARGS
1284EOF
1285else
1286
1287cat >>e${EMULATION_NAME}.c <<EOF
1288#define gld_${EMULATION_NAME}_parse_args   NULL
1289#define gld_${EMULATION_NAME}_list_options NULL
1290EOF
1291
1292fi
1293
1294cat >>e${EMULATION_NAME}.c <<EOF
1295
1296struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation = 
1297{
1298  gld${EMULATION_NAME}_before_parse,
1299  syslib_default,
1300  hll_default,
1301  after_parse_default,
1302  gld${EMULATION_NAME}_after_open,
1303  after_allocation_default,
1304  set_output_arch_default,
1305  ldemul_default_target,
1306  gld${EMULATION_NAME}_before_allocation,
1307  gld${EMULATION_NAME}_get_script,
1308  "${EMULATION_NAME}",
1309  "${OUTPUT_FORMAT}",
1310  NULL, 	/* finish */
1311  NULL, 	/* create output section statements */
1312  gld${EMULATION_NAME}_open_dynamic_archive,
1313  gld${EMULATION_NAME}_place_orphan,
1314  NULL,		/* set_symbols */
1315  gld_${EMULATION_NAME}_parse_args,
1316  NULL,		/* unrecognized_file */
1317  gld_${EMULATION_NAME}_list_options,
1318  NULL,		/* recognized_file */
1319  NULL		/* find_potential_libraries */
1320};
1321EOF
1322