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