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