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