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