2000-07-20 H.J. Lu <hjl@gnu.org>
[binutils.git] / ld / emultempl / elf32.em
blob0662e221afe421f5a1d9ba2de2a9936657d8993e
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 cat >e${EMULATION_NAME}.c <<EOF
7 /* This file is is generated by a shell script.  DO NOT EDIT! */
9 /* ${ELFSIZE} bit ELF emulation code for ${EMULATION_NAME}
10    Copyright (C) 1991, 93, 94, 95, 96, 97, 98, 99, 2000
11    Free Software Foundation, Inc.
12    Written by Steve Chamberlain <sac@cygnus.com>
13    ELF support by Ian Lance Taylor <ian@cygnus.com>
15 This file is part of GLD, the Gnu Linker.
17 This program is free software; you can redistribute it and/or modify
18 it under the terms of the GNU General Public License as published by
19 the Free Software Foundation; either version 2 of the License, or
20 (at your option) any later version.
22 This program is distributed in the hope that it will be useful,
23 but WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25 GNU General Public License for more details.
27 You should have received a copy of the GNU General Public License
28 along with this program; if not, write to the Free Software
29 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
31 #define TARGET_IS_${EMULATION_NAME}
33 #include "bfd.h"
34 #include "sysdep.h"
36 #include <ctype.h>
38 #include "bfdlink.h"
40 #include "ld.h"
41 #include "ldmain.h"
42 #include "ldmisc.h"
43 #include "ldexp.h"
44 #include "ldlang.h"
45 #include "ldfile.h"
46 #include "ldemul.h"
47 #include "ldgram.h"
48 #include "elf/common.h"
50 static void gld${EMULATION_NAME}_before_parse PARAMS ((void));
51 static boolean gld${EMULATION_NAME}_open_dynamic_archive
52   PARAMS ((const char *, search_dirs_type *, lang_input_statement_type *));
53 static void gld${EMULATION_NAME}_after_open PARAMS ((void));
54 static void gld${EMULATION_NAME}_check_needed
55   PARAMS ((lang_input_statement_type *));
56 static void gld${EMULATION_NAME}_stat_needed
57   PARAMS ((lang_input_statement_type *));
58 static boolean gld${EMULATION_NAME}_search_needed
59   PARAMS ((const char *, const char *, int));
60 static boolean gld${EMULATION_NAME}_try_needed PARAMS ((const char *, int));
61 static void gld${EMULATION_NAME}_vercheck
62   PARAMS ((lang_input_statement_type *));
63 static void gld${EMULATION_NAME}_before_allocation PARAMS ((void));
64 static void gld${EMULATION_NAME}_find_statement_assignment
65   PARAMS ((lang_statement_union_type *));
66 static void gld${EMULATION_NAME}_find_exp_assignment PARAMS ((etree_type *));
67 static boolean gld${EMULATION_NAME}_place_orphan
68   PARAMS ((lang_input_statement_type *, asection *));
69 static lang_output_section_statement_type *output_rel_find PARAMS ((void));
70 static char *gld${EMULATION_NAME}_get_script PARAMS ((int *isfile));
72 static void
73 gld${EMULATION_NAME}_before_parse()
75   ldfile_output_architecture = bfd_arch_`echo ${ARCH} | sed -e 's/:.*//'`;
76   config.dynamic_link = ${DYNAMIC_LINK-true};
77   config.has_shared = `if test -n "$GENERATE_SHLIB_SCRIPT" ; then echo true ; else echo false ; fi`;
80 /* Try to open a dynamic archive.  This is where we know that ELF
81    dynamic libraries have an extension of .so (or .sl on oddball systems
82    like hpux).  */
84 static boolean
85 gld${EMULATION_NAME}_open_dynamic_archive (arch, search, entry)
86      const char *arch;
87      search_dirs_type *search;
88      lang_input_statement_type *entry;
90   const char *filename;
91   char *string;
93   if (! entry->is_archive)
94     return false;
96   filename = entry->filename;
98   /* This allocates a few bytes too many when EXTRA_SHLIB_EXTENSION
99      is defined, but it does not seem worth the headache to optimize
100      away those two bytes of space.  */
101   string = (char *) xmalloc (strlen (search->name)
102                              + strlen (filename)
103                              + strlen (arch)
104 #ifdef EXTRA_SHLIB_EXTENSION
105                              + strlen (EXTRA_SHLIB_EXTENSION)
106 #endif
107                              + sizeof "/lib.so");
109   sprintf (string, "%s/lib%s%s.so", search->name, filename, arch);
111 #ifdef EXTRA_SHLIB_EXTENSION
112   /* Try the .so extension first.  If that fails build a new filename
113      using EXTRA_SHLIB_EXTENSION.  */
114   if (! ldfile_try_open_bfd (string, entry))
115     sprintf (string, "%s/lib%s%s%s", search->name,
116              filename, arch, EXTRA_SHLIB_EXTENSION);
117 #endif
119   if (! ldfile_try_open_bfd (string, entry))
120     {
121       free (string);
122       return false;
123     }
125   entry->filename = string;
127   /* We have found a dynamic object to include in the link.  The ELF
128      backend linker will create a DT_NEEDED entry in the .dynamic
129      section naming this file.  If this file includes a DT_SONAME
130      entry, it will be used.  Otherwise, the ELF linker will just use
131      the name of the file.  For an archive found by searching, like
132      this one, the DT_NEEDED entry should consist of just the name of
133      the file, without the path information used to find it.  Note
134      that we only need to do this if we have a dynamic object; an
135      archive will never be referenced by a DT_NEEDED entry.
137      FIXME: This approach--using bfd_elf_set_dt_needed_name--is not
138      very pretty.  I haven't been able to think of anything that is
139      pretty, though.  */
140   if (bfd_check_format (entry->the_bfd, bfd_object)
141       && (entry->the_bfd->flags & DYNAMIC) != 0)
142     {
143       char *needed_name;
145       ASSERT (entry->is_archive && entry->search_dirs_flag);
147       /* Rather than duplicating the logic above.  Just use the
148          filename we recorded earlier.
150          First strip off everything before the last '/'.  */
151       filename = strrchr (entry->filename, '/');
152       filename++;
154       needed_name = (char *) xmalloc (strlen (filename) + 1);
155       strcpy (needed_name, filename);
156       bfd_elf_set_dt_needed_name (entry->the_bfd, needed_name);
157     }
159   return true;
163 if [ "x${host}" = "x${target}" ] ; then
164   case " ${EMULATION_LIBPATH} " in
165   *" ${EMULATION_NAME} "*)
166 cat >>e${EMULATION_NAME}.c <<EOF
168 /* For a native linker, check the file /etc/ld.so.conf for directories
169    in which we may find shared libraries.  /etc/ld.so.conf is really
170    only meaningful on Linux, but we check it on other systems anyhow.  */
172 static boolean gld${EMULATION_NAME}_check_ld_so_conf
173   PARAMS ((const char *, int));
175 static boolean
176 gld${EMULATION_NAME}_check_ld_so_conf (name, force)
177      const char *name;
178      int force;
180   static boolean initialized;
181   static char *ld_so_conf;
183   if (! initialized)
184     {
185       FILE *f;
187       f = fopen ("/etc/ld.so.conf", FOPEN_RT);
188       if (f != NULL)
189         {
190           char *b;
191           size_t len, alloc;
192           int c;
194           len = 0;
195           alloc = 100;
196           b = (char *) xmalloc (alloc);
198           while ((c = getc (f)) != EOF)
199             {
200               if (len + 1 >= alloc)
201                 {
202                   alloc *= 2;
203                   b = (char *) xrealloc (b, alloc);
204                 }
205               if (c != ':'
206                   && c != ' '
207                   && c != '\t'
208                   && c != '\n'
209                   && c != ',')
210                 {
211                   b[len] = c;
212                   ++len;
213                 }
214               else
215                 {
216                   if (len > 0 && b[len - 1] != ':')
217                     {
218                       b[len] = ':';
219                       ++len;
220                     }
221                 }
222             }
224           if (len > 0 && b[len - 1] == ':')
225             --len;
227           if (len > 0)
228             b[len] = '\0';
229           else
230             {
231               free (b);
232               b = NULL;
233             }
235           fclose (f);
237           ld_so_conf = b;
238         }
240       initialized = true;
241     }
243   if (ld_so_conf == NULL)
244     return false;
246   return gld${EMULATION_NAME}_search_needed (ld_so_conf, name, force);
250   ;;
251   esac
253 cat >>e${EMULATION_NAME}.c <<EOF
255 /* These variables are required to pass information back and forth
256    between after_open and check_needed and stat_needed and vercheck.  */
258 static struct bfd_link_needed_list *global_needed;
259 static struct stat global_stat;
260 static boolean global_found;
261 static struct bfd_link_needed_list *global_vercheck_needed;
262 static boolean global_vercheck_failed;
264 /* This is called after all the input files have been opened.  */
266 static void
267 gld${EMULATION_NAME}_after_open ()
269   struct bfd_link_needed_list *needed, *l;
271   /* We only need to worry about this when doing a final link.  */
272   if (link_info.relocateable || link_info.shared)
273     return;
275   /* Get the list of files which appear in DT_NEEDED entries in
276      dynamic objects included in the link (often there will be none).
277      For each such file, we want to track down the corresponding
278      library, and include the symbol table in the link.  This is what
279      the runtime dynamic linker will do.  Tracking the files down here
280      permits one dynamic object to include another without requiring
281      special action by the person doing the link.  Note that the
282      needed list can actually grow while we are stepping through this
283      loop.  */
284   needed = bfd_elf_get_needed_list (output_bfd, &link_info);
285   for (l = needed; l != NULL; l = l->next)
286     {
287       struct bfd_link_needed_list *ll;
288       int force;
290       /* If we've already seen this file, skip it.  */
291       for (ll = needed; ll != l; ll = ll->next)
292         if (strcmp (ll->name, l->name) == 0)
293           break;
294       if (ll != l)
295         continue;
297       /* See if this file was included in the link explicitly.  */
298       global_needed = l;
299       global_found = false;
300       lang_for_each_input_file (gld${EMULATION_NAME}_check_needed);
301       if (global_found)
302         continue;
304       /* We need to find this file and include the symbol table.  We
305          want to search for the file in the same way that the dynamic
306          linker will search.  That means that we want to use
307          rpath_link, rpath, then the environment variable
308          LD_LIBRARY_PATH (native only), then the linker script
309          LIB_SEARCH_DIRS.  We do not search using the -L arguments.
311          We search twice.  The first time, we skip objects which may
312          introduce version mismatches.  The second time, we force
313          their use.  See gld${EMULATION_NAME}_vercheck comment.  */
314       for (force = 0; force < 2; force++)
315         {
316           const char *lib_path;
317           size_t len;
318           search_dirs_type *search;
320           if (gld${EMULATION_NAME}_search_needed (command_line.rpath_link,
321                                                   l->name, force))
322             break;
323           if (gld${EMULATION_NAME}_search_needed (command_line.rpath,
324                                                   l->name, force))
325             break;
326           if (command_line.rpath_link == NULL
327               && command_line.rpath == NULL)
328             {
329               lib_path = (const char *) getenv ("LD_RUN_PATH");
330               if (gld${EMULATION_NAME}_search_needed (lib_path, l->name,
331                                                       force))
332                 break;
333             }
335 if [ "x${host}" = "x${target}" ] ; then
336   case " ${EMULATION_LIBPATH} " in
337   *" ${EMULATION_NAME} "*)
338 cat >>e${EMULATION_NAME}.c <<EOF
339           lib_path = (const char *) getenv ("LD_LIBRARY_PATH");
340           if (gld${EMULATION_NAME}_search_needed (lib_path, l->name, force))
341             break;
343   ;;
344   esac
346 cat >>e${EMULATION_NAME}.c <<EOF
347           len = strlen (l->name);
348           for (search = search_head; search != NULL; search = search->next)
349             {
350               char *filename;
352               if (search->cmdline)
353                 continue;
354               filename = (char *) xmalloc (strlen (search->name) + len + 2);
355               sprintf (filename, "%s/%s", search->name, l->name);
356               if (gld${EMULATION_NAME}_try_needed (filename, force))
357                 break;
358               free (filename);
359             }
360           if (search != NULL)
361             break;
363 if [ "x${host}" = "x${target}" ] ; then
364   case " ${EMULATION_LIBPATH} " in
365   *" ${EMULATION_NAME} "*)
366 cat >>e${EMULATION_NAME}.c <<EOF
367           if (gld${EMULATION_NAME}_check_ld_so_conf (l->name, force))
368             break;
370   ;;
371   esac
373 cat >>e${EMULATION_NAME}.c <<EOF
374         }
376       if (force < 2)
377         continue;
379       einfo ("%P: warning: %s, needed by %B, not found (try using --rpath)\n",
380              l->name, l->by);
381     }
384 /* Search for a needed file in a path.  */
386 static boolean
387 gld${EMULATION_NAME}_search_needed (path, name, force)
388      const char *path;
389      const char *name;
390      int force;
392   const char *s;
393   size_t len;
395   if (path == NULL || *path == '\0')
396     return false;
397   len = strlen (name);
398   while (1)
399     {
400       char *filename, *sset;
402       s = strchr (path, ':');
403       if (s == NULL)
404         s = path + strlen (path);
406       filename = (char *) xmalloc (s - path + len + 2);
407       if (s == path)
408         sset = filename;
409       else
410         {
411           memcpy (filename, path, s - path);
412           filename[s - path] = '/';
413           sset = filename + (s - path) + 1;
414         }
415       strcpy (sset, name);
417       if (gld${EMULATION_NAME}_try_needed (filename, force))
418         return true;
420       free (filename);
422       if (*s == '\0')
423         break;
424       path = s + 1;
425     }
427   return false;
430 /* This function is called for each possible name for a dynamic object
431    named by a DT_NEEDED entry.  The FORCE parameter indicates whether
432    to skip the check for a conflicting version.  */
434 static boolean
435 gld${EMULATION_NAME}_try_needed (name, force)
436      const char *name;
437      int force;
439   bfd *abfd;
441   abfd = bfd_openr (name, bfd_get_target (output_bfd));
442   if (abfd == NULL)
443     return false;
444   if (! bfd_check_format (abfd, bfd_object))
445     {
446       (void) bfd_close (abfd);
447       return false;
448     }
449   if ((bfd_get_file_flags (abfd) & DYNAMIC) == 0)
450     {
451       (void) bfd_close (abfd);
452       return false;
453     }
455   /* Check whether this object would include any conflicting library
456      versions.  If FORCE is set, then we skip this check; we use this
457      the second time around, if we couldn't find any compatible
458      instance of the shared library.  */
460   if (! force)
461     {
462       struct bfd_link_needed_list *needed;
464       if (! bfd_elf_get_bfd_needed_list (abfd, &needed))
465         einfo ("%F%P:%B: bfd_elf_get_bfd_needed_list failed: %E\n", abfd);
467       if (needed != NULL)
468         {
469           global_vercheck_needed = needed;
470           global_vercheck_failed = false;
471           lang_for_each_input_file (gld${EMULATION_NAME}_vercheck);
472           if (global_vercheck_failed)
473             {
474               (void) bfd_close (abfd);
475               /* Return false to force the caller to move on to try
476                  another file on the search path.  */
477               return false;
478             }
480           /* But wait!  It gets much worse.  On Linux, if a shared
481              library does not use libc at all, we are supposed to skip
482              it the first time around in case we encounter a shared
483              library later on with the same name which does use the
484              version of libc that we want.  This is much too horrible
485              to use on any system other than Linux.  */
488 case ${target} in
489   *-*-linux-gnu*)
490     cat >>e${EMULATION_NAME}.c <<EOF
491           {
492             struct bfd_link_needed_list *l;
494             for (l = needed; l != NULL; l = l->next)
495               if (strncmp (l->name, "libc.so", 7) == 0)
496                 break;
497             if (l == NULL)
498               {
499                 (void) bfd_close (abfd);
500                 return false;
501               }
502           }
505     ;;
506 esac
507 cat >>e${EMULATION_NAME}.c <<EOF
508         }
509     }
511   /* We've found a dynamic object matching the DT_NEEDED entry.  */
513   /* We have already checked that there is no other input file of the
514      same name.  We must now check again that we are not including the
515      same file twice.  We need to do this because on many systems
516      libc.so is a symlink to, e.g., libc.so.1.  The SONAME entry will
517      reference libc.so.1.  If we have already included libc.so, we
518      don't want to include libc.so.1 if they are the same file, and we
519      can only check that using stat.  */
521   if (bfd_stat (abfd, &global_stat) != 0)
522     einfo ("%F%P:%B: bfd_stat failed: %E\n", abfd);
523   global_found = false;
524   lang_for_each_input_file (gld${EMULATION_NAME}_stat_needed);
525   if (global_found)
526     {
527       /* Return true to indicate that we found the file, even though
528          we aren't going to do anything with it.  */
529       return true;
530     }
532   /* Tell the ELF backend that don't want the output file to have a
533      DT_NEEDED entry for this file.  */
534   bfd_elf_set_dt_needed_name (abfd, "");
536   /* First strip off everything before the last '/'.  */
537   name = strrchr (abfd->filename, '/');
538   if (name)
539     name++;
540   else
541     name = abfd->filename;
543   /* Tell the ELF backend that the output file needs a DT_NEEDED
544      entry for this file if it is used to resolve the reference in
545      a regular object.  */
546   bfd_elf_set_dt_needed_soname (abfd, name);
548   /* Add this file into the symbol table.  */
549   if (! bfd_link_add_symbols (abfd, &link_info))
550     einfo ("%F%B: could not read symbols: %E\n", abfd);
552   return true;
555 /* See if an input file matches a DT_NEEDED entry by name.  */
557 static void
558 gld${EMULATION_NAME}_check_needed (s)
559      lang_input_statement_type *s;
561   if (global_found)
562     return;
564   if (s->filename != NULL
565       && strcmp (s->filename, global_needed->name) == 0)
566     {
567       global_found = true;
568       return;
569     }
571   if (s->the_bfd != NULL)
572     {
573       const char *soname;
575       soname = bfd_elf_get_dt_soname (s->the_bfd);
576       if (soname != NULL
577           && strcmp (soname, global_needed->name) == 0)
578         {
579           global_found = true;
580           return;
581         }
582     }
583           
584   if (s->search_dirs_flag
585       && s->filename != NULL
586       && strchr (global_needed->name, '/') == NULL)
587     {
588       const char *f;
590       f = strrchr (s->filename, '/');
591       if (f != NULL
592           && strcmp (f + 1, global_needed->name) == 0)
593         {
594           global_found = true;
595           return;
596         }
597     }
600 /* See if an input file matches a DT_NEEDED entry by running stat on
601    the file.  */
603 static void
604 gld${EMULATION_NAME}_stat_needed (s)
605      lang_input_statement_type *s;
607   struct stat st;
608   const char *suffix;
609   const char *soname;
610   const char *f;
612   if (global_found)
613     return;
614   if (s->the_bfd == NULL)
615     return;
617   if (bfd_stat (s->the_bfd, &st) != 0)
618     {
619       einfo ("%P:%B: bfd_stat failed: %E\n", s->the_bfd);
620       return;
621     }
623   if (st.st_dev == global_stat.st_dev
624       && st.st_ino == global_stat.st_ino)
625     {
626       global_found = true;
627       return;
628     }
630   /* We issue a warning if it looks like we are including two
631      different versions of the same shared library.  For example,
632      there may be a problem if -lc picks up libc.so.6 but some other
633      shared library has a DT_NEEDED entry of libc.so.5.  This is a
634      hueristic test, and it will only work if the name looks like
635      NAME.so.VERSION.  FIXME: Depending on file names is error-prone.
636      If we really want to issue warnings about mixing version numbers
637      of shared libraries, we need to find a better way.  */
639   if (strchr (global_needed->name, '/') != NULL)
640     return;
641   suffix = strstr (global_needed->name, ".so.");
642   if (suffix == NULL)
643     return;
644   suffix += sizeof ".so." - 1;
646   soname = bfd_elf_get_dt_soname (s->the_bfd);
647   if (soname == NULL)
648     soname = s->filename;
650   f = strrchr (soname, '/');
651   if (f != NULL)
652     ++f;
653   else
654     f = soname;
656   if (strncmp (f, global_needed->name, suffix - global_needed->name) == 0)
657     einfo ("%P: warning: %s, needed by %B, may conflict with %s\n",
658            global_needed->name, global_needed->by, f);
661 /* On Linux, it's possible to have different versions of the same
662    shared library linked against different versions of libc.  The
663    dynamic linker somehow tags which libc version to use in
664    /etc/ld.so.cache, and, based on the libc that it sees in the
665    executable, chooses which version of the shared library to use.
667    We try to do a similar check here by checking whether this shared
668    library needs any other shared libraries which may conflict with
669    libraries we have already included in the link.  If it does, we
670    skip it, and try to find another shared library farther on down the
671    link path.
673    This is called via lang_for_each_input_file.
674    GLOBAL_VERCHECK_NEEDED is the list of objects needed by the object
675    which we ar checking.  This sets GLOBAL_VERCHECK_FAILED if we find
676    a conflicting version.  */
678 static void
679 gld${EMULATION_NAME}_vercheck (s)
680      lang_input_statement_type *s;
682   const char *soname, *f;
683   struct bfd_link_needed_list *l;
685   if (global_vercheck_failed)
686     return;
687   if (s->the_bfd == NULL
688       || (bfd_get_file_flags (s->the_bfd) & DYNAMIC) == 0)
689     return;
691   soname = bfd_elf_get_dt_soname (s->the_bfd);
692   if (soname == NULL)
693     soname = bfd_get_filename (s->the_bfd);
695   f = strrchr (soname, '/');
696   if (f != NULL)
697     ++f;
698   else
699     f = soname;
701   for (l = global_vercheck_needed; l != NULL; l = l->next)
702     {
703       const char *suffix;
705       if (strcmp (f, l->name) == 0)
706         {
707           /* Probably can't happen, but it's an easy check.  */
708           continue;
709         }
711       if (strchr (l->name, '/') != NULL)
712         continue;
714       suffix = strstr (l->name, ".so.");
715       if (suffix == NULL)
716         continue;
718       suffix += sizeof ".so." - 1;
720       if (strncmp (f, l->name, suffix - l->name) == 0)
721         {
722           /* Here we know that S is a dynamic object FOO.SO.VER1, and
723              the object we are considering needs a dynamic object
724              FOO.SO.VER2, and VER1 and VER2 are different.  This
725              appears to be a version mismatch, so we tell the caller
726              to try a different version of this library.  */
727           global_vercheck_failed = true;
728           return;
729         }
730     }
733 /* This is called after the sections have been attached to output
734    sections, but before any sizes or addresses have been set.  */
736 static void
737 gld${EMULATION_NAME}_before_allocation ()
739   const char *rpath;
740   asection *sinterp;
742   /* If we are going to make any variable assignments, we need to let
743      the ELF backend know about them in case the variables are
744      referred to by dynamic objects.  */
745   lang_for_each_statement (gld${EMULATION_NAME}_find_statement_assignment);
747   /* Let the ELF backend work out the sizes of any sections required
748      by dynamic linking.  */
749   rpath = command_line.rpath;
750   if (rpath == NULL)
751     rpath = (const char *) getenv ("LD_RUN_PATH");
752   if (! (bfd_elf${ELFSIZE}_size_dynamic_sections
753          (output_bfd, command_line.soname, rpath,
754           command_line.export_dynamic, command_line.filter_shlib,
755           (const char * const *) command_line.auxiliary_filters,
756           &link_info, &sinterp, lang_elf_version_info)))
757     einfo ("%P%F: failed to set dynamic section sizes: %E\n");
759   /* Let the user override the dynamic linker we are using.  */
760   if (command_line.interpreter != NULL
761       && sinterp != NULL)
762     {
763       sinterp->contents = (bfd_byte *) command_line.interpreter;
764       sinterp->_raw_size = strlen (command_line.interpreter) + 1;
765     }
767   /* Look for any sections named .gnu.warning.  As a GNU extensions,
768      we treat such sections as containing warning messages.  We print
769      out the warning message, and then zero out the section size so
770      that it does not get copied into the output file.  */
772   {
773     LANG_FOR_EACH_INPUT_STATEMENT (is)
774       {
775         asection *s;
776         bfd_size_type sz;
777         char *msg;
778         boolean ret;
780         if (is->just_syms_flag)
781           continue;
783         s = bfd_get_section_by_name (is->the_bfd, ".gnu.warning");
784         if (s == NULL)
785           continue;
787         sz = bfd_section_size (is->the_bfd, s);
788         msg = xmalloc ((size_t) sz + 1);
789         if (! bfd_get_section_contents (is->the_bfd, s, msg, (file_ptr) 0, sz))
790           einfo ("%F%B: Can't read contents of section .gnu.warning: %E\n",
791                  is->the_bfd);
792         msg[sz] = '\0';
793         ret = link_info.callbacks->warning (&link_info, msg,
794                                             (const char *) NULL,
795                                             is->the_bfd, (asection *) NULL,
796                                             (bfd_vma) 0);
797         ASSERT (ret);
798         free (msg);
800         /* Clobber the section size, so that we don't waste copying the
801            warning into the output file.  */
802         s->_raw_size = 0;
803       }
804   }
807 /* This is called by the before_allocation routine via
808    lang_for_each_statement.  It locates any assignment statements, and
809    tells the ELF backend about them, in case they are assignments to
810    symbols which are referred to by dynamic objects.  */
812 static void
813 gld${EMULATION_NAME}_find_statement_assignment (s)
814      lang_statement_union_type *s;
816   if (s->header.type == lang_assignment_statement_enum)
817     gld${EMULATION_NAME}_find_exp_assignment (s->assignment_statement.exp);
820 /* Look through an expression for an assignment statement.  */
822 static void
823 gld${EMULATION_NAME}_find_exp_assignment (exp)
824      etree_type *exp;
826   struct bfd_link_hash_entry *h;
828   switch (exp->type.node_class)
829     {
830     case etree_provide:
831       h = bfd_link_hash_lookup (link_info.hash, exp->assign.dst,
832                                 false, false, false);
833       if (h == NULL)
834         break;
836       /* We call record_link_assignment even if the symbol is defined.
837          This is because if it is defined by a dynamic object, we
838          actually want to use the value defined by the linker script,
839          not the value from the dynamic object (because we are setting
840          symbols like etext).  If the symbol is defined by a regular
841          object, then, as it happens, calling record_link_assignment
842          will do no harm.  */
844       /* Fall through.  */
845     case etree_assign:
846       if (strcmp (exp->assign.dst, ".") != 0)
847         {
848           if (! (bfd_elf${ELFSIZE}_record_link_assignment
849                  (output_bfd, &link_info, exp->assign.dst,
850                   exp->type.node_class == etree_provide ? true : false)))
851             einfo ("%P%F: failed to record assignment to %s: %E\n",
852                    exp->assign.dst);
853         }
854       gld${EMULATION_NAME}_find_exp_assignment (exp->assign.src);
855       break;
857     case etree_binary:
858       gld${EMULATION_NAME}_find_exp_assignment (exp->binary.lhs);
859       gld${EMULATION_NAME}_find_exp_assignment (exp->binary.rhs);
860       break;
862     case etree_trinary:
863       gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.cond);
864       gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.lhs);
865       gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.rhs);
866       break;
868     case etree_unary:
869       gld${EMULATION_NAME}_find_exp_assignment (exp->unary.child);
870       break;
872     default:
873       break;
874     }
877 /* Place an orphan section.  We use this to put random SHF_ALLOC
878    sections in the right segment.  */
880 struct orphan_save
882   lang_output_section_statement_type *os;
883   asection **section;
884   lang_statement_union_type **stmt;
887 /*ARGSUSED*/
888 static boolean
889 gld${EMULATION_NAME}_place_orphan (file, s)
890      lang_input_statement_type *file;
891      asection *s;
893   static struct orphan_save hold_text;
894   static struct orphan_save hold_rodata;
895   static struct orphan_save hold_data;
896   static struct orphan_save hold_bss;
897   static struct orphan_save hold_rel;
898   static struct orphan_save hold_interp;
899   struct orphan_save *place;
900   lang_statement_list_type *old;
901   lang_statement_list_type add;
902   etree_type *address;
903   const char *secname, *ps;
904   const char *outsecname;
905   lang_output_section_statement_type *os;
907   secname = bfd_get_section_name (s->owner, s);
909   /* Look through the script to see where to place this section.  */
910   os = lang_output_section_find (secname);
912   if (os != NULL
913       && os->bfd_section != NULL
914       && ((s->flags ^ os->bfd_section->flags) & (SEC_LOAD | SEC_ALLOC)) == 0)
915     {
916       /* We have already placed a section with this name.  */
917       wild_doit (&os->children, s, os, file);
918       return true;
919     }
921   if (hold_text.os == NULL)
922     hold_text.os = lang_output_section_find (".text");
924   /* If this is a final link, then always put .gnu.warning.SYMBOL
925      sections into the .text section to get them out of the way.  */
926   if (! link_info.shared
927       && ! link_info.relocateable
928       && strncmp (secname, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0
929       && hold_text.os != NULL)
930     {
931       wild_doit (&hold_text.os->children, s, hold_text.os, file);
932       return true;
933     }
935   /* Decide which segment the section should go in based on the
936      section name and section flags.  We put loadable .note sections
937      right after the .interp section, so that the PT_NOTE segment is
938      stored right after the program headers where the OS can read it
939      in the first page.  */
940 #define HAVE_SECTION(hold, name) \
941 (hold.os != NULL || (hold.os = lang_output_section_find (name)) != NULL)
943   if (s->flags & SEC_EXCLUDE)
944     return false;
945   else if ((s->flags & SEC_ALLOC) == 0)
946     place = NULL;
947   else if ((s->flags & SEC_LOAD) != 0
948            && strncmp (secname, ".note", 4) == 0
949            && HAVE_SECTION (hold_interp, ".interp"))
950     place = &hold_interp;
951   else if ((s->flags & SEC_HAS_CONTENTS) == 0
952            && HAVE_SECTION (hold_bss, ".bss"))
953     place = &hold_bss;
954   else if ((s->flags & SEC_READONLY) == 0
955            && HAVE_SECTION (hold_data, ".data"))
956     place = &hold_data;
957   else if (strncmp (secname, ".rel", 4) == 0
958            && (hold_rel.os != NULL
959                || (hold_rel.os = output_rel_find ()) != NULL))
960     place = &hold_rel;
961   else if ((s->flags & SEC_CODE) == 0
962            && (s->flags & SEC_READONLY) != 0
963            && HAVE_SECTION (hold_rodata, ".rodata"))
964     place = &hold_rodata;
965   else if ((s->flags & SEC_READONLY) != 0
966            && hold_text.os != NULL)
967     place = &hold_text;
968   else
969     place = NULL;
971 #undef HAVE_SECTION
973   /* Choose a unique name for the section.  This will be needed if the
974      same section name appears in the input file with different
975      loadable or allocateable characteristics.  */
976   outsecname = secname;
977   if (bfd_get_section_by_name (output_bfd, outsecname) != NULL)
978     {
979       unsigned int len;
980       char *newname;
981       unsigned int i;
983       len = strlen (outsecname);
984       newname = xmalloc (len + 5);
985       strcpy (newname, outsecname);
986       i = 0;
987       do
988         {
989           sprintf (newname + len, "%d", i);
990           ++i;
991         }
992       while (bfd_get_section_by_name (output_bfd, newname) != NULL);
994       outsecname = newname;
995     }
997   if (place != NULL)
998     {
999       /* Start building a list of statements for this section.  */
1000       old = stat_ptr;
1001       stat_ptr = &add;
1002       lang_list_init (stat_ptr);
1004       /* If the name of the section is representable in C, then create
1005          symbols to mark the start and the end of the section.  */
1006       for (ps = outsecname; *ps != '\0'; ps++)
1007         if (! isalnum ((unsigned char) *ps) && *ps != '_')
1008           break;
1009       if (*ps == '\0' && config.build_constructors)
1010         {
1011           char *symname;
1012           etree_type *e_align;
1014           symname = (char *) xmalloc (ps - outsecname + sizeof "__start_");
1015           sprintf (symname, "__start_%s", outsecname);
1016           e_align = exp_unop (ALIGN_K,
1017                               exp_intop ((bfd_vma) 1 << s->alignment_power));
1018           lang_add_assignment (exp_assop ('=', symname, e_align));
1019         }
1020     }
1022   if (link_info.relocateable || (s->flags & (SEC_LOAD | SEC_ALLOC)) == 0)
1023     address = exp_intop ((bfd_vma) 0);
1024   else
1025     address = NULL;
1027   os = lang_enter_output_section_statement (outsecname, address, 0,
1028                                             (bfd_vma) 0,
1029                                             (etree_type *) NULL,
1030                                             (etree_type *) NULL,
1031                                             (etree_type *) NULL);
1033   wild_doit (&os->children, s, os, file);
1035   lang_leave_output_section_statement
1036     ((bfd_vma) 0, "*default*",
1037      (struct lang_output_section_phdr_list *) NULL, "*default*");
1039   if (place != NULL)
1040     {
1041       asection *snew, **pps;
1043       stat_ptr = &add;
1045       if (*ps == '\0' && config.build_constructors)
1046         {
1047           char *symname;
1049           symname = (char *) xmalloc (ps - outsecname + sizeof "__stop_");
1050           sprintf (symname, "__stop_%s", outsecname);
1051           lang_add_assignment (exp_assop ('=', symname,
1052                                           exp_nameop (NAME, ".")));
1053         }
1054       stat_ptr = old;
1056       snew = os->bfd_section;
1057       if (place->os->bfd_section != NULL || place->section != NULL)
1058         {
1059           /* Shuffle the section to make the output file look neater.  */
1060           if (place->section == NULL)
1061             {
1062 #if 0
1063               /* Finding the end of the list is a little tricky.  We
1064                  make a wild stab at it by comparing section flags.  */
1065               flagword first_flags = place->os->bfd_section->flags;
1066               for (pps = &place->os->bfd_section->next;
1067                    *pps != NULL && (*pps)->flags == first_flags;
1068                    pps = &(*pps)->next)
1069                 ;
1070               place->section = pps;
1071 #else
1072               /* Put orphans after the first section on the list.  */
1073               place->section = &place->os->bfd_section->next;
1074 #endif
1075             }
1077           /*  Unlink the section.  */
1078           for (pps = &output_bfd->sections; *pps != snew; pps = &(*pps)->next)
1079             ;
1080           *pps = snew->next;
1082           /* Now tack it on to the "place->os" section list.  */
1083           snew->next = *place->section;
1084           *place->section = snew;
1085         }
1086       place->section = &snew->next;     /* Save the end of this list.  */
1088       if (place->stmt == NULL)
1089         {
1090           /* Put the new statement list right at the head.  */
1091           *add.tail = place->os->header.next;
1092           place->os->header.next = add.head;
1093         }
1094       else
1095         {
1096           /* Put it after the last orphan statement we added.  */
1097           *add.tail = *place->stmt;
1098           *place->stmt = add.head;
1099         }
1100       place->stmt = add.tail;           /* Save the end of this list.  */
1101     }
1103   return true;
1106 /* A variant of lang_output_section_find.  */
1107 static lang_output_section_statement_type *
1108 output_rel_find ()
1110   lang_statement_union_type *u;
1111   lang_output_section_statement_type *lookup;
1113   for (u = lang_output_section_statement.head;
1114        u != (lang_statement_union_type *) NULL;
1115        u = lookup->next)
1116     {
1117       lookup = &u->output_section_statement;
1118       if (strncmp (".rel", lookup->name, 4) == 0
1119           && lookup->bfd_section != NULL
1120           && (lookup->bfd_section->flags & SEC_ALLOC) != 0)
1121         {
1122           return lookup;
1123         }
1124     }
1125   return (lang_output_section_statement_type *) NULL;
1128 static char *
1129 gld${EMULATION_NAME}_get_script(isfile)
1130      int *isfile;
1133 if test -n "$COMPILE_IN"
1134 then
1135 # Scripts compiled in.
1137 # sed commands to quote an ld script as a C string.
1138 sc="-f stringify.sed"
1140 cat >>e${EMULATION_NAME}.c <<EOF
1142   *isfile = 0;
1144   if (link_info.relocateable == true && config.build_constructors == true)
1145     return
1147 sed $sc ldscripts/${EMULATION_NAME}.xu                     >> e${EMULATION_NAME}.c
1148 echo '  ; else if (link_info.relocateable == true) return' >> e${EMULATION_NAME}.c
1149 sed $sc ldscripts/${EMULATION_NAME}.xr                     >> e${EMULATION_NAME}.c
1150 echo '  ; else if (!config.text_read_only) return'         >> e${EMULATION_NAME}.c
1151 sed $sc ldscripts/${EMULATION_NAME}.xbn                    >> e${EMULATION_NAME}.c
1152 echo '  ; else if (!config.magic_demand_paged) return'     >> e${EMULATION_NAME}.c
1153 sed $sc ldscripts/${EMULATION_NAME}.xn                     >> e${EMULATION_NAME}.c
1155 if test -n "$GENERATE_SHLIB_SCRIPT" ; then
1156 echo '  ; else if (link_info.shared) return'               >> e${EMULATION_NAME}.c
1157 sed $sc ldscripts/${EMULATION_NAME}.xs                     >> e${EMULATION_NAME}.c
1160 echo '  ; else return'                                     >> e${EMULATION_NAME}.c
1161 sed $sc ldscripts/${EMULATION_NAME}.x                      >> e${EMULATION_NAME}.c
1162 echo '; }'                                                 >> e${EMULATION_NAME}.c
1164 else
1165 # Scripts read from the filesystem.
1167 cat >>e${EMULATION_NAME}.c <<EOF
1169   *isfile = 1;
1171   if (link_info.relocateable == true && config.build_constructors == true)
1172     return "ldscripts/${EMULATION_NAME}.xu";
1173   else if (link_info.relocateable == true)
1174     return "ldscripts/${EMULATION_NAME}.xr";
1175   else if (!config.text_read_only)
1176     return "ldscripts/${EMULATION_NAME}.xbn";
1177   else if (!config.magic_demand_paged)
1178     return "ldscripts/${EMULATION_NAME}.xn";
1179   else if (link_info.shared)
1180     return "ldscripts/${EMULATION_NAME}.xs";
1181   else
1182     return "ldscripts/${EMULATION_NAME}.x";
1188 if test -n "$PARSE_AND_LIST_ARGS_CASES" || test x"$GENERATE_SHLIB_SCRIPT" = xyes; then
1189 NEED_PARSE_AND_LIST=yes
1191 cat >>e${EMULATION_NAME}.c <<EOF
1192 static int  gld_${EMULATION_NAME}_parse_args PARAMS ((int, char **));
1193 static void gld_${EMULATION_NAME}_list_options PARAMS ((FILE * file));
1195 else
1196 NEED_PARSE_AND_LIST=no
1198 cat >>e${EMULATION_NAME}.c <<EOF
1199 #define gld_${EMULATION_NAME}_parse_args   NULL
1200 #define gld_${EMULATION_NAME}_list_options NULL
1205 if test -n "$PARSE_AND_LIST_PROLOGUE" ; then
1206 cat >>e${EMULATION_NAME}.c <<EOF
1207  $PARSE_AND_LIST_PROLOGUE
1211 if test "$NEED_PARSE_AND_LIST" = yes; then
1212 cat >>e${EMULATION_NAME}.c <<EOF
1214 #include "getopt.h"
1216 #define OPTION_DISABLE_NEW_DTAGS        (400)
1217 #define OPTION_ENABLE_NEW_DTAGS         (OPTION_DISABLE_NEW_DTAGS + 1)
1219 static struct option longopts[] =
1224 if test x"$GENERATE_SHLIB_SCRIPT" = xyes; then
1225 cat >>e${EMULATION_NAME}.c <<EOF
1226   {"disable-new-dtags", no_argument, NULL, OPTION_DISABLE_NEW_DTAGS},
1227   {"enable-new-dtags", no_argument, NULL, OPTION_ENABLE_NEW_DTAGS},
1231 if test -n "$PARSE_AND_LIST_LONGOPTS" ; then
1232 cat >>e${EMULATION_NAME}.c <<EOF
1233  $PARSE_AND_LIST_LONGOPTS
1237 if test "$NEED_PARSE_AND_LIST" = yes; then
1238 cat >>e${EMULATION_NAME}.c <<EOF
1239   {NULL, no_argument, NULL, 0}
1242 static int
1243 gld_${EMULATION_NAME}_parse_args (argc, argv)
1244      int argc;
1245      char ** argv;
1247   int longind, optc;
1248   int prevoptind = optind;
1249   int prevopterr = opterr;
1250   int wanterror;
1251   static int lastoptind = -1;
1253   if (lastoptind != optind)
1254     opterr = 0;
1256   wanterror  = opterr;
1257   optc = getopt_long_only (argc, argv, "-z:", longopts, &longind);
1258   opterr = prevopterr;
1260   switch (optc)
1261     {
1262     default:
1263       if (wanterror)
1264         xexit (1);
1265       optind =  prevoptind;
1266       return 0;
1271 if test x"$GENERATE_SHLIB_SCRIPT" = xyes; then
1272 cat >>e${EMULATION_NAME}.c <<EOF
1273     case OPTION_DISABLE_NEW_DTAGS:
1274       link_info.new_dtags = false;
1275       break;
1277     case OPTION_ENABLE_NEW_DTAGS:
1278       link_info.new_dtags = true;
1279       break;
1281     case 'z':
1282       if (strcmp (optarg, "initfirst") == 0)
1283         link_info.flags_1 |= (bfd_vma) DF_1_INITFIRST;
1284       else if (strcmp (optarg, "interpose") == 0)
1285         link_info.flags_1 |= (bfd_vma) DF_1_INTERPOSE;
1286       else if (strcmp (optarg, "loadfltr") == 0)
1287         link_info.flags_1 |= (bfd_vma) DF_1_LOADFLTR;
1288       else if (strcmp (optarg, "nodefaultlib") == 0)
1289         link_info.flags_1 |= (bfd_vma) DF_1_NODEFLIB;
1290       else if (strcmp (optarg, "nodelete") == 0)
1291         link_info.flags_1 |= (bfd_vma) DF_1_NODELETE;
1292       else if (strcmp (optarg, "nodlopen") == 0)
1293         link_info.flags_1 |= (bfd_vma) DF_1_NOOPEN;
1294       else if (strcmp (optarg, "nodump") == 0)
1295         link_info.flags_1 |= (bfd_vma) DF_1_NODUMP;
1296       else if (strcmp (optarg, "now") == 0)
1297         {
1298           link_info.flags |= (bfd_vma) DF_BIND_NOW;
1299           link_info.flags_1 |= (bfd_vma) DF_1_NOW;
1300         }
1301       else if (strcmp (optarg, "origin") == 0)
1302         {
1303           link_info.flags |= (bfd_vma) DF_ORIGIN;
1304           link_info.flags_1 |= (bfd_vma) DF_1_ORIGIN;
1305         }
1306       /* What about the other Solaris -z options? FIXME.  */
1307       break;
1311 if test -n "$PARSE_AND_LIST_ARGS_CASES" ; then
1312 cat >>e${EMULATION_NAME}.c <<EOF
1313  $PARSE_AND_LIST_ARGS_CASES
1317 if test "$NEED_PARSE_AND_LIST" = yes; then
1318 cat >>e${EMULATION_NAME}.c <<EOF
1319     }
1321   return 1;
1324 static void
1325 gld_${EMULATION_NAME}_list_options (file)
1326      FILE * file;
1331 if test x"$GENERATE_SHLIB_SCRIPT" = xyes; then
1332 cat >>e${EMULATION_NAME}.c <<EOF
1333   fprintf (file, _("  --disable-new-dtags\tDisable new dynamic tags\n"));
1334   fprintf (file, _("  --enable-new-dtags\tEnable new dynamic tags\n"));
1335   fprintf (file, _("  -z initfirst\t\tMark DSO to be initialized first at rutime\n"));
1336   fprintf (file, _("  -z interpose\t\tMark object to interpose all DSOs but execuable\n"));
1337   fprintf (file, _("  -z loadfltr\t\tMark object requiring immediate process\n"));
1338   fprintf (file, _("  -z nodefaultlib\tMark object not to use default search paths\n"));
1339   fprintf (file, _("  -z nodelete\t\tMark DSO non-deletable at runtime\n"));
1340   fprintf (file, _("  -z nodlopen\t\tMark DSO not availale to dlopen\n"));
1341   fprintf (file, _("  -z nodump\t\tMark DSO not availale to dldump\n"));
1342   fprintf (file, _("  -z now\t\tMark object non-lazy runtime binding\n"));
1343   fprintf (file, _("  -z origin\t\tMark object requiring immediate \$ORIGIN processing\n"));
1344   fprintf (file, _("\t\t\t  at runtime\n"));
1345   fprintf (file, _("  -z KEYWORD\t\tIgnored for Solaris compatibility\n"));
1349 if test -n "$PARSE_AND_LIST_OPTIONS" ; then
1350 cat >>e${EMULATION_NAME}.c <<EOF
1351  $PARSE_AND_LIST_OPTIONS
1355 if test "$NEED_PARSE_AND_LIST" = yes; then
1356 cat >>e${EMULATION_NAME}.c <<EOF
1361 if test -n "$PARSE_AND_LIST_EPILOGUE" ; then
1362 cat >>e${EMULATION_NAME}.c <<EOF
1363  $PARSE_AND_LIST_EPILOGUE
1367 cat >>e${EMULATION_NAME}.c <<EOF
1369 struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
1371   gld${EMULATION_NAME}_before_parse,
1372   syslib_default,
1373   hll_default,
1374   after_parse_default,
1375   gld${EMULATION_NAME}_after_open,
1376   after_allocation_default,
1377   set_output_arch_default,
1378   ldemul_default_target,
1379   gld${EMULATION_NAME}_before_allocation,
1380   gld${EMULATION_NAME}_get_script,
1381   "${EMULATION_NAME}",
1382   "${OUTPUT_FORMAT}",
1383   NULL,         /* finish */
1384   NULL,         /* create output section statements */
1385   gld${EMULATION_NAME}_open_dynamic_archive,
1386   gld${EMULATION_NAME}_place_orphan,
1387   NULL,         /* set_symbols */
1388   gld_${EMULATION_NAME}_parse_args,
1389   NULL,         /* unrecognized_file */
1390   gld_${EMULATION_NAME}_list_options,
1391   NULL,         /* recognized_file */
1392   NULL          /* find_potential_libraries */