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