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