Correct spelling of "relocatable".
[external/binutils.git] / ld / emultempl / sunos.em
1 # This shell script emits a C file. -*- C -*-
2 # It does some substitutions.
3 if [ -z "$MACHINE" ]; then
4   OUTPUT_ARCH=${ARCH}
5 else
6   OUTPUT_ARCH=${ARCH}:${MACHINE}
7 fi
8 cat >e${EMULATION_NAME}.c <<EOF
9 /* This file is is generated by a shell script.  DO NOT EDIT! */
10
11 /* SunOS emulation code for ${EMULATION_NAME}
12    Copyright 1991, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2002, 2003
13    Free Software Foundation, Inc.
14    Written by Steve Chamberlain <sac@cygnus.com>
15    SunOS shared library support by Ian Lance Taylor <ian@cygnus.com>
16
17 This file is part of GLD, the Gnu Linker.
18
19 This program is free software; you can redistribute it and/or modify
20 it under the terms of the GNU General Public License as published by
21 the Free Software Foundation; either version 2 of the License, or
22 (at your option) any later version.
23
24 This program is distributed in the hope that it will be useful,
25 but WITHOUT ANY WARRANTY; without even the implied warranty of
26 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27 GNU General Public License for more details.
28
29 You should have received a copy of the GNU General Public License
30 along with this program; if not, write to the Free Software
31 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
32
33 #define TARGET_IS_${EMULATION_NAME}
34
35 #include "bfd.h"
36 #include "sysdep.h"
37 #include "bfdlink.h"
38 #include "libiberty.h"
39 #include "safe-ctype.h"
40
41 #include "ld.h"
42 #include "ldmain.h"
43 #include "ldmisc.h"
44 #include "ldexp.h"
45 #include "ldlang.h"
46 #include "ldfile.h"
47 #include "ldemul.h"
48
49 #ifdef HAVE_DIRENT_H
50 # include <dirent.h>
51 #else
52 # define dirent direct
53 # ifdef HAVE_SYS_NDIR_H
54 #  include <sys/ndir.h>
55 # endif
56 # ifdef HAVE_SYS_DIR_H
57 #  include <sys/dir.h>
58 # endif
59 # ifdef HAVE_NDIR_H
60 #  include <ndir.h>
61 # endif
62 #endif
63
64 static void gld${EMULATION_NAME}_before_parse PARAMS ((void));
65 static void gld${EMULATION_NAME}_set_symbols PARAMS ((void));
66 static void gld${EMULATION_NAME}_create_output_section_statements
67   PARAMS ((void));
68 static void gld${EMULATION_NAME}_find_so
69   PARAMS ((lang_input_statement_type *));
70 static char *gld${EMULATION_NAME}_search_dir
71   PARAMS ((const char *, const char *, bfd_boolean *));
72 static void gld${EMULATION_NAME}_after_open PARAMS ((void));
73 static void gld${EMULATION_NAME}_check_needed
74   PARAMS ((lang_input_statement_type *));
75 static bfd_boolean gld${EMULATION_NAME}_search_needed
76   PARAMS ((const char *, const char *));
77 static bfd_boolean gld${EMULATION_NAME}_try_needed
78   PARAMS ((const char *, const char *));
79 static void gld${EMULATION_NAME}_before_allocation PARAMS ((void));
80 static void gld${EMULATION_NAME}_find_assignment
81   PARAMS ((lang_statement_union_type *));
82 static void gld${EMULATION_NAME}_find_exp_assignment PARAMS ((etree_type *));
83 static void gld${EMULATION_NAME}_count_need
84   PARAMS ((lang_input_statement_type *));
85 static void gld${EMULATION_NAME}_set_need
86   PARAMS ((lang_input_statement_type *));
87 static char *gld${EMULATION_NAME}_get_script PARAMS ((int *isfile));
88
89 static void
90 gld${EMULATION_NAME}_before_parse()
91 {
92   const bfd_arch_info_type *arch = bfd_scan_arch ("${OUTPUT_ARCH}");
93   if (arch)
94     {
95       ldfile_output_architecture = arch->arch;
96       ldfile_output_machine = arch->mach;
97       ldfile_output_machine_name = arch->printable_name;
98     }
99   else
100     ldfile_output_architecture = bfd_arch_${ARCH};
101   config.dynamic_link = TRUE;
102   config.has_shared = TRUE;
103 }
104
105 /* This is called after the command line arguments have been parsed,
106    but before the linker script has been read.  If this is a native
107    linker, we add the directories in LD_LIBRARY_PATH to the search
108    list.  */
109
110 static void
111 gld${EMULATION_NAME}_set_symbols ()
112 {
113 EOF
114 if [ "x${host}" = "x${target}" ] ; then
115   case " ${EMULATION_LIBPATH} " in
116   *" ${EMULATION_NAME} "*)
117 cat >>e${EMULATION_NAME}.c <<EOF
118   const char *env;
119
120   env = (const char *) getenv ("LD_LIBRARY_PATH");
121   if (env != NULL)
122     {
123       char *l;
124
125       l = xstrdup (env);
126       while (1)
127         {
128           char *c;
129
130           c = strchr (l, ':');
131           if (c != NULL)
132             *c++ = '\0';
133           if (*l != '\0')
134             ldfile_add_library_path (l, FALSE);
135           if (c == NULL)
136             break;
137           l = c;
138         }
139     }
140 EOF
141   ;;
142   esac
143 fi
144 cat >>e${EMULATION_NAME}.c <<EOF
145 }
146
147 /* Despite the name, we use this routine to search for dynamic
148    libraries.  On SunOS this requires a directory search.  We need to
149    find the .so file with the highest version number.  The user may
150    restrict the major version by saying, e.g., -lc.1.  Also, if we
151    find a .so file, we need to look for a the same file after
152    replacing .so with .sa; if it exists, it will be an archive which
153    provide some initializations for data symbols, and we need to
154    search it after including the .so file.  */
155
156 static void
157 gld${EMULATION_NAME}_create_output_section_statements ()
158 {
159   lang_for_each_input_file (gld${EMULATION_NAME}_find_so);
160 }
161
162 /* Search the directory for a .so file for each library search.  */
163
164 static void
165 gld${EMULATION_NAME}_find_so (inp)
166      lang_input_statement_type *inp;
167 {
168   search_dirs_type *search;
169   char *found = NULL;
170   char *alc;
171   struct stat st;
172
173   if (! inp->search_dirs_flag
174       || ! inp->is_archive
175       || ! inp->dynamic)
176     return;
177
178   ASSERT (strncmp (inp->local_sym_name, "-l", 2) == 0);
179
180   for (search = search_head; search != NULL; search = search->next)
181     {
182       bfd_boolean found_static;
183
184       found = gld${EMULATION_NAME}_search_dir (search->name, inp->filename,
185                                                &found_static);
186       if (found != NULL || found_static)
187         break;
188     }
189
190   if (found == NULL)
191     {
192       /* We did not find a matching .so file.  This isn't an error,
193          since there might still be a matching .a file, which will be
194          found by the usual search.  */
195       return;
196     }
197
198   /* Replace the filename with the one we have found.  */
199   alc = (char *) xmalloc (strlen (search->name) + strlen (found) + 2);
200   sprintf (alc, "%s/%s", search->name, found);
201   inp->filename = alc;
202
203   /* Turn off the search_dirs_flag to prevent ldfile_open_file from
204      searching for this file again.  */
205   inp->search_dirs_flag = FALSE;
206
207   free (found);
208
209   /* Now look for the same file name, but with .sa instead of .so.  If
210      found, add it to the list of input files.  */
211   alc = (char *) xmalloc (strlen (inp->filename) + 1);
212   strcpy (alc, inp->filename);
213   strstr (alc + strlen (search->name), ".so")[2] = 'a';
214   if (stat (alc, &st) != 0)
215     free (alc);
216   else
217     {
218       lang_input_statement_type *sa;
219
220       /* Add the .sa file to the statement list just before the .so
221          file.  This is really a hack.  */
222       sa = ((lang_input_statement_type *)
223             xmalloc (sizeof (lang_input_statement_type)));
224       *sa = *inp;
225
226       inp->filename = alc;
227       inp->local_sym_name = alc;
228
229       inp->header.next = (lang_statement_union_type *) sa;
230       inp->next_real_file = (lang_statement_union_type *) sa;
231     }
232 }
233
234 /* Search a directory for a .so file.  */
235
236 static char *
237 gld${EMULATION_NAME}_search_dir (dirname, filename, found_static)
238      const char *dirname;
239      const char *filename;
240      bfd_boolean *found_static;
241 {
242   int force_maj, force_min;
243   const char *dot;
244   unsigned int len;
245   char *alc;
246   char *found;
247   int max_maj, max_min;
248   DIR *dir;
249   struct dirent *entry;
250   unsigned int dirnamelen;
251   char *full_path;
252   int statval;
253   struct stat st;
254
255   *found_static = FALSE;
256
257   force_maj = -1;
258   force_min = -1;
259   dot = strchr (filename, '.');
260   if (dot == NULL)
261     {
262       len = strlen (filename);
263       alc = NULL;
264     }
265   else
266     {
267       force_maj = atoi (dot + 1);
268
269       len = dot - filename;
270       alc = (char *) xmalloc (len + 1);
271       strncpy (alc, filename, len);
272       alc[len] = '\0';
273       filename = alc;
274
275       dot = strchr (dot + 1, '.');
276       if (dot != NULL)
277         force_min = atoi (dot + 1);
278     }
279
280   found = NULL;
281   max_maj = max_min = 0;
282
283   dir = opendir (dirname);
284   if (dir == NULL)
285     return NULL;
286   dirnamelen = strlen (dirname);
287
288   while ((entry = readdir (dir)) != NULL)
289     {
290       const char *s;
291       int found_maj, found_min;
292
293       if (strncmp (entry->d_name, "lib", 3) != 0
294           || strncmp (entry->d_name + 3, filename, len) != 0)
295         continue;
296
297       if (dot == NULL
298           && strcmp (entry->d_name + 3 + len, ".a") == 0)
299         {
300           *found_static = TRUE;
301           continue;
302         }
303
304       /* We accept libfoo.so without a version number, even though the
305          native linker does not.  This is more convenient for packages
306          which just generate .so files for shared libraries, as on ELF
307          systems.  */
308       if (strncmp (entry->d_name + 3 + len, ".so", 3) != 0)
309         continue;
310       if (entry->d_name[6 + len] == '\0')
311         ;
312       else if (entry->d_name[6 + len] == '.'
313                && ISDIGIT (entry->d_name[7 + len]))
314         ;
315       else
316         continue;
317
318       for (s = entry->d_name + 6 + len; *s != '\0'; s++)
319         if (*s != '.' && ! ISDIGIT (*s))
320           break;
321       if (*s != '\0')
322         continue;
323
324       /* We've found a .so file.  Work out the major and minor
325          version numbers.  */
326       found_maj = 0;
327       found_min = 0;
328       sscanf (entry->d_name + 3 + len, ".so.%d.%d",
329               &found_maj, &found_min);
330
331       if ((force_maj != -1 && force_maj != found_maj)
332           || (force_min != -1 && force_min != found_min))
333         continue;
334
335       /* Make sure the file really exists (ignore broken symlinks).  */
336       full_path = xmalloc (dirnamelen + 1 + strlen (entry->d_name) + 1);
337       sprintf (full_path, "%s/%s", dirname, entry->d_name);
338       statval = stat (full_path, &st);
339       free (full_path);
340       if (statval != 0)
341         continue;
342
343       /* We've found a match for the name we are searching for.  See
344          if this is the version we should use.  If the major and minor
345          versions match, we use the last entry in alphabetical order;
346          I don't know if this is how SunOS distinguishes libc.so.1.8
347          from libc.so.1.8.1, but it ought to suffice.  */
348       if (found == NULL
349           || (found_maj > max_maj)
350           || (found_maj == max_maj
351               && (found_min > max_min
352                   || (found_min == max_min
353                       && strcmp (entry->d_name, found) > 0))))
354         {
355           if (found != NULL)
356             free (found);
357           found = (char *) xmalloc (strlen (entry->d_name) + 1);
358           strcpy (found, entry->d_name);
359           max_maj = found_maj;
360           max_min = found_min;
361         }
362     }
363
364   closedir (dir);
365
366   if (alc != NULL)
367     free (alc);
368
369   return found;
370 }
371
372 /* These variables are required to pass information back and forth
373    between after_open and check_needed.  */
374
375 static struct bfd_link_needed_list *global_needed;
376 static bfd_boolean global_found;
377
378 /* This is called after all the input files have been opened.  */
379
380 static void
381 gld${EMULATION_NAME}_after_open ()
382 {
383   struct bfd_link_needed_list *needed, *l;
384
385   /* We only need to worry about this when doing a final link.  */
386   if (link_info.relocatable || link_info.shared)
387     return;
388
389   /* Get the list of files which appear in ld_need entries in dynamic
390      objects included in the link.  For each such file, we want to
391      track down the corresponding library, and include the symbol
392      table in the link.  This is what the runtime dynamic linker will
393      do.  Tracking the files down here permits one dynamic object to
394      include another without requiring special action by the person
395      doing the link.  Note that the needed list can actually grow
396      while we are stepping through this loop.  */
397   needed = bfd_sunos_get_needed_list (output_bfd, &link_info);
398   for (l = needed; l != NULL; l = l->next)
399     {
400       struct bfd_link_needed_list *ll;
401       const char *lname;
402       search_dirs_type *search;
403
404       lname = l->name;
405
406       /* If we've already seen this file, skip it.  */
407       for (ll = needed; ll != l; ll = ll->next)
408         if (strcmp (ll->name, lname) == 0)
409           break;
410       if (ll != l)
411         continue;
412
413       /* See if this file was included in the link explicitly.  */
414       global_needed = l;
415       global_found = FALSE;
416       lang_for_each_input_file (gld${EMULATION_NAME}_check_needed);
417       if (global_found)
418         continue;
419
420       if (strncmp (lname, "-l", 2) != 0)
421         {
422           bfd *abfd;
423
424           abfd = bfd_openr (lname, bfd_get_target (output_bfd));
425           if (abfd != NULL)
426             {
427               if (! bfd_check_format (abfd, bfd_object))
428                 {
429                   (void) bfd_close (abfd);
430                   abfd = NULL;
431                 }
432             }
433           if (abfd != NULL)
434             {
435               if ((bfd_get_file_flags (abfd) & DYNAMIC) == 0)
436                 {
437                   (void) bfd_close (abfd);
438                   abfd = NULL;
439                 }
440             }
441           if (abfd != NULL)
442             {
443               /* We've found the needed dynamic object.  */
444               if (! bfd_link_add_symbols (abfd, &link_info))
445                 einfo ("%F%B: could not read symbols: %E\n", abfd);
446             }
447           else
448             {
449               einfo ("%P: warning: %s, needed by %B, not found\n",
450                      lname, l->by);
451             }
452
453           continue;
454         }
455
456       lname += 2;
457
458       /* We want to search for the file in the same way that the
459          dynamic linker will search.  That means that we want to use
460          rpath_link, rpath or -L, then the environment variable
461          LD_LIBRARY_PATH (native only), then (if rpath was used) the
462          linker script LIB_SEARCH_DIRS.  */
463       if (gld${EMULATION_NAME}_search_needed (command_line.rpath_link,
464                                               lname))
465         continue;
466       if (command_line.rpath != NULL)
467         {
468           if (gld${EMULATION_NAME}_search_needed (command_line.rpath, lname))
469             continue;
470         }
471       else
472         {
473           for (search = search_head; search != NULL; search = search->next)
474             if (gld${EMULATION_NAME}_try_needed (search->name, lname))
475               break;
476           if (search != NULL)
477             continue;
478         }
479 EOF
480 if [ "x${host}" = "x${target}" ] ; then
481   case " ${EMULATION_LIBPATH} " in
482   *" ${EMULATION_NAME} "*)
483 cat >>e${EMULATION_NAME}.c <<EOF
484       {
485         const char *lib_path;
486
487         lib_path = (const char *) getenv ("LD_LIBRARY_PATH");
488         if (gld${EMULATION_NAME}_search_needed (lib_path, lname))
489           continue;
490       }
491 EOF
492   ;;
493   esac
494 fi
495 cat >>e${EMULATION_NAME}.c <<EOF
496       if (command_line.rpath != NULL)
497         {
498           for (search = search_head; search != NULL; search = search->next)
499             {
500               if (search->cmdline)
501                 continue;
502               if (gld${EMULATION_NAME}_try_needed (search->name, lname))
503                 break;
504             }
505           if (search != NULL)
506             continue;
507         }
508
509       einfo ("%P: warning: %s, needed by %B, not found\n",
510              l->name, l->by);
511     }
512 }
513
514 /* Search for a needed file in a path.  */
515
516 static bfd_boolean
517 gld${EMULATION_NAME}_search_needed (path, name)
518      const char *path;
519      const char *name;
520 {
521   const char *s;
522
523   if (path == NULL || *path == '\0')
524     return FALSE;
525   while (1)
526     {
527       const char *dir;
528       char *dircopy;
529
530       s = strchr (path, ':');
531       if (s == NULL)
532         {
533           dircopy = NULL;
534           dir = path;
535         }
536       else
537         {
538           dircopy = (char *) xmalloc (s - path + 1);
539           memcpy (dircopy, path, s - path);
540           dircopy[s - path] = '\0';
541           dir = dircopy;
542         }
543
544       if (gld${EMULATION_NAME}_try_needed (dir, name))
545         return TRUE;
546
547       if (dircopy != NULL)
548         free (dircopy);
549
550       if (s == NULL)
551         break;
552       path = s + 1;
553     }
554
555   return FALSE;
556 }
557
558 /* This function is called for each possible directory for a needed
559    dynamic object.  */
560
561 static bfd_boolean
562 gld${EMULATION_NAME}_try_needed (dir, name)
563      const char *dir;
564      const char *name;
565 {
566   char *file;
567   char *alc;
568   bfd_boolean ignore;
569   bfd *abfd;
570
571   file = gld${EMULATION_NAME}_search_dir (dir, name, &ignore);
572   if (file == NULL)
573     return FALSE;
574
575   alc = (char *) xmalloc (strlen (dir) + strlen (file) + 2);
576   sprintf (alc, "%s/%s", dir, file);
577   free (file);
578   abfd = bfd_openr (alc, bfd_get_target (output_bfd));
579   if (abfd == NULL)
580     return FALSE;
581   if (! bfd_check_format (abfd, bfd_object))
582     {
583       (void) bfd_close (abfd);
584       return FALSE;
585     }
586   if ((bfd_get_file_flags (abfd) & DYNAMIC) == 0)
587     {
588       (void) bfd_close (abfd);
589       return FALSE;
590     }
591
592   /* We've found the needed dynamic object.  */
593
594   /* Add this file into the symbol table.  */
595   if (! bfd_link_add_symbols (abfd, &link_info))
596     einfo ("%F%B: could not read symbols: %E\n", abfd);
597
598   return TRUE;
599 }
600
601 /* See if we have already included a needed object in the link.  This
602    does not have to be precise, as it does no harm to include a
603    dynamic object more than once.  */
604
605 static void
606 gld${EMULATION_NAME}_check_needed (s)
607      lang_input_statement_type *s;
608 {
609   if (s->filename == NULL)
610     return;
611   if (strncmp (global_needed->name, "-l", 2) != 0)
612     {
613       if (strcmp (s->filename, global_needed->name) == 0)
614         global_found = TRUE;
615     }
616   else
617     {
618       const char *sname, *lname;
619       const char *sdot, *ldot;
620       int lmaj, lmin, smaj, smin;
621
622       lname = global_needed->name + 2;
623
624       sname = strrchr (s->filename, '/');
625       if (sname == NULL)
626         sname = s->filename;
627       else
628         ++sname;
629
630       if (strncmp (sname, "lib", 3) != 0)
631         return;
632       sname += 3;
633
634       ldot = strchr (lname, '.');
635       if (ldot == NULL)
636         ldot = lname + strlen (lname);
637
638       sdot = strstr (sname, ".so.");
639       if (sdot == NULL)
640         return;
641
642       if (sdot - sname != ldot - lname
643           || strncmp (lname, sname, sdot - sname) != 0)
644         return;
645
646       lmaj = lmin = -1;
647       sscanf (ldot, ".%d.%d", &lmaj, &lmin);
648       smaj = smin = -1;
649       sscanf (sdot, ".so.%d.%d", &smaj, &smin);
650       if ((smaj != lmaj && smaj != -1 && lmaj != -1)
651           || (smin != lmin && smin != -1 && lmin != -1))
652         return;
653
654       global_found = TRUE;
655     }
656 }
657
658 /* We need to use static variables to pass information around the call
659    to lang_for_each_statement.  Ick.  */
660
661 static const char *find_assign;
662 static bfd_boolean found_assign;
663
664 /* We need to use static variables to pass information around the call
665    to lang_for_each_input_file.  Ick.  */
666
667 static bfd_size_type need_size;
668 static bfd_size_type need_entries;
669 static bfd_byte *need_contents;
670 static bfd_byte *need_pinfo;
671 static bfd_byte *need_pnames;
672
673 /* The size of one entry in the .need section, not including the file
674    name.  */
675
676 #define NEED_ENTRY_SIZE (16)
677
678 /* This is called after the sections have been attached to output
679    sections, but before any sizes or addresses have been set.  */
680
681 static void
682 gld${EMULATION_NAME}_before_allocation ()
683 {
684   struct bfd_link_hash_entry *hdyn = NULL;
685   asection *sneed;
686   asection *srules;
687   asection *sdyn;
688
689   /* The SunOS native linker creates a shared library whenever there
690      are any undefined symbols in a link, unless -e is used.  This is
691      pretty weird, but we are compatible.  */
692   if (! link_info.shared && ! link_info.relocatable && ! entry_from_cmdline)
693     {
694       struct bfd_link_hash_entry *h;
695
696       for (h = link_info.hash->undefs; h != NULL; h = h->next)
697         {
698           if (h->type == bfd_link_hash_undefined
699               && h->u.undef.abfd != NULL
700               && (h->u.undef.abfd->flags & DYNAMIC) == 0
701               && strcmp (h->root.string, "__DYNAMIC") != 0
702               && strcmp (h->root.string, "__GLOBAL_OFFSET_TABLE_") != 0)
703             {
704               find_assign = h->root.string;
705               found_assign = FALSE;
706               lang_for_each_statement (gld${EMULATION_NAME}_find_assignment);
707               if (! found_assign)
708                 {
709                   link_info.shared = TRUE;
710                   break;
711                 }
712             }
713         }
714     }
715
716   if (link_info.shared)
717     {
718       lang_output_section_statement_type *os;
719
720       /* Set the .text section to start at 0x20, not 0x2020.  FIXME:
721          This is too magical.  */
722       os = lang_output_section_statement_lookup (".text");
723       if (os->addr_tree == NULL)
724         os->addr_tree = exp_intop (0x20);
725     }
726
727   /* We need to create a __DYNAMIC symbol.  We don't do this in the
728      linker script because we want to set the value to the start of
729      the dynamic section if there is one, or to zero if there isn't
730      one.  We need to create the symbol before calling
731      size_dynamic_sections, although we can't set the value until
732      afterward.  */
733   if (! link_info.relocatable)
734     {
735       hdyn = bfd_link_hash_lookup (link_info.hash, "__DYNAMIC", TRUE, FALSE,
736                                    FALSE);
737       if (hdyn == NULL)
738         einfo ("%P%F: bfd_link_hash_lookup: %E\n");
739       if (! bfd_sunos_record_link_assignment (output_bfd, &link_info,
740                                               "__DYNAMIC"))
741         einfo ("%P%F: failed to record assignment to __DYNAMIC: %E\n");
742     }
743
744   /* If we are going to make any variable assignments, we need to let
745      the backend linker know about them in case the variables are
746      referred to by dynamic objects.  */
747   lang_for_each_statement (gld${EMULATION_NAME}_find_assignment);
748
749   /* Let the backend linker work out the sizes of any sections
750      required by dynamic linking.  */
751   if (! bfd_sunos_size_dynamic_sections (output_bfd, &link_info, &sdyn,
752                                          &sneed, &srules))
753     einfo ("%P%F: failed to set dynamic section sizes: %E\n");
754
755   if (sneed != NULL)
756     {
757       /* Set up the .need section.  See the description of the ld_need
758          field in include/aout/sun4.h.  */
759
760       need_entries = 0;
761       need_size = 0;
762
763       lang_for_each_input_file (gld${EMULATION_NAME}_count_need);
764
765       /* We should only have a .need section if we have at least one
766          dynamic object.  */
767       ASSERT (need_entries != 0);
768
769       sneed->_raw_size = need_size;
770       sneed->contents = (bfd_byte *) xmalloc (need_size);
771
772       need_contents = sneed->contents;
773       need_pinfo = sneed->contents;
774       need_pnames = sneed->contents + need_entries * 16;
775
776       lang_for_each_input_file (gld${EMULATION_NAME}_set_need);
777
778       ASSERT ((bfd_size_type) (need_pnames - sneed->contents) == need_size);
779     }
780
781   if (srules != NULL)
782     {
783       /* Set up the .rules section.  This is just a PATH like string
784          of the -L arguments given on the command line.  We permit the
785          user to specify the directories using the -rpath command line
786          option.  */
787       if (command_line.rpath)
788         {
789           srules->_raw_size = strlen (command_line.rpath);
790           srules->contents = (bfd_byte *) command_line.rpath;
791         }
792       else
793         {
794           unsigned int size;
795           search_dirs_type *search;
796
797           size = 0;
798           for (search = search_head; search != NULL; search = search->next)
799             if (search->cmdline)
800               size += strlen (search->name) + 1;
801           srules->_raw_size = size;
802           if (size > 0)
803             {
804               char *p;
805
806               srules->contents = (bfd_byte *) xmalloc (size);
807               p = (char *) srules->contents;
808               *p = '\0';
809               for (search = search_head; search != NULL; search = search->next)
810                 {
811                   if (search->cmdline)
812                     {
813                       if (p != (char *) srules->contents)
814                         *p++ = ':';
815                       strcpy (p, search->name);
816                       p += strlen (p);
817                     }
818                 }
819             }
820         }
821     }
822
823   /* We must assign a value to __DYNAMIC.  It should be zero if we are
824      not doing a dynamic link, or the start of the .dynamic section if
825      we are doing one.  */
826   if (! link_info.relocatable)
827     {
828       hdyn->type = bfd_link_hash_defined;
829       hdyn->u.def.value = 0;
830       if (sdyn != NULL)
831         hdyn->u.def.section = sdyn;
832       else
833         hdyn->u.def.section = bfd_abs_section_ptr;
834     }
835 }
836
837 /* This is called by the before_allocation routine via
838    lang_for_each_statement.  It does one of two things: if the
839    variable find_assign is set, it sets found_assign if it finds an
840    assignment to that variable; otherwise it tells the backend linker
841    about all assignment statements, in case they are assignments to
842    symbols which are referred to by dynamic objects.  */
843
844 static void
845 gld${EMULATION_NAME}_find_assignment (s)
846      lang_statement_union_type *s;
847 {
848   if (s->header.type == lang_assignment_statement_enum
849       && (find_assign == NULL || ! found_assign))
850     gld${EMULATION_NAME}_find_exp_assignment (s->assignment_statement.exp);
851 }
852
853 /* Look through an expression for an assignment statement.  */
854
855 static void
856 gld${EMULATION_NAME}_find_exp_assignment (exp)
857      etree_type *exp;
858 {
859   switch (exp->type.node_class)
860     {
861     case etree_assign:
862       if (find_assign != NULL)
863         {
864           if (strcmp (find_assign, exp->assign.dst) == 0)
865             found_assign = TRUE;
866           return;
867         }
868
869       if (strcmp (exp->assign.dst, ".") != 0)
870         {
871           if (! bfd_sunos_record_link_assignment (output_bfd, &link_info,
872                                                   exp->assign.dst))
873             einfo ("%P%F: failed to record assignment to %s: %E\n",
874                    exp->assign.dst);
875         }
876       gld${EMULATION_NAME}_find_exp_assignment (exp->assign.src);
877       break;
878
879     case etree_binary:
880       gld${EMULATION_NAME}_find_exp_assignment (exp->binary.lhs);
881       gld${EMULATION_NAME}_find_exp_assignment (exp->binary.rhs);
882       break;
883
884     case etree_trinary:
885       gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.cond);
886       gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.lhs);
887       gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.rhs);
888       break;
889
890     case etree_unary:
891       gld${EMULATION_NAME}_find_exp_assignment (exp->unary.child);
892       break;
893
894     default:
895       break;
896     }
897 }
898
899 /* Work out the size of the .need section, and the number of entries.
900    The backend will set the ld_need field of the dynamic linking
901    information to point to the .need section.  See include/aout/sun4.h
902    for more information.  */
903
904 static void
905 gld${EMULATION_NAME}_count_need (inp)
906      lang_input_statement_type *inp;
907 {
908   if (inp->the_bfd != NULL
909       && (inp->the_bfd->flags & DYNAMIC) != 0)
910     {
911       ++need_entries;
912       need_size += NEED_ENTRY_SIZE;
913       if (! inp->is_archive)
914         need_size += strlen (inp->filename) + 1;
915       else
916         {
917           ASSERT (inp->local_sym_name[0] == '-'
918                   && inp->local_sym_name[1] == 'l');
919           need_size += strlen (inp->local_sym_name + 2) + 1;
920         }
921     }
922 }
923
924 /* Fill in the contents of the .need section.  */
925
926 static void
927 gld${EMULATION_NAME}_set_need (inp)
928      lang_input_statement_type *inp;
929 {
930   if (inp->the_bfd != NULL
931       && (inp->the_bfd->flags & DYNAMIC) != 0)
932     {
933       bfd_size_type c;
934
935       /* To really fill in the .need section contents, we need to know
936          the final file position of the section, but we don't.
937          Instead, we use offsets, and rely on the BFD backend to
938          finish the section up correctly.  FIXME: Talk about lack of
939          referential locality.  */
940       bfd_put_32 (output_bfd, need_pnames - need_contents, need_pinfo);
941       if (! inp->is_archive)
942         {
943           bfd_put_32 (output_bfd, (bfd_vma) 0, need_pinfo + 4);
944           bfd_put_16 (output_bfd, (bfd_vma) 0, need_pinfo + 8);
945           bfd_put_16 (output_bfd, (bfd_vma) 0, need_pinfo + 10);
946           strcpy (need_pnames, inp->filename);
947         }
948       else
949         {
950           char *verstr;
951           int maj, min;
952
953           bfd_put_32 (output_bfd, (bfd_vma) 0x80000000, need_pinfo + 4);
954           maj = 0;
955           min = 0;
956           verstr = strstr (inp->filename, ".so.");
957           if (verstr != NULL)
958             sscanf (verstr, ".so.%d.%d", &maj, &min);
959           bfd_put_16 (output_bfd, (bfd_vma) maj, need_pinfo + 8);
960           bfd_put_16 (output_bfd, (bfd_vma) min, need_pinfo + 10);
961           strcpy (need_pnames, inp->local_sym_name + 2);
962         }
963
964       c = (need_pinfo - need_contents) / NEED_ENTRY_SIZE;
965       if (c + 1 >= need_entries)
966         bfd_put_32 (output_bfd, (bfd_vma) 0, need_pinfo + 12);
967       else
968         bfd_put_32 (output_bfd, (bfd_vma) (c + 1) * NEED_ENTRY_SIZE,
969                     need_pinfo + 12);
970
971       need_pinfo += NEED_ENTRY_SIZE;
972       need_pnames += strlen (need_pnames) + 1;
973     }
974 }
975
976 static char *
977 gld${EMULATION_NAME}_get_script(isfile)
978      int *isfile;
979 EOF
980
981 if test -n "$COMPILE_IN"
982 then
983 # Scripts compiled in.
984
985 # sed commands to quote an ld script as a C string.
986 sc="-f stringify.sed"
987
988 cat >>e${EMULATION_NAME}.c <<EOF
989 {
990   *isfile = 0;
991
992   if (link_info.relocatable && config.build_constructors)
993     return
994 EOF
995 sed $sc ldscripts/${EMULATION_NAME}.xu                     >> e${EMULATION_NAME}.c
996 echo '  ; else if (link_info.relocatable) return'         >> e${EMULATION_NAME}.c
997 sed $sc ldscripts/${EMULATION_NAME}.xr                     >> e${EMULATION_NAME}.c
998 echo '  ; else if (!config.text_read_only) return'         >> e${EMULATION_NAME}.c
999 sed $sc ldscripts/${EMULATION_NAME}.xbn                    >> e${EMULATION_NAME}.c
1000 echo '  ; else if (!config.magic_demand_paged) return'     >> e${EMULATION_NAME}.c
1001 sed $sc ldscripts/${EMULATION_NAME}.xn                     >> e${EMULATION_NAME}.c
1002 echo '  ; else return'                                     >> e${EMULATION_NAME}.c
1003 sed $sc ldscripts/${EMULATION_NAME}.x                      >> e${EMULATION_NAME}.c
1004 echo '; }'                                                 >> e${EMULATION_NAME}.c
1005
1006 else
1007 # Scripts read from the filesystem.
1008
1009 cat >>e${EMULATION_NAME}.c <<EOF
1010 {
1011   *isfile = 1;
1012
1013   if (link_info.relocatable && config.build_constructors)
1014     return "ldscripts/${EMULATION_NAME}.xu";
1015   else if (link_info.relocatable)
1016     return "ldscripts/${EMULATION_NAME}.xr";
1017   else if (!config.text_read_only)
1018     return "ldscripts/${EMULATION_NAME}.xbn";
1019   else if (!config.magic_demand_paged)
1020     return "ldscripts/${EMULATION_NAME}.xn";
1021   else
1022     return "ldscripts/${EMULATION_NAME}.x";
1023 }
1024 EOF
1025
1026 fi
1027
1028 cat >>e${EMULATION_NAME}.c <<EOF
1029
1030 struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
1031 {
1032   gld${EMULATION_NAME}_before_parse,
1033   syslib_default,
1034   hll_default,
1035   after_parse_default,
1036   gld${EMULATION_NAME}_after_open,
1037   after_allocation_default,
1038   set_output_arch_default,
1039   ldemul_default_target,
1040   gld${EMULATION_NAME}_before_allocation,
1041   gld${EMULATION_NAME}_get_script,
1042   "${EMULATION_NAME}",
1043   "${OUTPUT_FORMAT}",
1044   NULL, /* finish */
1045   gld${EMULATION_NAME}_create_output_section_statements,
1046   NULL, /* open dynamic archive */
1047   NULL, /* place orphan */
1048   gld${EMULATION_NAME}_set_symbols,
1049   NULL, /* parse args */
1050   NULL, /* add_options */
1051   NULL, /* handle_option */
1052   NULL, /* unrecognized file */
1053   NULL, /* list options */
1054   NULL, /* recognized file */
1055   NULL, /* find_potential_libraries */
1056   NULL  /* new_vers_pattern */
1057 };
1058 EOF