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