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