* emultempl/aix.em (gld${EMULATION_NAME}_before_allocation): Add
[external/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   asection *special_sections[6];
437   int i;
438
439   /* Handle the import and export files, if any.  */
440   for (fl = import_files; fl != NULL; fl = fl->next)
441     gld${EMULATION_NAME}_read_file (fl->name, true);
442   for (el = export_symbols; el != NULL; el = el->next)
443     {
444       struct bfd_link_hash_entry *h;
445
446       h = bfd_link_hash_lookup (link_info.hash, el->name, false, false, false);
447       if (h == NULL)
448         einfo ("%P%F: bfd_link_hash_lookup of export symbol failed: %E\n");
449       if (! bfd_xcoff_export_symbol (output_bfd, &link_info, h, el->syscall))
450         einfo ("%P%F: bfd_xcoff_export_symbol failed: %E\n");
451     }
452
453   /* Track down all relocations called for by the linker script (these
454      are typically constructor/destructor entries created by
455      CONSTRUCTORS) and let the backend know it will need to create
456      .loader relocs for them.  */
457   lang_for_each_statement (gld${EMULATION_NAME}_find_relocs);
458
459   /* We need to build LIBPATH from the -L arguments.  If any -rpath
460      arguments were used, though, we use -rpath instead, as a GNU
461      extension.  */
462   if (command_line.rpath != NULL)
463     libpath = command_line.rpath;
464   else if (search_head == NULL)
465     libpath = (char *) "";
466   else
467     {
468       size_t len;
469       search_dirs_type *search;
470
471       len = strlen (search_head->name);
472       libpath = xmalloc (len + 1);
473       strcpy (libpath, search_head->name);
474       for (search = search_head->next; search != NULL; search = search->next)
475         {
476           size_t nlen;
477
478           nlen = strlen (search->name);
479           libpath = xrealloc (libpath, len + nlen + 2);
480           libpath[len] = ':';
481           strcpy (libpath + len + 1, search->name);
482           len += nlen + 1;
483         }
484     }
485
486   /* Let the XCOFF backend set up the .loader section.  */
487   if (! bfd_xcoff_size_dynamic_sections (output_bfd, &link_info, libpath,
488                                          entry_symbol, file_align,
489                                          maxstack, maxdata,
490                                          gc ? true : false,
491                                          modtype,
492                                          textro ? true : false,
493                                          special_sections))
494     einfo ("%P%F: failed to set dynamic section sizes: %E\n");
495
496   /* Look through the special sections, and put them in the right
497      place in the link ordering.  This is especially magic.  */
498   for (i = 0; i < 6; i++)
499     {
500       asection *sec;
501       lang_output_section_statement_type *os;
502       lang_statement_union_type **pls;
503       lang_input_section_type *is;
504       const char *oname;
505       boolean start;
506
507       sec = special_sections[i];
508       if (sec == NULL)
509         continue;
510
511       /* Remove this section from the list of the output section.
512          This assumes we know what the script looks like.  */
513       is = NULL;
514       os = lang_output_section_find (sec->output_section->name);
515       if (os == NULL)
516         einfo ("%P%F: can't find output section %s\n",
517                sec->output_section->name);
518       for (pls = &os->children.head; *pls != NULL; pls = &(*pls)->next)
519         {
520           if ((*pls)->header.type == lang_input_section_enum
521               && (*pls)->input_section.section == sec)
522             {
523               is = (lang_input_section_type *) *pls;
524               *pls = (*pls)->next;
525               break;
526             }
527           if ((*pls)->header.type == lang_wild_statement_enum)
528             {
529               lang_statement_union_type **pwls;
530
531               for (pwls = &(*pls)->wild_statement.children.head;
532                    *pwls != NULL;
533                    pwls = &(*pwls)->next)
534                 {
535                   if ((*pwls)->header.type == lang_input_section_enum
536                       && (*pwls)->input_section.section == sec)
537                     {
538                       is = (lang_input_section_type *) *pwls;
539                       *pwls = (*pwls)->next;
540                       break;
541                     }
542                 }
543               if (is != NULL)
544                 break;
545             }
546         }       
547
548       if (is == NULL)
549         einfo ("%P%F: can't find %s in output section\n",
550                bfd_get_section_name (sec->owner, sec));
551
552       /* Now figure out where the section should go.  */
553       switch (i)
554         {
555         default: /* to avoid warnings */
556         case 0:
557           /* _text */
558           oname = ".text";
559           start = true;
560           break;
561         case 1:
562           /* _etext */
563           oname = ".text";
564           start = false;
565           break;
566         case 2:
567           /* _data */
568           oname = ".data";
569           start = true;
570           break;
571         case 3:
572           /* _edata */
573           oname = ".data";
574           start = false;
575           break;
576         case 4:
577         case 5:
578           /* _end and end */
579           oname = ".bss";
580           start = false;
581           break;
582         }
583
584       os = lang_output_section_find (oname);
585
586       if (start)
587         {
588           is->header.next = os->children.head;
589           os->children.head = (lang_statement_union_type *) is;
590         }
591       else
592         {
593           is->header.next = NULL;
594           lang_statement_append (&os->children,
595                                  (lang_statement_union_type *) is,
596                                  &is->header.next);
597         }
598     }
599 }
600
601 /* Read an import or export file.  For an import file, this is called
602    by the before_allocation emulation routine.  For an export file,
603    this is called by the parse_args emulation routine.  */
604
605 static void
606 gld${EMULATION_NAME}_read_file (filename, import)
607      const char *filename;
608      boolean import;
609 {
610   struct obstack *o;
611   FILE *f;
612   int lineno;
613   int c;
614   boolean keep;
615   const char *imppath;
616   const char *impfile;
617   const char *impmember;
618
619   o = (struct obstack *) xmalloc (sizeof (struct obstack));
620   obstack_specify_allocation (o, 0, 0, xmalloc, gld${EMULATION_NAME}_free);
621
622   f = fopen (filename, "r");
623   if (f == NULL)
624     {
625       bfd_set_error (bfd_error_system_call);
626       einfo ("%F%s: %E\n", filename);
627     }
628
629   keep = false;
630
631   imppath = NULL;
632   impfile = NULL;
633   impmember = NULL;
634
635   lineno = 0;
636   while ((c = getc (f)) != EOF)
637     {
638       char *s;
639       char *symname;
640       boolean syscall;
641       bfd_vma address;
642       struct bfd_link_hash_entry *h;
643
644       if (c != '\n')
645         {
646           obstack_1grow (o, c);
647           continue;
648         }
649
650       obstack_1grow (o, '\0');
651       ++lineno;
652
653       s = (char *) obstack_base (o);
654       while (isspace ((unsigned char) *s))
655         ++s;
656       if (*s == '\0'
657           || *s == '*'
658           || (*s == '#' && s[1] == ' ')
659           || (! import && *s == '#' && s[1] == '!'))
660         {
661           obstack_free (o, obstack_base (o));
662           continue;
663         }
664
665       if (*s == '#' && s[1] == '!')
666         {
667           s += 2;
668           while (isspace ((unsigned char) *s))
669             ++s;
670           if (*s == '\0')
671             {
672               imppath = NULL;
673               impfile = NULL;
674               impmember = NULL;
675               obstack_free (o, obstack_base (o));
676             }
677           else if (*s == '(')
678             einfo ("%F%s%d: #! ([member]) is not supported in import files",
679                    filename, lineno);
680           else
681             {
682               char cs;
683               char *file;
684
685               (void) obstack_finish (o);
686               keep = true;
687               imppath = s;
688               impfile = NULL;
689               while (! isspace ((unsigned char) *s) && *s != '(' && *s != '\0')
690                 {
691                   if (*s == '/')
692                     file = s + 1;
693                   ++s;
694                 }
695               if (file != NULL)
696                 {
697                   file[-1] = '\0';
698                   impfile = file;
699                   if (imppath == file - 1)
700                     imppath = "/";
701                 }
702               else
703                 {
704                   impfile = imppath;
705                   imppath = "";
706                 }
707               cs = *s;
708               *s = '\0';
709               while (isspace ((unsigned char) cs))
710                 {
711                   ++s;
712                   cs = *s;
713                 }
714               if (cs != '(')
715                 {
716                   impmember = "";
717                   if (cs != '\0')
718                     einfo ("%s:%d: warning: syntax error in import file\n",
719                            filename, lineno);
720                 }
721               else
722                 {
723                   ++s;
724                   impmember = s;
725                   while (*s != ')' && *s != '\0')
726                     ++s;
727                   if (*s == ')')
728                     *s = '\0';
729                   else
730                     einfo ("%s:%d: warning: syntax error in import file\n",
731                            filename, lineno);
732                 }
733             }
734
735           continue;
736         }
737
738       /* This is a symbol to be imported or exported.  */
739       symname = s;
740       syscall = false;
741       address = (bfd_vma) -1;
742
743       while (! isspace ((unsigned char) *s) && *s != '\0')
744         ++s;
745       if (*s != '\0')
746         {
747           char *se;
748
749           *s++ = '\0';
750
751           while (isspace ((unsigned char) *s))
752             ++s;
753
754           se = s;
755           while (! isspace ((unsigned char) *se) && *se != '\0')
756             ++se;
757           if (*se != '\0')
758             {
759               *se++ = '\0';
760               while (isspace ((unsigned char) *se))
761                 ++se;
762               if (*se != '\0')
763                 einfo ("%s%d: warning: syntax error in import/export file\n",
764                        filename, lineno);
765             }
766
767           if (strcasecmp (s, "svc") == 0
768               || strcasecmp (s, "syscall") == 0)
769             syscall = true;
770           else
771             {
772               char *end;
773
774               address = strtoul (s, &end, 0);
775               if (*end != '\0')
776                 einfo ("%s:%d: warning: syntax error in import/export file\n",
777                        filename, lineno);
778             }
779         }
780
781       if (! import)
782         {
783           struct export_symbol_list *n;
784
785           ldlang_add_undef (symname);
786           n = ((struct export_symbol_list *)
787                xmalloc (sizeof (struct export_symbol_list)));
788           n->next = export_symbols;
789           n->name = buystring (symname);
790           n->syscall = syscall;
791           export_symbols = n;
792         }
793       else
794         {
795           h = bfd_link_hash_lookup (link_info.hash, symname, false, false,
796                                     true);
797           if (h == NULL || h->type == bfd_link_hash_new)
798             {
799               /* We can just ignore attempts to import an unreferenced
800                  symbol.  */
801             }
802           else
803             {
804               if (! bfd_xcoff_import_symbol (output_bfd, &link_info, h,
805                                              address, imppath, impfile,
806                                              impmember))
807                 einfo ("%X%s:%d: failed to import symbol %s: %E\n",
808                        filename, lineno, symname);
809             }
810         }
811
812       obstack_free (o, obstack_base (o));
813     }
814
815   if (obstack_object_size (o) > 0)
816     {
817       einfo ("%s:%d: warning: ignoring unterminated last line\n",
818              filename, lineno);
819       obstack_free (o, obstack_base (o));
820     }
821
822   if (! keep)
823     {
824       obstack_free (o, NULL);
825       free (o);
826     }
827 }
828
829 /* This routine saves us from worrying about declaring free.  */
830
831 static void
832 gld${EMULATION_NAME}_free (p)
833      PTR p;
834 {
835   free (p);
836 }
837
838 /* This is called by the before_allocation routine via
839    lang_for_each_statement.  It looks for relocations and assignments
840    to symbols.  */
841
842 static void
843 gld${EMULATION_NAME}_find_relocs (s)
844      lang_statement_union_type *s;
845 {
846   if (s->header.type == lang_reloc_statement_enum)
847     {
848       lang_reloc_statement_type *rs;
849
850       rs = &s->reloc_statement;
851       if (rs->name == NULL)
852         einfo ("%F%P: only relocations against symbols are permitted\n");
853       if (! bfd_xcoff_link_count_reloc (output_bfd, &link_info, rs->name))
854         einfo ("%F%P: bfd_xcoff_link_count_reloc failed: %E\n");
855     }
856
857   if (s->header.type == lang_assignment_statement_enum)
858     gld${EMULATION_NAME}_find_exp_assignment (s->assignment_statement.exp);
859 }
860
861 /* Look through an expression for an assignment statement.  */
862
863 static void
864 gld${EMULATION_NAME}_find_exp_assignment (exp)
865      etree_type *exp;
866 {
867   struct bfd_link_hash_entry *h;
868
869   switch (exp->type.node_class)
870     {
871     case etree_provide:
872       h = bfd_link_hash_lookup (link_info.hash, exp->assign.dst,
873                                 false, false, false);
874       if (h == NULL)
875         break;
876       /* Fall through.  */
877     case etree_assign:
878       if (strcmp (exp->assign.dst, ".") != 0)
879         {
880           if (! bfd_xcoff_record_link_assignment (output_bfd, &link_info,
881                                                   exp->assign.dst))
882             einfo ("%P%F: failed to record assignment to %s: %E\n",
883                    exp->assign.dst);
884         }
885       gld${EMULATION_NAME}_find_exp_assignment (exp->assign.src);
886       break;
887
888     case etree_binary:
889       gld${EMULATION_NAME}_find_exp_assignment (exp->binary.lhs);
890       gld${EMULATION_NAME}_find_exp_assignment (exp->binary.rhs);
891       break;
892
893     case etree_trinary:
894       gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.cond);
895       gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.lhs);
896       gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.rhs);
897       break;
898
899     case etree_unary:
900       gld${EMULATION_NAME}_find_exp_assignment (exp->unary.child);
901       break;
902
903     default:
904       break;
905     }
906 }
907
908 static char *
909 gld${EMULATION_NAME}_get_script(isfile)
910      int *isfile;
911 EOF
912
913 if test -n "$COMPILE_IN"
914 then
915 # Scripts compiled in.
916
917 # sed commands to quote an ld script as a C string.
918 sc="-f ${srcdir}/emultempl/stringify.sed"
919
920 cat >>e${EMULATION_NAME}.c <<EOF
921 {                            
922   *isfile = 0;
923
924   if (link_info.relocateable == true && config.build_constructors == true)
925     return
926 EOF
927 sed $sc ldscripts/${EMULATION_NAME}.xu                     >> e${EMULATION_NAME}.c
928 echo '  ; else if (link_info.relocateable == true) return' >> e${EMULATION_NAME}.c
929 sed $sc ldscripts/${EMULATION_NAME}.xr                     >> e${EMULATION_NAME}.c
930 echo '  ; else if (!config.text_read_only) return'         >> e${EMULATION_NAME}.c
931 sed $sc ldscripts/${EMULATION_NAME}.xbn                    >> e${EMULATION_NAME}.c
932 echo '  ; else if (!config.magic_demand_paged) return'     >> e${EMULATION_NAME}.c
933 sed $sc ldscripts/${EMULATION_NAME}.xn                     >> e${EMULATION_NAME}.c
934 echo '  ; else return'                                     >> e${EMULATION_NAME}.c
935 sed $sc ldscripts/${EMULATION_NAME}.x                      >> e${EMULATION_NAME}.c
936 echo '; }'                                                 >> e${EMULATION_NAME}.c
937
938 else
939 # Scripts read from the filesystem.
940
941 cat >>e${EMULATION_NAME}.c <<EOF
942 {                            
943   *isfile = 1;
944
945   if (link_info.relocateable == true && config.build_constructors == true)
946     return "ldscripts/${EMULATION_NAME}.xu";
947   else if (link_info.relocateable == true)
948     return "ldscripts/${EMULATION_NAME}.xr";
949   else if (!config.text_read_only)
950     return "ldscripts/${EMULATION_NAME}.xbn";
951   else if (!config.magic_demand_paged)
952     return "ldscripts/${EMULATION_NAME}.xn";
953   else
954     return "ldscripts/${EMULATION_NAME}.x";
955 }
956 EOF
957
958 fi
959
960 cat >>e${EMULATION_NAME}.c <<EOF
961
962 struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation = 
963 {
964   gld${EMULATION_NAME}_before_parse,
965   syslib_default,
966   hll_default,
967   after_parse_default,
968   gld${EMULATION_NAME}_after_open,
969   after_allocation_default,
970   set_output_arch_default,
971   ldemul_default_target,
972   gld${EMULATION_NAME}_before_allocation,
973   gld${EMULATION_NAME}_get_script,
974   "${EMULATION_NAME}",
975   "${OUTPUT_FORMAT}",
976   0,    /* finish */
977   0,    /* create_output_section_statements */
978   0,    /* open_dynamic_archive */
979   0,    /* place_orphan */
980   0,    /* set_symbols */
981   gld${EMULATION_NAME}_parse_args,
982 };
983 EOF