Implement new command line option for new ABI: -p which prevents the ARM code
[platform/upstream/binutils.git] / ld / emultempl / armelf.em
1 # This shell script emits a C file. -*- C -*-
2 # It does some substitutions.
3 ELFSIZE=32
4 cat >e${EMULATION_NAME}.c <<EOF
5 /* This file is is generated by a shell script.  DO NOT EDIT! */
6
7 /* emulate the original gld for the given ${EMULATION_NAME}
8    Copyright (C) 1991, 93, 96, 97, 98, 1999 Free Software Foundation, Inc.
9    Written by Steve Chamberlain steve@cygnus.com
10
11 This file is part of GLD, the Gnu Linker.
12
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
17
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 GNU General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
26
27 #define TARGET_IS_${EMULATION_NAME}
28
29 #include "bfd.h"
30 #include "sysdep.h"
31 #include "bfdlink.h"
32 #include "getopt.h"
33
34 #include "ld.h"
35 #include "ldmain.h"
36 #include "ldemul.h"
37 #include "ldfile.h"
38 #include "ldmisc.h"
39
40 #include "ldexp.h"
41 #include "ldlang.h"
42 #include "ldgram.h"
43
44 static boolean gld${EMULATION_NAME}_open_dynamic_archive
45   PARAMS ((const char *, search_dirs_type *, lang_input_statement_type *));
46 static void gld${EMULATION_NAME}_after_open PARAMS ((void));
47 static void gld${EMULATION_NAME}_check_needed
48   PARAMS ((lang_input_statement_type *));
49 static void gld${EMULATION_NAME}_stat_needed
50   PARAMS ((lang_input_statement_type *));
51 static boolean gld${EMULATION_NAME}_search_needed
52   PARAMS ((const char *, const char *, int));
53 static boolean gld${EMULATION_NAME}_try_needed PARAMS ((const char *, int));
54 static void gld${EMULATION_NAME}_vercheck
55   PARAMS ((lang_input_statement_type *));
56 static void gld${EMULATION_NAME}_find_statement_assignment
57   PARAMS ((lang_statement_union_type *));
58 static void gld${EMULATION_NAME}_find_exp_assignment PARAMS ((etree_type *));
59 static boolean gld${EMULATION_NAME}_place_orphan
60   PARAMS ((lang_input_statement_type *, asection *));
61 static void gld${EMULATION_NAME}_place_section
62   PARAMS ((lang_statement_union_type *));
63 static void gld${EMULATION_NAME}_before_parse PARAMS ((void));
64 static void gld${EMULATION_NAME}_before_allocation PARAMS ((void));
65 static char *gld${EMULATION_NAME}_get_script PARAMS ((int *isfile));
66 static int  gld${EMULATION_NAME}_parse_args PARAMS((int, char **));
67 static void gld${EMULATION_NAME}_list_options PARAMS ((FILE *));
68
69 \f
70 static int no_pipeline_knowledge = 0;
71
72 static struct option longopts[] =
73 {
74   { "no-pipeline-knowledge", no_argument, NULL, 'p'},
75   { NULL, no_argument, NULL, 0 }
76 };
77
78 static void
79 gld${EMULATION_NAME}_list_options (file)
80      FILE * file;
81 {
82   fprintf (file, _("  -p --no-pipeline-knowledge  Stop the linker knowing about the pipeline length\n"));
83 }
84
85 static int
86 gld${EMULATION_NAME}_parse_args (argc, argv)
87      int     argc;
88      char ** argv;
89 {
90   int        longind;
91   int        optc;
92   int        prevoptind = optind;
93   int        prevopterr = opterr;
94   int        wanterror;
95   static int lastoptind = -1;
96
97   if (lastoptind != optind)
98     opterr = 0;
99   
100   wanterror  = opterr;
101   lastoptind = optind;
102
103   optc   = getopt_long_only (argc, argv, "-p", longopts, & longind);
104   opterr = prevopterr;
105
106   switch (optc)
107     {
108     default:
109       if (wanterror)
110         xexit (1);
111       optind =  prevoptind;
112       return 0;
113
114     case 'p':
115       no_pipeline_knowledge = 1;
116       break;
117     }
118   
119   return 1;
120 }
121 \f
122
123 static void
124 gld${EMULATION_NAME}_before_parse ()
125 {
126 #ifndef TARGET_                 /* I.e., if not generic.  */
127   ldfile_set_output_arch ("`echo ${ARCH}`");
128 #endif /* not TARGET_ */
129   config.dynamic_link = ${DYNAMIC_LINK-true};
130   config.has_shared = `if test -n "$GENERATE_SHLIB_SCRIPT" ; then echo true ; else echo false ; fi`;
131 }
132
133 /* Try to open a dynamic archive.  This is where we know that ELF
134    dynamic libraries have an extension of .so.  */
135
136 static boolean
137 gld${EMULATION_NAME}_open_dynamic_archive (arch, search, entry)
138      const char *arch;
139      search_dirs_type *search;
140      lang_input_statement_type *entry;
141 {
142   const char *filename;
143   char *string;
144
145   if (! entry->is_archive)
146     return false;
147
148   filename = entry->filename;
149
150   string = (char *) xmalloc (strlen (search->name)
151                              + strlen (filename)
152                              + strlen (arch)
153                              + sizeof "/lib.so");
154
155   sprintf (string, "%s/lib%s%s.so", search->name, filename, arch);
156
157   if (! ldfile_try_open_bfd (string, entry))
158     {
159       free (string);
160       return false;
161     }
162
163   entry->filename = string;
164
165   /* We have found a dynamic object to include in the link.  The ELF
166      backend linker will create a DT_NEEDED entry in the .dynamic
167      section naming this file.  If this file includes a DT_SONAME
168      entry, it will be used.  Otherwise, the ELF linker will just use
169      the name of the file.  For an archive found by searching, like
170      this one, the DT_NEEDED entry should consist of just the name of
171      the file, without the path information used to find it.  Note
172      that we only need to do this if we have a dynamic object; an
173      archive will never be referenced by a DT_NEEDED entry.
174
175      FIXME: This approach--using bfd_elf_set_dt_needed_name--is not
176      very pretty.  I haven't been able to think of anything that is
177      pretty, though.  */
178   if (bfd_check_format (entry->the_bfd, bfd_object)
179       && (entry->the_bfd->flags & DYNAMIC) != 0)
180     {
181       char *needed_name;
182
183       ASSERT (entry->is_archive && entry->search_dirs_flag);
184       needed_name = (char *) xmalloc (strlen (filename)
185                                       + strlen (arch)
186                                       + sizeof "lib.so");
187       sprintf (needed_name, "lib%s%s.so", filename, arch);
188       bfd_elf_set_dt_needed_name (entry->the_bfd, needed_name);
189     }
190
191   return true;
192 }
193
194 EOF
195 if [ "x${host}" = "x${target}" ] ; then
196   if [ "x${DEFAULT_EMULATION}" = "x${EMULATION_NAME}" ] ; then
197 cat >>e${EMULATION_NAME}.c <<EOF
198
199 /* For a native linker, check the file /etc/ld.so.conf for directories
200    in which we may find shared libraries.  /etc/ld.so.conf is really
201    only meaningful on Linux, but we check it on other systems anyhow.  */
202
203 static boolean gld${EMULATION_NAME}_check_ld_so_conf
204   PARAMS ((const char *, int));
205
206 static boolean
207 gld${EMULATION_NAME}_check_ld_so_conf (name, force)
208      const char *name;
209      int force;
210 {
211   static boolean initialized;
212   static char *ld_so_conf;
213
214   if (! initialized)
215     {
216       FILE *f;
217
218       f = fopen ("/etc/ld.so.conf", FOPEN_RT);
219       if (f != NULL)
220         {
221           char *b;
222           size_t len, alloc;
223           int c;
224
225           len = 0;
226           alloc = 100;
227           b = (char *) xmalloc (alloc);
228
229           while ((c = getc (f)) != EOF)
230             {
231               if (len + 1 >= alloc)
232                 {
233                   alloc *= 2;
234                   b = (char *) xrealloc (b, alloc);
235                 }
236               if (c != ':'
237                   && c != ' '
238                   && c != '\t'
239                   && c != '\n'
240                   && c != ',')
241                 {
242                   b[len] = c;
243                   ++len;
244                 }
245               else
246                 {
247                   if (len > 0 && b[len - 1] != ':')
248                     {
249                       b[len] = ':';
250                       ++len;
251                     }
252                 }
253             }
254
255           if (len > 0 && b[len - 1] == ':')
256             --len;
257
258           if (len > 0)
259             b[len] = '\0';
260           else
261             {
262               free (b);
263               b = NULL;
264             }
265
266           fclose (f);
267
268           ld_so_conf = b;
269         }
270
271       initialized = true;
272     }
273
274   if (ld_so_conf == NULL)
275     return false;
276
277   return gld${EMULATION_NAME}_search_needed (ld_so_conf, name, force);
278 }
279
280 EOF
281   fi
282 fi
283 cat >>e${EMULATION_NAME}.c <<EOF
284
285 /* These variables are required to pass information back and forth
286    between after_open and check_needed and stat_needed and vercheck.  */
287
288 static struct bfd_link_needed_list *global_needed;
289 static struct stat global_stat;
290 static boolean global_found;
291 static struct bfd_link_needed_list *global_vercheck_needed;
292 static boolean global_vercheck_failed;
293
294 static void
295 gld${EMULATION_NAME}_after_open ()
296 {
297   struct bfd_link_needed_list *needed, *l;
298
299   LANG_FOR_EACH_INPUT_STATEMENT (is)
300     {
301       /* The interworking bfd must be the last one to be processed */
302       if (!is->next)
303          bfd_elf32_arm_get_bfd_for_interworking (is->the_bfd, & link_info);
304     }
305
306   /* We only need to worry about this when doing a final link.  */
307   if (link_info.relocateable || link_info.shared)
308     return;
309
310   /* Get the list of files which appear in DT_NEEDED entries in
311      dynamic objects included in the link (often there will be none).
312      For each such file, we want to track down the corresponding
313      library, and include the symbol table in the link.  This is what
314      the runtime dynamic linker will do.  Tracking the files down here
315      permits one dynamic object to include another without requiring
316      special action by the person doing the link.  Note that the
317      needed list can actually grow while we are stepping through this
318      loop.  */
319   needed = bfd_elf_get_needed_list (output_bfd, &link_info);
320   for (l = needed; l != NULL; l = l->next)
321     {
322       struct bfd_link_needed_list *ll;
323       int force;
324
325       /* If we've already seen this file, skip it.  */
326       for (ll = needed; ll != l; ll = ll->next)
327         if (strcmp (ll->name, l->name) == 0)
328           break;
329       if (ll != l)
330         continue;
331
332       /* See if this file was included in the link explicitly.  */
333       global_needed = l;
334       global_found = false;
335       lang_for_each_input_file (gld${EMULATION_NAME}_check_needed);
336       if (global_found)
337         continue;
338
339       /* We need to find this file and include the symbol table.  We
340          want to search for the file in the same way that the dynamic
341          linker will search.  That means that we want to use
342          rpath_link, rpath, then the environment variable
343          LD_LIBRARY_PATH (native only), then the linker script
344          LIB_SEARCH_DIRS.  We do not search using the -L arguments.
345
346          We search twice.  The first time, we skip objects which may
347          introduce version mismatches.  The second time, we force
348          their use.  See gld${EMULATION_NAME}_vercheck comment.  */
349       for (force = 0; force < 2; force++)
350         {
351           const char *lib_path;
352           size_t len;
353           search_dirs_type *search;
354
355           if (gld${EMULATION_NAME}_search_needed (command_line.rpath_link,
356                                                   l->name, force))
357             break;
358           if (gld${EMULATION_NAME}_search_needed (command_line.rpath,
359                                                   l->name, force))
360             break;
361           if (command_line.rpath_link == NULL
362               && command_line.rpath == NULL)
363             {
364               lib_path = (const char *) getenv ("LD_RUN_PATH");
365               if (gld${EMULATION_NAME}_search_needed (lib_path, l->name,
366                                                       force))
367                 break;
368             }
369 EOF
370 if [ "x${host}" = "x${target}" ] ; then
371   if [ "x${DEFAULT_EMULATION}" = "x${EMULATION_NAME}" ] ; then
372 cat >>e${EMULATION_NAME}.c <<EOF
373           lib_path = (const char *) getenv ("LD_LIBRARY_PATH");
374           if (gld${EMULATION_NAME}_search_needed (lib_path, l->name, force))
375             break;
376 EOF
377   fi
378 fi
379 cat >>e${EMULATION_NAME}.c <<EOF
380           len = strlen (l->name);
381           for (search = search_head; search != NULL; search = search->next)
382             {
383               char *filename;
384
385               if (search->cmdline)
386                 continue;
387               filename = (char *) xmalloc (strlen (search->name) + len + 2);
388               sprintf (filename, "%s/%s", search->name, l->name);
389               if (gld${EMULATION_NAME}_try_needed (filename, force))
390                 break;
391               free (filename);
392             }
393           if (search != NULL)
394             break;
395 EOF
396 if [ "x${host}" = "x${target}" ] ; then
397   if [ "x${DEFAULT_EMULATION}" = "x${EMULATION_NAME}" ] ; then
398 cat >>e${EMULATION_NAME}.c <<EOF
399           if (gld${EMULATION_NAME}_check_ld_so_conf (l->name, force))
400             break;
401 EOF
402   fi
403 fi
404 cat >>e${EMULATION_NAME}.c <<EOF
405         }
406
407       if (force < 2)
408         continue;
409
410       einfo ("%P: warning: %s, needed by %B, not found (try using --rpath)\n",
411              l->name, l->by);
412     }
413 }
414
415 /* Search for a needed file in a path.  */
416
417 static boolean
418 gld${EMULATION_NAME}_search_needed (path, name, force)
419      const char *path;
420      const char *name;
421      int force;
422 {
423   const char *s;
424   size_t len;
425
426   if (path == NULL || *path == '\0')
427     return false;
428   len = strlen (name);
429   while (1)
430     {
431       char *filename, *sset;
432
433       s = strchr (path, ':');
434       if (s == NULL)
435         s = path + strlen (path);
436
437       filename = (char *) xmalloc (s - path + len + 2);
438       if (s == path)
439         sset = filename;
440       else
441         {
442           memcpy (filename, path, s - path);
443           filename[s - path] = '/';
444           sset = filename + (s - path) + 1;
445         }
446       strcpy (sset, name);
447
448       if (gld${EMULATION_NAME}_try_needed (filename, force))
449         return true;
450
451       free (filename);
452
453       if (*s == '\0')
454         break;
455       path = s + 1;
456     }
457
458   return false;   
459 }
460
461 /* This function is called for each possible name for a dynamic object
462    named by a DT_NEEDED entry.  The FORCE parameter indicates whether
463    to skip the check for a conflicting version.  */
464
465 static boolean
466 gld${EMULATION_NAME}_try_needed (name, force)
467      const char *name;
468      int force;
469 {
470   bfd *abfd;
471
472   abfd = bfd_openr (name, bfd_get_target (output_bfd));
473   if (abfd == NULL)
474     return false;
475   if (! bfd_check_format (abfd, bfd_object))
476     {
477       (void) bfd_close (abfd);
478       return false;
479     }
480   if ((bfd_get_file_flags (abfd) & DYNAMIC) == 0)
481     {
482       (void) bfd_close (abfd);
483       return false;
484     }
485
486   /* Check whether this object would include any conflicting library
487      versions.  If FORCE is set, then we skip this check; we use this
488      the second time around, if we couldn't find any compatible
489      instance of the shared library.  */
490
491   if (! force)
492     {
493       struct bfd_link_needed_list *needed;
494
495       if (! bfd_elf_get_bfd_needed_list (abfd, &needed))
496         einfo ("%F%P:%B: bfd_elf_get_bfd_needed_list failed: %E\n", abfd);
497
498       if (needed != NULL)
499         {
500           global_vercheck_needed = needed;
501           global_vercheck_failed = false;
502           lang_for_each_input_file (gld${EMULATION_NAME}_vercheck);
503           if (global_vercheck_failed)
504             {
505               (void) bfd_close (abfd);
506               /* Return false to force the caller to move on to try
507                  another file on the search path.  */
508               return false;
509             }
510
511           /* But wait!  It gets much worse.  On Linux, if a shared
512              library does not use libc at all, we are supposed to skip
513              it the first time around in case we encounter a shared
514              library later on with the same name which does use the
515              version of libc that we want.  This is much too horrible
516              to use on any system other than Linux.  */
517
518 EOF
519 case ${target} in
520   *-*-linux-gnu*)
521     cat >>e${EMULATION_NAME}.c <<EOF
522           {
523             struct bfd_link_needed_list *l;
524
525             for (l = needed; l != NULL; l = l->next)
526               if (strncmp (l->name, "libc.so", 7) == 0)
527                 break;
528             if (l == NULL)
529               {
530                 (void) bfd_close (abfd);
531                 return false;
532               }
533           }
534
535 EOF
536     ;;
537 esac
538 cat >>e${EMULATION_NAME}.c <<EOF
539         }
540     }
541
542   /* We've found a dynamic object matching the DT_NEEDED entry.  */
543
544   /* We have already checked that there is no other input file of the
545      same name.  We must now check again that we are not including the
546      same file twice.  We need to do this because on many systems
547      libc.so is a symlink to, e.g., libc.so.1.  The SONAME entry will
548      reference libc.so.1.  If we have already included libc.so, we
549      don't want to include libc.so.1 if they are the same file, and we
550      can only check that using stat.  */
551
552   if (bfd_stat (abfd, &global_stat) != 0)
553     einfo ("%F%P:%B: bfd_stat failed: %E\n", abfd);
554   global_found = false;
555   lang_for_each_input_file (gld${EMULATION_NAME}_stat_needed);
556   if (global_found)
557     {
558       /* Return true to indicate that we found the file, even though
559          we aren't going to do anything with it.  */
560       return true;
561     }
562
563   /* Tell the ELF backend that don't want the output file to have a
564      DT_NEEDED entry for this file.  */
565   bfd_elf_set_dt_needed_name (abfd, "");
566
567   /* Add this file into the symbol table.  */
568   if (! bfd_link_add_symbols (abfd, &link_info))
569     einfo ("%F%B: could not read symbols: %E\n", abfd);
570
571   return true;
572 }
573
574 /* See if an input file matches a DT_NEEDED entry by name.  */
575
576 static void
577 gld${EMULATION_NAME}_check_needed (s)
578      lang_input_statement_type *s;
579 {
580   if (global_found)
581     return;
582
583   if (s->filename != NULL
584       && strcmp (s->filename, global_needed->name) == 0)
585     {
586       global_found = true;
587       return;
588     }
589
590   if (s->the_bfd != NULL)
591     {
592       const char *soname;
593
594       soname = bfd_elf_get_dt_soname (s->the_bfd);
595       if (soname != NULL
596           && strcmp (soname, global_needed->name) == 0)
597         {
598           global_found = true;
599           return;
600         }
601     }
602           
603   if (s->search_dirs_flag
604       && s->filename != NULL
605       && strchr (global_needed->name, '/') == NULL)
606     {
607       const char *f;
608
609       f = strrchr (s->filename, '/');
610       if (f != NULL
611           && strcmp (f + 1, global_needed->name) == 0)
612         {
613           global_found = true;
614           return;
615         }
616     }
617 }
618
619 /* See if an input file matches a DT_NEEDED entry by running stat on
620    the file.  */
621
622 static void
623 gld${EMULATION_NAME}_stat_needed (s)
624      lang_input_statement_type *s;
625 {
626   struct stat st;
627   const char *suffix;
628   const char *soname;
629   const char *f;
630
631   if (global_found)
632     return;
633   if (s->the_bfd == NULL)
634     return;
635
636   if (bfd_stat (s->the_bfd, &st) != 0)
637     {
638       einfo ("%P:%B: bfd_stat failed: %E\n", s->the_bfd);
639       return;
640     }
641
642   if (st.st_dev == global_stat.st_dev
643       && st.st_ino == global_stat.st_ino)
644     {
645       global_found = true;
646       return;
647     }
648
649   /* We issue a warning if it looks like we are including two
650      different versions of the same shared library.  For example,
651      there may be a problem if -lc picks up libc.so.6 but some other
652      shared library has a DT_NEEDED entry of libc.so.5.  This is a
653      hueristic test, and it will only work if the name looks like
654      NAME.so.VERSION.  FIXME: Depending on file names is error-prone.
655      If we really want to issue warnings about mixing version numbers
656      of shared libraries, we need to find a better way.  */
657
658   if (strchr (global_needed->name, '/') != NULL)
659     return;
660   suffix = strstr (global_needed->name, ".so.");
661   if (suffix == NULL)
662     return;
663   suffix += sizeof ".so." - 1;
664
665   soname = bfd_elf_get_dt_soname (s->the_bfd);
666   if (soname == NULL)
667     soname = s->filename;
668
669   f = strrchr (soname, '/');
670   if (f != NULL)
671     ++f;
672   else
673     f = soname;
674
675   if (strncmp (f, global_needed->name, suffix - global_needed->name) == 0)
676     einfo ("%P: warning: %s, needed by %B, may conflict with %s\n",
677            global_needed->name, global_needed->by, f);
678 }
679
680 /* On Linux, it's possible to have different versions of the same
681    shared library linked against different versions of libc.  The
682    dynamic linker somehow tags which libc version to use in
683    /etc/ld.so.cache, and, based on the libc that it sees in the
684    executable, chooses which version of the shared library to use.
685
686    We try to do a similar check here by checking whether this shared
687    library needs any other shared libraries which may conflict with
688    libraries we have already included in the link.  If it does, we
689    skip it, and try to find another shared library farther on down the
690    link path.
691
692    This is called via lang_for_each_input_file.
693    GLOBAL_VERCHECK_NEEDED is the list of objects needed by the object
694    which we ar checking.  This sets GLOBAL_VERCHECK_FAILED if we find
695    a conflicting version.  */
696
697 static void
698 gld${EMULATION_NAME}_vercheck (s)
699      lang_input_statement_type *s;
700 {
701   const char *soname, *f;
702   struct bfd_link_needed_list *l;
703
704   if (global_vercheck_failed)
705     return;
706   if (s->the_bfd == NULL
707       || (bfd_get_file_flags (s->the_bfd) & DYNAMIC) == 0)
708     return;
709
710   soname = bfd_elf_get_dt_soname (s->the_bfd);
711   if (soname == NULL)
712     soname = bfd_get_filename (s->the_bfd);
713
714   f = strrchr (soname, '/');
715   if (f != NULL)
716     ++f;
717   else
718     f = soname;
719
720   for (l = global_vercheck_needed; l != NULL; l = l->next)
721     {
722       const char *suffix;
723
724       if (strcmp (f, l->name) == 0)
725         {
726           /* Probably can't happen, but it's an easy check.  */
727           continue;
728         }
729
730       if (strchr (l->name, '/') != NULL)
731         continue;
732
733       suffix = strstr (l->name, ".so.");
734       if (suffix == NULL)
735         continue;
736
737       suffix += sizeof ".so." - 1;
738
739       if (strncmp (f, l->name, suffix - l->name) == 0)
740         {
741           /* Here we know that S is a dynamic object FOO.SO.VER1, and
742              the object we are considering needs a dynamic object
743              FOO.SO.VER2, and VER1 and VER2 are different.  This
744              appears to be a version mismatch, so we tell the caller
745              to try a different version of this library.  */
746           global_vercheck_failed = true;
747           return;
748         }
749     }
750 }
751
752 /* Place an orphan section.  We use this to put random SHF_ALLOC
753    sections in the right segment.  */
754
755 static asection *hold_section;
756 static lang_output_section_statement_type *hold_use;
757 static lang_output_section_statement_type *hold_text;
758 static lang_output_section_statement_type *hold_rodata;
759 static lang_output_section_statement_type *hold_data;
760 static lang_output_section_statement_type *hold_bss;
761 static lang_output_section_statement_type *hold_rel;
762 static lang_output_section_statement_type *hold_interp;
763
764 /*ARGSUSED*/
765 static boolean
766 gld${EMULATION_NAME}_place_orphan (file, s)
767      lang_input_statement_type *file;
768      asection *s;
769 {
770   lang_output_section_statement_type *place;
771   asection *snew, **pps;
772   lang_statement_list_type *old;
773   lang_statement_list_type add;
774   etree_type *address;
775   const char *secname, *ps;
776   const char *outsecname;
777   lang_output_section_statement_type *os;
778
779   if ((s->flags & SEC_ALLOC) == 0)
780     return false;
781
782   /* Look through the script to see where to place this section.  */
783   hold_section = s;
784   hold_use = NULL;
785   lang_for_each_statement (gld${EMULATION_NAME}_place_section);
786
787   if (hold_use != NULL)
788     {
789       /* We have already placed a section with this name.  */
790       wild_doit (&hold_use->children, s, hold_use, file);
791       return true;
792     }
793
794   secname = bfd_get_section_name (s->owner, s);
795
796   /* If this is a final link, then always put .gnu.warning.SYMBOL
797      sections into the .text section to get them out of the way.  */
798   if (! link_info.shared
799       && ! link_info.relocateable
800       && strncmp (secname, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0
801       && hold_text != NULL)
802     {
803       wild_doit (&hold_text->children, s, hold_text, file);
804       return true;
805     }
806
807   /* Decide which segment the section should go in based on the
808      section name and section flags.  We put loadable .note sections
809      right after the .interp section, so that the PT_NOTE segment is
810      stored right after the program headers where the OS can read it
811      in the first page.  */
812   place = NULL;
813   if (s->flags & SEC_EXCLUDE)
814     return false;
815   else if ((s->flags & SEC_LOAD) != 0
816       && strncmp (secname, ".note", 4) == 0
817       && hold_interp != NULL)
818     place = hold_interp;
819   else if ((s->flags & SEC_HAS_CONTENTS) == 0
820            && hold_bss != NULL)
821     place = hold_bss;
822   else if ((s->flags & SEC_READONLY) == 0
823            && hold_data != NULL)
824     place = hold_data;
825   else if (strncmp (secname, ".rel", 4) == 0
826            && hold_rel != NULL)
827     place = hold_rel;
828   else if ((s->flags & SEC_CODE) == 0
829            && (s->flags & SEC_READONLY) != 0
830            && hold_rodata != NULL)
831     place = hold_rodata;
832   else if ((s->flags & SEC_READONLY) != 0
833            && hold_text != NULL)
834     place = hold_text;
835   if (place == NULL)
836     return false;
837
838   /* Choose a unique name for the section.  This will be needed if the
839      same section name appears in the input file with different
840      loadable or allocateable characteristics.  */
841   outsecname = secname;
842   if (bfd_get_section_by_name (output_bfd, outsecname) != NULL)
843     {
844       unsigned int len;
845       char *newname;
846       unsigned int i;
847
848       len = strlen (outsecname);
849       newname = xmalloc (len + 5);
850       strcpy (newname, outsecname);
851       i = 0;
852       do
853         {
854           sprintf (newname + len, "%d", i);
855           ++i;
856         }
857       while (bfd_get_section_by_name (output_bfd, newname) != NULL);
858
859       outsecname = newname;
860     }
861
862   /* Create the section in the output file, and put it in the right
863      place.  This shuffling is to make the output file look neater.  */
864   snew = bfd_make_section (output_bfd, outsecname);
865   if (snew == NULL)
866       einfo ("%P%F: output format %s cannot represent section called %s\n",
867              output_bfd->xvec->name, outsecname);
868   if (place->bfd_section != NULL)
869     {
870       for (pps = &output_bfd->sections; *pps != snew; pps = &(*pps)->next)
871         ;
872       *pps = snew->next;
873       snew->next = place->bfd_section->next;
874       place->bfd_section->next = snew;
875     }
876
877   /* Start building a list of statements for this section.  */
878   old = stat_ptr;
879   stat_ptr = &add;
880   lang_list_init (stat_ptr);
881
882   /* If the name of the section is representable in C, then create
883      symbols to mark the start and the end of the section.  */
884   for (ps = outsecname; *ps != '\0'; ps++)
885     if (! isalnum ((unsigned char) *ps) && *ps != '_')
886       break;
887   if (*ps == '\0' && config.build_constructors)
888     {
889       char *symname;
890
891       symname = (char *) xmalloc (ps - outsecname + sizeof "__start_");
892       sprintf (symname, "__start_%s", outsecname);
893       lang_add_assignment (exp_assop ('=', symname,
894                                       exp_unop (ALIGN_K,
895                                                 exp_intop ((bfd_vma) 1
896                                                            << s->alignment_power))));
897     }
898
899   if (! link_info.relocateable)
900     address = NULL;
901   else
902     address = exp_intop ((bfd_vma) 0);
903
904   lang_enter_output_section_statement (outsecname, address, 0,
905                                        (bfd_vma) 0,
906                                        (etree_type *) NULL,
907                                        (etree_type *) NULL,
908                                        (etree_type *) NULL);
909
910   os = lang_output_section_statement_lookup (outsecname);
911   wild_doit (&os->children, s, os, file);
912
913   lang_leave_output_section_statement
914     ((bfd_vma) 0, "*default*", (struct lang_output_section_phdr_list *) NULL);
915   stat_ptr = &add;
916
917   if (*ps == '\0' && config.build_constructors)
918     {
919       char *symname;
920
921       symname = (char *) xmalloc (ps - outsecname + sizeof "__stop_");
922       sprintf (symname, "__stop_%s", outsecname);
923       lang_add_assignment (exp_assop ('=', symname,
924                                       exp_nameop (NAME, ".")));
925     }
926
927   /* Now stick the new statement list right after PLACE.  */
928   *add.tail = place->header.next;
929   place->header.next = add.head;
930
931   stat_ptr = old;
932
933   return true;
934 }
935
936 static void
937 gld${EMULATION_NAME}_place_section (s)
938      lang_statement_union_type *s;
939 {
940   lang_output_section_statement_type *os;
941
942   if (s->header.type != lang_output_section_statement_enum)
943     return;
944
945   os = &s->output_section_statement;
946
947   if (strcmp (os->name, hold_section->name) == 0
948       && os->bfd_section != NULL
949       && ((hold_section->flags & (SEC_LOAD | SEC_ALLOC))
950           == (os->bfd_section->flags & (SEC_LOAD | SEC_ALLOC))))
951     hold_use = os;
952
953   if (strcmp (os->name, ".text") == 0)
954     hold_text = os;
955   else if (strcmp (os->name, ".rodata") == 0)
956     hold_rodata = os;
957   else if (strcmp (os->name, ".data") == 0)
958     hold_data = os;
959   else if (strcmp (os->name, ".bss") == 0)
960     hold_bss = os;
961   else if (hold_rel == NULL
962            && os->bfd_section != NULL
963            && (os->bfd_section->flags & SEC_ALLOC) != 0
964            && strncmp (os->name, ".rel", 4) == 0)
965     hold_rel = os;
966   else if (strcmp (os->name, ".interp") == 0)
967     hold_interp = os;
968 }
969
970 /* Look through an expression for an assignment statement.  */
971
972 static void
973 gld${EMULATION_NAME}_find_exp_assignment (exp)
974      etree_type *exp;
975 {
976   struct bfd_link_hash_entry *h;
977
978   switch (exp->type.node_class)
979     {
980     case etree_provide:
981       h = bfd_link_hash_lookup (link_info.hash, exp->assign.dst,
982                                 false, false, false);
983       if (h == NULL)
984         break;
985
986       /* We call record_link_assignment even if the symbol is defined.
987          This is because if it is defined by a dynamic object, we
988          actually want to use the value defined by the linker script,
989          not the value from the dynamic object (because we are setting
990          symbols like etext).  If the symbol is defined by a regular
991          object, then, as it happens, calling record_link_assignment
992          will do no harm.  */
993
994       /* Fall through.  */
995     case etree_assign:
996       if (strcmp (exp->assign.dst, ".") != 0)
997         {
998           if (! (bfd_elf${ELFSIZE}_record_link_assignment
999                  (output_bfd, &link_info, exp->assign.dst,
1000                   exp->type.node_class == etree_provide ? true : false)))
1001             einfo ("%P%F: failed to record assignment to %s: %E\n",
1002                    exp->assign.dst);
1003         }
1004       gld${EMULATION_NAME}_find_exp_assignment (exp->assign.src);
1005       break;
1006
1007     case etree_binary:
1008       gld${EMULATION_NAME}_find_exp_assignment (exp->binary.lhs);
1009       gld${EMULATION_NAME}_find_exp_assignment (exp->binary.rhs);
1010       break;
1011
1012     case etree_trinary:
1013       gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.cond);
1014       gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.lhs);
1015       gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.rhs);
1016       break;
1017
1018     case etree_unary:
1019       gld${EMULATION_NAME}_find_exp_assignment (exp->unary.child);
1020       break;
1021
1022     default:
1023       break;
1024     }
1025 }
1026
1027 /* This is called by the before_allocation routine via
1028    lang_for_each_statement.  It locates any assignment statements, and
1029    tells the ELF backend about them, in case they are assignments to
1030    symbols which are referred to by dynamic objects.  */
1031
1032 static void
1033 gld${EMULATION_NAME}_find_statement_assignment (s)
1034      lang_statement_union_type *s;
1035 {
1036   if (s->header.type == lang_assignment_statement_enum)
1037     gld${EMULATION_NAME}_find_exp_assignment (s->assignment_statement.exp);
1038 }
1039
1040 /* This is called after the sections have been attached to output
1041    sections, but before any sizes or addresses have been set.  */
1042
1043 static void
1044 gld${EMULATION_NAME}_before_allocation ()
1045 {
1046   const char *rpath;
1047   asection *sinterp;
1048
1049   /* If we are going to make any variable assignments, we need to let
1050      the ELF backend know about them in case the variables are
1051      referred to by dynamic objects.  */
1052   lang_for_each_statement (gld${EMULATION_NAME}_find_statement_assignment);
1053
1054   /* Let the ELF backend work out the sizes of any sections required
1055      by dynamic linking.  */
1056   rpath = command_line.rpath;
1057   if (rpath == NULL)
1058     rpath = (const char *) getenv ("LD_RUN_PATH");
1059   if (! (bfd_elf${ELFSIZE}_size_dynamic_sections
1060          (output_bfd, command_line.soname, rpath,
1061           command_line.export_dynamic, command_line.filter_shlib,
1062           (const char * const *) command_line.auxiliary_filters,
1063           &link_info, &sinterp, lang_elf_version_info)))
1064     einfo ("%P%F: failed to set dynamic section sizes: %E\n");
1065
1066   /* Let the user override the dynamic linker we are using.  */
1067   if (command_line.interpreter != NULL
1068       && sinterp != NULL)
1069     {
1070       sinterp->contents = (bfd_byte *) command_line.interpreter;
1071       sinterp->_raw_size = strlen (command_line.interpreter) + 1;
1072     }
1073
1074   /* Look for any sections named .gnu.warning.  As a GNU extensions,
1075      we treat such sections as containing warning messages.  We print
1076      out the warning message, and then zero out the section size so
1077      that it does not get copied into the output file.  */
1078
1079   {
1080     LANG_FOR_EACH_INPUT_STATEMENT (is)
1081       {
1082         asection *s;
1083         bfd_size_type sz;
1084         char *msg;
1085         boolean ret;
1086
1087         if (is->just_syms_flag)
1088           continue;
1089
1090         s = bfd_get_section_by_name (is->the_bfd, ".gnu.warning");
1091         if (s == NULL)
1092           continue;
1093
1094         sz = bfd_section_size (is->the_bfd, s);
1095         msg = xmalloc ((size_t) sz + 1);
1096         if (! bfd_get_section_contents (is->the_bfd, s, msg, (file_ptr) 0, sz))
1097           einfo ("%F%B: Can't read contents of section .gnu.warning: %E\n",
1098                  is->the_bfd);
1099         msg[sz] = '\0';
1100         ret = link_info.callbacks->warning (&link_info, msg,
1101                                             (const char *) NULL,
1102                                             is->the_bfd, (asection *) NULL,
1103                                             (bfd_vma) 0);
1104         ASSERT (ret);
1105         free (msg);
1106
1107         /* Clobber the section size, so that we don't waste copying the
1108            warning into the output file.  */
1109         s->_raw_size = 0;
1110       }
1111   }
1112
1113   /* we should be able to set the size of the interworking stub section */
1114
1115   /* Here we rummage through the found bfds to collect glue information */
1116   /* FIXME: should this be based on a command line option? krk@cygnus.com */
1117   {
1118     LANG_FOR_EACH_INPUT_STATEMENT (is)
1119       {
1120         if (!bfd_elf32_arm_process_before_allocation (is->the_bfd, & link_info,
1121                                                       no_pipeline_knowledge))
1122           {
1123             /* xgettext:c-format */
1124             einfo (_("Errors encountered processing file %s"), is->filename);
1125           }
1126       }
1127   }
1128
1129   /* We have seen it all. Allocate it, and carry on */
1130   bfd_elf32_arm_allocate_interworking_sections (& link_info);
1131 }
1132
1133 static char *
1134 gld${EMULATION_NAME}_get_script (isfile)
1135      int *isfile;
1136 EOF
1137
1138 if test -n "$COMPILE_IN"
1139 then
1140 # Scripts compiled in.
1141
1142 # sed commands to quote an ld script as a C string.
1143 sc="-f ${srcdir}/emultempl/stringify.sed"
1144
1145 cat >>e${EMULATION_NAME}.c <<EOF
1146 {                            
1147   *isfile = 0;
1148
1149   if (link_info.relocateable == true && config.build_constructors == true)
1150     return
1151 EOF
1152 sed $sc ldscripts/${EMULATION_NAME}.xu                     >> e${EMULATION_NAME}.c
1153 echo '  ; else if (link_info.relocateable == true) return' >> e${EMULATION_NAME}.c
1154 sed $sc ldscripts/${EMULATION_NAME}.xr                     >> e${EMULATION_NAME}.c
1155 echo '  ; else if (!config.text_read_only) return'         >> e${EMULATION_NAME}.c
1156 sed $sc ldscripts/${EMULATION_NAME}.xbn                    >> e${EMULATION_NAME}.c
1157 echo '  ; else if (!config.magic_demand_paged) return'     >> e${EMULATION_NAME}.c
1158 sed $sc ldscripts/${EMULATION_NAME}.xn                     >> 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
1162
1163 else
1164 # Scripts read from the filesystem.
1165
1166 cat >>e${EMULATION_NAME}.c <<EOF
1167 {                            
1168   *isfile = 1;
1169
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
1179     return "ldscripts/${EMULATION_NAME}.x";
1180 }
1181 EOF
1182
1183 fi
1184
1185 cat >>e${EMULATION_NAME}.c <<EOF
1186
1187 struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation = 
1188 {
1189   gld${EMULATION_NAME}_before_parse,
1190   syslib_default,
1191   hll_default,
1192   after_parse_default,
1193   gld${EMULATION_NAME}_after_open,
1194   after_allocation_default,
1195   set_output_arch_default,
1196   ldemul_default_target,
1197   gld${EMULATION_NAME}_before_allocation,
1198   gld${EMULATION_NAME}_get_script,
1199   "${EMULATION_NAME}",
1200   "${OUTPUT_FORMAT}",
1201   NULL, /* finish */
1202   NULL, /* create output section statements */
1203   gld${EMULATION_NAME}_open_dynamic_archive,
1204   gld${EMULATION_NAME}_place_orphan,
1205   NULL, /* set_symbols */
1206   gld${EMULATION_NAME}_parse_args,
1207   NULL, /* unrecognised file */
1208   gld${EMULATION_NAME}_list_options,
1209   NULL /* recognized file */
1210 };
1211 EOF