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, 1999
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 /* Add this file into the symbol table. */
536 if (! bfd_link_add_symbols (abfd, &link_info))
537 einfo ("%F%B: could not read symbols: %E\n", abfd);
542 /* See if an input file matches a DT_NEEDED entry by name. */
545 gld${EMULATION_NAME}_check_needed (s)
546 lang_input_statement_type *s;
551 if (s->filename != NULL
552 && strcmp (s->filename, global_needed->name) == 0)
558 if (s->the_bfd != NULL)
562 soname = bfd_elf_get_dt_soname (s->the_bfd);
564 && strcmp (soname, global_needed->name) == 0)
571 if (s->search_dirs_flag
572 && s->filename != NULL
573 && strchr (global_needed->name, '/') == NULL)
577 f = strrchr (s->filename, '/');
579 && strcmp (f + 1, global_needed->name) == 0)
587 /* See if an input file matches a DT_NEEDED entry by running stat on
591 gld${EMULATION_NAME}_stat_needed (s)
592 lang_input_statement_type *s;
601 if (s->the_bfd == NULL)
604 if (bfd_stat (s->the_bfd, &st) != 0)
606 einfo ("%P:%B: bfd_stat failed: %E\n", s->the_bfd);
610 if (st.st_dev == global_stat.st_dev
611 && st.st_ino == global_stat.st_ino)
617 /* We issue a warning if it looks like we are including two
618 different versions of the same shared library. For example,
619 there may be a problem if -lc picks up libc.so.6 but some other
620 shared library has a DT_NEEDED entry of libc.so.5. This is a
621 hueristic test, and it will only work if the name looks like
622 NAME.so.VERSION. FIXME: Depending on file names is error-prone.
623 If we really want to issue warnings about mixing version numbers
624 of shared libraries, we need to find a better way. */
626 if (strchr (global_needed->name, '/') != NULL)
628 suffix = strstr (global_needed->name, ".so.");
631 suffix += sizeof ".so." - 1;
633 soname = bfd_elf_get_dt_soname (s->the_bfd);
635 soname = s->filename;
637 f = strrchr (soname, '/');
643 if (strncmp (f, global_needed->name, suffix - global_needed->name) == 0)
644 einfo ("%P: warning: %s, needed by %B, may conflict with %s\n",
645 global_needed->name, global_needed->by, f);
648 /* On Linux, it's possible to have different versions of the same
649 shared library linked against different versions of libc. The
650 dynamic linker somehow tags which libc version to use in
651 /etc/ld.so.cache, and, based on the libc that it sees in the
652 executable, chooses which version of the shared library to use.
654 We try to do a similar check here by checking whether this shared
655 library needs any other shared libraries which may conflict with
656 libraries we have already included in the link. If it does, we
657 skip it, and try to find another shared library farther on down the
660 This is called via lang_for_each_input_file.
661 GLOBAL_VERCHECK_NEEDED is the list of objects needed by the object
662 which we ar checking. This sets GLOBAL_VERCHECK_FAILED if we find
663 a conflicting version. */
666 gld${EMULATION_NAME}_vercheck (s)
667 lang_input_statement_type *s;
669 const char *soname, *f;
670 struct bfd_link_needed_list *l;
672 if (global_vercheck_failed)
674 if (s->the_bfd == NULL
675 || (bfd_get_file_flags (s->the_bfd) & DYNAMIC) == 0)
678 soname = bfd_elf_get_dt_soname (s->the_bfd);
680 soname = bfd_get_filename (s->the_bfd);
682 f = strrchr (soname, '/');
688 for (l = global_vercheck_needed; l != NULL; l = l->next)
692 if (strcmp (f, l->name) == 0)
694 /* Probably can't happen, but it's an easy check. */
698 if (strchr (l->name, '/') != NULL)
701 suffix = strstr (l->name, ".so.");
705 suffix += sizeof ".so." - 1;
707 if (strncmp (f, l->name, suffix - l->name) == 0)
709 /* Here we know that S is a dynamic object FOO.SO.VER1, and
710 the object we are considering needs a dynamic object
711 FOO.SO.VER2, and VER1 and VER2 are different. This
712 appears to be a version mismatch, so we tell the caller
713 to try a different version of this library. */
714 global_vercheck_failed = true;
720 /* This is called after the sections have been attached to output
721 sections, but before any sizes or addresses have been set. */
724 gld${EMULATION_NAME}_before_allocation ()
729 /* If we are going to make any variable assignments, we need to let
730 the ELF backend know about them in case the variables are
731 referred to by dynamic objects. */
732 lang_for_each_statement (gld${EMULATION_NAME}_find_statement_assignment);
734 /* Let the ELF backend work out the sizes of any sections required
735 by dynamic linking. */
736 rpath = command_line.rpath;
738 rpath = (const char *) getenv ("LD_RUN_PATH");
739 if (! (bfd_elf${ELFSIZE}_size_dynamic_sections
740 (output_bfd, command_line.soname, rpath,
741 command_line.export_dynamic, command_line.filter_shlib,
742 (const char * const *) command_line.auxiliary_filters,
743 &link_info, &sinterp, lang_elf_version_info)))
744 einfo ("%P%F: failed to set dynamic section sizes: %E\n");
746 /* Let the user override the dynamic linker we are using. */
747 if (command_line.interpreter != NULL
750 sinterp->contents = (bfd_byte *) command_line.interpreter;
751 sinterp->_raw_size = strlen (command_line.interpreter) + 1;
754 /* Look for any sections named .gnu.warning. As a GNU extensions,
755 we treat such sections as containing warning messages. We print
756 out the warning message, and then zero out the section size so
757 that it does not get copied into the output file. */
760 LANG_FOR_EACH_INPUT_STATEMENT (is)
767 if (is->just_syms_flag)
770 s = bfd_get_section_by_name (is->the_bfd, ".gnu.warning");
774 sz = bfd_section_size (is->the_bfd, s);
775 msg = xmalloc ((size_t) sz + 1);
776 if (! bfd_get_section_contents (is->the_bfd, s, msg, (file_ptr) 0, sz))
777 einfo ("%F%B: Can't read contents of section .gnu.warning: %E\n",
780 ret = link_info.callbacks->warning (&link_info, msg,
782 is->the_bfd, (asection *) NULL,
787 /* Clobber the section size, so that we don't waste copying the
788 warning into the output file. */
794 /* This is called by the before_allocation routine via
795 lang_for_each_statement. It locates any assignment statements, and
796 tells the ELF backend about them, in case they are assignments to
797 symbols which are referred to by dynamic objects. */
800 gld${EMULATION_NAME}_find_statement_assignment (s)
801 lang_statement_union_type *s;
803 if (s->header.type == lang_assignment_statement_enum)
804 gld${EMULATION_NAME}_find_exp_assignment (s->assignment_statement.exp);
807 /* Look through an expression for an assignment statement. */
810 gld${EMULATION_NAME}_find_exp_assignment (exp)
813 struct bfd_link_hash_entry *h;
815 switch (exp->type.node_class)
818 h = bfd_link_hash_lookup (link_info.hash, exp->assign.dst,
819 false, false, false);
823 /* We call record_link_assignment even if the symbol is defined.
824 This is because if it is defined by a dynamic object, we
825 actually want to use the value defined by the linker script,
826 not the value from the dynamic object (because we are setting
827 symbols like etext). If the symbol is defined by a regular
828 object, then, as it happens, calling record_link_assignment
833 if (strcmp (exp->assign.dst, ".") != 0)
835 if (! (bfd_elf${ELFSIZE}_record_link_assignment
836 (output_bfd, &link_info, exp->assign.dst,
837 exp->type.node_class == etree_provide ? true : false)))
838 einfo ("%P%F: failed to record assignment to %s: %E\n",
841 gld${EMULATION_NAME}_find_exp_assignment (exp->assign.src);
845 gld${EMULATION_NAME}_find_exp_assignment (exp->binary.lhs);
846 gld${EMULATION_NAME}_find_exp_assignment (exp->binary.rhs);
850 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.cond);
851 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.lhs);
852 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.rhs);
856 gld${EMULATION_NAME}_find_exp_assignment (exp->unary.child);
864 /* Place an orphan section. We use this to put random SHF_ALLOC
865 sections in the right segment. */
869 lang_output_section_statement_type *os;
871 lang_statement_union_type **stmt;
876 gld${EMULATION_NAME}_place_orphan (file, s)
877 lang_input_statement_type *file;
880 static struct orphan_save hold_text;
881 static struct orphan_save hold_rodata;
882 static struct orphan_save hold_data;
883 static struct orphan_save hold_bss;
884 static struct orphan_save hold_rel;
885 static struct orphan_save hold_interp;
886 struct orphan_save *place;
887 lang_statement_list_type *old;
888 lang_statement_list_type add;
890 const char *secname, *ps;
891 const char *outsecname;
892 lang_output_section_statement_type *os;
894 secname = bfd_get_section_name (s->owner, s);
896 /* Look through the script to see where to place this section. */
897 os = lang_output_section_find (secname);
900 && os->bfd_section != NULL
901 && ((s->flags ^ os->bfd_section->flags) & (SEC_LOAD | SEC_ALLOC)) == 0)
903 /* We have already placed a section with this name. */
904 wild_doit (&os->children, s, os, file);
908 if (hold_text.os == NULL)
909 hold_text.os = lang_output_section_find (".text");
911 /* If this is a final link, then always put .gnu.warning.SYMBOL
912 sections into the .text section to get them out of the way. */
913 if (! link_info.shared
914 && ! link_info.relocateable
915 && strncmp (secname, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0
916 && hold_text.os != NULL)
918 wild_doit (&hold_text.os->children, s, hold_text.os, file);
922 /* Decide which segment the section should go in based on the
923 section name and section flags. We put loadable .note sections
924 right after the .interp section, so that the PT_NOTE segment is
925 stored right after the program headers where the OS can read it
926 in the first page. */
927 #define HAVE_SECTION(hold, name) \
928 (hold.os != NULL || (hold.os = lang_output_section_find (name)) != NULL)
930 if (s->flags & SEC_EXCLUDE)
932 else if ((s->flags & SEC_ALLOC) == 0)
934 else if ((s->flags & SEC_LOAD) != 0
935 && strncmp (secname, ".note", 4) == 0
936 && HAVE_SECTION (hold_interp, ".interp"))
937 place = &hold_interp;
938 else if ((s->flags & SEC_HAS_CONTENTS) == 0
939 && HAVE_SECTION (hold_bss, ".bss"))
941 else if ((s->flags & SEC_READONLY) == 0
942 && HAVE_SECTION (hold_data, ".data"))
944 else if (strncmp (secname, ".rel", 4) == 0
945 && (hold_rel.os != NULL
946 || (hold_rel.os = output_rel_find ()) != NULL))
948 else if ((s->flags & SEC_CODE) == 0
949 && (s->flags & SEC_READONLY) != 0
950 && HAVE_SECTION (hold_rodata, ".rodata"))
951 place = &hold_rodata;
952 else if ((s->flags & SEC_READONLY) != 0
953 && hold_text.os != NULL)
960 /* Choose a unique name for the section. This will be needed if the
961 same section name appears in the input file with different
962 loadable or allocateable characteristics. */
963 outsecname = secname;
964 if (bfd_get_section_by_name (output_bfd, outsecname) != NULL)
970 len = strlen (outsecname);
971 newname = xmalloc (len + 5);
972 strcpy (newname, outsecname);
976 sprintf (newname + len, "%d", i);
979 while (bfd_get_section_by_name (output_bfd, newname) != NULL);
981 outsecname = newname;
986 /* Start building a list of statements for this section. */
989 lang_list_init (stat_ptr);
991 /* If the name of the section is representable in C, then create
992 symbols to mark the start and the end of the section. */
993 for (ps = outsecname; *ps != '\0'; ps++)
994 if (! isalnum ((unsigned char) *ps) && *ps != '_')
996 if (*ps == '\0' && config.build_constructors)
1001 symname = (char *) xmalloc (ps - outsecname + sizeof "__start_");
1002 sprintf (symname, "__start_%s", outsecname);
1003 e_align = exp_unop (ALIGN_K,
1004 exp_intop ((bfd_vma) 1 << s->alignment_power));
1005 lang_add_assignment (exp_assop ('=', symname, e_align));
1009 if (link_info.relocateable || (s->flags & (SEC_LOAD | SEC_ALLOC)) == 0)
1010 address = exp_intop ((bfd_vma) 0);
1014 os = lang_enter_output_section_statement (outsecname, address, 0,
1016 (etree_type *) NULL,
1017 (etree_type *) NULL,
1018 (etree_type *) NULL);
1020 wild_doit (&os->children, s, os, file);
1022 lang_leave_output_section_statement
1023 ((bfd_vma) 0, "*default*",
1024 (struct lang_output_section_phdr_list *) NULL, "*default*");
1028 asection *snew, **pps;
1032 if (*ps == '\0' && config.build_constructors)
1036 symname = (char *) xmalloc (ps - outsecname + sizeof "__stop_");
1037 sprintf (symname, "__stop_%s", outsecname);
1038 lang_add_assignment (exp_assop ('=', symname,
1039 exp_nameop (NAME, ".")));
1043 snew = os->bfd_section;
1044 if (place->os->bfd_section != NULL || place->section != NULL)
1046 /* Shuffle the section to make the output file look neater. */
1047 if (place->section == NULL)
1050 /* Finding the end of the list is a little tricky. We
1051 make a wild stab at it by comparing section flags. */
1052 flagword first_flags = place->os->bfd_section->flags;
1053 for (pps = &place->os->bfd_section->next;
1054 *pps != NULL && (*pps)->flags == first_flags;
1055 pps = &(*pps)->next)
1057 place->section = pps;
1059 /* Put orphans after the first section on the list. */
1060 place->section = &place->os->bfd_section->next;
1064 /* Unlink the section. */
1065 for (pps = &output_bfd->sections; *pps != snew; pps = &(*pps)->next)
1069 /* Now tack it on to the "place->os" section list. */
1070 snew->next = *place->section;
1071 *place->section = snew;
1073 place->section = &snew->next; /* Save the end of this list. */
1075 if (place->stmt == NULL)
1077 /* Put the new statement list right at the head. */
1078 *add.tail = place->os->header.next;
1079 place->os->header.next = add.head;
1083 /* Put it after the last orphan statement we added. */
1084 *add.tail = *place->stmt;
1085 *place->stmt = add.head;
1087 place->stmt = add.tail; /* Save the end of this list. */
1093 /* A variant of lang_output_section_find. */
1094 static lang_output_section_statement_type *
1097 lang_statement_union_type *u;
1098 lang_output_section_statement_type *lookup;
1100 for (u = lang_output_section_statement.head;
1101 u != (lang_statement_union_type *) NULL;
1104 lookup = &u->output_section_statement;
1105 if (strncmp (".rel", lookup->name, 4) == 0
1106 && lookup->bfd_section != NULL
1107 && (lookup->bfd_section->flags & SEC_ALLOC) != 0)
1112 return (lang_output_section_statement_type *) NULL;
1116 gld${EMULATION_NAME}_get_script(isfile)
1120 if test -n "$COMPILE_IN"
1122 # Scripts compiled in.
1124 # sed commands to quote an ld script as a C string.
1125 sc="-f stringify.sed"
1127 cat >>e${EMULATION_NAME}.c <<EOF
1131 if (link_info.relocateable == true && config.build_constructors == true)
1134 sed $sc ldscripts/${EMULATION_NAME}.xu >> e${EMULATION_NAME}.c
1135 echo ' ; else if (link_info.relocateable == true) return' >> e${EMULATION_NAME}.c
1136 sed $sc ldscripts/${EMULATION_NAME}.xr >> e${EMULATION_NAME}.c
1137 echo ' ; else if (!config.text_read_only) return' >> e${EMULATION_NAME}.c
1138 sed $sc ldscripts/${EMULATION_NAME}.xbn >> e${EMULATION_NAME}.c
1139 echo ' ; else if (!config.magic_demand_paged) return' >> e${EMULATION_NAME}.c
1140 sed $sc ldscripts/${EMULATION_NAME}.xn >> e${EMULATION_NAME}.c
1142 if test -n "$GENERATE_SHLIB_SCRIPT" ; then
1143 echo ' ; else if (link_info.shared) return' >> e${EMULATION_NAME}.c
1144 sed $sc ldscripts/${EMULATION_NAME}.xs >> e${EMULATION_NAME}.c
1147 echo ' ; else return' >> e${EMULATION_NAME}.c
1148 sed $sc ldscripts/${EMULATION_NAME}.x >> e${EMULATION_NAME}.c
1149 echo '; }' >> e${EMULATION_NAME}.c
1152 # Scripts read from the filesystem.
1154 cat >>e${EMULATION_NAME}.c <<EOF
1158 if (link_info.relocateable == true && config.build_constructors == true)
1159 return "ldscripts/${EMULATION_NAME}.xu";
1160 else if (link_info.relocateable == true)
1161 return "ldscripts/${EMULATION_NAME}.xr";
1162 else if (!config.text_read_only)
1163 return "ldscripts/${EMULATION_NAME}.xbn";
1164 else if (!config.magic_demand_paged)
1165 return "ldscripts/${EMULATION_NAME}.xn";
1166 else if (link_info.shared)
1167 return "ldscripts/${EMULATION_NAME}.xs";
1169 return "ldscripts/${EMULATION_NAME}.x";
1175 if test -n "$PARSE_AND_LIST_ARGS" ; then
1176 cat >>e${EMULATION_NAME}.c <<EOF
1177 static int gld_${EMULATION_NAME}_parse_args PARAMS ((int, char **));
1178 static void gld_${EMULATION_NAME}_list_options PARAMS ((FILE * file));
1180 $PARSE_AND_LIST_ARGS
1184 cat >>e${EMULATION_NAME}.c <<EOF
1185 #define gld_${EMULATION_NAME}_parse_args NULL
1186 #define gld_${EMULATION_NAME}_list_options NULL
1191 cat >>e${EMULATION_NAME}.c <<EOF
1193 struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
1195 gld${EMULATION_NAME}_before_parse,
1198 after_parse_default,
1199 gld${EMULATION_NAME}_after_open,
1200 after_allocation_default,
1201 set_output_arch_default,
1202 ldemul_default_target,
1203 gld${EMULATION_NAME}_before_allocation,
1204 gld${EMULATION_NAME}_get_script,
1205 "${EMULATION_NAME}",
1208 NULL, /* create output section statements */
1209 gld${EMULATION_NAME}_open_dynamic_archive,
1210 gld${EMULATION_NAME}_place_orphan,
1211 NULL, /* set_symbols */
1212 gld_${EMULATION_NAME}_parse_args,
1213 NULL, /* unrecognized_file */
1214 gld_${EMULATION_NAME}_list_options,
1215 NULL, /* recognized_file */
1216 NULL /* find_potential_libraries */