Add Linux style directory searching.
[platform/upstream/binutils.git] / ld / emultempl / elf32.em
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! */
5
6 /* 32 bit ELF emulation code for ${EMULATION_NAME}
7    Copyright (C) 1991, 1993, 1994, 1995 Free Software Foundation, Inc.
8    Written by Steve Chamberlain <sac@cygnus.com>
9    ELF support by Ian Lance Taylor <ian@cygnus.com>
10
11 This file is part of GLD, the Gnu Linker.
12
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.
17
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.
22
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.  */
26
27 #define TARGET_IS_${EMULATION_NAME}
28
29 #include "bfd.h"
30 #include "sysdep.h"
31
32 #include <ctype.h>
33
34 #include "bfdlink.h"
35
36 #include "ld.h"
37 #include "ldmain.h"
38 #include "ldemul.h"
39 #include "ldfile.h"
40 #include "ldmisc.h"
41 #include "ldexp.h"
42 #include "ldlang.h"
43 #include "ldgram.h"
44
45 #ifdef TARGET_IS_i386lelf
46 #ifdef HAVE_DIRENT_H
47 # include <dirent.h>
48 #else
49 # define dirent direct
50 # ifdef HAVE_SYS_NDIR_H
51 #  include <sys/ndir.h>
52 # endif
53 # ifdef HAVE_SYS_DIR_H
54 #  include <sys/dir.h>
55 # endif
56 # ifdef HAVE_NDIR_H
57 #  include <ndir.h>
58 # endif
59 #endif
60 #endif
61
62 static void gld${EMULATION_NAME}_before_parse PARAMS ((void));
63 static boolean gld${EMULATION_NAME}_open_dynamic_archive
64   PARAMS ((const char *, search_dirs_type *, lang_input_statement_type *));
65 static void gld${EMULATION_NAME}_after_open PARAMS ((void));
66 static void gld${EMULATION_NAME}_check_needed
67   PARAMS ((lang_input_statement_type *));
68 static boolean gld${EMULATION_NAME}_search_needed
69   PARAMS ((const char *, const char *));
70 static boolean gld${EMULATION_NAME}_try_needed PARAMS ((const char *));
71 static void gld${EMULATION_NAME}_before_allocation PARAMS ((void));
72 static void gld${EMULATION_NAME}_find_statement_assignment
73   PARAMS ((lang_statement_union_type *));
74 static void gld${EMULATION_NAME}_find_exp_assignment PARAMS ((etree_type *));
75 static boolean gld${EMULATION_NAME}_place_orphan
76   PARAMS ((lang_input_statement_type *, asection *));
77 static void gld${EMULATION_NAME}_place_section
78   PARAMS ((lang_statement_union_type *));
79 static char *gld${EMULATION_NAME}_get_script PARAMS ((int *isfile));
80
81 static void
82 gld${EMULATION_NAME}_before_parse()
83 {
84   ldfile_output_architecture = bfd_arch_${ARCH};
85   config.dynamic_link = ${DYNAMIC_LINK-true};
86 }
87
88 #ifndef TARGET_IS_i386lelf
89
90 /* Try to open a dynamic archive.  This is where we know that ELF
91    dynamic libraries have an extension of .so.  */
92
93 static boolean
94 gld${EMULATION_NAME}_open_dynamic_archive (arch, search, entry)
95      const char *arch;
96      search_dirs_type *search;
97      lang_input_statement_type *entry;
98 {
99   const char *filename;
100   char *string;
101
102   if (! entry->is_archive)
103     return false;
104
105   filename = entry->filename;
106
107   string = (char *) xmalloc (strlen (search->name)
108                              + strlen (filename)
109                              + strlen (arch)
110                              + sizeof "/lib.so");
111
112   sprintf (string, "%s/lib%s%s.so", search->name, filename, arch);
113
114   if (! ldfile_try_open_bfd (string, entry))
115     {
116       free (string);
117       return false;
118     }
119
120   entry->filename = string;
121
122   /* We have found a dynamic object to include in the link.  The ELF
123      backend linker will create a DT_NEEDED entry in the .dynamic
124      section naming this file.  If this file includes a DT_SONAME
125      entry, it will be used.  Otherwise, the ELF linker will just use
126      the name of the file.  For an archive found by searching, like
127      this one, the DT_NEEDED entry should consist of just the name of
128      the file, without the path information used to find it.  Note
129      that we only need to do this if we have a dynamic object; an
130      archive will never be referenced by a DT_NEEDED entry.
131
132      FIXME: This approach--using bfd_elf_set_dt_needed_name--is not
133      very pretty.  I haven't been able to think of anything that is
134      pretty, though.  */
135   if (bfd_check_format (entry->the_bfd, bfd_object)
136       && (entry->the_bfd->flags & DYNAMIC) != 0)
137     {
138       char *needed_name;
139
140       ASSERT (entry->is_archive && entry->search_dirs_flag);
141       needed_name = (char *) xmalloc (strlen (filename)
142                                       + strlen (arch)
143                                       + sizeof "lib.so");
144       sprintf (needed_name, "lib%s%s.so", filename, arch);
145       bfd_elf_set_dt_needed_name (entry->the_bfd, needed_name);
146     }
147
148   return true;
149 }
150
151 #else /* TARGET_IS_i386lelf */
152
153 /* Linux deviates from the SVR4 standard in that its archives use
154    version numbers in the file name.  I think this is bogus, but
155    H.J. Lu insists that it be done this way.  */
156
157 static int libcmp PARAMS ((const char *, const char *));
158
159 /* Choose between two library names.  This is like a string
160    comparison, except that numbers are compared by value.  */
161
162 static int
163 libcmp (p1, p2)
164      const char *p1;
165      const char *p2;
166 {
167   while (*p1 != '\0')
168     {
169       if (isdigit (*p1) && isdigit (*p2))
170         {
171           unsigned long v1, v2;
172
173           v1 = strtoul (p1, (char **) &p1, 10);
174           v2 = strtoul (p2, (char **) &p2, 10);
175           if (v1 < v2)
176             return -1;
177           else if (v1 > v2)
178             return 1;
179         }
180       else if (*p1 < *p2)
181         return -1;
182       else if (*p1 > *p2)
183         return 1;
184       else
185         {
186           ++p1;
187           ++p2;
188         }
189     }
190
191   if (*p2 != '\0')
192     return -1;
193
194   return 0;
195 }
196
197 /* Search for the library to open.  */
198
199 /*ARGSUSED*/
200 static boolean
201 gld${EMULATION_NAME}_open_dynamic_archive (arch, search, entry)
202      const char *arch;
203      search_dirs_type *search;
204      lang_input_statement_type *entry;
205 {
206   size_t len;
207   char *found;
208   DIR *dir;
209   struct dirent *ent;
210   char *string;
211
212   len = strlen (entry->filename);
213
214   found = NULL;
215
216   dir = opendir (search->name);
217   if (dir == NULL)
218     return false;
219
220   while ((ent = readdir (dir)) != NULL)
221     {
222       if (strncmp (ent->d_name, "lib", 3) != 0
223           || strncmp (ent->d_name + 3, entry->filename, len) != 0
224           || strncmp (ent->d_name + len + 3, ".so", 3) != 0)
225         continue;
226
227       if (found != NULL)
228         {
229           if (libcmp (ent->d_name + len + 6, found + len + 6) < 0)
230             continue;
231           free (found);
232         }
233
234       found = (char *) xmalloc (strlen (ent->d_name) + 1);
235       strcpy (found, ent->d_name);
236     }
237
238   closedir (dir);
239
240   if (found == NULL)
241     return false;
242
243   string = (char *) xmalloc (strlen (search->name) + strlen (found) + 2);
244   sprintf (string, "%s/%s", search->name, found);
245   if (! ldfile_try_open_bfd (string, entry))
246     {
247       free (found);
248       return false;
249     }
250
251   entry->filename = string;
252
253   if (bfd_check_format (entry->the_bfd, bfd_object)
254       && (entry->the_bfd->flags & DYNAMIC) != 0)
255     {
256       ASSERT (entry->is_archive && entry->search_dirs_flag);
257       bfd_elf_set_dt_needed_name (entry->the_bfd, found);
258     }
259
260   return true;
261
262 }
263
264 #endif /* TARGET_IS_i386lelf */
265
266 /* These variables are required to pass information back and forth
267    between after_open and check_needed.  */
268
269 static struct bfd_elf_link_needed_list *global_needed;
270 static boolean global_found;
271
272 /* This is called after all the input files have been opened.  */
273
274 static void
275 gld${EMULATION_NAME}_after_open ()
276 {
277   struct bfd_elf_link_needed_list *needed, *l;
278
279   /* Get the list of files which appear in DT_NEEDED entries in
280      dynamic objects included in the link (often there will be none).
281      For each such file, we want to track down the corresponding
282      library, and include the symbol table in the link.  This is what
283      the runtime dynamic linker will do.  Tracking the files down here
284      permits one dynamic object to include another without requiring
285      special action by the person doing the link.  Note that the
286      needed list can actually grow while we are stepping through this
287      loop.  */
288   needed = bfd_elf_get_needed_list (output_bfd, &link_info);
289   for (l = needed; l != NULL; l = l->next)
290     {
291       struct bfd_elf_link_needed_list *ll;
292       const char *lib_path;
293       size_t len;
294       search_dirs_type *search;
295
296       /* If we've already seen this file, skip it.  */
297       for (ll = needed; ll != l; ll = ll->next)
298         if (strcmp (ll->name, l->name) == 0)
299           break;
300       if (ll != l)
301         continue;
302
303       /* See if this file was included in the link explicitly.  */
304       global_needed = l;
305       global_found = false;
306       lang_for_each_input_file (gld${EMULATION_NAME}_check_needed);
307       if (global_found)
308         continue;
309
310       /* We need to find this file and include the symbol table.  We
311          want to search for the file in the same way that the dynamic
312          linker will search.  That means that we want to use rpath,
313          then the environment variable LD_LIBRARY_PATH, then the
314          linker script LIB_SEARCH_DIRS.  We do not search using the -L
315          arguments.  */
316       if (gld${EMULATION_NAME}_search_needed (command_line.rpath, l->name))
317         continue;
318       lib_path = (const char *) getenv ("LD_LIBRARY_PATH");
319       if (gld${EMULATION_NAME}_search_needed (lib_path, l->name))
320         continue;
321       len = strlen (l->name);
322       for (search = search_head; search != NULL; search = search->next)
323         {
324           char *filename;
325
326           if (search->cmdline)
327             continue;
328           filename = (char *) xmalloc (strlen (search->name) + len + 2);
329           sprintf (filename, "%s/%s", search->name, l->name);
330           if (gld${EMULATION_NAME}_try_needed (filename))
331             break;
332           free (filename);
333         }
334       if (search != NULL)
335         continue;
336
337       einfo ("%P: warning: %s, needed by %B, not found\n",
338              l->name, l->by);
339     }
340 }
341
342 /* Search for a needed file in a path.  */
343
344 static boolean
345 gld${EMULATION_NAME}_search_needed (path, name)
346      const char *path;
347      const char *name;
348 {
349   const char *s;
350   size_t len;
351
352   if (path == NULL || *path == '\0')
353     return false;
354   len = strlen (name);
355   while (1)
356     {
357       char *filename, *sset;
358
359       s = strchr (path, ':');
360       if (s == NULL)
361         s = path + strlen (path);
362
363       filename = (char *) xmalloc (s - path + len + 2);
364       if (s == path)
365         sset = filename;
366       else
367         {
368           memcpy (filename, path, s - path);
369           filename[s - path] = '/';
370           sset = filename + (s - path) + 1;
371         }
372       strcpy (sset, name);
373
374       if (gld${EMULATION_NAME}_try_needed (filename))
375         return true;
376
377       free (filename);
378
379       if (*s == '\0')
380         break;
381       path = s + 1;
382     }
383
384   return false;   
385 }
386
387 /* This function is called for each possible name for a dynamic object
388    named by a DT_NEEDED entry.  */
389
390 static boolean
391 gld${EMULATION_NAME}_try_needed (name)
392      const char *name;
393 {
394   bfd *abfd;
395
396   abfd = bfd_openr (name, bfd_get_target (output_bfd));
397   if (abfd == NULL)
398     return false;
399   if (! bfd_check_format (abfd, bfd_object))
400     {
401       (void) bfd_close (abfd);
402       return false;
403     }
404   if ((bfd_get_file_flags (abfd) & DYNAMIC) == 0)
405     {
406       (void) bfd_close (abfd);
407       return false;
408     }
409
410   /* We've found a dynamic object matching the DT_NEEDED entry.  */
411
412   /* Tell the ELF backend that don't want the output file to have a
413      DT_NEEDED entry for this file.  */
414   bfd_elf_set_dt_needed_name (abfd, "");
415
416   /* Add this file into the symbol table.  */
417   if (! bfd_link_add_symbols (abfd, &link_info))
418     einfo ("%F%B: could not read symbols: %E\n", abfd);
419
420   return true;
421 }
422
423 /* See if an input file matches a DT_NEEDED entry.  */
424
425 static void
426 gld${EMULATION_NAME}_check_needed (s)
427      lang_input_statement_type *s;
428 {
429   if (s->filename != NULL
430       && strcmp (s->filename, global_needed->name) == 0)
431     global_found = true;
432   else if (s->search_dirs_flag
433            && s->filename != NULL
434            && strchr (global_needed->name, '/') == NULL)
435     {
436       const char *f;
437
438       f = strrchr (s->filename, '/');
439       if (f != NULL
440           && strcmp (f + 1, global_needed->name) == 0)
441         global_found = true;
442     }
443 }
444
445 /* This is called after the sections have been attached to output
446    sections, but before any sizes or addresses have been set.  */
447
448 static void
449 gld${EMULATION_NAME}_before_allocation ()
450 {
451   asection *sinterp;
452
453   /* If we are going to make any variable assignments, we need to let
454      the ELF backend know about them in case the variables are
455      referred to by dynamic objects.  */
456   lang_for_each_statement (gld${EMULATION_NAME}_find_statement_assignment);
457
458   /* Let the ELF backend work out the sizes of any sections required
459      by dynamic linking.  */
460   if (! bfd_elf32_size_dynamic_sections (output_bfd,
461                                          command_line.soname,
462                                          command_line.rpath,
463                                          command_line.export_dynamic,
464                                          &link_info,
465                                          &sinterp))
466     einfo ("%P%F: failed to set dynamic section sizes: %E\n");
467
468   /* Let the user override the dynamic linker we are using.  */
469   if (command_line.interpreter != NULL
470       && sinterp != NULL)
471     {
472       sinterp->contents = (bfd_byte *) command_line.interpreter;
473       sinterp->_raw_size = strlen (command_line.interpreter) + 1;
474     }
475
476   /* Look for any sections named .gnu.warning.  As a GNU extensions,
477      we treat such sections as containing warning messages.  We print
478      out the warning message, and then zero out the section size so
479      that it does not get copied into the output file.  */
480
481   {
482     LANG_FOR_EACH_INPUT_STATEMENT (is)
483       {
484         asection *s;
485         bfd_size_type sz;
486         char *msg;
487         boolean ret;
488
489         if (is->just_syms_flag)
490           continue;
491
492         s = bfd_get_section_by_name (is->the_bfd, ".gnu.warning");
493         if (s == NULL)
494           continue;
495
496         sz = bfd_section_size (is->the_bfd, s);
497         msg = xmalloc ((size_t) sz + 1);
498         if (! bfd_get_section_contents (is->the_bfd, s, msg, (file_ptr) 0, sz))
499           einfo ("%F%B: Can't read contents of section .gnu.warning: %E\n",
500                  is->the_bfd);
501         msg[sz] = '\0';
502         ret = link_info.callbacks->warning (&link_info, msg, is->the_bfd,
503                                             (asection *) NULL, (bfd_vma) 0);
504         ASSERT (ret);
505         free (msg);
506
507         /* Clobber the section size, so that we don't waste copying the
508            warning into the output file.  */
509         s->_raw_size = 0;
510       }
511   }
512
513 #if defined (TARGET_IS_elf32bmip) || defined (TARGET_IS_elf32lmip)
514   /* For MIPS ELF the .reginfo section requires special handling.
515      Each input section is 24 bytes, and the final output section must
516      also be 24 bytes.  We handle this by clobbering all but the first
517      input section size to 0.  The .reginfo section is handled
518      specially by the backend code anyhow.  */
519   {
520     boolean found = false;
521     LANG_FOR_EACH_INPUT_STATEMENT (is)
522       {
523         asection *s;
524
525         if (is->just_syms_flag)
526           continue;
527
528         s = bfd_get_section_by_name (is->the_bfd, ".reginfo");
529         if (s == NULL)
530           continue;
531
532         if (! found)
533           {
534             found = true;
535             continue;
536           }
537
538         s->_raw_size = 0;
539         s->_cooked_size = 0;
540       }
541   }
542 #endif
543 }
544
545 /* This is called by the before_allocation routine via
546    lang_for_each_statement.  It locates any assignment statements, and
547    tells the ELF backend about them, in case they are assignments to
548    symbols which are referred to by dynamic objects.  */
549
550 static void
551 gld${EMULATION_NAME}_find_statement_assignment (s)
552      lang_statement_union_type *s;
553 {
554   if (s->header.type == lang_assignment_statement_enum)
555     gld${EMULATION_NAME}_find_exp_assignment (s->assignment_statement.exp);
556 }
557
558 /* Look through an expression for an assignment statement.  */
559
560 static void
561 gld${EMULATION_NAME}_find_exp_assignment (exp)
562      etree_type *exp;
563 {
564   struct bfd_link_hash_entry *h;
565
566   switch (exp->type.node_class)
567     {
568     case etree_provide:
569       h = bfd_link_hash_lookup (link_info.hash, exp->assign.dst,
570                                 false, false, false);
571       if (h == NULL)
572         break;
573
574       /* We call record_link_assignment even if the symbol is defined.
575          This is because if it is defined by a dynamic object, we
576          actually want to use the value defined by the linker script,
577          not the value from the dynamic object (because we are setting
578          symbols like etext).  If the symbol is defined by a regular
579          object, then, as it happens, calling record_link_assignment
580          will do no harm.  */
581
582       /* Fall through.  */
583     case etree_assign:
584       if (strcmp (exp->assign.dst, ".") != 0)
585         {
586           if (! (bfd_elf32_record_link_assignment
587                  (output_bfd, &link_info, exp->assign.dst,
588                   exp->type.node_class == etree_provide ? true : false)))
589             einfo ("%P%F: failed to record assignment to %s: %E\n",
590                    exp->assign.dst);
591         }
592       gld${EMULATION_NAME}_find_exp_assignment (exp->assign.src);
593       break;
594
595     case etree_binary:
596       gld${EMULATION_NAME}_find_exp_assignment (exp->binary.lhs);
597       gld${EMULATION_NAME}_find_exp_assignment (exp->binary.rhs);
598       break;
599
600     case etree_trinary:
601       gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.lhs);
602       gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.lhs);
603       gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.rhs);
604       break;
605
606     case etree_unary:
607       gld${EMULATION_NAME}_find_exp_assignment (exp->unary.child);
608       break;
609
610     default:
611       break;
612     }
613 }
614
615 /* Place an orphan section.  We use this to put random SHF_ALLOC
616    sections in the right segment.  */
617
618 static asection *hold_section;
619 static lang_output_section_statement_type *hold_use;
620 static lang_output_section_statement_type *hold_text;
621 static lang_output_section_statement_type *hold_data;
622 static lang_output_section_statement_type *hold_bss;
623 static lang_output_section_statement_type *hold_rel;
624
625 /*ARGSUSED*/
626 static boolean
627 gld${EMULATION_NAME}_place_orphan (file, s)
628      lang_input_statement_type *file;
629      asection *s;
630 {
631   lang_output_section_statement_type *place;
632   asection *snew, **pps;
633   lang_statement_list_type *old;
634   lang_statement_list_type add;
635   etree_type *address;
636   const char *secname, *ps;
637   lang_output_section_statement_type *os;
638
639   if ((s->flags & SEC_ALLOC) == 0)
640     return false;
641
642   /* Look through the script to see where to place this section.  */
643   hold_section = s;
644   hold_use = NULL;
645   lang_for_each_statement (gld${EMULATION_NAME}_place_section);
646
647   if (hold_use != NULL)
648     {
649       /* We have already placed a section with this name.  */
650       wild_doit (&hold_use->children, s, hold_use, file);
651       return true;
652     }
653
654   secname = bfd_get_section_name (s->owner, s);
655
656   /* If this is a final link, then always put .gnu.warning.SYMBOL
657      sections into the .text section to get them out of the way.  */
658   if (! link_info.shared
659       && ! link_info.relocateable
660       && strncmp (secname, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0
661       && hold_text != NULL)
662     {
663       wild_doit (&hold_text->children, s, hold_text, file);
664       return true;
665     }
666
667   /* Decide which segment the section should go in based on the
668      section name and section flags.  */
669   place = NULL;
670   if ((s->flags & SEC_HAS_CONTENTS) == 0
671       && hold_bss != NULL)
672     place = hold_bss;
673   else if ((s->flags & SEC_READONLY) == 0
674            && hold_data != NULL)
675     place = hold_data;
676   else if (strncmp (secname, ".rel", 4) == 0
677            && hold_rel != NULL)
678     place = hold_rel;
679   else if ((s->flags & SEC_READONLY) != 0
680            && hold_text != NULL)
681     place = hold_text;
682   if (place == NULL)
683     return false;
684
685   /* Create the section in the output file, and put it in the right
686      place.  This shuffling is to make the output file look neater.  */
687   snew = bfd_make_section (output_bfd, secname);
688   if (snew == NULL)
689       einfo ("%P%F: output format %s cannot represent section called %s\n",
690              output_bfd->xvec->name, secname);
691   if (place->bfd_section != NULL)
692     {
693       for (pps = &output_bfd->sections; *pps != snew; pps = &(*pps)->next)
694         ;
695       *pps = snew->next;
696       snew->next = place->bfd_section->next;
697       place->bfd_section->next = snew;
698     }
699
700   /* Start building a list of statements for this section.  */
701   old = stat_ptr;
702   stat_ptr = &add;
703   lang_list_init (stat_ptr);
704
705   /* If the name of the section is representable in C, then create
706      symbols to mark the start and the end of the section.  */
707   for (ps = secname; *ps != '\0'; ps++)
708     if (! isalnum (*ps) && *ps != '_')
709       break;
710   if (*ps == '\0' && config.build_constructors)
711     {
712       char *symname;
713
714       symname = (char *) xmalloc (ps - secname + sizeof "__start_");
715       sprintf (symname, "__start_%s", secname);
716       lang_add_assignment (exp_assop ('=', symname,
717                                       exp_nameop (NAME, ".")));
718     }
719
720   if (! link_info.relocateable)
721     address = NULL;
722   else
723     address = exp_intop ((bfd_vma) 0);
724
725   lang_enter_output_section_statement (secname, address, 0,
726                                        (bfd_vma) 0,
727                                        (etree_type *) NULL,
728                                        (etree_type *) NULL,
729                                        (etree_type *) NULL);
730
731   os = lang_output_section_statement_lookup (secname);
732   wild_doit (&os->children, s, os, file);
733
734   lang_leave_output_section_statement ((bfd_vma) 0, "*default*");
735   stat_ptr = &add;
736
737   if (*ps == '\0' && config.build_constructors)
738     {
739       char *symname;
740
741       symname = (char *) xmalloc (ps - secname + sizeof "__stop_");
742       sprintf (symname, "__stop_%s", secname);
743       lang_add_assignment (exp_assop ('=', symname,
744                                       exp_nameop (NAME, ".")));
745     }
746
747   /* Now stick the new statement list right after PLACE.  */
748   *add.tail = place->header.next;
749   place->header.next = add.head;
750
751   stat_ptr = old;
752
753   return true;
754 }
755
756 static void
757 gld${EMULATION_NAME}_place_section (s)
758      lang_statement_union_type *s;
759 {
760   lang_output_section_statement_type *os;
761
762   if (s->header.type != lang_output_section_statement_enum)
763     return;
764
765   os = &s->output_section_statement;
766
767   if (strcmp (os->name, hold_section->name) == 0)
768     hold_use = os;
769
770   if (strcmp (os->name, ".text") == 0)
771     hold_text = os;
772   else if (strcmp (os->name, ".data") == 0)
773     hold_data = os;
774   else if (strcmp (os->name, ".bss") == 0)
775     hold_bss = os;
776   else if (hold_rel == NULL
777            && strncmp (os->name, ".rel", 4) == 0)
778     hold_rel = os;
779 }
780
781 static char *
782 gld${EMULATION_NAME}_get_script(isfile)
783      int *isfile;
784 EOF
785
786 if test -n "$COMPILE_IN"
787 then
788 # Scripts compiled in.
789
790 # sed commands to quote an ld script as a C string.
791 sc='s/["\\]/\\&/g
792 s/$/\\n\\/
793 1s/^/"/
794 $s/$/n"/
795 '
796
797 cat >>e${EMULATION_NAME}.c <<EOF
798 {                            
799   *isfile = 0;
800
801   if (link_info.relocateable == true && config.build_constructors == true)
802     return `sed "$sc" ldscripts/${EMULATION_NAME}.xu`;
803   else if (link_info.relocateable == true)
804     return `sed "$sc" ldscripts/${EMULATION_NAME}.xr`;
805   else if (!config.text_read_only)
806     return `sed "$sc" ldscripts/${EMULATION_NAME}.xbn`;
807   else if (!config.magic_demand_paged)
808     return `sed "$sc" ldscripts/${EMULATION_NAME}.xn`;
809   else if (link_info.shared)
810     return `sed "$sc" ldscripts/${EMULATION_NAME}.xs`;
811   else
812     return `sed "$sc" ldscripts/${EMULATION_NAME}.x`;
813 }
814 EOF
815
816 else
817 # Scripts read from the filesystem.
818
819 cat >>e${EMULATION_NAME}.c <<EOF
820 {                            
821   *isfile = 1;
822
823   if (link_info.relocateable == true && config.build_constructors == true)
824     return "ldscripts/${EMULATION_NAME}.xu";
825   else if (link_info.relocateable == true)
826     return "ldscripts/${EMULATION_NAME}.xr";
827   else if (!config.text_read_only)
828     return "ldscripts/${EMULATION_NAME}.xbn";
829   else if (!config.magic_demand_paged)
830     return "ldscripts/${EMULATION_NAME}.xn";
831   else if (link_info.shared)
832     return "ldscripts/${EMULATION_NAME}.xs";
833   else
834     return "ldscripts/${EMULATION_NAME}.x";
835 }
836 EOF
837
838 fi
839
840 cat >>e${EMULATION_NAME}.c <<EOF
841
842 struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation = 
843 {
844   gld${EMULATION_NAME}_before_parse,
845   syslib_default,
846   hll_default,
847   after_parse_default,
848   gld${EMULATION_NAME}_after_open,
849   after_allocation_default,
850   set_output_arch_default,
851   ldemul_default_target,
852   gld${EMULATION_NAME}_before_allocation,
853   gld${EMULATION_NAME}_get_script,
854   "${EMULATION_NAME}",
855   "${OUTPUT_FORMAT}",
856   NULL,
857   NULL,
858   gld${EMULATION_NAME}_open_dynamic_archive,
859   gld${EMULATION_NAME}_place_orphan
860 };
861 EOF