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