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