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
621 && strcmp (h->root.string, "__GLOBAL_OFFSET_TABLE_") != 0)
623 find_assign = h->root.string;
624 found_assign = false;
625 lang_for_each_statement (gld${EMULATION_NAME}_find_assignment);
628 link_info.shared = true;
635 if (link_info.shared)
637 lang_output_section_statement_type *os;
639 /* Set the .text section to start at 0x20, not 0x2020. FIXME:
640 This is too magical. */
641 os = lang_output_section_statement_lookup (".text");
642 if (os->addr_tree == NULL)
643 os->addr_tree = exp_intop (0x20);
646 /* We need to create a __DYNAMIC symbol. We don't do this in the
647 linker script because we want to set the value to the start of
648 the dynamic section if there is one, or to zero if there isn't
649 one. We need to create the symbol before calling
650 size_dynamic_sections, although we can't set the value until
652 if (! link_info.relocateable)
654 hdyn = bfd_link_hash_lookup (link_info.hash, "__DYNAMIC", true, false,
657 einfo ("%P%F: bfd_link_hash_lookup: %E\n");
658 if (! bfd_sunos_record_link_assignment (output_bfd, &link_info,
660 einfo ("%P%F: failed to record assignment to __DYNAMIC: %E\n");
663 /* If we are going to make any variable assignments, we need to let
664 the backend linker know about them in case the variables are
665 referred to by dynamic objects. */
666 lang_for_each_statement (gld${EMULATION_NAME}_find_assignment);
668 /* Let the backend linker work out the sizes of any sections
669 required by dynamic linking. */
670 if (! bfd_sunos_size_dynamic_sections (output_bfd, &link_info, &sdyn,
672 einfo ("%P%F: failed to set dynamic section sizes: %E\n");
676 /* Set up the .need section. See the description of the ld_need
677 field in include/aout/sun4.h. */
682 lang_for_each_input_file (gld${EMULATION_NAME}_count_need);
684 /* We should only have a .need section if we have at least one
686 ASSERT (need_entries != 0);
688 sneed->_raw_size = need_size;
689 sneed->contents = (bfd_byte *) xmalloc (need_size);
691 need_contents = sneed->contents;
692 need_pinfo = sneed->contents;
693 need_pnames = sneed->contents + need_entries * 16;
695 lang_for_each_input_file (gld${EMULATION_NAME}_set_need);
697 ASSERT ((bfd_size_type) (need_pnames - sneed->contents) == need_size);
702 /* Set up the .rules section. This is just a PATH like string
703 of the -L arguments given on the command line. We permit the
704 user to specify the directories using the -rpath command line
706 if (command_line.rpath)
708 srules->_raw_size = strlen (command_line.rpath);
709 srules->contents = (bfd_byte *) command_line.rpath;
714 search_dirs_type *search;
717 for (search = search_head; search != NULL; search = search->next)
719 size += strlen (search->name) + 1;
720 srules->_raw_size = size;
725 srules->contents = (bfd_byte *) xmalloc (size);
726 p = (char *) srules->contents;
728 for (search = search_head; search != NULL; search = search->next)
732 if (p != (char *) srules->contents)
734 strcpy (p, search->name);
742 /* We must assign a value to __DYNAMIC. It should be zero if we are
743 not doing a dynamic link, or the start of the .dynamic section if
745 if (! link_info.relocateable)
747 hdyn->type = bfd_link_hash_defined;
748 hdyn->u.def.value = 0;
750 hdyn->u.def.section = sdyn;
752 hdyn->u.def.section = bfd_abs_section_ptr;
756 /* This is called by the before_allocation routine via
757 lang_for_each_statement. It does one of two things: if the
758 variable find_assign is set, it sets found_assign if it finds an
759 assignment to that variable; otherwise it tells the backend linker
760 about all assignment statements, in case they are assignments to
761 symbols which are referred to by dynamic objects. */
764 gld${EMULATION_NAME}_find_assignment (s)
765 lang_statement_union_type *s;
767 if (s->header.type == lang_assignment_statement_enum
768 && (find_assign == NULL || ! found_assign))
769 gld${EMULATION_NAME}_find_exp_assignment (s->assignment_statement.exp);
772 /* Look through an expression for an assignment statement. */
775 gld${EMULATION_NAME}_find_exp_assignment (exp)
778 switch (exp->type.node_class)
781 if (find_assign != NULL)
783 if (strcmp (find_assign, exp->assign.dst) == 0)
788 if (strcmp (exp->assign.dst, ".") != 0)
790 if (! bfd_sunos_record_link_assignment (output_bfd, &link_info,
792 einfo ("%P%F: failed to record assignment to %s: %E\n",
795 gld${EMULATION_NAME}_find_exp_assignment (exp->assign.src);
799 gld${EMULATION_NAME}_find_exp_assignment (exp->binary.lhs);
800 gld${EMULATION_NAME}_find_exp_assignment (exp->binary.rhs);
804 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.cond);
805 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.lhs);
806 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.rhs);
810 gld${EMULATION_NAME}_find_exp_assignment (exp->unary.child);
818 /* Work out the size of the .need section, and the number of entries.
819 The backend will set the ld_need field of the dynamic linking
820 information to point to the .need section. See include/aout/sun4.h
821 for more information. */
824 gld${EMULATION_NAME}_count_need (inp)
825 lang_input_statement_type *inp;
827 if (inp->the_bfd != NULL
828 && (inp->the_bfd->flags & DYNAMIC) != 0)
831 need_size += NEED_ENTRY_SIZE;
832 if (! inp->is_archive)
833 need_size += strlen (inp->filename) + 1;
836 ASSERT (inp->local_sym_name[0] == '-'
837 && inp->local_sym_name[1] == 'l');
838 need_size += strlen (inp->local_sym_name + 2) + 1;
843 /* Fill in the contents of the .need section. */
846 gld${EMULATION_NAME}_set_need (inp)
847 lang_input_statement_type *inp;
849 if (inp->the_bfd != NULL
850 && (inp->the_bfd->flags & DYNAMIC) != 0)
854 /* To really fill in the .need section contents, we need to know
855 the final file position of the section, but we don't.
856 Instead, we use offsets, and rely on the BFD backend to
857 finish the section up correctly. FIXME: Talk about lack of
858 referential locality. */
859 bfd_put_32 (output_bfd, need_pnames - need_contents, need_pinfo);
860 if (! inp->is_archive)
862 bfd_put_32 (output_bfd, (bfd_vma) 0, need_pinfo + 4);
863 bfd_put_16 (output_bfd, (bfd_vma) 0, need_pinfo + 8);
864 bfd_put_16 (output_bfd, (bfd_vma) 0, need_pinfo + 10);
865 strcpy (need_pnames, inp->filename);
872 bfd_put_32 (output_bfd, (bfd_vma) 0x80000000, need_pinfo + 4);
875 verstr = strstr (inp->filename, ".so.");
877 sscanf (verstr, ".so.%d.%d", &maj, &min);
878 bfd_put_16 (output_bfd, (bfd_vma) maj, need_pinfo + 8);
879 bfd_put_16 (output_bfd, (bfd_vma) min, need_pinfo + 10);
880 strcpy (need_pnames, inp->local_sym_name + 2);
883 c = (need_pinfo - need_contents) / NEED_ENTRY_SIZE;
884 if (c + 1 >= need_entries)
885 bfd_put_32 (output_bfd, (bfd_vma) 0, need_pinfo + 12);
887 bfd_put_32 (output_bfd, (bfd_vma) (c + 1) * NEED_ENTRY_SIZE,
890 need_pinfo += NEED_ENTRY_SIZE;
891 need_pnames += strlen (need_pnames) + 1;
896 gld${EMULATION_NAME}_get_script(isfile)
900 if test -n "$COMPILE_IN"
902 # Scripts compiled in.
904 # sed commands to quote an ld script as a C string.
911 cat >>e${EMULATION_NAME}.c <<EOF
915 if (link_info.relocateable == true && config.build_constructors == true)
916 return `sed "$sc" ldscripts/${EMULATION_NAME}.xu`;
917 else if (link_info.relocateable == true)
918 return `sed "$sc" ldscripts/${EMULATION_NAME}.xr`;
919 else if (!config.text_read_only)
920 return `sed "$sc" ldscripts/${EMULATION_NAME}.xbn`;
921 else if (!config.magic_demand_paged)
922 return `sed "$sc" ldscripts/${EMULATION_NAME}.xn`;
924 return `sed "$sc" ldscripts/${EMULATION_NAME}.x`;
929 # Scripts read from the filesystem.
931 cat >>e${EMULATION_NAME}.c <<EOF
935 if (link_info.relocateable == true && config.build_constructors == true)
936 return "ldscripts/${EMULATION_NAME}.xu";
937 else if (link_info.relocateable == true)
938 return "ldscripts/${EMULATION_NAME}.xr";
939 else if (!config.text_read_only)
940 return "ldscripts/${EMULATION_NAME}.xbn";
941 else if (!config.magic_demand_paged)
942 return "ldscripts/${EMULATION_NAME}.xn";
944 return "ldscripts/${EMULATION_NAME}.x";
950 cat >>e${EMULATION_NAME}.c <<EOF
952 struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
954 gld${EMULATION_NAME}_before_parse,
958 gld${EMULATION_NAME}_after_open,
959 after_allocation_default,
960 set_output_arch_default,
961 ldemul_default_target,
962 gld${EMULATION_NAME}_before_allocation,
963 gld${EMULATION_NAME}_get_script,
967 gld${EMULATION_NAME}_create_output_section_statements,
968 NULL /* open_dynamic_library */