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