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