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