This commit was generated by cvs2svn to track changes on a CVS vendor
[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 cat >e${EMULATION_NAME}.c <<EOF
7 /* This file is is generated by a shell script.  DO NOT EDIT! */
8
9 /* ${ELFSIZE} bit ELF emulation code for ${EMULATION_NAME}
10    Copyright (C) 1991, 93, 94, 95, 96, 97, 98, 1999
11    Free Software Foundation, Inc.
12    Written by Steve Chamberlain <sac@cygnus.com>
13    ELF support by Ian Lance Taylor <ian@cygnus.com>
14
15 This file is part of GLD, the Gnu Linker.
16
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.
21
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.
26
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.  */
30
31 #define TARGET_IS_${EMULATION_NAME}
32
33 #include "bfd.h"
34 #include "sysdep.h"
35
36 #include <ctype.h>
37
38 #include "bfdlink.h"
39
40 #include "ld.h"
41 #include "ldmain.h"
42 #include "ldemul.h"
43 #include "ldfile.h"
44 #include "ldmisc.h"
45 #include "ldexp.h"
46 #include "ldlang.h"
47 #include "ldgram.h"
48
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));
70
71 static void
72 gld${EMULATION_NAME}_before_parse()
73 {
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`;
77 }
78
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).  */
82
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;
88 {
89   const char *filename;
90   char *string;
91
92   if (! entry->is_archive)
93     return false;
94
95   filename = entry->filename;
96
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");
107
108   sprintf (string, "%s/lib%s%s.so", search->name, filename, arch);
109
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
117
118   if (! ldfile_try_open_bfd (string, entry))
119     {
120       free (string);
121       return false;
122     }
123
124   entry->filename = string;
125
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.
135
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;
143
144       ASSERT (entry->is_archive && entry->search_dirs_flag);
145
146       /* Rather than duplicating the logic above.  Just use the
147          filename we recorded earlier.
148
149          First strip off everything before the last '/'.  */
150       filename = strrchr (entry->filename, '/');
151       filename++;
152
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     }
157
158   return true;
159 }
160
161 EOF
162 if [ "x${host}" = "x${target}" ] ; then
163   case " ${EMULATION_LIBPATH} " in
164   *" ${EMULATION_NAME} "*)
165 cat >>e${EMULATION_NAME}.c <<EOF
166
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.  */
170
171 static boolean gld${EMULATION_NAME}_check_ld_so_conf
172   PARAMS ((const char *, int));
173
174 static boolean
175 gld${EMULATION_NAME}_check_ld_so_conf (name, force)
176      const char *name;
177      int force;
178 {
179   static boolean initialized;
180   static char *ld_so_conf;
181
182   if (! initialized)
183     {
184       FILE *f;
185
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;
192
193           len = 0;
194           alloc = 100;
195           b = (char *) xmalloc (alloc);
196
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             }
222
223           if (len > 0 && b[len - 1] == ':')
224             --len;
225
226           if (len > 0)
227             b[len] = '\0';
228           else
229             {
230               free (b);
231               b = NULL;
232             }
233
234           fclose (f);
235
236           ld_so_conf = b;
237         }
238
239       initialized = true;
240     }
241
242   if (ld_so_conf == NULL)
243     return false;
244
245   return gld${EMULATION_NAME}_search_needed (ld_so_conf, name, force);
246 }
247
248 EOF
249   ;;
250   esac
251 fi
252 cat >>e${EMULATION_NAME}.c <<EOF
253
254 /* These variables are required to pass information back and forth
255    between after_open and check_needed and stat_needed and vercheck.  */
256
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;
262
263 /* This is called after all the input files have been opened.  */
264
265 static void
266 gld${EMULATION_NAME}_after_open ()
267 {
268   struct bfd_link_needed_list *needed, *l;
269
270   /* We only need to worry about this when doing a final link.  */
271   if (link_info.relocateable || link_info.shared)
272     return;
273
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;
288
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;
295
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;
302
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.
309
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;
318
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             }
333 EOF
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;
341 EOF
342   ;;
343   esac
344 fi
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;
350
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;
361 EOF
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;
368 EOF
369   ;;
370   esac
371 fi
372 cat >>e${EMULATION_NAME}.c <<EOF
373         }
374
375       if (force < 2)
376         continue;
377
378       einfo ("%P: warning: %s, needed by %B, not found (try using --rpath)\n",
379              l->name, l->by);
380     }
381 }
382
383 /* Search for a needed file in a path.  */
384
385 static boolean
386 gld${EMULATION_NAME}_search_needed (path, name, force)
387      const char *path;
388      const char *name;
389      int force;
390 {
391   const char *s;
392   size_t len;
393
394   if (path == NULL || *path == '\0')
395     return false;
396   len = strlen (name);
397   while (1)
398     {
399       char *filename, *sset;
400
401       s = strchr (path, ':');
402       if (s == NULL)
403         s = path + strlen (path);
404
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);
415
416       if (gld${EMULATION_NAME}_try_needed (filename, force))
417         return true;
418
419       free (filename);
420
421       if (*s == '\0')
422         break;
423       path = s + 1;
424     }
425
426   return false;
427 }
428
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.  */
432
433 static boolean
434 gld${EMULATION_NAME}_try_needed (name, force)
435      const char *name;
436      int force;
437 {
438   bfd *abfd;
439
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     }
453
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.  */
458
459   if (! force)
460     {
461       struct bfd_link_needed_list *needed;
462
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);
465
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             }
478
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.  */
485
486 EOF
487 case ${target} in
488   *-*-linux-gnu*)
489     cat >>e${EMULATION_NAME}.c <<EOF
490           {
491             struct bfd_link_needed_list *l;
492
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           }
502
503 EOF
504     ;;
505 esac
506 cat >>e${EMULATION_NAME}.c <<EOF
507         }
508     }
509
510   /* We've found a dynamic object matching the DT_NEEDED entry.  */
511
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.  */
519
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     }
530
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, "");
534
535   /* Add this file into the symbol table.  */
536   if (! bfd_link_add_symbols (abfd, &link_info))
537     einfo ("%F%B: could not read symbols: %E\n", abfd);
538
539   return true;
540 }
541
542 /* See if an input file matches a DT_NEEDED entry by name.  */
543
544 static void
545 gld${EMULATION_NAME}_check_needed (s)
546      lang_input_statement_type *s;
547 {
548   if (global_found)
549     return;
550
551   if (s->filename != NULL
552       && strcmp (s->filename, global_needed->name) == 0)
553     {
554       global_found = true;
555       return;
556     }
557
558   if (s->the_bfd != NULL)
559     {
560       const char *soname;
561
562       soname = bfd_elf_get_dt_soname (s->the_bfd);
563       if (soname != NULL
564           && strcmp (soname, global_needed->name) == 0)
565         {
566           global_found = true;
567           return;
568         }
569     }
570           
571   if (s->search_dirs_flag
572       && s->filename != NULL
573       && strchr (global_needed->name, '/') == NULL)
574     {
575       const char *f;
576
577       f = strrchr (s->filename, '/');
578       if (f != NULL
579           && strcmp (f + 1, global_needed->name) == 0)
580         {
581           global_found = true;
582           return;
583         }
584     }
585 }
586
587 /* See if an input file matches a DT_NEEDED entry by running stat on
588    the file.  */
589
590 static void
591 gld${EMULATION_NAME}_stat_needed (s)
592      lang_input_statement_type *s;
593 {
594   struct stat st;
595   const char *suffix;
596   const char *soname;
597   const char *f;
598
599   if (global_found)
600     return;
601   if (s->the_bfd == NULL)
602     return;
603
604   if (bfd_stat (s->the_bfd, &st) != 0)
605     {
606       einfo ("%P:%B: bfd_stat failed: %E\n", s->the_bfd);
607       return;
608     }
609
610   if (st.st_dev == global_stat.st_dev
611       && st.st_ino == global_stat.st_ino)
612     {
613       global_found = true;
614       return;
615     }
616
617   /* We issue a warning if it looks like we are including two
618      different versions of the same shared library.  For example,
619      there may be a problem if -lc picks up libc.so.6 but some other
620      shared library has a DT_NEEDED entry of libc.so.5.  This is a
621      hueristic test, and it will only work if the name looks like
622      NAME.so.VERSION.  FIXME: Depending on file names is error-prone.
623      If we really want to issue warnings about mixing version numbers
624      of shared libraries, we need to find a better way.  */
625
626   if (strchr (global_needed->name, '/') != NULL)
627     return;
628   suffix = strstr (global_needed->name, ".so.");
629   if (suffix == NULL)
630     return;
631   suffix += sizeof ".so." - 1;
632
633   soname = bfd_elf_get_dt_soname (s->the_bfd);
634   if (soname == NULL)
635     soname = s->filename;
636
637   f = strrchr (soname, '/');
638   if (f != NULL)
639     ++f;
640   else
641     f = soname;
642
643   if (strncmp (f, global_needed->name, suffix - global_needed->name) == 0)
644     einfo ("%P: warning: %s, needed by %B, may conflict with %s\n",
645            global_needed->name, global_needed->by, f);
646 }
647
648 /* On Linux, it's possible to have different versions of the same
649    shared library linked against different versions of libc.  The
650    dynamic linker somehow tags which libc version to use in
651    /etc/ld.so.cache, and, based on the libc that it sees in the
652    executable, chooses which version of the shared library to use.
653
654    We try to do a similar check here by checking whether this shared
655    library needs any other shared libraries which may conflict with
656    libraries we have already included in the link.  If it does, we
657    skip it, and try to find another shared library farther on down the
658    link path.
659
660    This is called via lang_for_each_input_file.
661    GLOBAL_VERCHECK_NEEDED is the list of objects needed by the object
662    which we ar checking.  This sets GLOBAL_VERCHECK_FAILED if we find
663    a conflicting version.  */
664
665 static void
666 gld${EMULATION_NAME}_vercheck (s)
667      lang_input_statement_type *s;
668 {
669   const char *soname, *f;
670   struct bfd_link_needed_list *l;
671
672   if (global_vercheck_failed)
673     return;
674   if (s->the_bfd == NULL
675       || (bfd_get_file_flags (s->the_bfd) & DYNAMIC) == 0)
676     return;
677
678   soname = bfd_elf_get_dt_soname (s->the_bfd);
679   if (soname == NULL)
680     soname = bfd_get_filename (s->the_bfd);
681
682   f = strrchr (soname, '/');
683   if (f != NULL)
684     ++f;
685   else
686     f = soname;
687
688   for (l = global_vercheck_needed; l != NULL; l = l->next)
689     {
690       const char *suffix;
691
692       if (strcmp (f, l->name) == 0)
693         {
694           /* Probably can't happen, but it's an easy check.  */
695           continue;
696         }
697
698       if (strchr (l->name, '/') != NULL)
699         continue;
700
701       suffix = strstr (l->name, ".so.");
702       if (suffix == NULL)
703         continue;
704
705       suffix += sizeof ".so." - 1;
706
707       if (strncmp (f, l->name, suffix - l->name) == 0)
708         {
709           /* Here we know that S is a dynamic object FOO.SO.VER1, and
710              the object we are considering needs a dynamic object
711              FOO.SO.VER2, and VER1 and VER2 are different.  This
712              appears to be a version mismatch, so we tell the caller
713              to try a different version of this library.  */
714           global_vercheck_failed = true;
715           return;
716         }
717     }
718 }
719
720 /* This is called after the sections have been attached to output
721    sections, but before any sizes or addresses have been set.  */
722
723 static void
724 gld${EMULATION_NAME}_before_allocation ()
725 {
726   const char *rpath;
727   asection *sinterp;
728
729   /* If we are going to make any variable assignments, we need to let
730      the ELF backend know about them in case the variables are
731      referred to by dynamic objects.  */
732   lang_for_each_statement (gld${EMULATION_NAME}_find_statement_assignment);
733
734   /* Let the ELF backend work out the sizes of any sections required
735      by dynamic linking.  */
736   rpath = command_line.rpath;
737   if (rpath == NULL)
738     rpath = (const char *) getenv ("LD_RUN_PATH");
739   if (! (bfd_elf${ELFSIZE}_size_dynamic_sections
740          (output_bfd, command_line.soname, rpath,
741           command_line.export_dynamic, command_line.filter_shlib,
742           (const char * const *) command_line.auxiliary_filters,
743           &link_info, &sinterp, lang_elf_version_info)))
744     einfo ("%P%F: failed to set dynamic section sizes: %E\n");
745
746   /* Let the user override the dynamic linker we are using.  */
747   if (command_line.interpreter != NULL
748       && sinterp != NULL)
749     {
750       sinterp->contents = (bfd_byte *) command_line.interpreter;
751       sinterp->_raw_size = strlen (command_line.interpreter) + 1;
752     }
753
754   /* Look for any sections named .gnu.warning.  As a GNU extensions,
755      we treat such sections as containing warning messages.  We print
756      out the warning message, and then zero out the section size so
757      that it does not get copied into the output file.  */
758
759   {
760     LANG_FOR_EACH_INPUT_STATEMENT (is)
761       {
762         asection *s;
763         bfd_size_type sz;
764         char *msg;
765         boolean ret;
766
767         if (is->just_syms_flag)
768           continue;
769
770         s = bfd_get_section_by_name (is->the_bfd, ".gnu.warning");
771         if (s == NULL)
772           continue;
773
774         sz = bfd_section_size (is->the_bfd, s);
775         msg = xmalloc ((size_t) sz + 1);
776         if (! bfd_get_section_contents (is->the_bfd, s, msg, (file_ptr) 0, sz))
777           einfo ("%F%B: Can't read contents of section .gnu.warning: %E\n",
778                  is->the_bfd);
779         msg[sz] = '\0';
780         ret = link_info.callbacks->warning (&link_info, msg,
781                                             (const char *) NULL,
782                                             is->the_bfd, (asection *) NULL,
783                                             (bfd_vma) 0);
784         ASSERT (ret);
785         free (msg);
786
787         /* Clobber the section size, so that we don't waste copying the
788            warning into the output file.  */
789         s->_raw_size = 0;
790       }
791   }
792 }
793
794 /* This is called by the before_allocation routine via
795    lang_for_each_statement.  It locates any assignment statements, and
796    tells the ELF backend about them, in case they are assignments to
797    symbols which are referred to by dynamic objects.  */
798
799 static void
800 gld${EMULATION_NAME}_find_statement_assignment (s)
801      lang_statement_union_type *s;
802 {
803   if (s->header.type == lang_assignment_statement_enum)
804     gld${EMULATION_NAME}_find_exp_assignment (s->assignment_statement.exp);
805 }
806
807 /* Look through an expression for an assignment statement.  */
808
809 static void
810 gld${EMULATION_NAME}_find_exp_assignment (exp)
811      etree_type *exp;
812 {
813   struct bfd_link_hash_entry *h;
814
815   switch (exp->type.node_class)
816     {
817     case etree_provide:
818       h = bfd_link_hash_lookup (link_info.hash, exp->assign.dst,
819                                 false, false, false);
820       if (h == NULL)
821         break;
822
823       /* We call record_link_assignment even if the symbol is defined.
824          This is because if it is defined by a dynamic object, we
825          actually want to use the value defined by the linker script,
826          not the value from the dynamic object (because we are setting
827          symbols like etext).  If the symbol is defined by a regular
828          object, then, as it happens, calling record_link_assignment
829          will do no harm.  */
830
831       /* Fall through.  */
832     case etree_assign:
833       if (strcmp (exp->assign.dst, ".") != 0)
834         {
835           if (! (bfd_elf${ELFSIZE}_record_link_assignment
836                  (output_bfd, &link_info, exp->assign.dst,
837                   exp->type.node_class == etree_provide ? true : false)))
838             einfo ("%P%F: failed to record assignment to %s: %E\n",
839                    exp->assign.dst);
840         }
841       gld${EMULATION_NAME}_find_exp_assignment (exp->assign.src);
842       break;
843
844     case etree_binary:
845       gld${EMULATION_NAME}_find_exp_assignment (exp->binary.lhs);
846       gld${EMULATION_NAME}_find_exp_assignment (exp->binary.rhs);
847       break;
848
849     case etree_trinary:
850       gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.cond);
851       gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.lhs);
852       gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.rhs);
853       break;
854
855     case etree_unary:
856       gld${EMULATION_NAME}_find_exp_assignment (exp->unary.child);
857       break;
858
859     default:
860       break;
861     }
862 }
863
864 /* Place an orphan section.  We use this to put random SHF_ALLOC
865    sections in the right segment.  */
866
867 struct orphan_save
868 {
869   lang_output_section_statement_type *os;
870   asection **section;
871   lang_statement_union_type **stmt;
872 };
873
874 /*ARGSUSED*/
875 static boolean
876 gld${EMULATION_NAME}_place_orphan (file, s)
877      lang_input_statement_type *file;
878      asection *s;
879 {
880   static struct orphan_save hold_text;
881   static struct orphan_save hold_rodata;
882   static struct orphan_save hold_data;
883   static struct orphan_save hold_bss;
884   static struct orphan_save hold_rel;
885   static struct orphan_save hold_interp;
886   struct orphan_save *place;
887   lang_statement_list_type *old;
888   lang_statement_list_type add;
889   etree_type *address;
890   const char *secname, *ps;
891   const char *outsecname;
892   lang_output_section_statement_type *os;
893
894   secname = bfd_get_section_name (s->owner, s);
895
896   /* Look through the script to see where to place this section.  */
897   os = lang_output_section_find (secname);
898
899   if (os != NULL
900       && os->bfd_section != NULL
901       && ((s->flags ^ os->bfd_section->flags) & (SEC_LOAD | SEC_ALLOC)) == 0)
902     {
903       /* We have already placed a section with this name.  */
904       wild_doit (&os->children, s, os, file);
905       return true;
906     }
907
908   if (hold_text.os == NULL)
909     hold_text.os = lang_output_section_find (".text");
910
911   /* If this is a final link, then always put .gnu.warning.SYMBOL
912      sections into the .text section to get them out of the way.  */
913   if (! link_info.shared
914       && ! link_info.relocateable
915       && strncmp (secname, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0
916       && hold_text.os != NULL)
917     {
918       wild_doit (&hold_text.os->children, s, hold_text.os, file);
919       return true;
920     }
921
922   /* Decide which segment the section should go in based on the
923      section name and section flags.  We put loadable .note sections
924      right after the .interp section, so that the PT_NOTE segment is
925      stored right after the program headers where the OS can read it
926      in the first page.  */
927 #define HAVE_SECTION(hold, name) \
928 (hold.os != NULL || (hold.os = lang_output_section_find (name)) != NULL)
929
930   if (s->flags & SEC_EXCLUDE)
931     return false;
932   else if ((s->flags & SEC_ALLOC) == 0)
933     place = NULL;
934   else if ((s->flags & SEC_LOAD) != 0
935            && strncmp (secname, ".note", 4) == 0
936            && HAVE_SECTION (hold_interp, ".interp"))
937     place = &hold_interp;
938   else if ((s->flags & SEC_HAS_CONTENTS) == 0
939            && HAVE_SECTION (hold_bss, ".bss"))
940     place = &hold_bss;
941   else if ((s->flags & SEC_READONLY) == 0
942            && HAVE_SECTION (hold_data, ".data"))
943     place = &hold_data;
944   else if (strncmp (secname, ".rel", 4) == 0
945            && (hold_rel.os != NULL
946                || (hold_rel.os = output_rel_find ()) != NULL))
947     place = &hold_rel;
948   else if ((s->flags & SEC_CODE) == 0
949            && (s->flags & SEC_READONLY) != 0
950            && HAVE_SECTION (hold_rodata, ".rodata"))
951     place = &hold_rodata;
952   else if ((s->flags & SEC_READONLY) != 0
953            && hold_text.os != NULL)
954     place = &hold_text;
955   else
956     place = NULL;
957
958 #undef HAVE_SECTION
959
960   /* Choose a unique name for the section.  This will be needed if the
961      same section name appears in the input file with different
962      loadable or allocateable characteristics.  */
963   outsecname = secname;
964   if (bfd_get_section_by_name (output_bfd, outsecname) != NULL)
965     {
966       unsigned int len;
967       char *newname;
968       unsigned int i;
969
970       len = strlen (outsecname);
971       newname = xmalloc (len + 5);
972       strcpy (newname, outsecname);
973       i = 0;
974       do
975         {
976           sprintf (newname + len, "%d", i);
977           ++i;
978         }
979       while (bfd_get_section_by_name (output_bfd, newname) != NULL);
980
981       outsecname = newname;
982     }
983
984   if (place != NULL)
985     {
986       /* Start building a list of statements for this section.  */
987       old = stat_ptr;
988       stat_ptr = &add;
989       lang_list_init (stat_ptr);
990
991       /* If the name of the section is representable in C, then create
992          symbols to mark the start and the end of the section.  */
993       for (ps = outsecname; *ps != '\0'; ps++)
994         if (! isalnum ((unsigned char) *ps) && *ps != '_')
995           break;
996       if (*ps == '\0' && config.build_constructors)
997         {
998           char *symname;
999           etree_type *e_align;
1000
1001           symname = (char *) xmalloc (ps - outsecname + sizeof "__start_");
1002           sprintf (symname, "__start_%s", outsecname);
1003           e_align = exp_unop (ALIGN_K,
1004                               exp_intop ((bfd_vma) 1 << s->alignment_power));
1005           lang_add_assignment (exp_assop ('=', symname, e_align));
1006         }
1007     }
1008
1009   if (link_info.relocateable || (s->flags & (SEC_LOAD | SEC_ALLOC)) == 0)
1010     address = exp_intop ((bfd_vma) 0);
1011   else
1012     address = NULL;
1013
1014   os = lang_enter_output_section_statement (outsecname, address, 0,
1015                                             (bfd_vma) 0,
1016                                             (etree_type *) NULL,
1017                                             (etree_type *) NULL,
1018                                             (etree_type *) NULL);
1019
1020   wild_doit (&os->children, s, os, file);
1021
1022   lang_leave_output_section_statement
1023     ((bfd_vma) 0, "*default*",
1024      (struct lang_output_section_phdr_list *) NULL, "*default*");
1025
1026   if (place != NULL)
1027     {
1028       asection *snew, **pps;
1029
1030       stat_ptr = &add;
1031
1032       if (*ps == '\0' && config.build_constructors)
1033         {
1034           char *symname;
1035
1036           symname = (char *) xmalloc (ps - outsecname + sizeof "__stop_");
1037           sprintf (symname, "__stop_%s", outsecname);
1038           lang_add_assignment (exp_assop ('=', symname,
1039                                           exp_nameop (NAME, ".")));
1040         }
1041       stat_ptr = old;
1042
1043       snew = os->bfd_section;
1044       if (place->os->bfd_section != NULL || place->section != NULL)
1045         {
1046           /* Shuffle the section to make the output file look neater.  */
1047           if (place->section == NULL)
1048             {
1049 #if 0
1050               /* Finding the end of the list is a little tricky.  We
1051                  make a wild stab at it by comparing section flags.  */
1052               flagword first_flags = place->os->bfd_section->flags;
1053               for (pps = &place->os->bfd_section->next;
1054                    *pps != NULL && (*pps)->flags == first_flags;
1055                    pps = &(*pps)->next)
1056                 ;
1057               place->section = pps;
1058 #else
1059               /* Put orphans after the first section on the list.  */
1060               place->section = &place->os->bfd_section->next;
1061 #endif
1062             }
1063
1064           /*  Unlink the section.  */
1065           for (pps = &output_bfd->sections; *pps != snew; pps = &(*pps)->next)
1066             ;
1067           *pps = snew->next;
1068
1069           /* Now tack it on to the "place->os" section list.  */
1070           snew->next = *place->section;
1071           *place->section = snew;
1072         }
1073       place->section = &snew->next;     /* Save the end of this list.  */
1074
1075       if (place->stmt == NULL)
1076         {
1077           /* Put the new statement list right at the head.  */
1078           *add.tail = place->os->header.next;
1079           place->os->header.next = add.head;
1080         }
1081       else
1082         {
1083           /* Put it after the last orphan statement we added.  */
1084           *add.tail = *place->stmt;
1085           *place->stmt = add.head;
1086         }
1087       place->stmt = add.tail;           /* Save the end of this list.  */
1088     }
1089
1090   return true;
1091 }
1092
1093 /* A variant of lang_output_section_find.  */
1094 static lang_output_section_statement_type *
1095 output_rel_find ()
1096 {
1097   lang_statement_union_type *u;
1098   lang_output_section_statement_type *lookup;
1099
1100   for (u = lang_output_section_statement.head;
1101        u != (lang_statement_union_type *) NULL;
1102        u = lookup->next)
1103     {
1104       lookup = &u->output_section_statement;
1105       if (strncmp (".rel", lookup->name, 4) == 0
1106           && lookup->bfd_section != NULL
1107           && (lookup->bfd_section->flags & SEC_ALLOC) != 0)
1108         {
1109           return lookup;
1110         }
1111     }
1112   return (lang_output_section_statement_type *) NULL;
1113 }
1114
1115 static char *
1116 gld${EMULATION_NAME}_get_script(isfile)
1117      int *isfile;
1118 EOF
1119
1120 if test -n "$COMPILE_IN"
1121 then
1122 # Scripts compiled in.
1123
1124 # sed commands to quote an ld script as a C string.
1125 sc="-f stringify.sed"
1126
1127 cat >>e${EMULATION_NAME}.c <<EOF
1128 {
1129   *isfile = 0;
1130
1131   if (link_info.relocateable == true && config.build_constructors == true)
1132     return
1133 EOF
1134 sed $sc ldscripts/${EMULATION_NAME}.xu                     >> e${EMULATION_NAME}.c
1135 echo '  ; else if (link_info.relocateable == true) return' >> e${EMULATION_NAME}.c
1136 sed $sc ldscripts/${EMULATION_NAME}.xr                     >> e${EMULATION_NAME}.c
1137 echo '  ; else if (!config.text_read_only) return'         >> e${EMULATION_NAME}.c
1138 sed $sc ldscripts/${EMULATION_NAME}.xbn                    >> e${EMULATION_NAME}.c
1139 echo '  ; else if (!config.magic_demand_paged) return'     >> e${EMULATION_NAME}.c
1140 sed $sc ldscripts/${EMULATION_NAME}.xn                     >> e${EMULATION_NAME}.c
1141
1142 if test -n "$GENERATE_SHLIB_SCRIPT" ; then
1143 echo '  ; else if (link_info.shared) return'               >> e${EMULATION_NAME}.c
1144 sed $sc ldscripts/${EMULATION_NAME}.xs                     >> e${EMULATION_NAME}.c
1145 fi
1146
1147 echo '  ; else return'                                     >> e${EMULATION_NAME}.c
1148 sed $sc ldscripts/${EMULATION_NAME}.x                      >> e${EMULATION_NAME}.c
1149 echo '; }'                                                 >> e${EMULATION_NAME}.c
1150
1151 else
1152 # Scripts read from the filesystem.
1153
1154 cat >>e${EMULATION_NAME}.c <<EOF
1155 {
1156   *isfile = 1;
1157
1158   if (link_info.relocateable == true && config.build_constructors == true)
1159     return "ldscripts/${EMULATION_NAME}.xu";
1160   else if (link_info.relocateable == true)
1161     return "ldscripts/${EMULATION_NAME}.xr";
1162   else if (!config.text_read_only)
1163     return "ldscripts/${EMULATION_NAME}.xbn";
1164   else if (!config.magic_demand_paged)
1165     return "ldscripts/${EMULATION_NAME}.xn";
1166   else if (link_info.shared)
1167     return "ldscripts/${EMULATION_NAME}.xs";
1168   else
1169     return "ldscripts/${EMULATION_NAME}.x";
1170 }
1171 EOF
1172
1173 fi
1174
1175 if test -n "$PARSE_AND_LIST_ARGS" ; then
1176 cat >>e${EMULATION_NAME}.c <<EOF
1177 static int  gld_${EMULATION_NAME}_parse_args PARAMS ((int, char **));
1178 static void gld_${EMULATION_NAME}_list_options PARAMS ((FILE * file));
1179
1180  $PARSE_AND_LIST_ARGS
1181 EOF
1182 else
1183
1184 cat >>e${EMULATION_NAME}.c <<EOF
1185 #define gld_${EMULATION_NAME}_parse_args   NULL
1186 #define gld_${EMULATION_NAME}_list_options NULL
1187 EOF
1188
1189 fi
1190
1191 cat >>e${EMULATION_NAME}.c <<EOF
1192
1193 struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
1194 {
1195   gld${EMULATION_NAME}_before_parse,
1196   syslib_default,
1197   hll_default,
1198   after_parse_default,
1199   gld${EMULATION_NAME}_after_open,
1200   after_allocation_default,
1201   set_output_arch_default,
1202   ldemul_default_target,
1203   gld${EMULATION_NAME}_before_allocation,
1204   gld${EMULATION_NAME}_get_script,
1205   "${EMULATION_NAME}",
1206   "${OUTPUT_FORMAT}",
1207   NULL,         /* finish */
1208   NULL,         /* create output section statements */
1209   gld${EMULATION_NAME}_open_dynamic_archive,
1210   gld${EMULATION_NAME}_place_orphan,
1211   NULL,         /* set_symbols */
1212   gld_${EMULATION_NAME}_parse_args,
1213   NULL,         /* unrecognized_file */
1214   gld_${EMULATION_NAME}_list_options,
1215   NULL,         /* recognized_file */
1216   NULL          /* find_potential_libraries */
1217 };
1218 EOF