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