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