1 # This shell script emits a C file. -*- C -*-
2 # It does some substitutions.
3 cat >e${EMULATION_NAME}.c <<EOF
4 /* This file is is generated by a shell script. DO NOT EDIT! */
6 /* SunOS emulation code for ${EMULATION_NAME}
7 Copyright (C) 1991, 1993, 1994 Free Software Foundation, Inc.
8 Written by Steve Chamberlain <sac@cygnus.com>
9 SunOS shared library support by Ian Lance Taylor <ian@cygnus.com>
11 This file is part of GLD, the Gnu Linker.
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
27 #define TARGET_IS_${EMULATION_NAME}
29 #include <sys/types.h>
47 # define dirent direct
48 # ifdef HAVE_SYS_NDIR_H
49 # include <sys/ndir.h>
51 # ifdef HAVE_SYS_DIR_H
59 static void gld${EMULATION_NAME}_before_parse PARAMS ((void));
60 static void gld${EMULATION_NAME}_create_output_section_statements
62 static void gld${EMULATION_NAME}_find_so
63 PARAMS ((lang_input_statement_type *));
64 static char *gld${EMULATION_NAME}_search_dir
65 PARAMS ((const char *, const char *, boolean *));
66 static void gld${EMULATION_NAME}_after_open PARAMS ((void));
67 static void gld${EMULATION_NAME}_check_needed
68 PARAMS ((lang_input_statement_type *));
69 static boolean gld${EMULATION_NAME}_search_needed
70 PARAMS ((const char *, const char *));
71 static boolean gld${EMULATION_NAME}_try_needed
72 PARAMS ((const char *, const char *));
73 static void gld${EMULATION_NAME}_before_allocation PARAMS ((void));
74 static void gld${EMULATION_NAME}_find_assignment
75 PARAMS ((lang_statement_union_type *));
76 static void gld${EMULATION_NAME}_find_exp_assignment PARAMS ((etree_type *));
77 static void gld${EMULATION_NAME}_count_need
78 PARAMS ((lang_input_statement_type *));
79 static void gld${EMULATION_NAME}_set_need
80 PARAMS ((lang_input_statement_type *));
81 static char *gld${EMULATION_NAME}_get_script PARAMS ((int *isfile));
84 gld${EMULATION_NAME}_before_parse()
86 ldfile_output_architecture = bfd_arch_${ARCH};
87 config.dynamic_link = true;
90 /* Despite the name, we use this routine to search for dynamic
91 libraries. On SunOS this requires a directory search. We need to
92 find the .so file with the highest version number. The user may
93 restrict the major version by saying, e.g., -lc.1. Also, if we
94 find a .so file, we need to look for a the same file after
95 replacing .so with .sa; if it exists, it will be an archive which
96 provide some initializations for data symbols, and we need to
97 search it after including the .so file. */
100 gld${EMULATION_NAME}_create_output_section_statements ()
102 lang_for_each_input_file (gld${EMULATION_NAME}_find_so);
105 /* Search the directory for a .so file for each library search. */
108 gld${EMULATION_NAME}_find_so (inp)
109 lang_input_statement_type *inp;
111 search_dirs_type *search;
116 if (! inp->search_dirs_flag
121 ASSERT (strncmp (inp->local_sym_name, "-l", 2) == 0);
123 for (search = search_head; search != NULL; search = search->next)
125 boolean found_static;
127 found = gld${EMULATION_NAME}_search_dir (search->name, inp->filename,
129 if (found != NULL || found_static)
135 /* We did not find a matching .so file. This isn't an error,
136 since there might still be a matching .a file, which will be
137 found by the usual search. */
141 /* Replace the filename with the one we have found. */
142 alc = (char *) xmalloc (strlen (search->name) + strlen (found) + 2);
143 sprintf (alc, "%s/%s", search->name, found);
146 /* Turn off the search_dirs_flag to prevent ldfile_open_file from
147 searching for this file again. */
148 inp->search_dirs_flag = false;
152 /* Now look for the same file name, but with .sa instead of .so. If
153 found, add it to the list of input files. */
154 alc = (char *) xmalloc (strlen (inp->filename) + 1);
155 strcpy (alc, inp->filename);
156 strstr (alc, ".so.")[2] = 'a';
157 if (stat (alc, &st) == 0)
159 lang_input_statement_type *sa;
162 /* Add the .sa file to the statement list just after the .so
163 file. This is really a hack. */
164 sa = ((lang_input_statement_type *)
165 xmalloc (sizeof (lang_input_statement_type)));
166 sa->header.next = inp->header.next;
167 sa->header.type = lang_input_statement_enum;
168 a = (char *) xmalloc (strlen (alc) + 1);
171 sa->local_sym_name = a;
174 sa->symbol_count = 0;
176 sa->next_real_file = inp->next_real_file;
177 sa->is_archive = false;
178 sa->search_dirs_flag = false;
179 sa->just_syms_flag = false;
182 sa->complained = false;
184 /* Put the new statement next on the list of statements and next
185 on the list of input files. */
186 inp->header.next = (lang_statement_union_type *) sa;
187 inp->next_real_file = (lang_statement_union_type *) sa;
191 /* Search a directory for a .so file. */
194 gld${EMULATION_NAME}_search_dir (dirname, filename, found_static)
196 const char *filename;
197 boolean *found_static;
199 int force_maj, force_min;
204 int max_maj, max_min;
206 struct dirent *entry;
208 *found_static = false;
212 dot = strchr (filename, '.');
215 len = strlen (filename);
220 force_maj = atoi (dot + 1);
222 len = dot - filename;
223 alc = (char *) xmalloc (len + 1);
224 strncpy (alc, filename, len);
228 dot = strchr (dot + 1, '.');
230 force_min = atoi (dot + 1);
234 max_maj = max_min = 0;
236 dir = opendir (dirname);
240 while ((entry = readdir (dir)) != NULL)
242 int found_maj, found_min;
244 if (strncmp (entry->d_name, "lib", 3) != 0
245 || strncmp (entry->d_name + 3, filename, len) != 0)
249 && strcmp (entry->d_name + 3 + len, ".a") == 0)
251 *found_static = true;
255 if (strncmp (entry->d_name + 3 + len, ".so", 3) != 0)
258 /* We've found a .so file. Work out the major and minor
262 sscanf (entry->d_name + 3 + len, ".so.%d.%d",
263 &found_maj, &found_min);
265 if ((force_maj != -1 && force_maj != found_maj)
266 || (force_min != -1 && force_min != found_min))
269 /* We've found a match for the name we are searching for. See
270 if this is the version we should use. If the major and minor
271 versions match, we use the last entry in alphabetical order;
272 I don't know if this is how SunOS distinguishes libc.so.1.8
273 from libc.so.1.8.1, but it ought to suffice. */
275 || (found_maj > max_maj)
276 || (found_maj == max_maj
277 && (found_min > max_min
278 || (found_min == max_min
279 && strcmp (entry->d_name, found) > 0))))
283 found = (char *) xmalloc (strlen (entry->d_name) + 1);
284 strcpy (found, entry->d_name);
298 /* These variables are required to pass information back and forth
299 between after_open and check_needed. */
301 static struct bfd_link_needed_list *global_needed;
302 static boolean global_found;
304 /* This is called after all the input files have been opened. */
307 gld${EMULATION_NAME}_after_open ()
309 struct bfd_link_needed_list *needed, *l;
311 /* We only need to worry about this when doing a final link. */
312 if (link_info.relocateable || link_info.shared)
315 /* Get the list of files which appear in ld_need entries in dynamic
316 objects included in the link. For each such file, we want to
317 track down the corresponding library, and include the symbol
318 table in the link. This is what the runtime dynamic linker will
319 do. Tracking the files down here permits one dynamic object to
320 include another without requiring special action by the person
321 doing the link. Note that the needed list can actually grow
322 while we are stepping through this loop. */
323 needed = bfd_sunos_get_needed_list (output_bfd, &link_info);
324 for (l = needed; l != NULL; l = l->next)
326 struct bfd_link_needed_list *ll;
328 const char *lib_path;
329 search_dirs_type *search;
333 /* If we've already seen this file, skip it. */
334 for (ll = needed; ll != l; ll = ll->next)
335 if (strcmp (ll->name, lname) == 0)
340 /* See if this file was included in the link explicitly. */
342 global_found = false;
343 lang_for_each_input_file (gld${EMULATION_NAME}_check_needed);
347 if (strncmp (lname, "-l", 2) != 0)
351 abfd = bfd_openr (lname, bfd_get_target (output_bfd));
354 if (! bfd_check_format (abfd, bfd_object))
356 (void) bfd_close (abfd);
362 if ((bfd_get_file_flags (abfd) & DYNAMIC) == 0)
364 (void) bfd_close (abfd);
370 /* We've found the needed dynamic object. */
371 if (! bfd_link_add_symbols (abfd, &link_info))
372 einfo ("%F%B: could not read symbols: %E\n", abfd);
376 einfo ("%P: warning: %s, needed by %B, not found\n",
385 /* We want to search for the file in the same way that the
386 dynamic linker will search. That means that we want to use
387 rpath_link, rpath or -L, then the environment variable
388 LD_LIBRARY_PATH (native only), then (if rpath was used) the
389 linker script LIB_SEARCH_DIRS. */
390 if (gld${EMULATION_NAME}_search_needed (command_line.rpath_link,
393 if (command_line.rpath != NULL)
395 if (gld${EMULATION_NAME}_search_needed (command_line.rpath, lname))
400 for (search = search_head; search != NULL; search = search->next)
401 if (gld${EMULATION_NAME}_try_needed (search->name, lname))
407 if [ "x${host_alias}" = "x${target_alias}" ] ; then
408 cat >>e${EMULATION_NAME}.c <<EOF
409 lib_path = (const char *) getenv ("LD_LIBRARY_PATH");
410 if (gld${EMULATION_NAME}_search_needed (lib_path, lname))
414 cat >>e${EMULATION_NAME}.c <<EOF
415 if (command_line.rpath != NULL)
417 for (search = search_head; search != NULL; search = search->next)
421 if (gld${EMULATION_NAME}_try_needed (search->name, lname))
428 einfo ("%P: warning: %s, needed by %B, not found\n",
433 /* Search for a needed file in a path. */
436 gld${EMULATION_NAME}_search_needed (path, name)
442 if (path == NULL || *path == '\0')
449 s = strchr (path, ':');
457 dircopy = (char *) xmalloc (s - path + 1);
458 memcpy (dircopy, path, s - path);
459 dircopy[s - path] = '\0';
463 if (gld${EMULATION_NAME}_try_needed (dir, name))
477 /* This function is called for each possible directory for a needed
481 gld${EMULATION_NAME}_try_needed (dir, name)
490 file = gld${EMULATION_NAME}_search_dir (dir, name, &ignore);
494 alc = (char *) xmalloc (strlen (dir) + strlen (file) + 2);
495 sprintf (alc, "%s/%s", dir, file);
497 abfd = bfd_openr (alc, bfd_get_target (output_bfd));
500 if (! bfd_check_format (abfd, bfd_object))
502 (void) bfd_close (abfd);
505 if ((bfd_get_file_flags (abfd) & DYNAMIC) == 0)
507 (void) bfd_close (abfd);
511 /* We've found the needed dynamic object. */
513 /* Add this file into the symbol table. */
514 if (! bfd_link_add_symbols (abfd, &link_info))
515 einfo ("%F%B: could not read symbols: %E\n", abfd);
520 /* See if we have already included a needed object in the link. This
521 does not have to be precise, as it does no harm to include a
522 dynamic object more than once. */
525 gld${EMULATION_NAME}_check_needed (s)
526 lang_input_statement_type *s;
528 if (s->filename == NULL)
530 if (strncmp (global_needed->name, "-l", 2) != 0)
532 if (strcmp (s->filename, global_needed->name) == 0)
537 const char *sname, *lname;
538 const char *sdot, *ldot;
539 int lmaj, lmin, smaj, smin;
541 lname = global_needed->name + 2;
543 sname = strrchr (s->filename, '/');
549 if (strncmp (sname, "lib", 3) != 0)
553 ldot = strchr (lname, '.');
555 ldot = lname + strlen (lname);
557 sdot = strstr (sname, ".so.");
561 if (sdot - sname != ldot - lname
562 || strncmp (lname, sname, sdot - sname) != 0)
566 sscanf (ldot, ".%d.%d", &lmaj, &lmin);
568 sscanf (sdot, ".so.%d.%d", &smaj, &smin);
569 if ((smaj != lmaj && smaj != -1 && lmaj != -1)
570 || (smin != lmin && smin != -1 && lmin != -1))
577 /* We need to use static variables to pass information around the call
578 to lang_for_each_statement. Ick. */
580 static const char *find_assign;
581 static boolean found_assign;
583 /* We need to use static variables to pass information around the call
584 to lang_for_each_input_file. Ick. */
586 static bfd_size_type need_size;
587 static bfd_size_type need_entries;
588 static bfd_byte *need_contents;
589 static bfd_byte *need_pinfo;
590 static bfd_byte *need_pnames;
592 /* The size of one entry in the .need section, not including the file
595 #define NEED_ENTRY_SIZE (16)
597 /* This is called after the sections have been attached to output
598 sections, but before any sizes or addresses have been set. */
601 gld${EMULATION_NAME}_before_allocation ()
603 struct bfd_link_hash_entry *hdyn = NULL;
608 /* The SunOS native linker creates a shared library whenever there
609 are any undefined symbols in a link, unless -e is used. This is
610 pretty weird, but we are compatible. */
611 if (! link_info.shared && ! link_info.relocateable && ! entry_from_cmdline)
613 struct bfd_link_hash_entry *h;
615 for (h = link_info.hash->undefs; h != NULL; h = h->next)
617 if (h->type == bfd_link_hash_undefined
618 && h->u.undef.abfd != NULL
619 && (h->u.undef.abfd->flags & DYNAMIC) == 0
620 && strcmp (h->root.string, "__DYNAMIC") != 0)
622 find_assign = h->root.string;
623 found_assign = false;
624 lang_for_each_statement (gld${EMULATION_NAME}_find_assignment);
627 link_info.shared = true;
634 if (link_info.shared)
636 lang_output_section_statement_type *os;
638 /* Set the .text section to start at 0x20, not 0x2020. FIXME:
639 This is too magical. */
640 os = lang_output_section_statement_lookup (".text");
641 if (os->addr_tree == NULL)
642 os->addr_tree = exp_intop (0x20);
645 /* We need to create a __DYNAMIC symbol. We don't do this in the
646 linker script because we want to set the value to the start of
647 the dynamic section if there is one, or to zero if there isn't
648 one. We need to create the symbol before calling
649 size_dynamic_sections, although we can't set the value until
651 if (! link_info.relocateable)
653 hdyn = bfd_link_hash_lookup (link_info.hash, "__DYNAMIC", true, false,
656 einfo ("%P%F: bfd_link_hash_lookup: %E\n");
657 if (! bfd_sunos_record_link_assignment (output_bfd, &link_info,
659 einfo ("%P%F: failed to record assignment to __DYNAMIC: %E\n");
662 /* If we are going to make any variable assignments, we need to let
663 the backend linker know about them in case the variables are
664 referred to by dynamic objects. */
665 lang_for_each_statement (gld${EMULATION_NAME}_find_assignment);
667 /* Let the backend linker work out the sizes of any sections
668 required by dynamic linking. */
669 if (! bfd_sunos_size_dynamic_sections (output_bfd, &link_info, &sdyn,
671 einfo ("%P%F: failed to set dynamic section sizes: %E\n");
675 /* Set up the .need section. See the description of the ld_need
676 field in include/aout/sun4.h. */
681 lang_for_each_input_file (gld${EMULATION_NAME}_count_need);
683 /* We should only have a .need section if we have at least one
685 ASSERT (need_entries != 0);
687 sneed->_raw_size = need_size;
688 sneed->contents = (bfd_byte *) xmalloc (need_size);
690 need_contents = sneed->contents;
691 need_pinfo = sneed->contents;
692 need_pnames = sneed->contents + need_entries * 16;
694 lang_for_each_input_file (gld${EMULATION_NAME}_set_need);
696 ASSERT ((bfd_size_type) (need_pnames - sneed->contents) == need_size);
701 /* Set up the .rules section. This is just a PATH like string
702 of the -L arguments given on the command line. We permit the
703 user to specify the directories using the -rpath command line
705 if (command_line.rpath)
707 srules->_raw_size = strlen (command_line.rpath);
708 srules->contents = (bfd_byte *) command_line.rpath;
713 search_dirs_type *search;
716 for (search = search_head; search != NULL; search = search->next)
718 size += strlen (search->name) + 1;
719 srules->_raw_size = size;
724 srules->contents = (bfd_byte *) xmalloc (size);
725 p = (char *) srules->contents;
727 for (search = search_head; search != NULL; search = search->next)
731 if (p != (char *) srules->contents)
733 strcpy (p, search->name);
741 /* We must assign a value to __DYNAMIC. It should be zero if we are
742 not doing a dynamic link, or the start of the .dynamic section if
744 if (! link_info.relocateable)
746 hdyn->type = bfd_link_hash_defined;
747 hdyn->u.def.value = 0;
749 hdyn->u.def.section = sdyn;
751 hdyn->u.def.section = bfd_abs_section_ptr;
755 /* This is called by the before_allocation routine via
756 lang_for_each_statement. It does one of two things: if the
757 variable find_assign is set, it sets found_assign if it finds an
758 assignment to that variable; otherwise it tells the backend linker
759 about all assignment statements, in case they are assignments to
760 symbols which are referred to by dynamic objects. */
763 gld${EMULATION_NAME}_find_assignment (s)
764 lang_statement_union_type *s;
766 if (s->header.type == lang_assignment_statement_enum
767 && (find_assign == NULL || ! found_assign))
768 gld${EMULATION_NAME}_find_exp_assignment (s->assignment_statement.exp);
771 /* Look through an expression for an assignment statement. */
774 gld${EMULATION_NAME}_find_exp_assignment (exp)
777 switch (exp->type.node_class)
780 if (find_assign != NULL)
782 if (strcmp (find_assign, exp->assign.dst) == 0)
787 if (strcmp (exp->assign.dst, ".") != 0)
789 if (! bfd_sunos_record_link_assignment (output_bfd, &link_info,
791 einfo ("%P%F: failed to record assignment to %s: %E\n",
794 gld${EMULATION_NAME}_find_exp_assignment (exp->assign.src);
798 gld${EMULATION_NAME}_find_exp_assignment (exp->binary.lhs);
799 gld${EMULATION_NAME}_find_exp_assignment (exp->binary.rhs);
803 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.lhs);
804 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.lhs);
805 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.rhs);
809 gld${EMULATION_NAME}_find_exp_assignment (exp->unary.child);
817 /* Work out the size of the .need section, and the number of entries.
818 The backend will set the ld_need field of the dynamic linking
819 information to point to the .need section. See include/aout/sun4.h
820 for more information. */
823 gld${EMULATION_NAME}_count_need (inp)
824 lang_input_statement_type *inp;
826 if (inp->the_bfd != NULL
827 && (inp->the_bfd->flags & DYNAMIC) != 0)
830 need_size += NEED_ENTRY_SIZE;
831 if (! inp->is_archive)
832 need_size += strlen (inp->filename) + 1;
835 ASSERT (inp->local_sym_name[0] == '-'
836 && inp->local_sym_name[1] == 'l');
837 need_size += strlen (inp->local_sym_name + 2) + 1;
842 /* Fill in the contents of the .need section. */
845 gld${EMULATION_NAME}_set_need (inp)
846 lang_input_statement_type *inp;
848 if (inp->the_bfd != NULL
849 && (inp->the_bfd->flags & DYNAMIC) != 0)
853 /* To really fill in the .need section contents, we need to know
854 the final file position of the section, but we don't.
855 Instead, we use offsets, and rely on the BFD backend to
856 finish the section up correctly. FIXME: Talk about lack of
857 referential locality. */
858 bfd_put_32 (output_bfd, need_pnames - need_contents, need_pinfo);
859 if (! inp->is_archive)
861 bfd_put_32 (output_bfd, (bfd_vma) 0, need_pinfo + 4);
862 bfd_put_16 (output_bfd, (bfd_vma) 0, need_pinfo + 8);
863 bfd_put_16 (output_bfd, (bfd_vma) 0, need_pinfo + 10);
864 strcpy (need_pnames, inp->filename);
871 bfd_put_32 (output_bfd, (bfd_vma) 0x80000000, need_pinfo + 4);
874 verstr = strstr (inp->filename, ".so.");
876 sscanf (verstr, ".so.%d.%d", &maj, &min);
877 bfd_put_16 (output_bfd, (bfd_vma) maj, need_pinfo + 8);
878 bfd_put_16 (output_bfd, (bfd_vma) min, need_pinfo + 10);
879 strcpy (need_pnames, inp->local_sym_name + 2);
882 c = (need_pinfo - need_contents) / NEED_ENTRY_SIZE;
883 if (c + 1 >= need_entries)
884 bfd_put_32 (output_bfd, (bfd_vma) 0, need_pinfo + 12);
886 bfd_put_32 (output_bfd, (bfd_vma) (c + 1) * NEED_ENTRY_SIZE,
889 need_pinfo += NEED_ENTRY_SIZE;
890 need_pnames += strlen (need_pnames) + 1;
895 gld${EMULATION_NAME}_get_script(isfile)
899 if test -n "$COMPILE_IN"
901 # Scripts compiled in.
903 # sed commands to quote an ld script as a C string.
910 cat >>e${EMULATION_NAME}.c <<EOF
914 if (link_info.relocateable == true && config.build_constructors == true)
915 return `sed "$sc" ldscripts/${EMULATION_NAME}.xu`;
916 else if (link_info.relocateable == true)
917 return `sed "$sc" ldscripts/${EMULATION_NAME}.xr`;
918 else if (!config.text_read_only)
919 return `sed "$sc" ldscripts/${EMULATION_NAME}.xbn`;
920 else if (!config.magic_demand_paged)
921 return `sed "$sc" ldscripts/${EMULATION_NAME}.xn`;
923 return `sed "$sc" ldscripts/${EMULATION_NAME}.x`;
928 # Scripts read from the filesystem.
930 cat >>e${EMULATION_NAME}.c <<EOF
934 if (link_info.relocateable == true && config.build_constructors == true)
935 return "ldscripts/${EMULATION_NAME}.xu";
936 else if (link_info.relocateable == true)
937 return "ldscripts/${EMULATION_NAME}.xr";
938 else if (!config.text_read_only)
939 return "ldscripts/${EMULATION_NAME}.xbn";
940 else if (!config.magic_demand_paged)
941 return "ldscripts/${EMULATION_NAME}.xn";
943 return "ldscripts/${EMULATION_NAME}.x";
949 cat >>e${EMULATION_NAME}.c <<EOF
951 struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
953 gld${EMULATION_NAME}_before_parse,
957 gld${EMULATION_NAME}_after_open,
958 after_allocation_default,
959 set_output_arch_default,
960 ldemul_default_target,
961 gld${EMULATION_NAME}_before_allocation,
962 gld${EMULATION_NAME}_get_script,
966 gld${EMULATION_NAME}_create_output_section_statements,
967 NULL /* open_dynamic_library */