* emultempl/aix.em: Include ldgram.h.
[platform/upstream/binutils.git] / ld / emultempl / aix.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 /* AIX emulation code for ${EMULATION_NAME}
7    Copyright (C) 1991, 1993, 1995 Free Software Foundation, Inc.
8    Written by Steve Chamberlain <sac@cygnus.com>
9    AIX 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 #include "libiberty.h"
32 #include "getopt.h"
33 #include "bfdlink.h"
34
35 #include <ctype.h>
36
37 #include "ld.h"
38 #include "ldmain.h"
39 #include "ldemul.h"
40 #include "ldfile.h"
41 #include "ldmisc.h"
42 #include "ldexp.h"
43 #include "ldlang.h"
44 #include "ldctor.h"
45 #include "ldgram.h"
46
47 static void gld${EMULATION_NAME}_before_parse PARAMS ((void));
48 static int gld${EMULATION_NAME}_parse_args PARAMS ((int, char **));
49 static void gld${EMULATION_NAME}_after_open PARAMS ((void));
50 static void gld${EMULATION_NAME}_before_allocation PARAMS ((void));
51 static void gld${EMULATION_NAME}_read_file PARAMS ((const char *, boolean));
52 static void gld${EMULATION_NAME}_free PARAMS ((PTR));
53 static void gld${EMULATION_NAME}_find_relocs
54   PARAMS ((lang_statement_union_type *));
55 static void gld${EMULATION_NAME}_find_exp_assignment PARAMS ((etree_type *));
56 static char *gld${EMULATION_NAME}_get_script PARAMS ((int *isfile));
57
58 /* The file alignment required for each section.  */
59 static unsigned long file_align;
60
61 /* The maximum size the stack is permitted to grow.  This is stored in
62    the a.out header.  */
63 static unsigned long maxstack;
64
65 /* The maximum data size.  This is stored in the a.out header.  */
66 static unsigned long maxdata;
67
68 /* Whether to perform garbage collection.  */
69 static int gc = 1;
70
71 /* The module type to use.  */
72 static unsigned short modtype = ('1' << 8) | 'L';
73
74 /* Whether the .text section must be read-only (i.e., no relocs
75    permitted).  */
76 static int textro;
77
78 /* Structure used to hold import file list.  */
79
80 struct filelist
81 {
82   struct filelist *next;
83   const char *name;
84 };
85
86 /* List of import files.  */
87 static struct filelist *import_files;
88
89 /* List of export symbols read from the export files.  */
90
91 struct export_symbol_list
92 {
93   struct export_symbol_list *next;
94   const char *name;
95   boolean syscall;
96 };
97
98 static struct export_symbol_list *export_symbols;
99
100 /* This routine is called before anything else is done.  */
101
102 static void
103 gld${EMULATION_NAME}_before_parse()
104 {
105 #ifndef TARGET_                 /* I.e., if not generic.  */
106   ldfile_output_architecture = bfd_arch_${ARCH};
107 #endif /* not TARGET_ */
108 }
109
110 /* Handle AIX specific options.  */
111
112 static int
113 gld${EMULATION_NAME}_parse_args (argc, argv)
114      int argc;
115      char **argv;
116 {
117   int prevoptind = optind;
118   int prevopterr = opterr;
119   int indx;
120   int longind;
121   int optc;
122   long val;
123   char *end;
124
125 #define OPTION_IGNORE (300)
126 #define OPTION_AUTOIMP (OPTION_IGNORE + 1)
127 #define OPTION_ERNOTOK (OPTION_AUTOIMP + 1)
128 #define OPTION_EROK (OPTION_ERNOTOK + 1)
129 #define OPTION_EXPORT (OPTION_EROK + 1)
130 #define OPTION_IMPORT (OPTION_EXPORT + 1)
131 #define OPTION_MAXDATA (OPTION_IMPORT + 1)
132 #define OPTION_MAXSTACK (OPTION_MAXDATA + 1)
133 #define OPTION_MODTYPE (OPTION_MAXSTACK + 1)
134 #define OPTION_NOAUTOIMP (OPTION_MODTYPE + 1)
135 #define OPTION_NOSTRCMPCT (OPTION_NOAUTOIMP + 1)
136 #define OPTION_PD (OPTION_NOSTRCMPCT + 1)
137 #define OPTION_PT (OPTION_PD + 1)
138 #define OPTION_STRCMPCT (OPTION_PT + 1)
139
140   static struct option longopts[] = {
141     {"basis", no_argument, NULL, OPTION_IGNORE},
142     {"bautoimp", no_argument, NULL, OPTION_AUTOIMP},
143     {"bcomprld", no_argument, NULL, OPTION_IGNORE},
144     {"bcrld", no_argument, NULL, OPTION_IGNORE},
145     {"bcror31", no_argument, NULL, OPTION_IGNORE},
146     {"bD", required_argument, NULL, OPTION_MAXDATA},
147     {"bE", required_argument, NULL, OPTION_EXPORT},
148     {"bernotok", no_argument, NULL, OPTION_ERNOTOK},
149     {"berok", no_argument, NULL, OPTION_EROK},
150     {"berrmsg", no_argument, NULL, OPTION_IGNORE},
151     {"bexport", required_argument, NULL, OPTION_EXPORT},
152     {"bf", no_argument, NULL, OPTION_ERNOTOK},
153     {"bgc", no_argument, &gc, 1},
154     {"bh", required_argument, NULL, OPTION_IGNORE},
155     {"bhalt", required_argument, NULL, OPTION_IGNORE},
156     {"bI", required_argument, NULL, OPTION_IMPORT},
157     {"bimport", required_argument, NULL, OPTION_IMPORT},
158     {"bmaxdata", required_argument, NULL, OPTION_MAXDATA},
159     {"bmaxstack", required_argument, NULL, OPTION_MAXSTACK},
160     {"bM", required_argument, NULL, OPTION_MODTYPE},
161     {"bmodtype", required_argument, NULL, OPTION_MODTYPE},
162     {"bnoautoimp", no_argument, NULL, OPTION_NOAUTOIMP},
163     {"bnodelcsect", no_argument, NULL, OPTION_IGNORE},
164     {"bnogc", no_argument, &gc, 0},
165     {"bnso", no_argument, NULL, OPTION_NOAUTOIMP},
166     {"bnostrcmpct", no_argument, NULL, OPTION_NOSTRCMPCT},
167     {"bnotextro", no_argument, &textro, 0},
168     {"bnro", no_argument, &textro, 0},
169     {"bpD", required_argument, NULL, OPTION_PD},
170     {"bpT", required_argument, NULL, OPTION_PT},
171     {"bro", no_argument, &textro, 1},
172     {"bS", required_argument, NULL, OPTION_MAXSTACK},
173     {"bso", no_argument, NULL, OPTION_AUTOIMP},
174     {"bstrcmpct", no_argument, NULL, OPTION_STRCMPCT},
175     {"btextro", no_argument, &textro, 1},
176     {"static", no_argument, NULL, OPTION_NOAUTOIMP},
177     {NULL, no_argument, NULL, 0}
178   };
179
180   /* Options supported by the AIX linker which we do not support: -f,
181      -S, -v, -Z, -bbindcmds, -bbinder, -bbindopts, -bcalls, -bcaps,
182      -bcror15, -bdebugopt, -bdbg, -bdelcsect, -bex?, -bfilelist, -bfl,
183      -bgcbypass, -bglink, -binsert, -bi, -bloadmap, -bl, -bmap, -bnl,
184      -bnobind, -bnocomprld, -bnocrld, -bnoerrmsg, -bnoglink,
185      -bnoloadmap, -bnl, -bnoobjreorder, -bnoquiet, -bnoreorder,
186      -bnotypchk, -bnox, -bquiet, -bR, -brename, -breorder, -btypchk,
187      -bx, -bX, -bxref.  */
188
189   /* If the current option starts with -b, change the first : to an =.
190      The AIX linker uses : to separate the option from the argument;
191      changing it to = lets us treat it as a getopt option.  */
192   indx = optind;
193   if (indx == 0)
194     indx = 1;
195   if (indx < argc && strncmp (argv[indx], "-b", 2) == 0)
196     {
197       char *s;
198
199       for (s = argv[indx]; *s != '\0'; s++)
200         {
201           if (*s == ':')
202             {
203               *s = '=';
204               break;
205             }
206         }
207     }
208
209   opterr = 0;
210   optc = getopt_long_only (argc, argv, "-D:H:KT:z", longopts, &longind);
211   opterr = prevopterr;
212
213   switch (optc)
214     {
215     default:
216       optind = prevoptind;
217       return 0;
218
219     case 0:
220       /* Long option which just sets a flag.  */
221       break;
222
223     case 'D':
224       val = strtol (optarg, &end, 0);
225       if (*end != '\0')
226         einfo ("%P: warning: ignoring invalid -D number %s\n", optarg);
227       else if (val != -1)
228         lang_section_start (".data", exp_intop (val));
229       break;
230
231     case 'H':
232       val = strtoul (optarg, &end, 0);
233       if (*end != '\0'
234           || (val & (val - 1)) != 0)
235         einfo ("%P: warning: ignoring invalid -H number %s\n", optarg);
236       else
237         file_align = val;
238       break;
239
240     case 'K':
241     case 'z':
242       /* FIXME: This should use the page size for the target system.  */
243       file_align = 4096;
244       break;
245
246     case 'T':
247       /* On AIX this is the same as GNU ld -Ttext.  When we see -T
248          number, we assume the AIX option is intended.  Otherwise, we
249          assume the usual GNU ld -T option is intended.  We can't just
250          ignore the AIX option, because gcc passes it to the linker.  */
251       val = strtoul (optarg, &end, 0);
252       if (*end != '\0')
253         {
254           optind = prevoptind;
255           return 0;
256         }
257       lang_section_start (".text", exp_intop (val));
258       break;
259
260     case OPTION_IGNORE:
261       break;
262
263     case OPTION_AUTOIMP:
264       link_info.static_link = false;
265       break;
266
267     case OPTION_ERNOTOK:
268       force_make_executable = false;
269       break;
270
271     case OPTION_EROK:
272       force_make_executable = true;
273       break;
274
275     case OPTION_EXPORT:
276       gld${EMULATION_NAME}_read_file (optarg, false);
277       break;
278
279     case OPTION_IMPORT:
280       {
281         struct filelist *n;
282         struct filelist **flpp;
283
284         n = (struct filelist *) xmalloc (sizeof (struct filelist));
285         n->next = NULL;
286         n->name = optarg;
287         flpp = &import_files;
288         while (*flpp != NULL)
289           flpp = &(*flpp)->next;
290         *flpp = n;
291       }
292       break;
293
294     case OPTION_MAXDATA:
295       val = strtoul (optarg, &end, 0);
296       if (*end != '\0')
297         einfo ("%P: warning: ignoring invalid -bmaxdata number %s\n",
298                optarg);
299       else
300         maxdata = val;
301       break;
302
303     case OPTION_MAXSTACK:
304       val = strtoul (optarg, &end, 0);
305       if (*end != '\0')
306         einfo ("%P: warning: ignoring invalid -bmaxstack number %s\n",
307                optarg);
308       else
309         maxstack = val;
310       break;
311
312     case OPTION_MODTYPE:
313       if (*optarg == 'S')
314         {
315           link_info.shared = true;
316           ++optarg;
317         }
318       if (*optarg == '\0' || optarg[1] == '\0')
319         einfo ("%P: warning: ignoring invalid module type %s\n", optarg);
320       else
321         modtype = (*optarg << 8) | optarg[1];
322       break;
323
324     case OPTION_NOAUTOIMP:
325       link_info.static_link = true;
326       break;
327
328     case OPTION_NOSTRCMPCT:
329       config.traditional_format = true;
330       break;
331
332     case OPTION_PD:
333       /* This sets the page that the .data section is supposed to
334          start on.  The offset within the page should still be the
335          offset within the file, so we need to build an appropriate
336          expression.  */
337       val = strtoul (optarg, &end, 0);
338       if (*end != '\0')
339         einfo ("%P: warning: ignoring invalid -pD number %s\n", optarg);
340       else
341         {
342           etree_type *t;
343
344           t = exp_binop ('+',
345                          exp_intop (val),
346                          exp_binop ('&',
347                                     exp_nameop (NAME, "."),
348                                     exp_intop (0xfff)));
349           t = exp_binop ('&',
350                          exp_binop ('+', t, exp_intop (7)),
351                          exp_intop (~ (bfd_vma) 7));
352           lang_section_start (".data", t);
353         }
354       break;
355
356     case OPTION_PT:
357       /* This set the page that the .text section is supposed to start
358          on.  The offset within the page should still be the offset
359          within the file.  */
360       val = strtoul (optarg, &end, 0);
361       if (*end != '\0')
362         einfo ("%P: warning: ignoring invalid -pT number %s\n", optarg);
363       else
364         {
365           etree_type *t;
366
367           t = exp_binop ('+',
368                          exp_intop (val),
369                          exp_nameop (SIZEOF_HEADERS, NULL));
370           t = exp_binop ('&',
371                          exp_binop ('+', t, exp_intop (7)),
372                          exp_intop (~ (bfd_vma) 7));
373           lang_section_start (".text", t);
374         }
375       break;
376
377     case OPTION_STRCMPCT:
378       config.traditional_format = false;
379       break;
380     }
381
382   return 1;
383 }
384
385 /* This is called after the input files have been opened.  */
386
387 static void
388 gld${EMULATION_NAME}_after_open ()
389 {
390   boolean r;
391   struct set_info *p;
392
393   /* Call ldctor_build_sets, after pretending that this is a
394      relocateable link.  We do this because AIX requires relocation
395      entries for all references to symbols, even in a final
396      executable.  */
397   r = link_info.relocateable;
398   link_info.relocateable = true;
399   ldctor_build_sets ();
400   link_info.relocateable = r;
401
402   /* For each set, record the size, so that the XCOFF backend can
403      output the correct csect length.  */
404   for (p = sets; p != (struct set_info *) NULL; p = p->next)
405     {
406       bfd_size_type size;
407
408       /* If the symbol is defined, we may have been invoked from
409          collect, and the sets may already have been built, so we do
410          not do anything.  */
411       if (p->h->type == bfd_link_hash_defined
412           || p->h->type == bfd_link_hash_defweak)
413         continue;
414
415       if (p->reloc != BFD_RELOC_CTOR)
416         {
417           /* Handle this if we need to.  */
418           abort ();
419         }
420
421       size = (p->count + 2) * 4;
422       if (! bfd_xcoff_link_record_set (output_bfd, &link_info, p->h, size))
423         einfo ("%F%P: bfd_xcoff_link_record_set failed: %E\n");
424     }
425 }
426
427 /* This is called after the sections have been attached to output
428    sections, but before any sizes or addresses have been set.  */
429
430 static void
431 gld${EMULATION_NAME}_before_allocation ()
432 {
433   struct filelist *fl;
434   struct export_symbol_list *el;
435   char *libpath;
436
437   /* Handle the import and export files, if any.  */
438   for (fl = import_files; fl != NULL; fl = fl->next)
439     gld${EMULATION_NAME}_read_file (fl->name, true);
440   for (el = export_symbols; el != NULL; el = el->next)
441     {
442       struct bfd_link_hash_entry *h;
443
444       h = bfd_link_hash_lookup (link_info.hash, el->name, false, false, false);
445       if (h == NULL)
446         einfo ("%P%F: bfd_link_hash_lookup of export symbol failed: %E\n");
447       if (! bfd_xcoff_export_symbol (output_bfd, &link_info, h, el->syscall))
448         einfo ("%P%F: bfd_xcoff_export_symbol failed: %E\n");
449     }
450
451   /* Track down all relocations called for by the linker script (these
452      are typically constructor/destructor entries created by
453      CONSTRUCTORS) and let the backend know it will need to create
454      .loader relocs for them.  */
455   lang_for_each_statement (gld${EMULATION_NAME}_find_relocs);
456
457   /* We need to build LIBPATH from the -L arguments.  If any -rpath
458      arguments were used, though, we use -rpath instead, as a GNU
459      extension.  */
460   if (command_line.rpath != NULL)
461     libpath = command_line.rpath;
462   else if (search_head == NULL)
463     libpath = (char *) "";
464   else
465     {
466       size_t len;
467       search_dirs_type *search;
468
469       len = strlen (search_head->name);
470       libpath = xmalloc (len + 1);
471       strcpy (libpath, search_head->name);
472       for (search = search_head->next; search != NULL; search = search->next)
473         {
474           size_t nlen;
475
476           nlen = strlen (search->name);
477           libpath = xrealloc (libpath, len + nlen + 2);
478           libpath[len] = ':';
479           strcpy (libpath + len + 1, search->name);
480           len += nlen + 1;
481         }
482     }
483
484   /* Let the XCOFF backend set up the .loader section.  */
485   if (! bfd_xcoff_size_dynamic_sections (output_bfd, &link_info, libpath,
486                                          entry_symbol, file_align,
487                                          maxstack, maxdata,
488                                          gc ? true : false,
489                                          modtype,
490                                          textro ? true : false))
491     einfo ("%P%F: failed to set dynamic section sizes: %E\n");
492 }
493
494 /* Read an import or export file.  For an import file, this is called
495    by the before_allocation emulation routine.  For an export file,
496    this is called by the parse_args emulation routine.  */
497
498 static void
499 gld${EMULATION_NAME}_read_file (filename, import)
500      const char *filename;
501      boolean import;
502 {
503   struct obstack *o;
504   FILE *f;
505   int lineno;
506   int c;
507   boolean keep;
508   const char *imppath;
509   const char *impfile;
510   const char *impmember;
511
512   o = (struct obstack *) xmalloc (sizeof (struct obstack));
513   obstack_specify_allocation (o, 0, 0, xmalloc, gld${EMULATION_NAME}_free);
514
515   f = fopen (filename, "r");
516   if (f == NULL)
517     {
518       bfd_set_error (bfd_error_system_call);
519       einfo ("%F%s: %E\n", filename);
520     }
521
522   keep = false;
523
524   imppath = NULL;
525   impfile = NULL;
526   impmember = NULL;
527
528   lineno = 0;
529   while ((c = getc (f)) != EOF)
530     {
531       char *s;
532       char *symname;
533       boolean syscall;
534       bfd_vma address;
535       struct bfd_link_hash_entry *h;
536
537       if (c != '\n')
538         {
539           obstack_1grow (o, c);
540           continue;
541         }
542
543       obstack_1grow (o, '\0');
544       ++lineno;
545
546       s = (char *) obstack_base (o);
547       while (isspace ((unsigned char) *s))
548         ++s;
549       if (*s == '\0'
550           || *s == '*'
551           || (*s == '#' && s[1] == ' ')
552           || (! import && *s == '#' && s[1] == '!'))
553         {
554           obstack_free (o, obstack_base (o));
555           continue;
556         }
557
558       if (*s == '#' && s[1] == '!')
559         {
560           s += 2;
561           while (isspace ((unsigned char) *s))
562             ++s;
563           if (*s == '\0')
564             {
565               imppath = NULL;
566               impfile = NULL;
567               impmember = NULL;
568               obstack_free (o, obstack_base (o));
569             }
570           else if (*s == '(')
571             einfo ("%F%s%d: #! ([member]) is not supported in import files",
572                    filename, lineno);
573           else
574             {
575               char cs;
576               char *file;
577
578               (void) obstack_finish (o);
579               keep = true;
580               imppath = s;
581               impfile = NULL;
582               while (! isspace ((unsigned char) *s) && *s != '(' && *s != '\0')
583                 {
584                   if (*s == '/')
585                     file = s + 1;
586                   ++s;
587                 }
588               if (file != NULL)
589                 {
590                   file[-1] = '\0';
591                   impfile = file;
592                   if (imppath == file - 1)
593                     imppath = "/";
594                 }
595               else
596                 {
597                   impfile = imppath;
598                   imppath = "";
599                 }
600               cs = *s;
601               *s = '\0';
602               while (isspace ((unsigned char) cs))
603                 {
604                   ++s;
605                   cs = *s;
606                 }
607               if (cs != '(')
608                 {
609                   impmember = "";
610                   if (cs != '\0')
611                     einfo ("%s:%d: warning: syntax error in import file\n",
612                            filename, lineno);
613                 }
614               else
615                 {
616                   ++s;
617                   impmember = s;
618                   while (*s != ')' && *s != '\0')
619                     ++s;
620                   if (*s == ')')
621                     *s = '\0';
622                   else
623                     einfo ("%s:%d: warning: syntax error in import file\n",
624                            filename, lineno);
625                 }
626             }
627
628           continue;
629         }
630
631       /* This is a symbol to be imported or exported.  */
632       symname = s;
633       syscall = false;
634       address = (bfd_vma) -1;
635
636       while (! isspace ((unsigned char) *s) && *s != '\0')
637         ++s;
638       if (*s != '\0')
639         {
640           char *se;
641
642           *s++ = '\0';
643
644           while (isspace ((unsigned char) *s))
645             ++s;
646
647           se = s;
648           while (! isspace ((unsigned char) *se) && *se != '\0')
649             ++se;
650           if (*se != '\0')
651             {
652               *se++ = '\0';
653               while (isspace ((unsigned char) *se))
654                 ++se;
655               if (*se != '\0')
656                 einfo ("%s%d: warning: syntax error in import/export file\n",
657                        filename, lineno);
658             }
659
660           if (strcasecmp (s, "svc") == 0
661               || strcasecmp (s, "syscall") == 0)
662             syscall = true;
663           else
664             {
665               char *end;
666
667               address = strtoul (s, &end, 0);
668               if (*end != '\0')
669                 einfo ("%s:%d: warning: syntax error in import/export file\n",
670                        filename, lineno);
671             }
672         }
673
674       if (! import)
675         {
676           struct export_symbol_list *n;
677
678           ldlang_add_undef (symname);
679           n = ((struct export_symbol_list *)
680                xmalloc (sizeof (struct export_symbol_list)));
681           n->next = export_symbols;
682           n->name = buystring (symname);
683           n->syscall = syscall;
684           export_symbols = n;
685         }
686       else
687         {
688           h = bfd_link_hash_lookup (link_info.hash, symname, false, false,
689                                     true);
690           if (h == NULL || h->type == bfd_link_hash_new)
691             {
692               /* We can just ignore attempts to import an unreferenced
693                  symbol.  */
694             }
695           else
696             {
697               if (! bfd_xcoff_import_symbol (output_bfd, &link_info, h,
698                                              address, imppath, impfile,
699                                              impmember))
700                 einfo ("%X%s:%d: failed to import symbol %s: %E\n",
701                        filename, lineno, symname);
702             }
703         }
704
705       obstack_free (o, obstack_base (o));
706     }
707
708   if (obstack_object_size (o) > 0)
709     {
710       einfo ("%s:%d: warning: ignoring unterminated last line\n",
711              filename, lineno);
712       obstack_free (o, obstack_base (o));
713     }
714
715   if (! keep)
716     {
717       obstack_free (o, NULL);
718       free (o);
719     }
720 }
721
722 /* This routine saves us from worrying about declaring free.  */
723
724 static void
725 gld${EMULATION_NAME}_free (p)
726      PTR p;
727 {
728   free (p);
729 }
730
731 /* This is called by the before_allocation routine via
732    lang_for_each_statement.  It looks for relocations and assignments
733    to symbols.  */
734
735 static void
736 gld${EMULATION_NAME}_find_relocs (s)
737      lang_statement_union_type *s;
738 {
739   if (s->header.type == lang_reloc_statement_enum)
740     {
741       lang_reloc_statement_type *rs;
742
743       rs = &s->reloc_statement;
744       if (rs->name == NULL)
745         einfo ("%F%P: only relocations against symbols are permitted\n");
746       if (! bfd_xcoff_link_count_reloc (output_bfd, &link_info, rs->name))
747         einfo ("%F%P: bfd_xcoff_link_count_reloc failed: %E\n");
748     }
749
750   if (s->header.type == lang_assignment_statement_enum)
751     gld${EMULATION_NAME}_find_exp_assignment (s->assignment_statement.exp);
752 }
753
754 /* Look through an expression for an assignment statement.  */
755
756 static void
757 gld${EMULATION_NAME}_find_exp_assignment (exp)
758      etree_type *exp;
759 {
760   struct bfd_link_hash_entry *h;
761
762   switch (exp->type.node_class)
763     {
764     case etree_provide:
765       h = bfd_link_hash_lookup (link_info.hash, exp->assign.dst,
766                                 false, false, false);
767       if (h == NULL)
768         break;
769       /* Fall through.  */
770     case etree_assign:
771       if (strcmp (exp->assign.dst, ".") != 0)
772         {
773           if (! bfd_xcoff_record_link_assignment (output_bfd, &link_info,
774                                                   exp->assign.dst))
775             einfo ("%P%F: failed to record assignment to %s: %E\n",
776                    exp->assign.dst);
777         }
778       gld${EMULATION_NAME}_find_exp_assignment (exp->assign.src);
779       break;
780
781     case etree_binary:
782       gld${EMULATION_NAME}_find_exp_assignment (exp->binary.lhs);
783       gld${EMULATION_NAME}_find_exp_assignment (exp->binary.rhs);
784       break;
785
786     case etree_trinary:
787       gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.cond);
788       gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.lhs);
789       gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.rhs);
790       break;
791
792     case etree_unary:
793       gld${EMULATION_NAME}_find_exp_assignment (exp->unary.child);
794       break;
795
796     default:
797       break;
798     }
799 }
800
801 static char *
802 gld${EMULATION_NAME}_get_script(isfile)
803      int *isfile;
804 EOF
805
806 if test -n "$COMPILE_IN"
807 then
808 # Scripts compiled in.
809
810 # sed commands to quote an ld script as a C string.
811 sc="-f ${srcdir}/emultempl/stringify.sed"
812
813 cat >>e${EMULATION_NAME}.c <<EOF
814 {                            
815   *isfile = 0;
816
817   if (link_info.relocateable == true && config.build_constructors == true)
818     return
819 EOF
820 sed $sc ldscripts/${EMULATION_NAME}.xu                     >> e${EMULATION_NAME}.c
821 echo '  ; else if (link_info.relocateable == true) return' >> e${EMULATION_NAME}.c
822 sed $sc ldscripts/${EMULATION_NAME}.xr                     >> e${EMULATION_NAME}.c
823 echo '  ; else if (!config.text_read_only) return'         >> e${EMULATION_NAME}.c
824 sed $sc ldscripts/${EMULATION_NAME}.xbn                    >> e${EMULATION_NAME}.c
825 echo '  ; else if (!config.magic_demand_paged) return'     >> e${EMULATION_NAME}.c
826 sed $sc ldscripts/${EMULATION_NAME}.xn                     >> e${EMULATION_NAME}.c
827 echo '  ; else return'                                     >> e${EMULATION_NAME}.c
828 sed $sc ldscripts/${EMULATION_NAME}.x                      >> e${EMULATION_NAME}.c
829 echo '; }'                                                 >> e${EMULATION_NAME}.c
830
831 else
832 # Scripts read from the filesystem.
833
834 cat >>e${EMULATION_NAME}.c <<EOF
835 {                            
836   *isfile = 1;
837
838   if (link_info.relocateable == true && config.build_constructors == true)
839     return "ldscripts/${EMULATION_NAME}.xu";
840   else if (link_info.relocateable == true)
841     return "ldscripts/${EMULATION_NAME}.xr";
842   else if (!config.text_read_only)
843     return "ldscripts/${EMULATION_NAME}.xbn";
844   else if (!config.magic_demand_paged)
845     return "ldscripts/${EMULATION_NAME}.xn";
846   else
847     return "ldscripts/${EMULATION_NAME}.x";
848 }
849 EOF
850
851 fi
852
853 cat >>e${EMULATION_NAME}.c <<EOF
854
855 struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation = 
856 {
857   gld${EMULATION_NAME}_before_parse,
858   syslib_default,
859   hll_default,
860   after_parse_default,
861   gld${EMULATION_NAME}_after_open,
862   after_allocation_default,
863   set_output_arch_default,
864   ldemul_default_target,
865   gld${EMULATION_NAME}_before_allocation,
866   gld${EMULATION_NAME}_get_script,
867   "${EMULATION_NAME}",
868   "${OUTPUT_FORMAT}",
869   0,    /* finish */
870   0,    /* create_output_section_statements */
871   0,    /* open_dynamic_archive */
872   0,    /* place_orphan */
873   0,    /* set_symbols */
874   gld${EMULATION_NAME}_parse_args,
875 };
876 EOF