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