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 if [ -z "$MACHINE" ]; then
9 OUTPUT_ARCH=${ARCH}:${MACHINE}
11 cat >e${EMULATION_NAME}.c <<EOF
12 /* This file is is generated by a shell script. DO NOT EDIT! */
14 /* ${ELFSIZE} bit ELF emulation code for ${EMULATION_NAME}
15 Copyright 1991, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001
16 Free Software Foundation, Inc.
17 Written by Steve Chamberlain <sac@cygnus.com>
18 ELF support by Ian Lance Taylor <ian@cygnus.com>
20 This file is part of GLD, the Gnu Linker.
22 This program is free software; you can redistribute it and/or modify
23 it under the terms of the GNU General Public License as published by
24 the Free Software Foundation; either version 2 of the License, or
25 (at your option) any later version.
27 This program is distributed in the hope that it will be useful,
28 but WITHOUT ANY WARRANTY; without even the implied warranty of
29 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 GNU General Public License for more details.
32 You should have received a copy of the GNU General Public License
33 along with this program; if not, write to the Free Software
34 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
36 #define TARGET_IS_${EMULATION_NAME}
40 #include "libiberty.h"
54 #include "elf/common.h"
56 static void gld${EMULATION_NAME}_before_parse PARAMS ((void));
57 static void gld${EMULATION_NAME}_vercheck
58 PARAMS ((lang_input_statement_type *));
59 static void gld${EMULATION_NAME}_stat_needed
60 PARAMS ((lang_input_statement_type *));
61 static boolean gld${EMULATION_NAME}_try_needed PARAMS ((const char *, int));
62 static boolean gld${EMULATION_NAME}_search_needed
63 PARAMS ((const char *, const char *, int));
64 static void gld${EMULATION_NAME}_check_needed
65 PARAMS ((lang_input_statement_type *));
66 static void gld${EMULATION_NAME}_after_open PARAMS ((void));
67 static void gld${EMULATION_NAME}_find_exp_assignment PARAMS ((etree_type *));
68 static void gld${EMULATION_NAME}_find_statement_assignment
69 PARAMS ((lang_statement_union_type *));
70 static void gld${EMULATION_NAME}_before_allocation PARAMS ((void));
71 static boolean gld${EMULATION_NAME}_open_dynamic_archive
72 PARAMS ((const char *, search_dirs_type *, lang_input_statement_type *));
73 static lang_output_section_statement_type *output_rel_find PARAMS ((void));
74 static asection *output_prev_sec_find
75 PARAMS ((lang_output_section_statement_type *));
76 static boolean gld${EMULATION_NAME}_place_orphan
77 PARAMS ((lang_input_statement_type *, asection *));
78 static char *gld${EMULATION_NAME}_get_script PARAMS ((int *isfile));
82 # Import any needed special functions and/or overrides.
84 if test -n "$EXTRA_EM_FILE" ; then
85 . ${srcdir}/emultempl/${EXTRA_EM_FILE}.em
88 # Functions in this file can be overridden by setting the LDEMUL_* shell
89 # variables. If the name of the overriding function is the same as is
90 # defined in this file, then don't output this file's version.
91 # If a different overriding name is given then output the standard function
92 # as presumably it is called from the overriding function.
94 if test x"$LDEMUL_BEFORE_PARSE" != xgld"$EMULATION_NAME"_before_parse; then
95 cat >>e${EMULATION_NAME}.c <<EOF
98 gld${EMULATION_NAME}_before_parse ()
100 const bfd_arch_info_type *arch = bfd_scan_arch ("${OUTPUT_ARCH}");
103 ldfile_output_architecture = arch->arch;
104 ldfile_output_machine = arch->mach;
105 ldfile_output_machine_name = arch->printable_name;
108 ldfile_output_architecture = bfd_arch_`echo ${ARCH} | sed -e 's/:.*//'`;
109 config.dynamic_link = ${DYNAMIC_LINK-true};
110 config.has_shared = `if test -n "$GENERATE_SHLIB_SCRIPT" ; then echo true ; else echo false ; fi`;
116 cat >>e${EMULATION_NAME}.c <<EOF
118 /* These variables are required to pass information back and forth
119 between after_open and check_needed and stat_needed and vercheck. */
121 static struct bfd_link_needed_list *global_needed;
122 static struct stat global_stat;
123 static boolean global_found;
124 static struct bfd_link_needed_list *global_vercheck_needed;
125 static boolean global_vercheck_failed;
128 /* On Linux, it's possible to have different versions of the same
129 shared library linked against different versions of libc. The
130 dynamic linker somehow tags which libc version to use in
131 /etc/ld.so.cache, and, based on the libc that it sees in the
132 executable, chooses which version of the shared library to use.
134 We try to do a similar check here by checking whether this shared
135 library needs any other shared libraries which may conflict with
136 libraries we have already included in the link. If it does, we
137 skip it, and try to find another shared library farther on down the
140 This is called via lang_for_each_input_file.
141 GLOBAL_VERCHECK_NEEDED is the list of objects needed by the object
142 which we are checking. This sets GLOBAL_VERCHECK_FAILED if we find
143 a conflicting version. */
146 gld${EMULATION_NAME}_vercheck (s)
147 lang_input_statement_type *s;
150 struct bfd_link_needed_list *l;
152 if (global_vercheck_failed)
154 if (s->the_bfd == NULL
155 || (bfd_get_file_flags (s->the_bfd) & DYNAMIC) == 0)
158 soname = bfd_elf_get_dt_soname (s->the_bfd);
160 soname = basename (bfd_get_filename (s->the_bfd));
162 for (l = global_vercheck_needed; l != NULL; l = l->next)
166 if (strcmp (soname, l->name) == 0)
168 /* Probably can't happen, but it's an easy check. */
172 if (strchr (l->name, '/') != NULL)
175 suffix = strstr (l->name, ".so.");
179 suffix += sizeof ".so." - 1;
181 if (strncmp (soname, l->name, suffix - l->name) == 0)
183 /* Here we know that S is a dynamic object FOO.SO.VER1, and
184 the object we are considering needs a dynamic object
185 FOO.SO.VER2, and VER1 and VER2 are different. This
186 appears to be a version mismatch, so we tell the caller
187 to try a different version of this library. */
188 global_vercheck_failed = true;
195 /* See if an input file matches a DT_NEEDED entry by running stat on
199 gld${EMULATION_NAME}_stat_needed (s)
200 lang_input_statement_type *s;
208 if (s->the_bfd == NULL)
211 if (bfd_stat (s->the_bfd, &st) != 0)
213 einfo ("%P:%B: bfd_stat failed: %E\n", s->the_bfd);
217 if (st.st_dev == global_stat.st_dev
218 && st.st_ino == global_stat.st_ino)
224 /* We issue a warning if it looks like we are including two
225 different versions of the same shared library. For example,
226 there may be a problem if -lc picks up libc.so.6 but some other
227 shared library has a DT_NEEDED entry of libc.so.5. This is a
228 heuristic test, and it will only work if the name looks like
229 NAME.so.VERSION. FIXME: Depending on file names is error-prone.
230 If we really want to issue warnings about mixing version numbers
231 of shared libraries, we need to find a better way. */
233 if (strchr (global_needed->name, '/') != NULL)
235 suffix = strstr (global_needed->name, ".so.");
238 suffix += sizeof ".so." - 1;
240 soname = bfd_elf_get_dt_soname (s->the_bfd);
242 soname = basename (s->filename);
244 if (strncmp (soname, global_needed->name,
245 suffix - global_needed->name) == 0)
246 einfo ("%P: warning: %s, needed by %B, may conflict with %s\n",
247 global_needed->name, global_needed->by, soname);
251 /* This function is called for each possible name for a dynamic object
252 named by a DT_NEEDED entry. The FORCE parameter indicates whether
253 to skip the check for a conflicting version. */
256 gld${EMULATION_NAME}_try_needed (name, force)
263 abfd = bfd_openr (name, bfd_get_target (output_bfd));
266 if (! bfd_check_format (abfd, bfd_object))
268 (void) bfd_close (abfd);
271 if ((bfd_get_file_flags (abfd) & DYNAMIC) == 0)
273 (void) bfd_close (abfd);
277 /* Check whether this object would include any conflicting library
278 versions. If FORCE is set, then we skip this check; we use this
279 the second time around, if we couldn't find any compatible
280 instance of the shared library. */
284 struct bfd_link_needed_list *needed;
286 if (! bfd_elf_get_bfd_needed_list (abfd, &needed))
287 einfo ("%F%P:%B: bfd_elf_get_bfd_needed_list failed: %E\n", abfd);
291 global_vercheck_needed = needed;
292 global_vercheck_failed = false;
293 lang_for_each_input_file (gld${EMULATION_NAME}_vercheck);
294 if (global_vercheck_failed)
296 (void) bfd_close (abfd);
297 /* Return false to force the caller to move on to try
298 another file on the search path. */
302 /* But wait! It gets much worse. On Linux, if a shared
303 library does not use libc at all, we are supposed to skip
304 it the first time around in case we encounter a shared
305 library later on with the same name which does use the
306 version of libc that we want. This is much too horrible
307 to use on any system other than Linux. */
312 cat >>e${EMULATION_NAME}.c <<EOF
314 struct bfd_link_needed_list *l;
316 for (l = needed; l != NULL; l = l->next)
317 if (strncmp (l->name, "libc.so", 7) == 0)
321 (void) bfd_close (abfd);
329 cat >>e${EMULATION_NAME}.c <<EOF
333 /* We've found a dynamic object matching the DT_NEEDED entry. */
335 /* We have already checked that there is no other input file of the
336 same name. We must now check again that we are not including the
337 same file twice. We need to do this because on many systems
338 libc.so is a symlink to, e.g., libc.so.1. The SONAME entry will
339 reference libc.so.1. If we have already included libc.so, we
340 don't want to include libc.so.1 if they are the same file, and we
341 can only check that using stat. */
343 if (bfd_stat (abfd, &global_stat) != 0)
344 einfo ("%F%P:%B: bfd_stat failed: %E\n", abfd);
346 /* First strip off everything before the last '/'. */
347 soname = basename (abfd->filename);
349 if (trace_file_tries)
350 info_msg (_("found %s at %s\n"), soname, name);
352 global_found = false;
353 lang_for_each_input_file (gld${EMULATION_NAME}_stat_needed);
356 /* Return true to indicate that we found the file, even though
357 we aren't going to do anything with it. */
361 /* Tell the ELF backend that we don't want the output file to have a
362 DT_NEEDED entry for this file. */
363 bfd_elf_set_dt_needed_name (abfd, "");
365 /* Previos basename call was clobbered in lang_for_each_input_file. */
366 soname = basename (abfd->filename);
368 /* Tell the ELF backend that the output file needs a DT_NEEDED
369 entry for this file if it is used to resolve the reference in
371 bfd_elf_set_dt_needed_soname (abfd, soname);
373 /* Add this file into the symbol table. */
374 if (! bfd_link_add_symbols (abfd, &link_info))
375 einfo ("%F%B: could not read symbols: %E\n", abfd);
381 /* Search for a needed file in a path. */
384 gld${EMULATION_NAME}_search_needed (path, name, force)
393 return gld${EMULATION_NAME}_try_needed (name, force);
395 if (path == NULL || *path == '\0')
400 char *filename, *sset;
402 s = strchr (path, ':');
404 s = path + strlen (path);
406 filename = (char *) xmalloc (s - path + len + 2);
411 memcpy (filename, path, s - path);
412 filename[s - path] = '/';
413 sset = filename + (s - path) + 1;
417 if (gld${EMULATION_NAME}_try_needed (filename, force))
431 if [ "x${host}" = "x${target}" ] ; then
432 case " ${EMULATION_LIBPATH} " in
433 *" ${EMULATION_NAME} "*)
436 cat >>e${EMULATION_NAME}.c <<EOF
438 /* For a native linker, check the file /etc/ld.so.conf for directories
439 in which we may find shared libraries. /etc/ld.so.conf is really
440 only meaningful on Linux. */
442 static boolean gld${EMULATION_NAME}_check_ld_so_conf
443 PARAMS ((const char *, int));
446 gld${EMULATION_NAME}_check_ld_so_conf (name, force)
450 static boolean initialized;
451 static char *ld_so_conf;
457 f = fopen ("/etc/ld.so.conf", FOPEN_RT);
466 b = (char *) xmalloc (alloc);
468 while ((c = getc (f)) != EOF)
470 if (len + 1 >= alloc)
473 b = (char *) xrealloc (b, alloc);
486 if (len > 0 && b[len - 1] != ':')
494 if (len > 0 && b[len - 1] == ':')
513 if (ld_so_conf == NULL)
516 return gld${EMULATION_NAME}_search_needed (ld_so_conf, name, force);
525 cat >>e${EMULATION_NAME}.c <<EOF
527 /* See if an input file matches a DT_NEEDED entry by name. */
530 gld${EMULATION_NAME}_check_needed (s)
531 lang_input_statement_type *s;
536 if (s->filename != NULL)
540 if (strcmp (s->filename, global_needed->name) == 0)
546 if (s->search_dirs_flag)
548 f = strrchr (s->filename, '/');
550 && strcmp (f + 1, 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)
574 if test x"$LDEMUL_AFTER_OPEN" != xgld"$EMULATION_NAME"_after_open; then
575 cat >>e${EMULATION_NAME}.c <<EOF
577 /* This is called after all the input files have been opened. */
580 gld${EMULATION_NAME}_after_open ()
582 struct bfd_link_needed_list *needed, *l;
584 /* We only need to worry about this when doing a final link. */
585 if (link_info.relocateable || link_info.shared)
588 /* Get the list of files which appear in DT_NEEDED entries in
589 dynamic objects included in the link (often there will be none).
590 For each such file, we want to track down the corresponding
591 library, and include the symbol table in the link. This is what
592 the runtime dynamic linker will do. Tracking the files down here
593 permits one dynamic object to include another without requiring
594 special action by the person doing the link. Note that the
595 needed list can actually grow while we are stepping through this
597 needed = bfd_elf_get_needed_list (output_bfd, &link_info);
598 for (l = needed; l != NULL; l = l->next)
600 struct bfd_link_needed_list *ll;
603 /* If we've already seen this file, skip it. */
604 for (ll = needed; ll != l; ll = ll->next)
605 if (strcmp (ll->name, l->name) == 0)
610 /* See if this file was included in the link explicitly. */
612 global_found = false;
613 lang_for_each_input_file (gld${EMULATION_NAME}_check_needed);
617 if (trace_file_tries)
618 info_msg (_("%s needed by %B\n"), l->name, l->by);
620 /* We need to find this file and include the symbol table. We
621 want to search for the file in the same way that the dynamic
622 linker will search. That means that we want to use
623 rpath_link, rpath, then the environment variable
624 LD_LIBRARY_PATH (native only), then the DT_RPATH/DT_RUNPATH
625 entries (native only), then the linker script LIB_SEARCH_DIRS.
626 We do not search using the -L arguments.
628 We search twice. The first time, we skip objects which may
629 introduce version mismatches. The second time, we force
630 their use. See gld${EMULATION_NAME}_vercheck comment. */
631 for (force = 0; force < 2; force++)
634 search_dirs_type *search;
636 if [ "x${host}" = "x${target}" ] ; then
637 case " ${EMULATION_LIBPATH} " in
638 *" ${EMULATION_NAME} "*)
639 cat >>e${EMULATION_NAME}.c <<EOF
640 const char *lib_path;
641 struct bfd_link_needed_list *rp;
647 cat >>e${EMULATION_NAME}.c <<EOF
649 if (gld${EMULATION_NAME}_search_needed (command_line.rpath_link,
653 if [ "x${host}" = "x${target}" ] ; then
654 case " ${EMULATION_LIBPATH} " in
655 *" ${EMULATION_NAME} "*)
656 cat >>e${EMULATION_NAME}.c <<EOF
657 if (gld${EMULATION_NAME}_search_needed (command_line.rpath,
660 if (command_line.rpath_link == NULL
661 && command_line.rpath == NULL)
663 lib_path = (const char *) getenv ("LD_RUN_PATH");
664 if (gld${EMULATION_NAME}_search_needed (lib_path, l->name,
668 lib_path = (const char *) getenv ("LD_LIBRARY_PATH");
669 if (gld${EMULATION_NAME}_search_needed (lib_path, l->name, force))
673 rp = bfd_elf_get_runpath_list (output_bfd, &link_info);
674 for (; !found && rp != NULL; rp = rp->next)
676 found = (rp->by == l->by
677 && gld${EMULATION_NAME}_search_needed (rp->name,
688 cat >>e${EMULATION_NAME}.c <<EOF
689 len = strlen (l->name);
690 for (search = search_head; search != NULL; search = search->next)
696 filename = (char *) xmalloc (strlen (search->name) + len + 2);
697 sprintf (filename, "%s/%s", search->name, l->name);
698 if (gld${EMULATION_NAME}_try_needed (filename, force))
705 if [ "x${host}" = "x${target}" ] ; then
706 case " ${EMULATION_LIBPATH} " in
707 *" ${EMULATION_NAME} "*)
710 cat >>e${EMULATION_NAME}.c <<EOF
711 if (gld${EMULATION_NAME}_check_ld_so_conf (l->name, force))
720 cat >>e${EMULATION_NAME}.c <<EOF
726 einfo ("%P: warning: %s, needed by %B, not found (try using -rpath or -rpath-link)\n",
734 cat >>e${EMULATION_NAME}.c <<EOF
736 /* Look through an expression for an assignment statement. */
739 gld${EMULATION_NAME}_find_exp_assignment (exp)
742 struct bfd_link_hash_entry *h;
744 switch (exp->type.node_class)
747 h = bfd_link_hash_lookup (link_info.hash, exp->assign.dst,
748 false, false, false);
752 /* We call record_link_assignment even if the symbol is defined.
753 This is because if it is defined by a dynamic object, we
754 actually want to use the value defined by the linker script,
755 not the value from the dynamic object (because we are setting
756 symbols like etext). If the symbol is defined by a regular
757 object, then, as it happens, calling record_link_assignment
762 if (strcmp (exp->assign.dst, ".") != 0)
764 if (! (bfd_elf${ELFSIZE}_record_link_assignment
765 (output_bfd, &link_info, exp->assign.dst,
766 exp->type.node_class == etree_provide ? true : false)))
767 einfo ("%P%F: failed to record assignment to %s: %E\n",
770 gld${EMULATION_NAME}_find_exp_assignment (exp->assign.src);
774 gld${EMULATION_NAME}_find_exp_assignment (exp->binary.lhs);
775 gld${EMULATION_NAME}_find_exp_assignment (exp->binary.rhs);
779 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.cond);
780 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.lhs);
781 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.rhs);
785 gld${EMULATION_NAME}_find_exp_assignment (exp->unary.child);
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);
809 if test x"$LDEMUL_BEFORE_ALLOCATION" != xgld"$EMULATION_NAME"_before_allocation; then
810 if test x"${ELF_INTERPRETER_NAME+set}" = xset; then
811 ELF_INTERPRETER_SET_DEFAULT="
814 sinterp->contents = ${ELF_INTERPRETER_NAME};
815 sinterp->_raw_size = strlen (sinterp->contents) + 1;
820 ELF_INTERPRETER_SET_DEFAULT=
822 cat >>e${EMULATION_NAME}.c <<EOF
824 /* This is called after the sections have been attached to output
825 sections, but before any sizes or addresses have been set. */
828 gld${EMULATION_NAME}_before_allocation ()
833 /* If we are going to make any variable assignments, we need to let
834 the ELF backend know about them in case the variables are
835 referred to by dynamic objects. */
836 lang_for_each_statement (gld${EMULATION_NAME}_find_statement_assignment);
838 /* Let the ELF backend work out the sizes of any sections required
839 by dynamic linking. */
840 rpath = command_line.rpath;
842 rpath = (const char *) getenv ("LD_RUN_PATH");
843 if (! (bfd_elf${ELFSIZE}_size_dynamic_sections
844 (output_bfd, command_line.soname, rpath,
845 command_line.filter_shlib,
846 (const char * const *) command_line.auxiliary_filters,
847 &link_info, &sinterp, lang_elf_version_info)))
848 einfo ("%P%F: failed to set dynamic section sizes: %E\n");
849 ${ELF_INTERPRETER_SET_DEFAULT}
850 /* Let the user override the dynamic linker we are using. */
851 if (command_line.interpreter != NULL
854 sinterp->contents = (bfd_byte *) command_line.interpreter;
855 sinterp->_raw_size = strlen (command_line.interpreter) + 1;
858 /* Look for any sections named .gnu.warning. As a GNU extensions,
859 we treat such sections as containing warning messages. We print
860 out the warning message, and then zero out the section size so
861 that it does not get copied into the output file. */
864 LANG_FOR_EACH_INPUT_STATEMENT (is)
871 if (is->just_syms_flag)
874 s = bfd_get_section_by_name (is->the_bfd, ".gnu.warning");
878 sz = bfd_section_size (is->the_bfd, s);
879 msg = xmalloc ((size_t) sz + 1);
880 if (! bfd_get_section_contents (is->the_bfd, s, msg, (file_ptr) 0, sz))
881 einfo ("%F%B: Can't read contents of section .gnu.warning: %E\n",
884 ret = link_info.callbacks->warning (&link_info, msg,
886 is->the_bfd, (asection *) NULL,
891 /* Clobber the section size, so that we don't waste copying the
892 warning into the output file. */
901 if test x"$LDEMUL_OPEN_DYNAMIC_ARCHIVE" != xgld"$EMULATION_NAME"_open_dynamic_archive; then
902 cat >>e${EMULATION_NAME}.c <<EOF
904 /* Try to open a dynamic archive. This is where we know that ELF
905 dynamic libraries have an extension of .so (or .sl on oddball systems
909 gld${EMULATION_NAME}_open_dynamic_archive (arch, search, entry)
911 search_dirs_type *search;
912 lang_input_statement_type *entry;
914 const char *filename;
917 if (! entry->is_archive)
920 filename = entry->filename;
922 /* This allocates a few bytes too many when EXTRA_SHLIB_EXTENSION
923 is defined, but it does not seem worth the headache to optimize
924 away those two bytes of space. */
925 string = (char *) xmalloc (strlen (search->name)
928 #ifdef EXTRA_SHLIB_EXTENSION
929 + strlen (EXTRA_SHLIB_EXTENSION)
933 sprintf (string, "%s/lib%s%s.so", search->name, filename, arch);
935 #ifdef EXTRA_SHLIB_EXTENSION
936 /* Try the .so extension first. If that fails build a new filename
937 using EXTRA_SHLIB_EXTENSION. */
938 if (! ldfile_try_open_bfd (string, entry))
939 sprintf (string, "%s/lib%s%s%s", search->name,
940 filename, arch, EXTRA_SHLIB_EXTENSION);
943 if (! ldfile_try_open_bfd (string, entry))
949 entry->filename = string;
951 /* We have found a dynamic object to include in the link. The ELF
952 backend linker will create a DT_NEEDED entry in the .dynamic
953 section naming this file. If this file includes a DT_SONAME
954 entry, it will be used. Otherwise, the ELF linker will just use
955 the name of the file. For an archive found by searching, like
956 this one, the DT_NEEDED entry should consist of just the name of
957 the file, without the path information used to find it. Note
958 that we only need to do this if we have a dynamic object; an
959 archive will never be referenced by a DT_NEEDED entry.
961 FIXME: This approach--using bfd_elf_set_dt_needed_name--is not
962 very pretty. I haven't been able to think of anything that is
964 if (bfd_check_format (entry->the_bfd, bfd_object)
965 && (entry->the_bfd->flags & DYNAMIC) != 0)
967 ASSERT (entry->is_archive && entry->search_dirs_flag);
969 /* Rather than duplicating the logic above. Just use the
970 filename we recorded earlier. */
972 filename = xstrdup (basename (entry->filename));
973 bfd_elf_set_dt_needed_name (entry->the_bfd, filename);
981 cat >>e${EMULATION_NAME}.c <<EOF
983 /* A variant of lang_output_section_find. Used by place_orphan. */
985 static lang_output_section_statement_type *
988 lang_statement_union_type *u;
989 lang_output_section_statement_type *lookup;
991 for (u = lang_output_section_statement.head;
992 u != (lang_statement_union_type *) NULL;
995 lookup = &u->output_section_statement;
996 if (strncmp (".rel", lookup->name, 4) == 0
997 && lookup->bfd_section != NULL
998 && (lookup->bfd_section->flags & SEC_ALLOC) != 0)
1003 return (lang_output_section_statement_type *) NULL;
1008 if test x"$LDEMUL_PLACE_ORPHAN" != xgld"$EMULATION_NAME"_place_orphan; then
1009 cat >>e${EMULATION_NAME}.c <<EOF
1011 /* Find the last output section before given output statement.
1012 Used by place_orphan. */
1015 output_prev_sec_find (os)
1016 lang_output_section_statement_type *os;
1018 asection *s = (asection *) NULL;
1019 lang_statement_union_type *u;
1020 lang_output_section_statement_type *lookup;
1022 for (u = lang_output_section_statement.head;
1023 u != (lang_statement_union_type *) NULL;
1026 lookup = &u->output_section_statement;
1029 if (lookup->bfd_section != NULL
1030 && lookup->bfd_section != bfd_abs_section_ptr
1031 && lookup->bfd_section != bfd_com_section_ptr
1032 && lookup->bfd_section != bfd_und_section_ptr)
1033 s = lookup->bfd_section;
1043 /* Place an orphan section. We use this to put random SHF_ALLOC
1044 sections in the right segment. */
1046 struct orphan_save {
1047 lang_output_section_statement_type *os;
1049 lang_statement_union_type **stmt;
1053 gld${EMULATION_NAME}_place_orphan (file, s)
1054 lang_input_statement_type *file;
1057 static struct orphan_save hold_text;
1058 static struct orphan_save hold_rodata;
1059 static struct orphan_save hold_data;
1060 static struct orphan_save hold_bss;
1061 static struct orphan_save hold_rel;
1062 static struct orphan_save hold_interp;
1063 static struct orphan_save hold_sdata;
1064 static int count = 1;
1065 struct orphan_save *place;
1066 lang_statement_list_type *old;
1067 lang_statement_list_type add;
1068 etree_type *address;
1069 const char *secname;
1070 const char *outsecname;
1071 const char *ps = NULL;
1072 lang_output_section_statement_type *os;
1074 secname = bfd_get_section_name (s->owner, s);
1076 if (! config.unique_orphan_sections && ! unique_section_p (secname))
1078 /* Look through the script to see where to place this section. */
1079 os = lang_output_section_find (secname);
1082 && os->bfd_section != NULL
1083 && ((s->flags ^ os->bfd_section->flags)
1084 & (SEC_LOAD | SEC_ALLOC)) == 0)
1086 /* We have already placed a section with this name. */
1087 wild_doit (&os->children, s, os, file);
1092 if (hold_text.os == NULL)
1093 hold_text.os = lang_output_section_find (".text");
1095 /* If this is a final link, then always put .gnu.warning.SYMBOL
1096 sections into the .text section to get them out of the way. */
1097 if (! link_info.shared
1098 && ! link_info.relocateable
1099 && strncmp (secname, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0
1100 && hold_text.os != NULL)
1102 wild_doit (&hold_text.os->children, s, hold_text.os, file);
1106 /* Decide which segment the section should go in based on the
1107 section name and section flags. We put loadable .note sections
1108 right after the .interp section, so that the PT_NOTE segment is
1109 stored right after the program headers where the OS can read it
1110 in the first page. */
1111 #define HAVE_SECTION(hold, name) \
1112 (hold.os != NULL || (hold.os = lang_output_section_find (name)) != NULL)
1114 if (s->flags & SEC_EXCLUDE)
1118 if ((s->flags & SEC_ALLOC) == 0)
1120 else if ((s->flags & SEC_LOAD) != 0
1121 && strncmp (secname, ".note", 5) == 0
1122 && HAVE_SECTION (hold_interp, ".interp"))
1123 place = &hold_interp;
1124 else if ((s->flags & SEC_HAS_CONTENTS) == 0
1125 && HAVE_SECTION (hold_bss, ".bss"))
1127 else if ((s->flags & SEC_SMALL_DATA) != 0
1128 && HAVE_SECTION (hold_sdata, ".sdata"))
1129 place = &hold_sdata;
1130 else if ((s->flags & SEC_READONLY) == 0
1131 && HAVE_SECTION (hold_data, ".data"))
1133 else if (strncmp (secname, ".rel", 4) == 0
1134 && (hold_rel.os != NULL
1135 || (hold_rel.os = output_rel_find ()) != NULL))
1137 else if ((s->flags & (SEC_CODE | SEC_READONLY)) == SEC_READONLY
1138 && HAVE_SECTION (hold_rodata, ".rodata"))
1139 place = &hold_rodata;
1140 else if ((s->flags & (SEC_CODE | SEC_READONLY)) == (SEC_CODE | SEC_READONLY)
1141 && hold_text.os != NULL)
1146 /* Choose a unique name for the section. This will be needed if the
1147 same section name appears in the input file with different
1148 loadable or allocatable characteristics. */
1149 outsecname = secname;
1150 if (bfd_get_section_by_name (output_bfd, outsecname) != NULL)
1152 outsecname = bfd_get_unique_section_name (output_bfd,
1155 if (outsecname == NULL)
1156 einfo ("%F%P: place_orphan failed: %E\n");
1159 /* Start building a list of statements for this section.
1160 First save the current statement pointer. */
1163 /* If we have found an appropriate place for the output section
1164 statements for this orphan, add them to our own private list,
1165 inserting them later into the global statement list. */
1169 lang_list_init (stat_ptr);
1172 if (config.build_constructors)
1174 /* If the name of the section is representable in C, then create
1175 symbols to mark the start and the end of the section. */
1176 for (ps = outsecname; *ps != '\0'; ps++)
1177 if (! isalnum ((unsigned char) *ps) && *ps != '_')
1182 etree_type *e_align;
1184 symname = (char *) xmalloc (ps - outsecname + sizeof "__start_");
1185 sprintf (symname, "__start_%s", outsecname);
1186 e_align = exp_unop (ALIGN_K,
1187 exp_intop ((bfd_vma) 1 << s->alignment_power));
1188 lang_add_assignment (exp_assop ('=', symname, e_align));
1192 if (link_info.relocateable || (s->flags & (SEC_LOAD | SEC_ALLOC)) == 0)
1193 address = exp_intop ((bfd_vma) 0);
1197 os = lang_enter_output_section_statement (outsecname, address, 0,
1199 (etree_type *) NULL,
1200 (etree_type *) NULL,
1201 (etree_type *) NULL);
1203 wild_doit (&os->children, s, os, file);
1205 lang_leave_output_section_statement
1206 ((bfd_vma) 0, "*default*",
1207 (struct lang_output_section_phdr_list *) NULL, "*default*");
1209 if (config.build_constructors && *ps == '\0')
1213 /* lang_leave_ouput_section_statement resets stat_ptr. Put
1214 stat_ptr back where we want it. */
1218 symname = (char *) xmalloc (ps - outsecname + sizeof "__stop_");
1219 sprintf (symname, "__stop_%s", outsecname);
1220 lang_add_assignment (exp_assop ('=', symname,
1221 exp_nameop (NAME, ".")));
1224 /* Restore the global list pointer. */
1229 asection *snew, **pps, *bfd_section;
1231 snew = os->bfd_section;
1232 bfd_section = place->os->bfd_section;
1233 if (place->section == NULL && bfd_section == NULL)
1234 bfd_section = output_prev_sec_find (place->os);
1236 if (place->section != NULL
1237 || (bfd_section != NULL
1238 && bfd_section != snew))
1240 /* Shuffle the section to make the output file look neater.
1241 This is really only cosmetic. */
1242 if (place->section == NULL)
1245 /* Finding the end of the list is a little tricky. We
1246 make a wild stab at it by comparing section flags. */
1247 flagword first_flags = bfd_section->flags;
1248 for (pps = &bfd_section->next;
1249 *pps != NULL && (*pps)->flags == first_flags;
1250 pps = &(*pps)->next)
1252 place->section = pps;
1254 /* Put orphans after the first section on the list. */
1255 place->section = &bfd_section->next;
1259 /* Unlink the section. */
1260 for (pps = &output_bfd->sections; *pps != snew; pps = &(*pps)->next)
1264 /* Now tack it on to the "place->os" section list. */
1265 snew->next = *place->section;
1266 *place->section = snew;
1268 place->section = &snew->next; /* Save the end of this list. */
1270 if (add.head != NULL)
1272 /* We try to put the output statements in some sort of
1273 reasonable order here, because they determine the final
1274 load addresses of the orphan sections. */
1275 if (place->stmt == NULL)
1277 /* Put the new statement list right at the head. */
1278 *add.tail = place->os->header.next;
1279 place->os->header.next = add.head;
1283 /* Put it after the last orphan statement we added. */
1284 *add.tail = *place->stmt;
1285 *place->stmt = add.head;
1288 /* Fix the global list pointer if we happened to tack our
1289 new list at the tail. */
1290 if (*old->tail == add.head)
1291 old->tail = add.tail;
1293 /* Save the end of this list. */
1294 place->stmt = add.tail;
1304 if test x"$LDEMUL_GET_SCRIPT" != xgld"$EMULATION_NAME"_get_script; then
1305 cat >>e${EMULATION_NAME}.c <<EOF
1308 gld${EMULATION_NAME}_get_script (isfile)
1312 if test -n "$COMPILE_IN"
1314 # Scripts compiled in.
1316 # sed commands to quote an ld script as a C string.
1317 sc="-f stringify.sed"
1319 cat >>e${EMULATION_NAME}.c <<EOF
1323 if (link_info.relocateable == true && config.build_constructors == true)
1326 sed $sc ldscripts/${EMULATION_NAME}.xu >> e${EMULATION_NAME}.c
1327 echo ' ; else if (link_info.relocateable == true) return' >> e${EMULATION_NAME}.c
1328 sed $sc ldscripts/${EMULATION_NAME}.xr >> e${EMULATION_NAME}.c
1329 echo ' ; else if (!config.text_read_only) return' >> e${EMULATION_NAME}.c
1330 sed $sc ldscripts/${EMULATION_NAME}.xbn >> e${EMULATION_NAME}.c
1331 echo ' ; else if (!config.magic_demand_paged) return' >> e${EMULATION_NAME}.c
1332 sed $sc ldscripts/${EMULATION_NAME}.xn >> e${EMULATION_NAME}.c
1334 if test -n "$GENERATE_SHLIB_SCRIPT" ; then
1335 echo ' ; else if (link_info.shared) return' >> e${EMULATION_NAME}.c
1336 sed $sc ldscripts/${EMULATION_NAME}.xs >> e${EMULATION_NAME}.c
1339 echo ' ; else return' >> e${EMULATION_NAME}.c
1340 sed $sc ldscripts/${EMULATION_NAME}.x >> e${EMULATION_NAME}.c
1341 echo '; }' >> e${EMULATION_NAME}.c
1344 # Scripts read from the filesystem.
1346 cat >>e${EMULATION_NAME}.c <<EOF
1350 if (link_info.relocateable == true && config.build_constructors == true)
1351 return "ldscripts/${EMULATION_NAME}.xu";
1352 else if (link_info.relocateable == true)
1353 return "ldscripts/${EMULATION_NAME}.xr";
1354 else if (!config.text_read_only)
1355 return "ldscripts/${EMULATION_NAME}.xbn";
1356 else if (!config.magic_demand_paged)
1357 return "ldscripts/${EMULATION_NAME}.xn";
1358 else if (link_info.shared)
1359 return "ldscripts/${EMULATION_NAME}.xs";
1361 return "ldscripts/${EMULATION_NAME}.x";
1368 if test -n "$PARSE_AND_LIST_ARGS_CASES" -o x"$GENERATE_SHLIB_SCRIPT" = xyes; then
1370 if test x"$LDEMUL_PARSE_ARGS" != xgld"$EMULATION_NAME"_parse_args; then
1372 if test -n "$PARSE_AND_LIST_PROLOGUE" ; then
1373 cat >>e${EMULATION_NAME}.c <<EOF
1374 $PARSE_AND_LIST_PROLOGUE
1378 cat >>e${EMULATION_NAME}.c <<EOF
1382 #define OPTION_DISABLE_NEW_DTAGS (400)
1383 #define OPTION_ENABLE_NEW_DTAGS (OPTION_DISABLE_NEW_DTAGS + 1)
1384 #define OPTION_GROUP (OPTION_ENABLE_NEW_DTAGS + 1)
1386 static struct option longopts[] =
1390 if test x"$GENERATE_SHLIB_SCRIPT" = xyes; then
1391 cat >>e${EMULATION_NAME}.c <<EOF
1392 /* getopt allows abbreviations, so we do this to stop it from
1393 treating -d/-e as abbreviations for these options. */
1394 {"disable-new-dtags", no_argument, NULL, OPTION_DISABLE_NEW_DTAGS},
1395 {"disable-new-dtags", no_argument, NULL, OPTION_DISABLE_NEW_DTAGS},
1396 {"enable-new-dtags", no_argument, NULL, OPTION_ENABLE_NEW_DTAGS},
1397 {"enable-new-dtags", no_argument, NULL, OPTION_ENABLE_NEW_DTAGS},
1398 {"Bgroup", no_argument, NULL, OPTION_GROUP},
1399 {"Bgroup", no_argument, NULL, OPTION_GROUP},
1403 if test -n "$PARSE_AND_LIST_LONGOPTS" ; then
1404 cat >>e${EMULATION_NAME}.c <<EOF
1405 $PARSE_AND_LIST_LONGOPTS
1409 cat >>e${EMULATION_NAME}.c <<EOF
1410 {NULL, no_argument, NULL, 0}
1414 static int gld${EMULATION_NAME}_parse_args PARAMS ((int, char **));
1417 gld${EMULATION_NAME}_parse_args (argc, argv)
1423 static int prevoptind = -1;
1424 int prevopterr = opterr;
1427 if (prevoptind != optind)
1431 prevoptind = optind;
1433 optc = getopt_long_only (argc, argv,
1434 "-${PARSE_AND_LIST_SHORTOPTS}z:", longopts,
1436 opterr = prevopterr;
1443 optind = prevoptind;
1448 if test x"$GENERATE_SHLIB_SCRIPT" = xyes; then
1449 cat >>e${EMULATION_NAME}.c <<EOF
1450 case OPTION_DISABLE_NEW_DTAGS:
1451 link_info.new_dtags = false;
1454 case OPTION_ENABLE_NEW_DTAGS:
1455 link_info.new_dtags = true;
1459 link_info.flags_1 |= (bfd_vma) DF_1_GROUP;
1460 /* Groups must be self-contained. */
1461 link_info.no_undefined = true;
1465 if (strcmp (optarg, "initfirst") == 0)
1466 link_info.flags_1 |= (bfd_vma) DF_1_INITFIRST;
1467 else if (strcmp (optarg, "interpose") == 0)
1468 link_info.flags_1 |= (bfd_vma) DF_1_INTERPOSE;
1469 else if (strcmp (optarg, "loadfltr") == 0)
1470 link_info.flags_1 |= (bfd_vma) DF_1_LOADFLTR;
1471 else if (strcmp (optarg, "nodefaultlib") == 0)
1472 link_info.flags_1 |= (bfd_vma) DF_1_NODEFLIB;
1473 else if (strcmp (optarg, "nodelete") == 0)
1474 link_info.flags_1 |= (bfd_vma) DF_1_NODELETE;
1475 else if (strcmp (optarg, "nodlopen") == 0)
1476 link_info.flags_1 |= (bfd_vma) DF_1_NOOPEN;
1477 else if (strcmp (optarg, "nodump") == 0)
1478 link_info.flags_1 |= (bfd_vma) DF_1_NODUMP;
1479 else if (strcmp (optarg, "now") == 0)
1481 link_info.flags |= (bfd_vma) DF_BIND_NOW;
1482 link_info.flags_1 |= (bfd_vma) DF_1_NOW;
1484 else if (strcmp (optarg, "origin") == 0)
1486 link_info.flags |= (bfd_vma) DF_ORIGIN;
1487 link_info.flags_1 |= (bfd_vma) DF_1_ORIGIN;
1489 else if (strcmp (optarg, "defs") == 0)
1490 link_info.no_undefined = true;
1491 /* What about the other Solaris -z options? FIXME. */
1496 if test -n "$PARSE_AND_LIST_ARGS_CASES" ; then
1497 cat >>e${EMULATION_NAME}.c <<EOF
1498 $PARSE_AND_LIST_ARGS_CASES
1502 cat >>e${EMULATION_NAME}.c <<EOF
1511 if test x"$LDEMUL_LIST_OPTIONS" != xgld"$EMULATION_NAME"_list_options; then
1512 cat >>e${EMULATION_NAME}.c <<EOF
1514 static void gld${EMULATION_NAME}_list_options PARAMS ((FILE * file));
1517 gld${EMULATION_NAME}_list_options (file)
1522 if test x"$GENERATE_SHLIB_SCRIPT" = xyes; then
1523 cat >>e${EMULATION_NAME}.c <<EOF
1524 fprintf (file, _(" -Bgroup\t\tSelects group name lookup rules for DSO\n"));
1525 fprintf (file, _(" --disable-new-dtags\tDisable new dynamic tags\n"));
1526 fprintf (file, _(" --enable-new-dtags\tEnable new dynamic tags\n"));
1527 fprintf (file, _(" -z defs\t\tDisallows undefined symbols\n"));
1528 fprintf (file, _(" -z initfirst\t\tMark DSO to be initialized first at runtime\n"));
1529 fprintf (file, _(" -z interpose\t\tMark object to interpose all DSOs but executable\n"));
1530 fprintf (file, _(" -z loadfltr\t\tMark object requiring immediate process\n"));
1531 fprintf (file, _(" -z nodefaultlib\tMark object not to use default search paths\n"));
1532 fprintf (file, _(" -z nodelete\t\tMark DSO non-deletable at runtime\n"));
1533 fprintf (file, _(" -z nodlopen\t\tMark DSO not available to dlopen\n"));
1534 fprintf (file, _(" -z nodump\t\tMark DSO not available to dldump\n"));
1535 fprintf (file, _(" -z now\t\tMark object non-lazy runtime binding\n"));
1536 fprintf (file, _(" -z origin\t\tMark object requiring immediate \$ORIGIN processing\n"));
1537 fprintf (file, _("\t\t\t at runtime\n"));
1538 fprintf (file, _(" -z KEYWORD\t\tIgnored for Solaris compatibility\n"));
1542 if test -n "$PARSE_AND_LIST_OPTIONS" ; then
1543 cat >>e${EMULATION_NAME}.c <<EOF
1544 $PARSE_AND_LIST_OPTIONS
1548 cat >>e${EMULATION_NAME}.c <<EOF
1552 if test -n "$PARSE_AND_LIST_EPILOGUE" ; then
1553 cat >>e${EMULATION_NAME}.c <<EOF
1554 $PARSE_AND_LIST_EPILOGUE
1559 if test x"$LDEMUL_PARSE_ARGS" != xgld"$EMULATION_NAME"_parse_args; then
1560 cat >>e${EMULATION_NAME}.c <<EOF
1561 #define gld${EMULATION_NAME}_parse_args NULL
1564 if test x"$LDEMUL_LIST_OPTIONS" != xgld"$EMULATION_NAME"_list_options; then
1565 cat >>e${EMULATION_NAME}.c <<EOF
1566 #define gld${EMULATION_NAME}_list_options NULL
1571 cat >>e${EMULATION_NAME}.c <<EOF
1573 struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
1575 ${LDEMUL_BEFORE_PARSE-gld${EMULATION_NAME}_before_parse},
1576 ${LDEMUL_SYSLIB-syslib_default},
1577 ${LDEMUL_HLL-hll_default},
1578 ${LDEMUL_AFTER_PARSE-after_parse_default},
1579 ${LDEMUL_AFTER_OPEN-gld${EMULATION_NAME}_after_open},
1580 ${LDEMUL_AFTER_ALLOCATION-after_allocation_default},
1581 ${LDEMUL_SET_OUTPUT_ARCH-set_output_arch_default},
1582 ${LDEMUL_CHOOSE_TARGET-ldemul_default_target},
1583 ${LDEMUL_BEFORE_ALLOCATION-gld${EMULATION_NAME}_before_allocation},
1584 ${LDEMUL_GET_SCRIPT-gld${EMULATION_NAME}_get_script},
1585 "${EMULATION_NAME}",
1587 ${LDEMUL_FINISH-NULL},
1588 ${LDEMUL_CREATE_OUTPUT_SECTION_STATEMENTS-NULL},
1589 ${LDEMUL_OPEN_DYNAMIC_ARCHIVE-gld${EMULATION_NAME}_open_dynamic_archive},
1590 ${LDEMUL_PLACE_ORPHAN-gld${EMULATION_NAME}_place_orphan},
1591 ${LDEMUL_SET_SYMBOLS-NULL},
1592 ${LDEMUL_PARSE_ARGS-gld${EMULATION_NAME}_parse_args},
1593 ${LDEMUL_UNRECOGNIZED_FILE-NULL},
1594 ${LDEMUL_LIST_OPTIONS-gld${EMULATION_NAME}_list_options},
1595 ${LDEMUL_RECOGNIZED_FILE-NULL},
1596 ${LDEMUL_FIND_POTENTIAL_LIBRARIES-NULL},