2000-07-19 H.J. Lu <hjl@gnu.org>
[binutils.git] / ld / emultempl / elf32.em
blob3cdb856861ed0a32223a25e24e2ceb8dd6d6bfad
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"
49 static void gld${EMULATION_NAME}_before_parse PARAMS ((void));
50 static boolean gld${EMULATION_NAME}_open_dynamic_archive
51   PARAMS ((const char *, search_dirs_type *, lang_input_statement_type *));
52 static void gld${EMULATION_NAME}_after_open PARAMS ((void));
53 static void gld${EMULATION_NAME}_check_needed
54   PARAMS ((lang_input_statement_type *));
55 static void gld${EMULATION_NAME}_stat_needed
56   PARAMS ((lang_input_statement_type *));
57 static boolean gld${EMULATION_NAME}_search_needed
58   PARAMS ((const char *, const char *, int));
59 static boolean gld${EMULATION_NAME}_try_needed PARAMS ((const char *, int));
60 static void gld${EMULATION_NAME}_vercheck
61   PARAMS ((lang_input_statement_type *));
62 static void gld${EMULATION_NAME}_before_allocation PARAMS ((void));
63 static void gld${EMULATION_NAME}_find_statement_assignment
64   PARAMS ((lang_statement_union_type *));
65 static void gld${EMULATION_NAME}_find_exp_assignment PARAMS ((etree_type *));
66 static boolean gld${EMULATION_NAME}_place_orphan
67   PARAMS ((lang_input_statement_type *, asection *));
68 static lang_output_section_statement_type *output_rel_find PARAMS ((void));
69 static char *gld${EMULATION_NAME}_get_script PARAMS ((int *isfile));
71 static void
72 gld${EMULATION_NAME}_before_parse()
74   ldfile_output_architecture = bfd_arch_`echo ${ARCH} | sed -e 's/:.*//'`;
75   config.dynamic_link = ${DYNAMIC_LINK-true};
76   config.has_shared = `if test -n "$GENERATE_SHLIB_SCRIPT" ; then echo true ; else echo false ; fi`;
79 /* Try to open a dynamic archive.  This is where we know that ELF
80    dynamic libraries have an extension of .so (or .sl on oddball systems
81    like hpux).  */
83 static boolean
84 gld${EMULATION_NAME}_open_dynamic_archive (arch, search, entry)
85      const char *arch;
86      search_dirs_type *search;
87      lang_input_statement_type *entry;
89   const char *filename;
90   char *string;
92   if (! entry->is_archive)
93     return false;
95   filename = entry->filename;
97   /* This allocates a few bytes too many when EXTRA_SHLIB_EXTENSION
98      is defined, but it does not seem worth the headache to optimize
99      away those two bytes of space.  */
100   string = (char *) xmalloc (strlen (search->name)
101                              + strlen (filename)
102                              + strlen (arch)
103 #ifdef EXTRA_SHLIB_EXTENSION
104                              + strlen (EXTRA_SHLIB_EXTENSION)
105 #endif
106                              + sizeof "/lib.so");
108   sprintf (string, "%s/lib%s%s.so", search->name, filename, arch);
110 #ifdef EXTRA_SHLIB_EXTENSION
111   /* Try the .so extension first.  If that fails build a new filename
112      using EXTRA_SHLIB_EXTENSION.  */
113   if (! ldfile_try_open_bfd (string, entry))
114     sprintf (string, "%s/lib%s%s%s", search->name,
115              filename, arch, EXTRA_SHLIB_EXTENSION);
116 #endif
118   if (! ldfile_try_open_bfd (string, entry))
119     {
120       free (string);
121       return false;
122     }
124   entry->filename = string;
126   /* We have found a dynamic object to include in the link.  The ELF
127      backend linker will create a DT_NEEDED entry in the .dynamic
128      section naming this file.  If this file includes a DT_SONAME
129      entry, it will be used.  Otherwise, the ELF linker will just use
130      the name of the file.  For an archive found by searching, like
131      this one, the DT_NEEDED entry should consist of just the name of
132      the file, without the path information used to find it.  Note
133      that we only need to do this if we have a dynamic object; an
134      archive will never be referenced by a DT_NEEDED entry.
136      FIXME: This approach--using bfd_elf_set_dt_needed_name--is not
137      very pretty.  I haven't been able to think of anything that is
138      pretty, though.  */
139   if (bfd_check_format (entry->the_bfd, bfd_object)
140       && (entry->the_bfd->flags & DYNAMIC) != 0)
141     {
142       char *needed_name;
144       ASSERT (entry->is_archive && entry->search_dirs_flag);
146       /* Rather than duplicating the logic above.  Just use the
147          filename we recorded earlier.
149          First strip off everything before the last '/'.  */
150       filename = strrchr (entry->filename, '/');
151       filename++;
153       needed_name = (char *) xmalloc (strlen (filename) + 1);
154       strcpy (needed_name, filename);
155       bfd_elf_set_dt_needed_name (entry->the_bfd, needed_name);
156     }
158   return true;
162 if [ "x${host}" = "x${target}" ] ; then
163   case " ${EMULATION_LIBPATH} " in
164   *" ${EMULATION_NAME} "*)
165 cat >>e${EMULATION_NAME}.c <<EOF
167 /* For a native linker, check the file /etc/ld.so.conf for directories
168    in which we may find shared libraries.  /etc/ld.so.conf is really
169    only meaningful on Linux, but we check it on other systems anyhow.  */
171 static boolean gld${EMULATION_NAME}_check_ld_so_conf
172   PARAMS ((const char *, int));
174 static boolean
175 gld${EMULATION_NAME}_check_ld_so_conf (name, force)
176      const char *name;
177      int force;
179   static boolean initialized;
180   static char *ld_so_conf;
182   if (! initialized)
183     {
184       FILE *f;
186       f = fopen ("/etc/ld.so.conf", FOPEN_RT);
187       if (f != NULL)
188         {
189           char *b;
190           size_t len, alloc;
191           int c;
193           len = 0;
194           alloc = 100;
195           b = (char *) xmalloc (alloc);
197           while ((c = getc (f)) != EOF)
198             {
199               if (len + 1 >= alloc)
200                 {
201                   alloc *= 2;
202                   b = (char *) xrealloc (b, alloc);
203                 }
204               if (c != ':'
205                   && c != ' '
206                   && c != '\t'
207                   && c != '\n'
208                   && c != ',')
209                 {
210                   b[len] = c;
211                   ++len;
212                 }
213               else
214                 {
215                   if (len > 0 && b[len - 1] != ':')
216                     {
217                       b[len] = ':';
218                       ++len;
219                     }
220                 }
221             }
223           if (len > 0 && b[len - 1] == ':')
224             --len;
226           if (len > 0)
227             b[len] = '\0';
228           else
229             {
230               free (b);
231               b = NULL;
232             }
234           fclose (f);
236           ld_so_conf = b;
237         }
239       initialized = true;
240     }
242   if (ld_so_conf == NULL)
243     return false;
245   return gld${EMULATION_NAME}_search_needed (ld_so_conf, name, force);
249   ;;
250   esac
252 cat >>e${EMULATION_NAME}.c <<EOF
254 /* These variables are required to pass information back and forth
255    between after_open and check_needed and stat_needed and vercheck.  */
257 static struct bfd_link_needed_list *global_needed;
258 static struct stat global_stat;
259 static boolean global_found;
260 static struct bfd_link_needed_list *global_vercheck_needed;
261 static boolean global_vercheck_failed;
263 /* This is called after all the input files have been opened.  */
265 static void
266 gld${EMULATION_NAME}_after_open ()
268   struct bfd_link_needed_list *needed, *l;
270   /* We only need to worry about this when doing a final link.  */
271   if (link_info.relocateable || link_info.shared)
272     return;
274   /* Get the list of files which appear in DT_NEEDED entries in
275      dynamic objects included in the link (often there will be none).
276      For each such file, we want to track down the corresponding
277      library, and include the symbol table in the link.  This is what
278      the runtime dynamic linker will do.  Tracking the files down here
279      permits one dynamic object to include another without requiring
280      special action by the person doing the link.  Note that the
281      needed list can actually grow while we are stepping through this
282      loop.  */
283   needed = bfd_elf_get_needed_list (output_bfd, &link_info);
284   for (l = needed; l != NULL; l = l->next)
285     {
286       struct bfd_link_needed_list *ll;
287       int force;
289       /* If we've already seen this file, skip it.  */
290       for (ll = needed; ll != l; ll = ll->next)
291         if (strcmp (ll->name, l->name) == 0)
292           break;
293       if (ll != l)
294         continue;
296       /* See if this file was included in the link explicitly.  */
297       global_needed = l;
298       global_found = false;
299       lang_for_each_input_file (gld${EMULATION_NAME}_check_needed);
300       if (global_found)
301         continue;
303       /* We need to find this file and include the symbol table.  We
304          want to search for the file in the same way that the dynamic
305          linker will search.  That means that we want to use
306          rpath_link, rpath, then the environment variable
307          LD_LIBRARY_PATH (native only), then the linker script
308          LIB_SEARCH_DIRS.  We do not search using the -L arguments.
310          We search twice.  The first time, we skip objects which may
311          introduce version mismatches.  The second time, we force
312          their use.  See gld${EMULATION_NAME}_vercheck comment.  */
313       for (force = 0; force < 2; force++)
314         {
315           const char *lib_path;
316           size_t len;
317           search_dirs_type *search;
319           if (gld${EMULATION_NAME}_search_needed (command_line.rpath_link,
320                                                   l->name, force))
321             break;
322           if (gld${EMULATION_NAME}_search_needed (command_line.rpath,
323                                                   l->name, force))
324             break;
325           if (command_line.rpath_link == NULL
326               && command_line.rpath == NULL)
327             {
328               lib_path = (const char *) getenv ("LD_RUN_PATH");
329               if (gld${EMULATION_NAME}_search_needed (lib_path, l->name,
330                                                       force))
331                 break;
332             }
334 if [ "x${host}" = "x${target}" ] ; then
335   case " ${EMULATION_LIBPATH} " in
336   *" ${EMULATION_NAME} "*)
337 cat >>e${EMULATION_NAME}.c <<EOF
338           lib_path = (const char *) getenv ("LD_LIBRARY_PATH");
339           if (gld${EMULATION_NAME}_search_needed (lib_path, l->name, force))
340             break;
342   ;;
343   esac
345 cat >>e${EMULATION_NAME}.c <<EOF
346           len = strlen (l->name);
347           for (search = search_head; search != NULL; search = search->next)
348             {
349               char *filename;
351               if (search->cmdline)
352                 continue;
353               filename = (char *) xmalloc (strlen (search->name) + len + 2);
354               sprintf (filename, "%s/%s", search->name, l->name);
355               if (gld${EMULATION_NAME}_try_needed (filename, force))
356                 break;
357               free (filename);
358             }
359           if (search != NULL)
360             break;
362 if [ "x${host}" = "x${target}" ] ; then
363   case " ${EMULATION_LIBPATH} " in
364   *" ${EMULATION_NAME} "*)
365 cat >>e${EMULATION_NAME}.c <<EOF
366           if (gld${EMULATION_NAME}_check_ld_so_conf (l->name, force))
367             break;
369   ;;
370   esac
372 cat >>e${EMULATION_NAME}.c <<EOF
373         }
375       if (force < 2)
376         continue;
378       einfo ("%P: warning: %s, needed by %B, not found (try using --rpath)\n",
379              l->name, l->by);
380     }
383 /* Search for a needed file in a path.  */
385 static boolean
386 gld${EMULATION_NAME}_search_needed (path, name, force)
387      const char *path;
388      const char *name;
389      int force;
391   const char *s;
392   size_t len;
394   if (path == NULL || *path == '\0')
395     return false;
396   len = strlen (name);
397   while (1)
398     {
399       char *filename, *sset;
401       s = strchr (path, ':');
402       if (s == NULL)
403         s = path + strlen (path);
405       filename = (char *) xmalloc (s - path + len + 2);
406       if (s == path)
407         sset = filename;
408       else
409         {
410           memcpy (filename, path, s - path);
411           filename[s - path] = '/';
412           sset = filename + (s - path) + 1;
413         }
414       strcpy (sset, name);
416       if (gld${EMULATION_NAME}_try_needed (filename, force))
417         return true;
419       free (filename);
421       if (*s == '\0')
422         break;
423       path = s + 1;
424     }
426   return false;
429 /* This function is called for each possible name for a dynamic object
430    named by a DT_NEEDED entry.  The FORCE parameter indicates whether
431    to skip the check for a conflicting version.  */
433 static boolean
434 gld${EMULATION_NAME}_try_needed (name, force)
435      const char *name;
436      int force;
438   bfd *abfd;
440   abfd = bfd_openr (name, bfd_get_target (output_bfd));
441   if (abfd == NULL)
442     return false;
443   if (! bfd_check_format (abfd, bfd_object))
444     {
445       (void) bfd_close (abfd);
446       return false;
447     }
448   if ((bfd_get_file_flags (abfd) & DYNAMIC) == 0)
449     {
450       (void) bfd_close (abfd);
451       return false;
452     }
454   /* Check whether this object would include any conflicting library
455      versions.  If FORCE is set, then we skip this check; we use this
456      the second time around, if we couldn't find any compatible
457      instance of the shared library.  */
459   if (! force)
460     {
461       struct bfd_link_needed_list *needed;
463       if (! bfd_elf_get_bfd_needed_list (abfd, &needed))
464         einfo ("%F%P:%B: bfd_elf_get_bfd_needed_list failed: %E\n", abfd);
466       if (needed != NULL)
467         {
468           global_vercheck_needed = needed;
469           global_vercheck_failed = false;
470           lang_for_each_input_file (gld${EMULATION_NAME}_vercheck);
471           if (global_vercheck_failed)
472             {
473               (void) bfd_close (abfd);
474               /* Return false to force the caller to move on to try
475                  another file on the search path.  */
476               return false;
477             }
479           /* But wait!  It gets much worse.  On Linux, if a shared
480              library does not use libc at all, we are supposed to skip
481              it the first time around in case we encounter a shared
482              library later on with the same name which does use the
483              version of libc that we want.  This is much too horrible
484              to use on any system other than Linux.  */
487 case ${target} in
488   *-*-linux-gnu*)
489     cat >>e${EMULATION_NAME}.c <<EOF
490           {
491             struct bfd_link_needed_list *l;
493             for (l = needed; l != NULL; l = l->next)
494               if (strncmp (l->name, "libc.so", 7) == 0)
495                 break;
496             if (l == NULL)
497               {
498                 (void) bfd_close (abfd);
499                 return false;
500               }
501           }
504     ;;
505 esac
506 cat >>e${EMULATION_NAME}.c <<EOF
507         }
508     }
510   /* We've found a dynamic object matching the DT_NEEDED entry.  */
512   /* We have already checked that there is no other input file of the
513      same name.  We must now check again that we are not including the
514      same file twice.  We need to do this because on many systems
515      libc.so is a symlink to, e.g., libc.so.1.  The SONAME entry will
516      reference libc.so.1.  If we have already included libc.so, we
517      don't want to include libc.so.1 if they are the same file, and we
518      can only check that using stat.  */
520   if (bfd_stat (abfd, &global_stat) != 0)
521     einfo ("%F%P:%B: bfd_stat failed: %E\n", abfd);
522   global_found = false;
523   lang_for_each_input_file (gld${EMULATION_NAME}_stat_needed);
524   if (global_found)
525     {
526       /* Return true to indicate that we found the file, even though
527          we aren't going to do anything with it.  */
528       return true;
529     }
531   /* Tell the ELF backend that don't want the output file to have a
532      DT_NEEDED entry for this file.  */
533   bfd_elf_set_dt_needed_name (abfd, "");
535   /* First strip off everything before the last '/'.  */
536   name = strrchr (abfd->filename, '/');
537   if (name)
538     name++;
539   else
540     name = abfd->filename;
542   /* Tell the ELF backend that the output file needs a DT_NEEDED
543      entry for this file if it is used to resolve the reference in
544      a regular object.  */
545   bfd_elf_set_dt_needed_soname (abfd, name);
547   /* Add this file into the symbol table.  */
548   if (! bfd_link_add_symbols (abfd, &link_info))
549     einfo ("%F%B: could not read symbols: %E\n", abfd);
551   return true;
554 /* See if an input file matches a DT_NEEDED entry by name.  */
556 static void
557 gld${EMULATION_NAME}_check_needed (s)
558      lang_input_statement_type *s;
560   if (global_found)
561     return;
563   if (s->filename != NULL
564       && strcmp (s->filename, global_needed->name) == 0)
565     {
566       global_found = true;
567       return;
568     }
570   if (s->the_bfd != NULL)
571     {
572       const char *soname;
574       soname = bfd_elf_get_dt_soname (s->the_bfd);
575       if (soname != NULL
576           && strcmp (soname, global_needed->name) == 0)
577         {
578           global_found = true;
579           return;
580         }
581     }
582           
583   if (s->search_dirs_flag
584       && s->filename != NULL
585       && strchr (global_needed->name, '/') == NULL)
586     {
587       const char *f;
589       f = strrchr (s->filename, '/');
590       if (f != NULL
591           && strcmp (f + 1, global_needed->name) == 0)
592         {
593           global_found = true;
594           return;
595         }
596     }
599 /* See if an input file matches a DT_NEEDED entry by running stat on
600    the file.  */
602 static void
603 gld${EMULATION_NAME}_stat_needed (s)
604      lang_input_statement_type *s;
606   struct stat st;
607   const char *suffix;
608   const char *soname;
609   const char *f;
611   if (global_found)
612     return;
613   if (s->the_bfd == NULL)
614     return;
616   if (bfd_stat (s->the_bfd, &st) != 0)
617     {
618       einfo ("%P:%B: bfd_stat failed: %E\n", s->the_bfd);
619       return;
620     }
622   if (st.st_dev == global_stat.st_dev
623       && st.st_ino == global_stat.st_ino)
624     {
625       global_found = true;
626       return;
627     }
629   /* We issue a warning if it looks like we are including two
630      different versions of the same shared library.  For example,
631      there may be a problem if -lc picks up libc.so.6 but some other
632      shared library has a DT_NEEDED entry of libc.so.5.  This is a
633      hueristic test, and it will only work if the name looks like
634      NAME.so.VERSION.  FIXME: Depending on file names is error-prone.
635      If we really want to issue warnings about mixing version numbers
636      of shared libraries, we need to find a better way.  */
638   if (strchr (global_needed->name, '/') != NULL)
639     return;
640   suffix = strstr (global_needed->name, ".so.");
641   if (suffix == NULL)
642     return;
643   suffix += sizeof ".so." - 1;
645   soname = bfd_elf_get_dt_soname (s->the_bfd);
646   if (soname == NULL)
647     soname = s->filename;
649   f = strrchr (soname, '/');
650   if (f != NULL)
651     ++f;
652   else
653     f = soname;
655   if (strncmp (f, global_needed->name, suffix - global_needed->name) == 0)
656     einfo ("%P: warning: %s, needed by %B, may conflict with %s\n",
657            global_needed->name, global_needed->by, f);
660 /* On Linux, it's possible to have different versions of the same
661    shared library linked against different versions of libc.  The
662    dynamic linker somehow tags which libc version to use in
663    /etc/ld.so.cache, and, based on the libc that it sees in the
664    executable, chooses which version of the shared library to use.
666    We try to do a similar check here by checking whether this shared
667    library needs any other shared libraries which may conflict with
668    libraries we have already included in the link.  If it does, we
669    skip it, and try to find another shared library farther on down the
670    link path.
672    This is called via lang_for_each_input_file.
673    GLOBAL_VERCHECK_NEEDED is the list of objects needed by the object
674    which we ar checking.  This sets GLOBAL_VERCHECK_FAILED if we find
675    a conflicting version.  */
677 static void
678 gld${EMULATION_NAME}_vercheck (s)
679      lang_input_statement_type *s;
681   const char *soname, *f;
682   struct bfd_link_needed_list *l;
684   if (global_vercheck_failed)
685     return;
686   if (s->the_bfd == NULL
687       || (bfd_get_file_flags (s->the_bfd) & DYNAMIC) == 0)
688     return;
690   soname = bfd_elf_get_dt_soname (s->the_bfd);
691   if (soname == NULL)
692     soname = bfd_get_filename (s->the_bfd);
694   f = strrchr (soname, '/');
695   if (f != NULL)
696     ++f;
697   else
698     f = soname;
700   for (l = global_vercheck_needed; l != NULL; l = l->next)
701     {
702       const char *suffix;
704       if (strcmp (f, l->name) == 0)
705         {
706           /* Probably can't happen, but it's an easy check.  */
707           continue;
708         }
710       if (strchr (l->name, '/') != NULL)
711         continue;
713       suffix = strstr (l->name, ".so.");
714       if (suffix == NULL)
715         continue;
717       suffix += sizeof ".so." - 1;
719       if (strncmp (f, l->name, suffix - l->name) == 0)
720         {
721           /* Here we know that S is a dynamic object FOO.SO.VER1, and
722              the object we are considering needs a dynamic object
723              FOO.SO.VER2, and VER1 and VER2 are different.  This
724              appears to be a version mismatch, so we tell the caller
725              to try a different version of this library.  */
726           global_vercheck_failed = true;
727           return;
728         }
729     }
732 /* This is called after the sections have been attached to output
733    sections, but before any sizes or addresses have been set.  */
735 static void
736 gld${EMULATION_NAME}_before_allocation ()
738   const char *rpath;
739   asection *sinterp;
741   /* If we are going to make any variable assignments, we need to let
742      the ELF backend know about them in case the variables are
743      referred to by dynamic objects.  */
744   lang_for_each_statement (gld${EMULATION_NAME}_find_statement_assignment);
746   /* Let the ELF backend work out the sizes of any sections required
747      by dynamic linking.  */
748   rpath = command_line.rpath;
749   if (rpath == NULL)
750     rpath = (const char *) getenv ("LD_RUN_PATH");
751   if (! (bfd_elf${ELFSIZE}_size_dynamic_sections
752          (output_bfd, command_line.soname, rpath,
753           command_line.export_dynamic, command_line.filter_shlib,
754           (const char * const *) command_line.auxiliary_filters,
755           &link_info, &sinterp, lang_elf_version_info)))
756     einfo ("%P%F: failed to set dynamic section sizes: %E\n");
758   /* Let the user override the dynamic linker we are using.  */
759   if (command_line.interpreter != NULL
760       && sinterp != NULL)
761     {
762       sinterp->contents = (bfd_byte *) command_line.interpreter;
763       sinterp->_raw_size = strlen (command_line.interpreter) + 1;
764     }
766   /* Look for any sections named .gnu.warning.  As a GNU extensions,
767      we treat such sections as containing warning messages.  We print
768      out the warning message, and then zero out the section size so
769      that it does not get copied into the output file.  */
771   {
772     LANG_FOR_EACH_INPUT_STATEMENT (is)
773       {
774         asection *s;
775         bfd_size_type sz;
776         char *msg;
777         boolean ret;
779         if (is->just_syms_flag)
780           continue;
782         s = bfd_get_section_by_name (is->the_bfd, ".gnu.warning");
783         if (s == NULL)
784           continue;
786         sz = bfd_section_size (is->the_bfd, s);
787         msg = xmalloc ((size_t) sz + 1);
788         if (! bfd_get_section_contents (is->the_bfd, s, msg, (file_ptr) 0, sz))
789           einfo ("%F%B: Can't read contents of section .gnu.warning: %E\n",
790                  is->the_bfd);
791         msg[sz] = '\0';
792         ret = link_info.callbacks->warning (&link_info, msg,
793                                             (const char *) NULL,
794                                             is->the_bfd, (asection *) NULL,
795                                             (bfd_vma) 0);
796         ASSERT (ret);
797         free (msg);
799         /* Clobber the section size, so that we don't waste copying the
800            warning into the output file.  */
801         s->_raw_size = 0;
802       }
803   }
806 /* This is called by the before_allocation routine via
807    lang_for_each_statement.  It locates any assignment statements, and
808    tells the ELF backend about them, in case they are assignments to
809    symbols which are referred to by dynamic objects.  */
811 static void
812 gld${EMULATION_NAME}_find_statement_assignment (s)
813      lang_statement_union_type *s;
815   if (s->header.type == lang_assignment_statement_enum)
816     gld${EMULATION_NAME}_find_exp_assignment (s->assignment_statement.exp);
819 /* Look through an expression for an assignment statement.  */
821 static void
822 gld${EMULATION_NAME}_find_exp_assignment (exp)
823      etree_type *exp;
825   struct bfd_link_hash_entry *h;
827   switch (exp->type.node_class)
828     {
829     case etree_provide:
830       h = bfd_link_hash_lookup (link_info.hash, exp->assign.dst,
831                                 false, false, false);
832       if (h == NULL)
833         break;
835       /* We call record_link_assignment even if the symbol is defined.
836          This is because if it is defined by a dynamic object, we
837          actually want to use the value defined by the linker script,
838          not the value from the dynamic object (because we are setting
839          symbols like etext).  If the symbol is defined by a regular
840          object, then, as it happens, calling record_link_assignment
841          will do no harm.  */
843       /* Fall through.  */
844     case etree_assign:
845       if (strcmp (exp->assign.dst, ".") != 0)
846         {
847           if (! (bfd_elf${ELFSIZE}_record_link_assignment
848                  (output_bfd, &link_info, exp->assign.dst,
849                   exp->type.node_class == etree_provide ? true : false)))
850             einfo ("%P%F: failed to record assignment to %s: %E\n",
851                    exp->assign.dst);
852         }
853       gld${EMULATION_NAME}_find_exp_assignment (exp->assign.src);
854       break;
856     case etree_binary:
857       gld${EMULATION_NAME}_find_exp_assignment (exp->binary.lhs);
858       gld${EMULATION_NAME}_find_exp_assignment (exp->binary.rhs);
859       break;
861     case etree_trinary:
862       gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.cond);
863       gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.lhs);
864       gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.rhs);
865       break;
867     case etree_unary:
868       gld${EMULATION_NAME}_find_exp_assignment (exp->unary.child);
869       break;
871     default:
872       break;
873     }
876 /* Place an orphan section.  We use this to put random SHF_ALLOC
877    sections in the right segment.  */
879 struct orphan_save
881   lang_output_section_statement_type *os;
882   asection **section;
883   lang_statement_union_type **stmt;
886 /*ARGSUSED*/
887 static boolean
888 gld${EMULATION_NAME}_place_orphan (file, s)
889      lang_input_statement_type *file;
890      asection *s;
892   static struct orphan_save hold_text;
893   static struct orphan_save hold_rodata;
894   static struct orphan_save hold_data;
895   static struct orphan_save hold_bss;
896   static struct orphan_save hold_rel;
897   static struct orphan_save hold_interp;
898   struct orphan_save *place;
899   lang_statement_list_type *old;
900   lang_statement_list_type add;
901   etree_type *address;
902   const char *secname, *ps;
903   const char *outsecname;
904   lang_output_section_statement_type *os;
906   secname = bfd_get_section_name (s->owner, s);
908   /* Look through the script to see where to place this section.  */
909   os = lang_output_section_find (secname);
911   if (os != NULL
912       && os->bfd_section != NULL
913       && ((s->flags ^ os->bfd_section->flags) & (SEC_LOAD | SEC_ALLOC)) == 0)
914     {
915       /* We have already placed a section with this name.  */
916       wild_doit (&os->children, s, os, file);
917       return true;
918     }
920   if (hold_text.os == NULL)
921     hold_text.os = lang_output_section_find (".text");
923   /* If this is a final link, then always put .gnu.warning.SYMBOL
924      sections into the .text section to get them out of the way.  */
925   if (! link_info.shared
926       && ! link_info.relocateable
927       && strncmp (secname, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0
928       && hold_text.os != NULL)
929     {
930       wild_doit (&hold_text.os->children, s, hold_text.os, file);
931       return true;
932     }
934   /* Decide which segment the section should go in based on the
935      section name and section flags.  We put loadable .note sections
936      right after the .interp section, so that the PT_NOTE segment is
937      stored right after the program headers where the OS can read it
938      in the first page.  */
939 #define HAVE_SECTION(hold, name) \
940 (hold.os != NULL || (hold.os = lang_output_section_find (name)) != NULL)
942   if (s->flags & SEC_EXCLUDE)
943     return false;
944   else if ((s->flags & SEC_ALLOC) == 0)
945     place = NULL;
946   else if ((s->flags & SEC_LOAD) != 0
947            && strncmp (secname, ".note", 4) == 0
948            && HAVE_SECTION (hold_interp, ".interp"))
949     place = &hold_interp;
950   else if ((s->flags & SEC_HAS_CONTENTS) == 0
951            && HAVE_SECTION (hold_bss, ".bss"))
952     place = &hold_bss;
953   else if ((s->flags & SEC_READONLY) == 0
954            && HAVE_SECTION (hold_data, ".data"))
955     place = &hold_data;
956   else if (strncmp (secname, ".rel", 4) == 0
957            && (hold_rel.os != NULL
958                || (hold_rel.os = output_rel_find ()) != NULL))
959     place = &hold_rel;
960   else if ((s->flags & SEC_CODE) == 0
961            && (s->flags & SEC_READONLY) != 0
962            && HAVE_SECTION (hold_rodata, ".rodata"))
963     place = &hold_rodata;
964   else if ((s->flags & SEC_READONLY) != 0
965            && hold_text.os != NULL)
966     place = &hold_text;
967   else
968     place = NULL;
970 #undef HAVE_SECTION
972   /* Choose a unique name for the section.  This will be needed if the
973      same section name appears in the input file with different
974      loadable or allocateable characteristics.  */
975   outsecname = secname;
976   if (bfd_get_section_by_name (output_bfd, outsecname) != NULL)
977     {
978       unsigned int len;
979       char *newname;
980       unsigned int i;
982       len = strlen (outsecname);
983       newname = xmalloc (len + 5);
984       strcpy (newname, outsecname);
985       i = 0;
986       do
987         {
988           sprintf (newname + len, "%d", i);
989           ++i;
990         }
991       while (bfd_get_section_by_name (output_bfd, newname) != NULL);
993       outsecname = newname;
994     }
996   if (place != NULL)
997     {
998       /* Start building a list of statements for this section.  */
999       old = stat_ptr;
1000       stat_ptr = &add;
1001       lang_list_init (stat_ptr);
1003       /* If the name of the section is representable in C, then create
1004          symbols to mark the start and the end of the section.  */
1005       for (ps = outsecname; *ps != '\0'; ps++)
1006         if (! isalnum ((unsigned char) *ps) && *ps != '_')
1007           break;
1008       if (*ps == '\0' && config.build_constructors)
1009         {
1010           char *symname;
1011           etree_type *e_align;
1013           symname = (char *) xmalloc (ps - outsecname + sizeof "__start_");
1014           sprintf (symname, "__start_%s", outsecname);
1015           e_align = exp_unop (ALIGN_K,
1016                               exp_intop ((bfd_vma) 1 << s->alignment_power));
1017           lang_add_assignment (exp_assop ('=', symname, e_align));
1018         }
1019     }
1021   if (link_info.relocateable || (s->flags & (SEC_LOAD | SEC_ALLOC)) == 0)
1022     address = exp_intop ((bfd_vma) 0);
1023   else
1024     address = NULL;
1026   os = lang_enter_output_section_statement (outsecname, address, 0,
1027                                             (bfd_vma) 0,
1028                                             (etree_type *) NULL,
1029                                             (etree_type *) NULL,
1030                                             (etree_type *) NULL);
1032   wild_doit (&os->children, s, os, file);
1034   lang_leave_output_section_statement
1035     ((bfd_vma) 0, "*default*",
1036      (struct lang_output_section_phdr_list *) NULL, "*default*");
1038   if (place != NULL)
1039     {
1040       asection *snew, **pps;
1042       stat_ptr = &add;
1044       if (*ps == '\0' && config.build_constructors)
1045         {
1046           char *symname;
1048           symname = (char *) xmalloc (ps - outsecname + sizeof "__stop_");
1049           sprintf (symname, "__stop_%s", outsecname);
1050           lang_add_assignment (exp_assop ('=', symname,
1051                                           exp_nameop (NAME, ".")));
1052         }
1053       stat_ptr = old;
1055       snew = os->bfd_section;
1056       if (place->os->bfd_section != NULL || place->section != NULL)
1057         {
1058           /* Shuffle the section to make the output file look neater.  */
1059           if (place->section == NULL)
1060             {
1061 #if 0
1062               /* Finding the end of the list is a little tricky.  We
1063                  make a wild stab at it by comparing section flags.  */
1064               flagword first_flags = place->os->bfd_section->flags;
1065               for (pps = &place->os->bfd_section->next;
1066                    *pps != NULL && (*pps)->flags == first_flags;
1067                    pps = &(*pps)->next)
1068                 ;
1069               place->section = pps;
1070 #else
1071               /* Put orphans after the first section on the list.  */
1072               place->section = &place->os->bfd_section->next;
1073 #endif
1074             }
1076           /*  Unlink the section.  */
1077           for (pps = &output_bfd->sections; *pps != snew; pps = &(*pps)->next)
1078             ;
1079           *pps = snew->next;
1081           /* Now tack it on to the "place->os" section list.  */
1082           snew->next = *place->section;
1083           *place->section = snew;
1084         }
1085       place->section = &snew->next;     /* Save the end of this list.  */
1087       if (place->stmt == NULL)
1088         {
1089           /* Put the new statement list right at the head.  */
1090           *add.tail = place->os->header.next;
1091           place->os->header.next = add.head;
1092         }
1093       else
1094         {
1095           /* Put it after the last orphan statement we added.  */
1096           *add.tail = *place->stmt;
1097           *place->stmt = add.head;
1098         }
1099       place->stmt = add.tail;           /* Save the end of this list.  */
1100     }
1102   return true;
1105 /* A variant of lang_output_section_find.  */
1106 static lang_output_section_statement_type *
1107 output_rel_find ()
1109   lang_statement_union_type *u;
1110   lang_output_section_statement_type *lookup;
1112   for (u = lang_output_section_statement.head;
1113        u != (lang_statement_union_type *) NULL;
1114        u = lookup->next)
1115     {
1116       lookup = &u->output_section_statement;
1117       if (strncmp (".rel", lookup->name, 4) == 0
1118           && lookup->bfd_section != NULL
1119           && (lookup->bfd_section->flags & SEC_ALLOC) != 0)
1120         {
1121           return lookup;
1122         }
1123     }
1124   return (lang_output_section_statement_type *) NULL;
1127 static char *
1128 gld${EMULATION_NAME}_get_script(isfile)
1129      int *isfile;
1132 if test -n "$COMPILE_IN"
1133 then
1134 # Scripts compiled in.
1136 # sed commands to quote an ld script as a C string.
1137 sc="-f stringify.sed"
1139 cat >>e${EMULATION_NAME}.c <<EOF
1141   *isfile = 0;
1143   if (link_info.relocateable == true && config.build_constructors == true)
1144     return
1146 sed $sc ldscripts/${EMULATION_NAME}.xu                     >> e${EMULATION_NAME}.c
1147 echo '  ; else if (link_info.relocateable == true) return' >> e${EMULATION_NAME}.c
1148 sed $sc ldscripts/${EMULATION_NAME}.xr                     >> e${EMULATION_NAME}.c
1149 echo '  ; else if (!config.text_read_only) return'         >> e${EMULATION_NAME}.c
1150 sed $sc ldscripts/${EMULATION_NAME}.xbn                    >> e${EMULATION_NAME}.c
1151 echo '  ; else if (!config.magic_demand_paged) return'     >> e${EMULATION_NAME}.c
1152 sed $sc ldscripts/${EMULATION_NAME}.xn                     >> e${EMULATION_NAME}.c
1154 if test -n "$GENERATE_SHLIB_SCRIPT" ; then
1155 echo '  ; else if (link_info.shared) return'               >> e${EMULATION_NAME}.c
1156 sed $sc ldscripts/${EMULATION_NAME}.xs                     >> e${EMULATION_NAME}.c
1159 echo '  ; else return'                                     >> e${EMULATION_NAME}.c
1160 sed $sc ldscripts/${EMULATION_NAME}.x                      >> e${EMULATION_NAME}.c
1161 echo '; }'                                                 >> e${EMULATION_NAME}.c
1163 else
1164 # Scripts read from the filesystem.
1166 cat >>e${EMULATION_NAME}.c <<EOF
1168   *isfile = 1;
1170   if (link_info.relocateable == true && config.build_constructors == true)
1171     return "ldscripts/${EMULATION_NAME}.xu";
1172   else if (link_info.relocateable == true)
1173     return "ldscripts/${EMULATION_NAME}.xr";
1174   else if (!config.text_read_only)
1175     return "ldscripts/${EMULATION_NAME}.xbn";
1176   else if (!config.magic_demand_paged)
1177     return "ldscripts/${EMULATION_NAME}.xn";
1178   else if (link_info.shared)
1179     return "ldscripts/${EMULATION_NAME}.xs";
1180   else
1181     return "ldscripts/${EMULATION_NAME}.x";
1187 if test -n "$PARSE_AND_LIST_ARGS" ; then
1188 cat >>e${EMULATION_NAME}.c <<EOF
1189 static int  gld_${EMULATION_NAME}_parse_args PARAMS ((int, char **));
1190 static void gld_${EMULATION_NAME}_list_options PARAMS ((FILE * file));
1192  $PARSE_AND_LIST_ARGS
1194 else
1196 cat >>e${EMULATION_NAME}.c <<EOF
1197 #define gld_${EMULATION_NAME}_parse_args   NULL
1198 #define gld_${EMULATION_NAME}_list_options NULL
1203 cat >>e${EMULATION_NAME}.c <<EOF
1205 struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
1207   gld${EMULATION_NAME}_before_parse,
1208   syslib_default,
1209   hll_default,
1210   after_parse_default,
1211   gld${EMULATION_NAME}_after_open,
1212   after_allocation_default,
1213   set_output_arch_default,
1214   ldemul_default_target,
1215   gld${EMULATION_NAME}_before_allocation,
1216   gld${EMULATION_NAME}_get_script,
1217   "${EMULATION_NAME}",
1218   "${OUTPUT_FORMAT}",
1219   NULL,         /* finish */
1220   NULL,         /* create output section statements */
1221   gld${EMULATION_NAME}_open_dynamic_archive,
1222   gld${EMULATION_NAME}_place_orphan,
1223   NULL,         /* set_symbols */
1224   gld_${EMULATION_NAME}_parse_args,
1225   NULL,         /* unrecognized_file */
1226   gld_${EMULATION_NAME}_list_options,
1227   NULL,         /* recognized_file */
1228   NULL          /* find_potential_libraries */