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