1 # This shell script emits a C file. -*- C -*-
2 # It does some substitutions.
4 cat >e${EMULATION_NAME}.c <<EOF
5 /* This file is is generated by a shell script. DO NOT EDIT! */
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
11 This file is part of GLD, the Gnu Linker.
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.
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.
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. */
27 #define TARGET_IS_${EMULATION_NAME}
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 void gld${EMULATION_NAME}_place_section
65 PARAMS ((lang_statement_union_type *));
66 static void gld${EMULATION_NAME}_before_parse PARAMS ((void));
67 static void gld${EMULATION_NAME}_before_allocation PARAMS ((void));
68 static char *gld${EMULATION_NAME}_get_script PARAMS ((int *isfile));
69 static int gld${EMULATION_NAME}_parse_args PARAMS((int, char **));
70 static void gld${EMULATION_NAME}_list_options PARAMS ((FILE *));
71 static void gld${EMULATION_NAME}_finish PARAMS ((void));
74 static int no_pipeline_knowledge = 0;
75 static char * thumb_entry_symbol = NULL;
77 #define OPTION_THUMB_ENTRY 301
79 static struct option longopts[] =
81 { "no-pipeline-knowledge", no_argument, NULL, 'p'},
82 { "thumb-entry", required_argument, NULL, OPTION_THUMB_ENTRY},
83 { NULL, no_argument, NULL, 0 }
87 gld${EMULATION_NAME}_list_options (file)
90 fprintf (file, _(" -p --no-pipeline-knowledge Stop the linker knowing about the pipeline length\n"));
91 fprintf (file, _(" --thumb-entry=<sym> Set the entry point to be Thumb symbol <sym>\n"));
95 gld${EMULATION_NAME}_parse_args (argc, argv)
101 int prevoptind = optind;
102 int prevopterr = opterr;
104 static int lastoptind = -1;
106 if (lastoptind != optind)
112 optc = getopt_long_only (argc, argv, "-p", longopts, & longind);
124 no_pipeline_knowledge = 1;
127 case OPTION_THUMB_ENTRY:
128 thumb_entry_symbol = optarg;
137 gld${EMULATION_NAME}_before_parse ()
139 #ifndef TARGET_ /* I.e., if not generic. */
140 ldfile_set_output_arch ("`echo ${ARCH}`");
141 #endif /* not TARGET_ */
142 config.dynamic_link = ${DYNAMIC_LINK-true};
143 config.has_shared = `if test -n "$GENERATE_SHLIB_SCRIPT" ; then echo true ; else echo false ; fi`;
146 /* Try to open a dynamic archive. This is where we know that ELF
147 dynamic libraries have an extension of .so. */
150 gld${EMULATION_NAME}_open_dynamic_archive (arch, search, entry)
152 search_dirs_type *search;
153 lang_input_statement_type *entry;
155 const char *filename;
158 if (! entry->is_archive)
161 filename = entry->filename;
163 string = (char *) xmalloc (strlen (search->name)
168 sprintf (string, "%s/lib%s%s.so", search->name, filename, arch);
170 if (! ldfile_try_open_bfd (string, entry))
176 entry->filename = string;
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.
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
191 if (bfd_check_format (entry->the_bfd, bfd_object)
192 && (entry->the_bfd->flags & DYNAMIC) != 0)
196 ASSERT (entry->is_archive && entry->search_dirs_flag);
197 needed_name = (char *) xmalloc (strlen (filename)
200 sprintf (needed_name, "lib%s%s.so", filename, arch);
201 bfd_elf_set_dt_needed_name (entry->the_bfd, needed_name);
208 if [ "x${host}" = "x${target}" ] ; then
209 case " ${EMULATION_LIBPATH} " in
210 *" ${EMULATION_NAME} "*)
211 cat >>e${EMULATION_NAME}.c <<EOF
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. */
217 static boolean gld${EMULATION_NAME}_check_ld_so_conf
218 PARAMS ((const char *, int));
221 gld${EMULATION_NAME}_check_ld_so_conf (name, force)
225 static boolean initialized;
226 static char *ld_so_conf;
232 f = fopen ("/etc/ld.so.conf", FOPEN_RT);
241 b = (char *) xmalloc (alloc);
243 while ((c = getc (f)) != EOF)
245 if (len + 1 >= alloc)
248 b = (char *) xrealloc (b, alloc);
261 if (len > 0 && b[len - 1] != ':')
269 if (len > 0 && b[len - 1] == ':')
288 if (ld_so_conf == NULL)
291 return gld${EMULATION_NAME}_search_needed (ld_so_conf, name, force);
298 cat >>e${EMULATION_NAME}.c <<EOF
300 /* These variables are required to pass information back and forth
301 between after_open and check_needed and stat_needed and vercheck. */
303 static struct bfd_link_needed_list *global_needed;
304 static struct stat global_stat;
305 static boolean global_found;
306 static struct bfd_link_needed_list *global_vercheck_needed;
307 static boolean global_vercheck_failed;
310 gld${EMULATION_NAME}_after_open ()
312 struct bfd_link_needed_list *needed, *l;
314 if (strstr (bfd_get_target (output_bfd), "arm") == NULL)
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");
325 LANG_FOR_EACH_INPUT_STATEMENT (is)
327 /* The interworking bfd must be the last one to be processed */
329 bfd_elf32_arm_get_bfd_for_interworking (is->the_bfd, & link_info);
333 /* We only need to worry about this when doing a final link. */
334 if (link_info.relocateable || link_info.shared)
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
346 needed = bfd_elf_get_needed_list (output_bfd, &link_info);
347 for (l = needed; l != NULL; l = l->next)
349 struct bfd_link_needed_list *ll;
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)
359 /* See if this file was included in the link explicitly. */
361 global_found = false;
362 lang_for_each_input_file (gld${EMULATION_NAME}_check_needed);
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.
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++)
378 const char *lib_path;
380 search_dirs_type *search;
382 if (gld${EMULATION_NAME}_search_needed (command_line.rpath_link,
385 if (gld${EMULATION_NAME}_search_needed (command_line.rpath,
388 if (command_line.rpath_link == NULL
389 && command_line.rpath == NULL)
391 lib_path = (const char *) getenv ("LD_RUN_PATH");
392 if (gld${EMULATION_NAME}_search_needed (lib_path, l->name,
397 if [ "x${host}" = "x${target}" ] ; then
398 case " ${EMULATION_LIBPATH} " in
399 *" ${EMULATION_NAME} "*)
400 cat >>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))
408 cat >>e${EMULATION_NAME}.c <<EOF
409 len = strlen (l->name);
410 for (search = search_head; search != NULL; search = search->next)
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))
425 if [ "x${host}" = "x${target}" ] ; then
426 case " ${EMULATION_LIBPATH} " in
427 *" ${EMULATION_NAME} "*)
428 cat >>e${EMULATION_NAME}.c <<EOF
429 if (gld${EMULATION_NAME}_check_ld_so_conf (l->name, force))
435 cat >>e${EMULATION_NAME}.c <<EOF
441 einfo ("%P: warning: %s, needed by %B, not found (try using --rpath)\n",
446 /* Search for a needed file in a path. */
449 gld${EMULATION_NAME}_search_needed (path, name, force)
457 if (path == NULL || *path == '\0')
462 char *filename, *sset;
464 s = strchr (path, ':');
466 s = path + strlen (path);
468 filename = (char *) xmalloc (s - path + len + 2);
473 memcpy (filename, path, s - path);
474 filename[s - path] = '/';
475 sset = filename + (s - path) + 1;
479 if (gld${EMULATION_NAME}_try_needed (filename, force))
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. */
497 gld${EMULATION_NAME}_try_needed (name, force)
503 abfd = bfd_openr (name, bfd_get_target (output_bfd));
506 if (! bfd_check_format (abfd, bfd_object))
508 (void) bfd_close (abfd);
511 if ((bfd_get_file_flags (abfd) & DYNAMIC) == 0)
513 (void) bfd_close (abfd);
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. */
524 struct bfd_link_needed_list *needed;
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);
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)
536 (void) bfd_close (abfd);
537 /* Return false to force the caller to move on to try
538 another file on the search path. */
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. */
552 cat >>e${EMULATION_NAME}.c <<EOF
554 struct bfd_link_needed_list *l;
556 for (l = needed; l != NULL; l = l->next)
557 if (strncmp (l->name, "libc.so", 7) == 0)
561 (void) bfd_close (abfd);
569 cat >>e${EMULATION_NAME}.c <<EOF
573 /* We've found a dynamic object matching the DT_NEEDED entry. */
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. */
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);
589 /* Return true to indicate that we found the file, even though
590 we aren't going to do anything with it. */
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, "");
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);
605 /* See if an input file matches a DT_NEEDED entry by name. */
608 gld${EMULATION_NAME}_check_needed (s)
609 lang_input_statement_type *s;
614 if (s->filename != NULL
615 && strcmp (s->filename, global_needed->name) == 0)
621 if (s->the_bfd != NULL)
625 soname = bfd_elf_get_dt_soname (s->the_bfd);
627 && strcmp (soname, global_needed->name) == 0)
634 if (s->search_dirs_flag
635 && s->filename != NULL
636 && strchr (global_needed->name, '/') == NULL)
640 f = strrchr (s->filename, '/');
642 && strcmp (f + 1, global_needed->name) == 0)
650 /* See if an input file matches a DT_NEEDED entry by running stat on
654 gld${EMULATION_NAME}_stat_needed (s)
655 lang_input_statement_type *s;
664 if (s->the_bfd == NULL)
667 if (bfd_stat (s->the_bfd, &st) != 0)
669 einfo ("%P:%B: bfd_stat failed: %E\n", s->the_bfd);
673 if (st.st_dev == global_stat.st_dev
674 && st.st_ino == global_stat.st_ino)
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. */
689 if (strchr (global_needed->name, '/') != NULL)
691 suffix = strstr (global_needed->name, ".so.");
694 suffix += sizeof ".so." - 1;
696 soname = bfd_elf_get_dt_soname (s->the_bfd);
698 soname = s->filename;
700 f = strrchr (soname, '/');
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);
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.
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
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. */
729 gld${EMULATION_NAME}_vercheck (s)
730 lang_input_statement_type *s;
732 const char *soname, *f;
733 struct bfd_link_needed_list *l;
735 if (global_vercheck_failed)
737 if (s->the_bfd == NULL
738 || (bfd_get_file_flags (s->the_bfd) & DYNAMIC) == 0)
741 soname = bfd_elf_get_dt_soname (s->the_bfd);
743 soname = bfd_get_filename (s->the_bfd);
745 f = strrchr (soname, '/');
751 for (l = global_vercheck_needed; l != NULL; l = l->next)
755 if (strcmp (f, l->name) == 0)
757 /* Probably can't happen, but it's an easy check. */
761 if (strchr (l->name, '/') != NULL)
764 suffix = strstr (l->name, ".so.");
768 suffix += sizeof ".so." - 1;
770 if (strncmp (f, l->name, suffix - l->name) == 0)
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;
783 /* Place an orphan section. We use this to put random SHF_ALLOC
784 sections in the right segment. */
786 static asection *hold_section;
787 static lang_output_section_statement_type *hold_use;
791 lang_output_section_statement_type *os;
793 lang_statement_union_type **stmt;
795 static struct orphan_save hold_text;
796 static struct orphan_save hold_rodata;
797 static struct orphan_save hold_data;
798 static struct orphan_save hold_bss;
799 static struct orphan_save hold_rel;
800 static struct orphan_save hold_interp;
804 gld${EMULATION_NAME}_place_orphan (file, s)
805 lang_input_statement_type *file;
808 struct orphan_save *place;
809 lang_statement_list_type *old;
810 lang_statement_list_type add;
812 const char *secname, *ps;
813 const char *outsecname;
814 lang_output_section_statement_type *os;
816 /* Look through the script to see where to place this section. */
819 lang_for_each_statement (gld${EMULATION_NAME}_place_section);
821 if (hold_use != NULL)
823 /* We have already placed a section with this name. */
824 wild_doit (&hold_use->children, s, hold_use, file);
828 secname = bfd_get_section_name (s->owner, s);
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
835 && hold_text.os != NULL)
837 wild_doit (&hold_text.os->children, s, hold_text.os, file);
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. */
846 if (s->flags & SEC_EXCLUDE)
848 else if ((s->flags & SEC_ALLOC) == 0)
850 else if ((s->flags & SEC_LOAD) != 0
851 && strncmp (secname, ".note", 4) == 0
852 && hold_interp.os != NULL)
853 place = &hold_interp;
854 else if ((s->flags & SEC_HAS_CONTENTS) == 0
855 && hold_bss.os != NULL)
857 else if ((s->flags & SEC_READONLY) == 0
858 && hold_data.os != NULL)
860 else if (strncmp (secname, ".rel", 4) == 0
861 && hold_rel.os != NULL)
863 else if ((s->flags & SEC_CODE) == 0
864 && (s->flags & SEC_READONLY) != 0
865 && hold_rodata.os != NULL)
866 place = &hold_rodata;
867 else if ((s->flags & SEC_READONLY) != 0
868 && hold_text.os != NULL)
873 /* Choose a unique name for the section. This will be needed if the
874 same section name appears in the input file with different
875 loadable or allocateable characteristics. */
876 outsecname = secname;
877 if (bfd_get_section_by_name (output_bfd, outsecname) != NULL)
883 len = strlen (outsecname);
884 newname = xmalloc (len + 5);
885 strcpy (newname, outsecname);
889 sprintf (newname + len, "%d", i);
892 while (bfd_get_section_by_name (output_bfd, newname) != NULL);
894 outsecname = newname;
899 /* Start building a list of statements for this section. */
902 lang_list_init (stat_ptr);
904 /* If the name of the section is representable in C, then create
905 symbols to mark the start and the end of the section. */
906 for (ps = outsecname; *ps != '\0'; ps++)
907 if (! isalnum ((unsigned char) *ps) && *ps != '_')
909 if (*ps == '\0' && config.build_constructors)
914 symname = (char *) xmalloc (ps - outsecname + sizeof "__start_");
915 sprintf (symname, "__start_%s", outsecname);
916 e_align = exp_unop (ALIGN_K,
917 exp_intop ((bfd_vma) 1 << s->alignment_power));
918 lang_add_assignment (exp_assop ('=', symname, e_align));
922 if (link_info.relocateable || (s->flags & (SEC_LOAD | SEC_ALLOC)) == 0)
923 address = exp_intop ((bfd_vma) 0);
927 lang_enter_output_section_statement (outsecname, address, 0,
931 (etree_type *) NULL);
933 os = lang_output_section_statement_lookup (outsecname);
934 wild_doit (&os->children, s, os, file);
936 lang_leave_output_section_statement
937 ((bfd_vma) 0, "*default*",
938 (struct lang_output_section_phdr_list *) NULL, "*default*");
942 asection *snew, **pps;
946 if (*ps == '\0' && config.build_constructors)
950 symname = (char *) xmalloc (ps - outsecname + sizeof "__stop_");
951 sprintf (symname, "__stop_%s", outsecname);
952 lang_add_assignment (exp_assop ('=', symname,
953 exp_nameop (NAME, ".")));
957 snew = os->bfd_section;
958 if (place->os->bfd_section != NULL || place->section != NULL)
960 /* Shuffle the section to make the output file look neater. */
961 if (place->section == NULL)
964 /* Finding the end of the list is a little tricky. We
965 make a wild stab at it by comparing section flags. */
966 flagword first_flags = place->os->bfd_section->flags;
967 for (pps = &place->os->bfd_section->next;
968 *pps != NULL && (*pps)->flags == first_flags;
971 place->section = pps;
973 /* Put orphans after the first section on the list. */
974 place->section = &place->os->bfd_section->next;
978 /* Unlink the section. */
979 for (pps = &output_bfd->sections; *pps != snew; pps = &(*pps)->next)
983 /* Now tack it on to the "place->os" section list. */
984 snew->next = *place->section;
985 *place->section = snew;
987 place->section = &snew->next; /* Save the end of this list. */
989 if (place->stmt == NULL)
991 /* Put the new statement list right at the head. */
992 *add.tail = place->os->header.next;
993 place->os->header.next = add.head;
997 /* Put it after the last orphan statement we added. */
998 *add.tail = *place->stmt;
999 *place->stmt = add.head;
1001 place->stmt = add.tail; /* Save the end of this list. */
1008 gld${EMULATION_NAME}_place_section (s)
1009 lang_statement_union_type *s;
1011 lang_output_section_statement_type *os;
1013 if (s->header.type != lang_output_section_statement_enum)
1016 os = &s->output_section_statement;
1018 if (strcmp (os->name, hold_section->name) == 0
1019 && os->bfd_section != NULL
1020 && ((hold_section->flags & (SEC_LOAD | SEC_ALLOC))
1021 == (os->bfd_section->flags & (SEC_LOAD | SEC_ALLOC))))
1024 if (strcmp (os->name, ".text") == 0)
1026 else if (strcmp (os->name, ".rodata") == 0)
1027 hold_rodata.os = os;
1028 else if (strcmp (os->name, ".data") == 0)
1030 else if (strcmp (os->name, ".bss") == 0)
1032 else if (hold_rel.os == NULL
1033 && os->bfd_section != NULL
1034 && (os->bfd_section->flags & SEC_ALLOC) != 0
1035 && strncmp (os->name, ".rel", 4) == 0)
1037 else if (strcmp (os->name, ".interp") == 0)
1038 hold_interp.os = os;
1041 /* Look through an expression for an assignment statement. */
1044 gld${EMULATION_NAME}_find_exp_assignment (exp)
1047 struct bfd_link_hash_entry *h;
1049 switch (exp->type.node_class)
1052 h = bfd_link_hash_lookup (link_info.hash, exp->assign.dst,
1053 false, false, false);
1057 /* We call record_link_assignment even if the symbol is defined.
1058 This is because if it is defined by a dynamic object, we
1059 actually want to use the value defined by the linker script,
1060 not the value from the dynamic object (because we are setting
1061 symbols like etext). If the symbol is defined by a regular
1062 object, then, as it happens, calling record_link_assignment
1067 if (strcmp (exp->assign.dst, ".") != 0)
1069 if (! (bfd_elf${ELFSIZE}_record_link_assignment
1070 (output_bfd, &link_info, exp->assign.dst,
1071 exp->type.node_class == etree_provide ? true : false)))
1072 einfo ("%P%F: failed to record assignment to %s: %E\n",
1075 gld${EMULATION_NAME}_find_exp_assignment (exp->assign.src);
1079 gld${EMULATION_NAME}_find_exp_assignment (exp->binary.lhs);
1080 gld${EMULATION_NAME}_find_exp_assignment (exp->binary.rhs);
1084 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.cond);
1085 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.lhs);
1086 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.rhs);
1090 gld${EMULATION_NAME}_find_exp_assignment (exp->unary.child);
1098 /* This is called by the before_allocation routine via
1099 lang_for_each_statement. It locates any assignment statements, and
1100 tells the ELF backend about them, in case they are assignments to
1101 symbols which are referred to by dynamic objects. */
1104 gld${EMULATION_NAME}_find_statement_assignment (s)
1105 lang_statement_union_type *s;
1107 if (s->header.type == lang_assignment_statement_enum)
1108 gld${EMULATION_NAME}_find_exp_assignment (s->assignment_statement.exp);
1111 /* This is called after the sections have been attached to output
1112 sections, but before any sizes or addresses have been set. */
1115 gld${EMULATION_NAME}_before_allocation ()
1120 /* If we are going to make any variable assignments, we need to let
1121 the ELF backend know about them in case the variables are
1122 referred to by dynamic objects. */
1123 lang_for_each_statement (gld${EMULATION_NAME}_find_statement_assignment);
1125 /* Let the ELF backend work out the sizes of any sections required
1126 by dynamic linking. */
1127 rpath = command_line.rpath;
1129 rpath = (const char *) getenv ("LD_RUN_PATH");
1130 if (! (bfd_elf${ELFSIZE}_size_dynamic_sections
1131 (output_bfd, command_line.soname, rpath,
1132 command_line.export_dynamic, command_line.filter_shlib,
1133 (const char * const *) command_line.auxiliary_filters,
1134 &link_info, &sinterp, lang_elf_version_info)))
1135 einfo ("%P%F: failed to set dynamic section sizes: %E\n");
1137 /* Let the user override the dynamic linker we are using. */
1138 if (command_line.interpreter != NULL
1141 sinterp->contents = (bfd_byte *) command_line.interpreter;
1142 sinterp->_raw_size = strlen (command_line.interpreter) + 1;
1145 /* Look for any sections named .gnu.warning. As a GNU extensions,
1146 we treat such sections as containing warning messages. We print
1147 out the warning message, and then zero out the section size so
1148 that it does not get copied into the output file. */
1151 LANG_FOR_EACH_INPUT_STATEMENT (is)
1158 if (is->just_syms_flag)
1161 s = bfd_get_section_by_name (is->the_bfd, ".gnu.warning");
1165 sz = bfd_section_size (is->the_bfd, s);
1166 msg = xmalloc ((size_t) sz + 1);
1167 if (! bfd_get_section_contents (is->the_bfd, s, msg, (file_ptr) 0, sz))
1168 einfo ("%F%B: Can't read contents of section .gnu.warning: %E\n",
1171 ret = link_info.callbacks->warning (&link_info, msg,
1172 (const char *) NULL,
1173 is->the_bfd, (asection *) NULL,
1178 /* Clobber the section size, so that we don't waste copying the
1179 warning into the output file. */
1184 /* we should be able to set the size of the interworking stub section */
1186 /* Here we rummage through the found bfds to collect glue information */
1187 /* FIXME: should this be based on a command line option? krk@cygnus.com */
1189 LANG_FOR_EACH_INPUT_STATEMENT (is)
1191 if (!bfd_elf32_arm_process_before_allocation (is->the_bfd, & link_info,
1192 no_pipeline_knowledge))
1194 /* xgettext:c-format */
1195 einfo (_("Errors encountered processing file %s"), is->filename);
1200 /* We have seen it all. Allocate it, and carry on */
1201 bfd_elf32_arm_allocate_interworking_sections (& link_info);
1205 gld${EMULATION_NAME}_finish PARAMS((void))
1207 struct bfd_link_hash_entry * h;
1209 if (thumb_entry_symbol == NULL)
1212 h = bfd_link_hash_lookup (link_info.hash, thumb_entry_symbol, false, false, true);
1214 if (h != (struct bfd_link_hash_entry *) NULL
1215 && (h->type == bfd_link_hash_defined
1216 || h->type == bfd_link_hash_defweak)
1217 && h->u.def.section->output_section != NULL)
1219 static char buffer[32];
1222 /* Special procesing is required for a Thumb entry symbol. The
1223 bottom bit of its address must be set. */
1224 val = (h->u.def.value
1225 + bfd_get_section_vma (output_bfd,
1226 h->u.def.section->output_section)
1227 + h->u.def.section->output_offset);
1231 /* Now convert this value into a string and store it in entry_symbol
1232 where the lang_finish() function will pick it up. */
1236 sprintf_vma (buffer + 2, val);
1238 if (entry_symbol != NULL && entry_from_cmdline)
1239 einfo (_("%P: warning: '--thumb-entry %s' is overriding '-e %s'\n"),
1240 thumb_entry_symbol, entry_symbol);
1241 entry_symbol = buffer;
1244 einfo (_("%P: warning: connot find thumb start symbol %s\n"), thumb_entry_symbol);
1248 gld${EMULATION_NAME}_get_script (isfile)
1252 if test -n "$COMPILE_IN"
1254 # Scripts compiled in.
1256 # sed commands to quote an ld script as a C string.
1257 sc="-f stringify.sed"
1259 cat >>e${EMULATION_NAME}.c <<EOF
1263 if (link_info.relocateable == true && config.build_constructors == true)
1266 sed $sc ldscripts/${EMULATION_NAME}.xu >> e${EMULATION_NAME}.c
1267 echo ' ; else if (link_info.relocateable == true) return' >> e${EMULATION_NAME}.c
1268 sed $sc ldscripts/${EMULATION_NAME}.xr >> e${EMULATION_NAME}.c
1269 echo ' ; else if (!config.text_read_only) return' >> e${EMULATION_NAME}.c
1270 sed $sc ldscripts/${EMULATION_NAME}.xbn >> e${EMULATION_NAME}.c
1271 echo ' ; else if (!config.magic_demand_paged) return' >> e${EMULATION_NAME}.c
1272 sed $sc ldscripts/${EMULATION_NAME}.xn >> e${EMULATION_NAME}.c
1273 if test -n "$GENERATE_SHLIB_SCRIPT" ; then
1274 echo ' ; else if (link_info.shared) return' >> e${EMULATION_NAME}.c
1275 sed $sc ldscripts/${EMULATION_NAME}.xs >> e${EMULATION_NAME}.c
1277 echo ' ; else return' >> e${EMULATION_NAME}.c
1278 sed $sc ldscripts/${EMULATION_NAME}.x >> e${EMULATION_NAME}.c
1279 echo '; }' >> e${EMULATION_NAME}.c
1282 # Scripts read from the filesystem.
1284 cat >>e${EMULATION_NAME}.c <<EOF
1288 if (link_info.relocateable == true && config.build_constructors == true)
1289 return "ldscripts/${EMULATION_NAME}.xu";
1290 else if (link_info.relocateable == true)
1291 return "ldscripts/${EMULATION_NAME}.xr";
1292 else if (!config.text_read_only)
1293 return "ldscripts/${EMULATION_NAME}.xbn";
1294 else if (!config.magic_demand_paged)
1295 return "ldscripts/${EMULATION_NAME}.xn";
1296 else if (link_info.shared)
1297 return "ldscripts/${EMULATION_NAME}.xs";
1299 return "ldscripts/${EMULATION_NAME}.x";
1305 cat >>e${EMULATION_NAME}.c <<EOF
1307 struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
1309 gld${EMULATION_NAME}_before_parse,
1312 after_parse_default,
1313 gld${EMULATION_NAME}_after_open,
1314 after_allocation_default,
1315 set_output_arch_default,
1316 ldemul_default_target,
1317 gld${EMULATION_NAME}_before_allocation,
1318 gld${EMULATION_NAME}_get_script,
1319 "${EMULATION_NAME}",
1321 gld${EMULATION_NAME}_finish,
1322 NULL, /* create output section statements */
1323 gld${EMULATION_NAME}_open_dynamic_archive,
1324 gld${EMULATION_NAME}_place_orphan,
1325 NULL, /* set symbols */
1326 gld${EMULATION_NAME}_parse_args,
1327 NULL, /* unrecognized file */
1328 gld${EMULATION_NAME}_list_options,
1329 NULL, /* recognized file */
1330 NULL /* find_potential_libraries */