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