1 # This shell script emits a C file. -*- C -*-
2 # It does some substitutions.
3 # This file is now misnamed, because it supports both 32 bit and 64 bit
5 test -z "${ELFSIZE}" && ELFSIZE=32
6 cat >e${EMULATION_NAME}.c <<EOF
7 /* This file is is generated by a shell script. DO NOT EDIT! */
9 /* ${ELFSIZE} bit ELF emulation code for ${EMULATION_NAME}
10 Copyright (C) 1991, 93, 94, 95, 96, 97, 98, 99, 2000
11 Free Software Foundation, Inc.
12 Written by Steve Chamberlain <sac@cygnus.com>
13 ELF support by Ian Lance Taylor <ian@cygnus.com>
15 This file is part of GLD, the Gnu Linker.
17 This program is free software; you can redistribute it and/or modify
18 it under the terms of the GNU General Public License as published by
19 the Free Software Foundation; either version 2 of the License, or
20 (at your option) any later version.
22 This program is distributed in the hope that it will be useful,
23 but WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 GNU General Public License for more details.
27 You should have received a copy of the GNU General Public License
28 along with this program; if not, write to the Free Software
29 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
31 #define TARGET_IS_${EMULATION_NAME}
49 static void gld${EMULATION_NAME}_before_parse PARAMS ((void));
50 static boolean gld${EMULATION_NAME}_open_dynamic_archive
51 PARAMS ((const char *, search_dirs_type *, lang_input_statement_type *));
52 static void gld${EMULATION_NAME}_after_open PARAMS ((void));
53 static void gld${EMULATION_NAME}_check_needed
54 PARAMS ((lang_input_statement_type *));
55 static void gld${EMULATION_NAME}_stat_needed
56 PARAMS ((lang_input_statement_type *));
57 static boolean gld${EMULATION_NAME}_search_needed
58 PARAMS ((const char *, const char *, int));
59 static boolean gld${EMULATION_NAME}_try_needed PARAMS ((const char *, int));
60 static void gld${EMULATION_NAME}_vercheck
61 PARAMS ((lang_input_statement_type *));
62 static void gld${EMULATION_NAME}_before_allocation PARAMS ((void));
63 static void gld${EMULATION_NAME}_find_statement_assignment
64 PARAMS ((lang_statement_union_type *));
65 static void gld${EMULATION_NAME}_find_exp_assignment PARAMS ((etree_type *));
66 static boolean gld${EMULATION_NAME}_place_orphan
67 PARAMS ((lang_input_statement_type *, asection *));
68 static lang_output_section_statement_type *output_rel_find PARAMS ((void));
69 static char *gld${EMULATION_NAME}_get_script PARAMS ((int *isfile));
72 gld${EMULATION_NAME}_before_parse()
74 ldfile_output_architecture = bfd_arch_`echo ${ARCH} | sed -e 's/:.*//'`;
75 config.dynamic_link = ${DYNAMIC_LINK-true};
76 config.has_shared = `if test -n "$GENERATE_SHLIB_SCRIPT" ; then echo true ; else echo false ; fi`;
79 /* Try to open a dynamic archive. This is where we know that ELF
80 dynamic libraries have an extension of .so (or .sl on oddball systems
84 gld${EMULATION_NAME}_open_dynamic_archive (arch, search, entry)
86 search_dirs_type *search;
87 lang_input_statement_type *entry;
92 if (! entry->is_archive)
95 filename = entry->filename;
97 /* This allocates a few bytes too many when EXTRA_SHLIB_EXTENSION
98 is defined, but it does not seem worth the headache to optimize
99 away those two bytes of space. */
100 string = (char *) xmalloc (strlen (search->name)
103 #ifdef EXTRA_SHLIB_EXTENSION
104 + strlen (EXTRA_SHLIB_EXTENSION)
108 sprintf (string, "%s/lib%s%s.so", search->name, filename, arch);
110 #ifdef EXTRA_SHLIB_EXTENSION
111 /* Try the .so extension first. If that fails build a new filename
112 using EXTRA_SHLIB_EXTENSION. */
113 if (! ldfile_try_open_bfd (string, entry))
114 sprintf (string, "%s/lib%s%s%s", search->name,
115 filename, arch, EXTRA_SHLIB_EXTENSION);
118 if (! ldfile_try_open_bfd (string, entry))
124 entry->filename = string;
126 /* We have found a dynamic object to include in the link. The ELF
127 backend linker will create a DT_NEEDED entry in the .dynamic
128 section naming this file. If this file includes a DT_SONAME
129 entry, it will be used. Otherwise, the ELF linker will just use
130 the name of the file. For an archive found by searching, like
131 this one, the DT_NEEDED entry should consist of just the name of
132 the file, without the path information used to find it. Note
133 that we only need to do this if we have a dynamic object; an
134 archive will never be referenced by a DT_NEEDED entry.
136 FIXME: This approach--using bfd_elf_set_dt_needed_name--is not
137 very pretty. I haven't been able to think of anything that is
139 if (bfd_check_format (entry->the_bfd, bfd_object)
140 && (entry->the_bfd->flags & DYNAMIC) != 0)
144 ASSERT (entry->is_archive && entry->search_dirs_flag);
146 /* Rather than duplicating the logic above. Just use the
147 filename we recorded earlier.
149 First strip off everything before the last '/'. */
150 filename = strrchr (entry->filename, '/');
153 needed_name = (char *) xmalloc (strlen (filename) + 1);
154 strcpy (needed_name, filename);
155 bfd_elf_set_dt_needed_name (entry->the_bfd, needed_name);
162 if [ "x${host}" = "x${target}" ] ; then
163 case " ${EMULATION_LIBPATH} " in
164 *" ${EMULATION_NAME} "*)
165 cat >>e${EMULATION_NAME}.c <<EOF
167 /* For a native linker, check the file /etc/ld.so.conf for directories
168 in which we may find shared libraries. /etc/ld.so.conf is really
169 only meaningful on Linux, but we check it on other systems anyhow. */
171 static boolean gld${EMULATION_NAME}_check_ld_so_conf
172 PARAMS ((const char *, int));
175 gld${EMULATION_NAME}_check_ld_so_conf (name, force)
179 static boolean initialized;
180 static char *ld_so_conf;
186 f = fopen ("/etc/ld.so.conf", FOPEN_RT);
195 b = (char *) xmalloc (alloc);
197 while ((c = getc (f)) != EOF)
199 if (len + 1 >= alloc)
202 b = (char *) xrealloc (b, alloc);
215 if (len > 0 && b[len - 1] != ':')
223 if (len > 0 && b[len - 1] == ':')
242 if (ld_so_conf == NULL)
245 return gld${EMULATION_NAME}_search_needed (ld_so_conf, name, force);
252 cat >>e${EMULATION_NAME}.c <<EOF
254 /* These variables are required to pass information back and forth
255 between after_open and check_needed and stat_needed and vercheck. */
257 static struct bfd_link_needed_list *global_needed;
258 static struct stat global_stat;
259 static boolean global_found;
260 static struct bfd_link_needed_list *global_vercheck_needed;
261 static boolean global_vercheck_failed;
263 /* This is called after all the input files have been opened. */
266 gld${EMULATION_NAME}_after_open ()
268 struct bfd_link_needed_list *needed, *l;
270 /* We only need to worry about this when doing a final link. */
271 if (link_info.relocateable || link_info.shared)
274 /* Get the list of files which appear in DT_NEEDED entries in
275 dynamic objects included in the link (often there will be none).
276 For each such file, we want to track down the corresponding
277 library, and include the symbol table in the link. This is what
278 the runtime dynamic linker will do. Tracking the files down here
279 permits one dynamic object to include another without requiring
280 special action by the person doing the link. Note that the
281 needed list can actually grow while we are stepping through this
283 needed = bfd_elf_get_needed_list (output_bfd, &link_info);
284 for (l = needed; l != NULL; l = l->next)
286 struct bfd_link_needed_list *ll;
289 /* If we've already seen this file, skip it. */
290 for (ll = needed; ll != l; ll = ll->next)
291 if (strcmp (ll->name, l->name) == 0)
296 /* See if this file was included in the link explicitly. */
298 global_found = false;
299 lang_for_each_input_file (gld${EMULATION_NAME}_check_needed);
303 /* We need to find this file and include the symbol table. We
304 want to search for the file in the same way that the dynamic
305 linker will search. That means that we want to use
306 rpath_link, rpath, then the environment variable
307 LD_LIBRARY_PATH (native only), then the linker script
308 LIB_SEARCH_DIRS. We do not search using the -L arguments.
310 We search twice. The first time, we skip objects which may
311 introduce version mismatches. The second time, we force
312 their use. See gld${EMULATION_NAME}_vercheck comment. */
313 for (force = 0; force < 2; force++)
315 const char *lib_path;
317 search_dirs_type *search;
319 if (gld${EMULATION_NAME}_search_needed (command_line.rpath_link,
322 if (gld${EMULATION_NAME}_search_needed (command_line.rpath,
325 if (command_line.rpath_link == NULL
326 && command_line.rpath == NULL)
328 lib_path = (const char *) getenv ("LD_RUN_PATH");
329 if (gld${EMULATION_NAME}_search_needed (lib_path, l->name,
334 if [ "x${host}" = "x${target}" ] ; then
335 case " ${EMULATION_LIBPATH} " in
336 *" ${EMULATION_NAME} "*)
337 cat >>e${EMULATION_NAME}.c <<EOF
338 lib_path = (const char *) getenv ("LD_LIBRARY_PATH");
339 if (gld${EMULATION_NAME}_search_needed (lib_path, l->name, force))
345 cat >>e${EMULATION_NAME}.c <<EOF
346 len = strlen (l->name);
347 for (search = search_head; search != NULL; search = search->next)
353 filename = (char *) xmalloc (strlen (search->name) + len + 2);
354 sprintf (filename, "%s/%s", search->name, l->name);
355 if (gld${EMULATION_NAME}_try_needed (filename, force))
362 if [ "x${host}" = "x${target}" ] ; then
363 case " ${EMULATION_LIBPATH} " in
364 *" ${EMULATION_NAME} "*)
365 cat >>e${EMULATION_NAME}.c <<EOF
366 if (gld${EMULATION_NAME}_check_ld_so_conf (l->name, force))
372 cat >>e${EMULATION_NAME}.c <<EOF
378 einfo ("%P: warning: %s, needed by %B, not found (try using --rpath)\n",
383 /* Search for a needed file in a path. */
386 gld${EMULATION_NAME}_search_needed (path, name, force)
394 if (path == NULL || *path == '\0')
399 char *filename, *sset;
401 s = strchr (path, ':');
403 s = path + strlen (path);
405 filename = (char *) xmalloc (s - path + len + 2);
410 memcpy (filename, path, s - path);
411 filename[s - path] = '/';
412 sset = filename + (s - path) + 1;
416 if (gld${EMULATION_NAME}_try_needed (filename, force))
429 /* This function is called for each possible name for a dynamic object
430 named by a DT_NEEDED entry. The FORCE parameter indicates whether
431 to skip the check for a conflicting version. */
434 gld${EMULATION_NAME}_try_needed (name, force)
440 abfd = bfd_openr (name, bfd_get_target (output_bfd));
443 if (! bfd_check_format (abfd, bfd_object))
445 (void) bfd_close (abfd);
448 if ((bfd_get_file_flags (abfd) & DYNAMIC) == 0)
450 (void) bfd_close (abfd);
454 /* Check whether this object would include any conflicting library
455 versions. If FORCE is set, then we skip this check; we use this
456 the second time around, if we couldn't find any compatible
457 instance of the shared library. */
461 struct bfd_link_needed_list *needed;
463 if (! bfd_elf_get_bfd_needed_list (abfd, &needed))
464 einfo ("%F%P:%B: bfd_elf_get_bfd_needed_list failed: %E\n", abfd);
468 global_vercheck_needed = needed;
469 global_vercheck_failed = false;
470 lang_for_each_input_file (gld${EMULATION_NAME}_vercheck);
471 if (global_vercheck_failed)
473 (void) bfd_close (abfd);
474 /* Return false to force the caller to move on to try
475 another file on the search path. */
479 /* But wait! It gets much worse. On Linux, if a shared
480 library does not use libc at all, we are supposed to skip
481 it the first time around in case we encounter a shared
482 library later on with the same name which does use the
483 version of libc that we want. This is much too horrible
484 to use on any system other than Linux. */
489 cat >>e${EMULATION_NAME}.c <<EOF
491 struct bfd_link_needed_list *l;
493 for (l = needed; l != NULL; l = l->next)
494 if (strncmp (l->name, "libc.so", 7) == 0)
498 (void) bfd_close (abfd);
506 cat >>e${EMULATION_NAME}.c <<EOF
510 /* We've found a dynamic object matching the DT_NEEDED entry. */
512 /* We have already checked that there is no other input file of the
513 same name. We must now check again that we are not including the
514 same file twice. We need to do this because on many systems
515 libc.so is a symlink to, e.g., libc.so.1. The SONAME entry will
516 reference libc.so.1. If we have already included libc.so, we
517 don't want to include libc.so.1 if they are the same file, and we
518 can only check that using stat. */
520 if (bfd_stat (abfd, &global_stat) != 0)
521 einfo ("%F%P:%B: bfd_stat failed: %E\n", abfd);
522 global_found = false;
523 lang_for_each_input_file (gld${EMULATION_NAME}_stat_needed);
526 /* Return true to indicate that we found the file, even though
527 we aren't going to do anything with it. */
531 /* Tell the ELF backend that don't want the output file to have a
532 DT_NEEDED entry for this file. */
533 bfd_elf_set_dt_needed_name (abfd, "");
535 /* First strip off everything before the last '/'. */
536 name = strrchr (abfd->filename, '/');
540 name = abfd->filename;
542 /* Tell the ELF backend that the output file needs a DT_NEEDED
543 entry for this file if it is used to resolve the reference in
545 bfd_elf_set_dt_needed_soname (abfd, name);
547 /* Add this file into the symbol table. */
548 if (! bfd_link_add_symbols (abfd, &link_info))
549 einfo ("%F%B: could not read symbols: %E\n", abfd);
554 /* See if an input file matches a DT_NEEDED entry by name. */
557 gld${EMULATION_NAME}_check_needed (s)
558 lang_input_statement_type *s;
563 if (s->filename != NULL
564 && strcmp (s->filename, global_needed->name) == 0)
570 if (s->the_bfd != NULL)
574 soname = bfd_elf_get_dt_soname (s->the_bfd);
576 && strcmp (soname, global_needed->name) == 0)
583 if (s->search_dirs_flag
584 && s->filename != NULL
585 && strchr (global_needed->name, '/') == NULL)
589 f = strrchr (s->filename, '/');
591 && strcmp (f + 1, global_needed->name) == 0)
599 /* See if an input file matches a DT_NEEDED entry by running stat on
603 gld${EMULATION_NAME}_stat_needed (s)
604 lang_input_statement_type *s;
613 if (s->the_bfd == NULL)
616 if (bfd_stat (s->the_bfd, &st) != 0)
618 einfo ("%P:%B: bfd_stat failed: %E\n", s->the_bfd);
622 if (st.st_dev == global_stat.st_dev
623 && st.st_ino == global_stat.st_ino)
629 /* We issue a warning if it looks like we are including two
630 different versions of the same shared library. For example,
631 there may be a problem if -lc picks up libc.so.6 but some other
632 shared library has a DT_NEEDED entry of libc.so.5. This is a
633 hueristic test, and it will only work if the name looks like
634 NAME.so.VERSION. FIXME: Depending on file names is error-prone.
635 If we really want to issue warnings about mixing version numbers
636 of shared libraries, we need to find a better way. */
638 if (strchr (global_needed->name, '/') != NULL)
640 suffix = strstr (global_needed->name, ".so.");
643 suffix += sizeof ".so." - 1;
645 soname = bfd_elf_get_dt_soname (s->the_bfd);
647 soname = s->filename;
649 f = strrchr (soname, '/');
655 if (strncmp (f, global_needed->name, suffix - global_needed->name) == 0)
656 einfo ("%P: warning: %s, needed by %B, may conflict with %s\n",
657 global_needed->name, global_needed->by, f);
660 /* On Linux, it's possible to have different versions of the same
661 shared library linked against different versions of libc. The
662 dynamic linker somehow tags which libc version to use in
663 /etc/ld.so.cache, and, based on the libc that it sees in the
664 executable, chooses which version of the shared library to use.
666 We try to do a similar check here by checking whether this shared
667 library needs any other shared libraries which may conflict with
668 libraries we have already included in the link. If it does, we
669 skip it, and try to find another shared library farther on down the
672 This is called via lang_for_each_input_file.
673 GLOBAL_VERCHECK_NEEDED is the list of objects needed by the object
674 which we ar checking. This sets GLOBAL_VERCHECK_FAILED if we find
675 a conflicting version. */
678 gld${EMULATION_NAME}_vercheck (s)
679 lang_input_statement_type *s;
681 const char *soname, *f;
682 struct bfd_link_needed_list *l;
684 if (global_vercheck_failed)
686 if (s->the_bfd == NULL
687 || (bfd_get_file_flags (s->the_bfd) & DYNAMIC) == 0)
690 soname = bfd_elf_get_dt_soname (s->the_bfd);
692 soname = bfd_get_filename (s->the_bfd);
694 f = strrchr (soname, '/');
700 for (l = global_vercheck_needed; l != NULL; l = l->next)
704 if (strcmp (f, l->name) == 0)
706 /* Probably can't happen, but it's an easy check. */
710 if (strchr (l->name, '/') != NULL)
713 suffix = strstr (l->name, ".so.");
717 suffix += sizeof ".so." - 1;
719 if (strncmp (f, l->name, suffix - l->name) == 0)
721 /* Here we know that S is a dynamic object FOO.SO.VER1, and
722 the object we are considering needs a dynamic object
723 FOO.SO.VER2, and VER1 and VER2 are different. This
724 appears to be a version mismatch, so we tell the caller
725 to try a different version of this library. */
726 global_vercheck_failed = true;
732 /* This is called after the sections have been attached to output
733 sections, but before any sizes or addresses have been set. */
736 gld${EMULATION_NAME}_before_allocation ()
741 /* If we are going to make any variable assignments, we need to let
742 the ELF backend know about them in case the variables are
743 referred to by dynamic objects. */
744 lang_for_each_statement (gld${EMULATION_NAME}_find_statement_assignment);
746 /* Let the ELF backend work out the sizes of any sections required
747 by dynamic linking. */
748 rpath = command_line.rpath;
750 rpath = (const char *) getenv ("LD_RUN_PATH");
751 if (! (bfd_elf${ELFSIZE}_size_dynamic_sections
752 (output_bfd, command_line.soname, rpath,
753 command_line.export_dynamic, command_line.filter_shlib,
754 (const char * const *) command_line.auxiliary_filters,
755 &link_info, &sinterp, lang_elf_version_info)))
756 einfo ("%P%F: failed to set dynamic section sizes: %E\n");
758 /* Let the user override the dynamic linker we are using. */
759 if (command_line.interpreter != NULL
762 sinterp->contents = (bfd_byte *) command_line.interpreter;
763 sinterp->_raw_size = strlen (command_line.interpreter) + 1;
766 /* Look for any sections named .gnu.warning. As a GNU extensions,
767 we treat such sections as containing warning messages. We print
768 out the warning message, and then zero out the section size so
769 that it does not get copied into the output file. */
772 LANG_FOR_EACH_INPUT_STATEMENT (is)
779 if (is->just_syms_flag)
782 s = bfd_get_section_by_name (is->the_bfd, ".gnu.warning");
786 sz = bfd_section_size (is->the_bfd, s);
787 msg = xmalloc ((size_t) sz + 1);
788 if (! bfd_get_section_contents (is->the_bfd, s, msg, (file_ptr) 0, sz))
789 einfo ("%F%B: Can't read contents of section .gnu.warning: %E\n",
792 ret = link_info.callbacks->warning (&link_info, msg,
794 is->the_bfd, (asection *) NULL,
799 /* Clobber the section size, so that we don't waste copying the
800 warning into the output file. */
806 /* This is called by the before_allocation routine via
807 lang_for_each_statement. It locates any assignment statements, and
808 tells the ELF backend about them, in case they are assignments to
809 symbols which are referred to by dynamic objects. */
812 gld${EMULATION_NAME}_find_statement_assignment (s)
813 lang_statement_union_type *s;
815 if (s->header.type == lang_assignment_statement_enum)
816 gld${EMULATION_NAME}_find_exp_assignment (s->assignment_statement.exp);
819 /* Look through an expression for an assignment statement. */
822 gld${EMULATION_NAME}_find_exp_assignment (exp)
825 struct bfd_link_hash_entry *h;
827 switch (exp->type.node_class)
830 h = bfd_link_hash_lookup (link_info.hash, exp->assign.dst,
831 false, false, false);
835 /* We call record_link_assignment even if the symbol is defined.
836 This is because if it is defined by a dynamic object, we
837 actually want to use the value defined by the linker script,
838 not the value from the dynamic object (because we are setting
839 symbols like etext). If the symbol is defined by a regular
840 object, then, as it happens, calling record_link_assignment
845 if (strcmp (exp->assign.dst, ".") != 0)
847 if (! (bfd_elf${ELFSIZE}_record_link_assignment
848 (output_bfd, &link_info, exp->assign.dst,
849 exp->type.node_class == etree_provide ? true : false)))
850 einfo ("%P%F: failed to record assignment to %s: %E\n",
853 gld${EMULATION_NAME}_find_exp_assignment (exp->assign.src);
857 gld${EMULATION_NAME}_find_exp_assignment (exp->binary.lhs);
858 gld${EMULATION_NAME}_find_exp_assignment (exp->binary.rhs);
862 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.cond);
863 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.lhs);
864 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.rhs);
868 gld${EMULATION_NAME}_find_exp_assignment (exp->unary.child);
876 /* Place an orphan section. We use this to put random SHF_ALLOC
877 sections in the right segment. */
881 lang_output_section_statement_type *os;
883 lang_statement_union_type **stmt;
888 gld${EMULATION_NAME}_place_orphan (file, s)
889 lang_input_statement_type *file;
892 static struct orphan_save hold_text;
893 static struct orphan_save hold_rodata;
894 static struct orphan_save hold_data;
895 static struct orphan_save hold_bss;
896 static struct orphan_save hold_rel;
897 static struct orphan_save hold_interp;
898 struct orphan_save *place;
899 lang_statement_list_type *old;
900 lang_statement_list_type add;
902 const char *secname, *ps;
903 const char *outsecname;
904 lang_output_section_statement_type *os;
906 secname = bfd_get_section_name (s->owner, s);
908 /* Look through the script to see where to place this section. */
909 os = lang_output_section_find (secname);
912 && os->bfd_section != NULL
913 && ((s->flags ^ os->bfd_section->flags) & (SEC_LOAD | SEC_ALLOC)) == 0)
915 /* We have already placed a section with this name. */
916 wild_doit (&os->children, s, os, file);
920 if (hold_text.os == NULL)
921 hold_text.os = lang_output_section_find (".text");
923 /* If this is a final link, then always put .gnu.warning.SYMBOL
924 sections into the .text section to get them out of the way. */
925 if (! link_info.shared
926 && ! link_info.relocateable
927 && strncmp (secname, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0
928 && hold_text.os != NULL)
930 wild_doit (&hold_text.os->children, s, hold_text.os, file);
934 /* Decide which segment the section should go in based on the
935 section name and section flags. We put loadable .note sections
936 right after the .interp section, so that the PT_NOTE segment is
937 stored right after the program headers where the OS can read it
938 in the first page. */
939 #define HAVE_SECTION(hold, name) \
940 (hold.os != NULL || (hold.os = lang_output_section_find (name)) != NULL)
942 if (s->flags & SEC_EXCLUDE)
944 else if ((s->flags & SEC_ALLOC) == 0)
946 else if ((s->flags & SEC_LOAD) != 0
947 && strncmp (secname, ".note", 4) == 0
948 && HAVE_SECTION (hold_interp, ".interp"))
949 place = &hold_interp;
950 else if ((s->flags & SEC_HAS_CONTENTS) == 0
951 && HAVE_SECTION (hold_bss, ".bss"))
953 else if ((s->flags & SEC_READONLY) == 0
954 && HAVE_SECTION (hold_data, ".data"))
956 else if (strncmp (secname, ".rel", 4) == 0
957 && (hold_rel.os != NULL
958 || (hold_rel.os = output_rel_find ()) != NULL))
960 else if ((s->flags & SEC_CODE) == 0
961 && (s->flags & SEC_READONLY) != 0
962 && HAVE_SECTION (hold_rodata, ".rodata"))
963 place = &hold_rodata;
964 else if ((s->flags & SEC_READONLY) != 0
965 && hold_text.os != NULL)
972 /* Choose a unique name for the section. This will be needed if the
973 same section name appears in the input file with different
974 loadable or allocateable characteristics. */
975 outsecname = secname;
976 if (bfd_get_section_by_name (output_bfd, outsecname) != NULL)
982 len = strlen (outsecname);
983 newname = xmalloc (len + 5);
984 strcpy (newname, outsecname);
988 sprintf (newname + len, "%d", i);
991 while (bfd_get_section_by_name (output_bfd, newname) != NULL);
993 outsecname = newname;
998 /* Start building a list of statements for this section. */
1001 lang_list_init (stat_ptr);
1003 /* If the name of the section is representable in C, then create
1004 symbols to mark the start and the end of the section. */
1005 for (ps = outsecname; *ps != '\0'; ps++)
1006 if (! isalnum ((unsigned char) *ps) && *ps != '_')
1008 if (*ps == '\0' && config.build_constructors)
1011 etree_type *e_align;
1013 symname = (char *) xmalloc (ps - outsecname + sizeof "__start_");
1014 sprintf (symname, "__start_%s", outsecname);
1015 e_align = exp_unop (ALIGN_K,
1016 exp_intop ((bfd_vma) 1 << s->alignment_power));
1017 lang_add_assignment (exp_assop ('=', symname, e_align));
1021 if (link_info.relocateable || (s->flags & (SEC_LOAD | SEC_ALLOC)) == 0)
1022 address = exp_intop ((bfd_vma) 0);
1026 os = lang_enter_output_section_statement (outsecname, address, 0,
1028 (etree_type *) NULL,
1029 (etree_type *) NULL,
1030 (etree_type *) NULL);
1032 wild_doit (&os->children, s, os, file);
1034 lang_leave_output_section_statement
1035 ((bfd_vma) 0, "*default*",
1036 (struct lang_output_section_phdr_list *) NULL, "*default*");
1040 asection *snew, **pps;
1044 if (*ps == '\0' && config.build_constructors)
1048 symname = (char *) xmalloc (ps - outsecname + sizeof "__stop_");
1049 sprintf (symname, "__stop_%s", outsecname);
1050 lang_add_assignment (exp_assop ('=', symname,
1051 exp_nameop (NAME, ".")));
1055 snew = os->bfd_section;
1056 if (place->os->bfd_section != NULL || place->section != NULL)
1058 /* Shuffle the section to make the output file look neater. */
1059 if (place->section == NULL)
1062 /* Finding the end of the list is a little tricky. We
1063 make a wild stab at it by comparing section flags. */
1064 flagword first_flags = place->os->bfd_section->flags;
1065 for (pps = &place->os->bfd_section->next;
1066 *pps != NULL && (*pps)->flags == first_flags;
1067 pps = &(*pps)->next)
1069 place->section = pps;
1071 /* Put orphans after the first section on the list. */
1072 place->section = &place->os->bfd_section->next;
1076 /* Unlink the section. */
1077 for (pps = &output_bfd->sections; *pps != snew; pps = &(*pps)->next)
1081 /* Now tack it on to the "place->os" section list. */
1082 snew->next = *place->section;
1083 *place->section = snew;
1085 place->section = &snew->next; /* Save the end of this list. */
1087 if (place->stmt == NULL)
1089 /* Put the new statement list right at the head. */
1090 *add.tail = place->os->header.next;
1091 place->os->header.next = add.head;
1095 /* Put it after the last orphan statement we added. */
1096 *add.tail = *place->stmt;
1097 *place->stmt = add.head;
1099 place->stmt = add.tail; /* Save the end of this list. */
1105 /* A variant of lang_output_section_find. */
1106 static lang_output_section_statement_type *
1109 lang_statement_union_type *u;
1110 lang_output_section_statement_type *lookup;
1112 for (u = lang_output_section_statement.head;
1113 u != (lang_statement_union_type *) NULL;
1116 lookup = &u->output_section_statement;
1117 if (strncmp (".rel", lookup->name, 4) == 0
1118 && lookup->bfd_section != NULL
1119 && (lookup->bfd_section->flags & SEC_ALLOC) != 0)
1124 return (lang_output_section_statement_type *) NULL;
1128 gld${EMULATION_NAME}_get_script(isfile)
1132 if test -n "$COMPILE_IN"
1134 # Scripts compiled in.
1136 # sed commands to quote an ld script as a C string.
1137 sc="-f stringify.sed"
1139 cat >>e${EMULATION_NAME}.c <<EOF
1143 if (link_info.relocateable == true && config.build_constructors == true)
1146 sed $sc ldscripts/${EMULATION_NAME}.xu >> e${EMULATION_NAME}.c
1147 echo ' ; else if (link_info.relocateable == true) return' >> e${EMULATION_NAME}.c
1148 sed $sc ldscripts/${EMULATION_NAME}.xr >> e${EMULATION_NAME}.c
1149 echo ' ; else if (!config.text_read_only) return' >> e${EMULATION_NAME}.c
1150 sed $sc ldscripts/${EMULATION_NAME}.xbn >> e${EMULATION_NAME}.c
1151 echo ' ; else if (!config.magic_demand_paged) return' >> e${EMULATION_NAME}.c
1152 sed $sc ldscripts/${EMULATION_NAME}.xn >> e${EMULATION_NAME}.c
1154 if test -n "$GENERATE_SHLIB_SCRIPT" ; then
1155 echo ' ; else if (link_info.shared) return' >> e${EMULATION_NAME}.c
1156 sed $sc ldscripts/${EMULATION_NAME}.xs >> e${EMULATION_NAME}.c
1159 echo ' ; else return' >> e${EMULATION_NAME}.c
1160 sed $sc ldscripts/${EMULATION_NAME}.x >> e${EMULATION_NAME}.c
1161 echo '; }' >> e${EMULATION_NAME}.c
1164 # Scripts read from the filesystem.
1166 cat >>e${EMULATION_NAME}.c <<EOF
1170 if (link_info.relocateable == true && config.build_constructors == true)
1171 return "ldscripts/${EMULATION_NAME}.xu";
1172 else if (link_info.relocateable == true)
1173 return "ldscripts/${EMULATION_NAME}.xr";
1174 else if (!config.text_read_only)
1175 return "ldscripts/${EMULATION_NAME}.xbn";
1176 else if (!config.magic_demand_paged)
1177 return "ldscripts/${EMULATION_NAME}.xn";
1178 else if (link_info.shared)
1179 return "ldscripts/${EMULATION_NAME}.xs";
1181 return "ldscripts/${EMULATION_NAME}.x";
1187 if test -n "$PARSE_AND_LIST_ARGS" ; then
1188 cat >>e${EMULATION_NAME}.c <<EOF
1189 static int gld_${EMULATION_NAME}_parse_args PARAMS ((int, char **));
1190 static void gld_${EMULATION_NAME}_list_options PARAMS ((FILE * file));
1192 $PARSE_AND_LIST_ARGS
1196 cat >>e${EMULATION_NAME}.c <<EOF
1197 #define gld_${EMULATION_NAME}_parse_args NULL
1198 #define gld_${EMULATION_NAME}_list_options NULL
1203 cat >>e${EMULATION_NAME}.c <<EOF
1205 struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
1207 gld${EMULATION_NAME}_before_parse,
1210 after_parse_default,
1211 gld${EMULATION_NAME}_after_open,
1212 after_allocation_default,
1213 set_output_arch_default,
1214 ldemul_default_target,
1215 gld${EMULATION_NAME}_before_allocation,
1216 gld${EMULATION_NAME}_get_script,
1217 "${EMULATION_NAME}",
1220 NULL, /* create output section statements */
1221 gld${EMULATION_NAME}_open_dynamic_archive,
1222 gld${EMULATION_NAME}_place_orphan,
1223 NULL, /* set_symbols */
1224 gld_${EMULATION_NAME}_parse_args,
1225 NULL, /* unrecognized_file */
1226 gld_${EMULATION_NAME}_list_options,
1227 NULL, /* recognized_file */
1228 NULL /* find_potential_libraries */