1# This shell script emits a C file. -*- C -*-
2# It does some substitutions.
3if [ -z "$MACHINE" ]; then
4  OUTPUT_ARCH=${ARCH}
5else
6  OUTPUT_ARCH=${ARCH}:${MACHINE}
7fi
8fragment <<EOF
9/* This file is is generated by a shell script.  DO NOT EDIT! */
10
11/* AIX emulation code for ${EMULATION_NAME}
12   Copyright (C) 1991-2020 Free Software Foundation, Inc.
13   Written by Steve Chamberlain <sac@cygnus.com>
14   AIX support by Ian Lance Taylor <ian@cygnus.com>
15   AIX 64 bit support by Tom Rix <trix@redhat.com>
16
17   This file is part of the GNU Binutils.
18
19   This program is free software; you can redistribute it and/or modify
20   it under the terms of the GNU General Public License as published by
21   the Free Software Foundation; either version 3 of the License, or
22   (at your option) any later version.
23
24   This program is distributed in the hope that it will be useful,
25   but WITHOUT ANY WARRANTY; without even the implied warranty of
26   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27   GNU General Public License for more details.
28
29   You should have received a copy of the GNU General Public License
30   along with this program; if not, write to the Free Software
31   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
32   MA 02110-1301, USA.  */
33
34#define TARGET_IS_${EMULATION_NAME}
35
36#include "sysdep.h"
37#include "bfd.h"
38#include "libiberty.h"
39#include "safe-ctype.h"
40#include "getopt.h"
41#include "obstack.h"
42#include "bfdlink.h"
43#include "ctf-api.h"
44
45#include "ld.h"
46#include "ldmain.h"
47#include "ldmisc.h"
48#include "ldexp.h"
49#include "ldlang.h"
50#include "ldfile.h"
51#include "ldemul.h"
52#include "ldctor.h"
53#include <ldgram.h>
54
55#include "coff/internal.h"
56#include "coff/xcoff.h"
57#include "libcoff.h"
58#include "libxcoff.h"
59#include "xcofflink.h"
60
61static void gld${EMULATION_NAME}_read_file (const char *, bfd_boolean);
62static void gld${EMULATION_NAME}_free (void *);
63static void gld${EMULATION_NAME}_find_relocs (lang_statement_union_type *);
64static void gld${EMULATION_NAME}_find_exp_assignment (etree_type *);
65
66
67/* The file alignment required for each section.  */
68static unsigned long file_align;
69
70/* The maximum size the stack is permitted to grow.  This is stored in
71   the a.out header.  */
72static unsigned long maxstack;
73
74/* The maximum data size.  This is stored in the a.out header.  */
75static unsigned long maxdata;
76
77/* Whether to perform garbage collection.  */
78static int gc = 1;
79
80/* The module type to use.  */
81static unsigned short modtype = ('1' << 8) | 'L';
82
83/* Whether the .text section must be read-only (i.e., no relocs
84   permitted).  */
85static int textro;
86
87/* A mask of XCOFF_EXPALL and XCOFF_EXPFULL flags, as set by their
88   associated -b and -bno options.  */
89static unsigned int auto_export_flags;
90
91/* A mask of auto_export_flags bits that were explicitly set on the
92   command line.  */
93static unsigned int explicit_auto_export_flags;
94
95/* Whether to implement Unix like linker semantics.  */
96static int unix_ld;
97
98/* Structure used to hold import file list.  */
99
100struct filelist
101{
102  struct filelist *next;
103  const char *name;
104};
105
106/* List of import files.  */
107static struct filelist *import_files;
108
109/* List of export symbols read from the export files.  */
110
111struct export_symbol_list
112{
113  struct export_symbol_list *next;
114  const char *name;
115};
116
117static struct export_symbol_list *export_symbols;
118
119/* Maintains the 32 or 64 bit mode state of import file */
120static unsigned int symbol_mode = 0x04;
121
122/* Which symbol modes are valid */
123static unsigned int symbol_mode_mask = 0x0d;
124
125/* Whether this is a 64 bit link */
126static int is_64bit = 0;
127
128/* Which syscalls from import file are valid */
129static unsigned int syscall_mask = 0x77;
130
131/* fake file for -binitfini support */
132static lang_input_statement_type *initfini_file;
133
134/* Whether to do run time linking
135   -brtl enables, -bnortl and -bnortllib disable. */
136static int rtld;
137
138/* Explicit command line library path, -blibpath */
139static char *command_line_blibpath = NULL;
140
141/* This routine is called before anything else is done.  */
142
143static void
144gld${EMULATION_NAME}_before_parse (void)
145{
146  ldfile_set_output_arch ("${OUTPUT_ARCH}", bfd_arch_`echo ${ARCH} | sed -e 's/:.*//'`);
147
148  input_flags.dynamic = TRUE;
149  config.has_shared = TRUE;
150
151  /* The link_info.[init|fini]_functions are initialized in ld/lexsup.c.
152     Override them here so we can use the link_info.init_function as a
153     state flag that lets the backend know that -binitfini has been done.  */
154
155  link_info.init_function = NULL;
156  link_info.fini_function = NULL;
157}
158
159/* Handle AIX specific options.  */
160
161enum
162  {
163    OPTION_IGNORE = 300,
164    OPTION_AUTOIMP,
165    OPTION_ERNOTOK,
166    OPTION_EROK,
167    OPTION_EXPALL,
168    OPTION_EXPFULL,
169    OPTION_EXPORT,
170    OPTION_IMPORT,
171    OPTION_INITFINI,
172    OPTION_LOADMAP,
173    OPTION_MAXDATA,
174    OPTION_MAXSTACK,
175    OPTION_MODTYPE,
176    OPTION_NOAUTOIMP,
177    OPTION_NOEXPALL,
178    OPTION_NOEXPFULL,
179    OPTION_NOSTRCMPCT,
180    OPTION_PD,
181    OPTION_PT,
182    OPTION_STRCMPCT,
183    OPTION_UNIX,
184    OPTION_32,
185    OPTION_64,
186    OPTION_LIBPATH,
187    OPTION_NOLIBPATH,
188  };
189
190static void
191gld${EMULATION_NAME}_add_options
192  (int ns, char **shortopts, int nl, struct option **longopts,
193   int nrl ATTRIBUTE_UNUSED, struct option **really_longopts ATTRIBUTE_UNUSED)
194{
195  static const char xtra_short[] = "D:H:KT:z";
196  static const struct option xtra_long[] = {
197  /* -binitfini has special handling in the linker backend.  The native linker
198     uses the arguemnts to generate a table of init and fini functions for
199     the executable.  The important use for this option is to support aix 4.2+
200     c++ constructors and destructors.  This is tied into gcc via collect2.c.
201
202     The function table is accessed by the runtime linker/loader by checking if
203     the first symbol in the loader symbol table is __rtinit.  The gnu linker
204     generates this symbol and makes it the first loader symbol.  */
205
206    {"basis", no_argument, NULL, OPTION_IGNORE},
207    {"bautoimp", no_argument, NULL, OPTION_AUTOIMP},
208    {"bcomprld", no_argument, NULL, OPTION_IGNORE},
209    {"bcrld", no_argument, NULL, OPTION_IGNORE},
210    {"bcror31", no_argument, NULL, OPTION_IGNORE},
211    {"bD", required_argument, NULL, OPTION_MAXDATA},
212    {"bE", required_argument, NULL, OPTION_EXPORT},
213    {"bernotok", no_argument, NULL, OPTION_ERNOTOK},
214    {"berok", no_argument, NULL, OPTION_EROK},
215    {"berrmsg", no_argument, NULL, OPTION_IGNORE},
216    {"bexpall", no_argument, NULL, OPTION_EXPALL},
217    {"bexpfull", no_argument, NULL, OPTION_EXPFULL},
218    {"bexport", required_argument, NULL, OPTION_EXPORT},
219    {"bbigtoc", no_argument, NULL, OPTION_IGNORE},
220    {"bf", no_argument, NULL, OPTION_ERNOTOK},
221    {"bgc", no_argument, &gc, 1},
222    {"bh", required_argument, NULL, OPTION_IGNORE},
223    {"bhalt", required_argument, NULL, OPTION_IGNORE},
224    {"bI", required_argument, NULL, OPTION_IMPORT},
225    {"bimport", required_argument, NULL, OPTION_IMPORT},
226    {"binitfini", required_argument, NULL, OPTION_INITFINI},
227    {"bl", required_argument, NULL, OPTION_LOADMAP},
228    {"bloadmap", required_argument, NULL, OPTION_LOADMAP},
229    {"bmaxdata", required_argument, NULL, OPTION_MAXDATA},
230    {"bmaxstack", required_argument, NULL, OPTION_MAXSTACK},
231    {"bM", required_argument, NULL, OPTION_MODTYPE},
232    {"bmodtype", required_argument, NULL, OPTION_MODTYPE},
233    {"bnoautoimp", no_argument, NULL, OPTION_NOAUTOIMP},
234    {"bnoexpall", no_argument, NULL, OPTION_NOEXPALL},
235    {"bnoexpfull", no_argument, NULL, OPTION_NOEXPFULL},
236    {"bnodelcsect", no_argument, NULL, OPTION_IGNORE},
237    {"bnoentry", no_argument, NULL, OPTION_IGNORE},
238    {"bnogc", no_argument, &gc, 0},
239    {"bnso", no_argument, NULL, OPTION_NOAUTOIMP},
240    {"bnostrcmpct", no_argument, NULL, OPTION_NOSTRCMPCT},
241    {"bnotextro", no_argument, &textro, 0},
242    {"bnro", no_argument, &textro, 0},
243    {"bpD", required_argument, NULL, OPTION_PD},
244    {"bpT", required_argument, NULL, OPTION_PT},
245    {"bro", no_argument, &textro, 1},
246    {"brtl", no_argument, &rtld, 1},
247    {"bnortl", no_argument, &rtld, 0},
248    {"bnortllib", no_argument, &rtld, 0},
249    {"bS", required_argument, NULL, OPTION_MAXSTACK},
250    {"bso", no_argument, NULL, OPTION_AUTOIMP},
251    {"bstrcmpct", no_argument, NULL, OPTION_STRCMPCT},
252    {"btextro", no_argument, &textro, 1},
253    {"b32", no_argument, NULL, OPTION_32},
254    {"b64", no_argument, NULL, OPTION_64},
255    {"static", no_argument, NULL, OPTION_NOAUTOIMP},
256    {"unix", no_argument, NULL, OPTION_UNIX},
257    {"blibpath", required_argument, NULL, OPTION_LIBPATH},
258    {"bnolibpath", required_argument, NULL, OPTION_NOLIBPATH},
259    {NULL, no_argument, NULL, 0}
260  };
261
262  /* Options supported by the AIX linker which we do not support:
263     -S, -v, -Z, -bbindcmds, -bbinder, -bbindopts, -bcalls, -bcaps,
264     -bcror15, -bdebugopt, -bdbg, -bdelcsect, -bex?, -bfilelist, -bfl,
265     -bgcbypass, -bglink, -binsert, -bi, -bloadmap, -bl, -bmap, -bnl,
266     -bnobind, -bnocomprld, -bnocrld, -bnoerrmsg, -bnoglink,
267     -bnoloadmap, -bnl, -bnoobjreorder, -bnoquiet, -bnoreorder,
268     -bnotypchk, -bnox, -bquiet, -bR, -brename, -breorder, -btypchk,
269     -bx, -bX, -bxref.  */
270
271  *shortopts = (char *) xrealloc (*shortopts, ns + sizeof (xtra_short));
272  memcpy (*shortopts + ns, &xtra_short, sizeof (xtra_short));
273  *longopts = xrealloc (*longopts,
274			nl * sizeof (struct option) + sizeof (xtra_long));
275  memcpy (*longopts + nl, &xtra_long, sizeof (xtra_long));
276}
277
278static bfd_boolean
279gld${EMULATION_NAME}_parse_args (int argc, char **argv)
280{
281  int indx;
282
283  /* If the current option starts with -b, change the first : to an =.
284     The AIX linker uses : to separate the option from the argument;
285     changing it to = lets us treat it as a getopt option.  */
286  indx = optind;
287  if (indx == 0)
288    indx = 1;
289
290  if (indx < argc && CONST_STRNEQ (argv[indx], "-b"))
291    {
292      char *s;
293
294      for (s = argv[indx]; *s != '\0'; s++)
295	{
296	  if (*s == ':')
297	    {
298	      *s = '=';
299	      break;
300	    }
301	}
302    }
303  return FALSE;
304}
305
306/* Helper for option '-f', which specify a list of input files.
307   Contrary to the native linker, we don't support shell patterns
308   (simply because glob isn't always available).  */
309
310static void
311read_file_list (const char *filename)
312{
313  FILE *f;
314  /* An upper bound on the number of characters in the file.  */
315  long pos;
316  /* File in memory.  */
317  char *buffer;
318  size_t len;
319  char *b;
320  char *e;
321
322  f = fopen (filename, FOPEN_RT);
323  if (f == NULL)
324    {
325      einfo (_("%F%P: cannot open %s\n"), filename);
326      return;
327    }
328  if (fseek (f, 0L, SEEK_END) == -1)
329    goto error;
330  pos = ftell (f);
331  if (pos == -1)
332    goto error;
333  if (fseek (f, 0L, SEEK_SET) == -1)
334    goto error;
335
336  buffer = (char *) xmalloc (pos + 1);
337  len = fread (buffer, sizeof (char), pos, f);
338  if (len != (size_t) pos && ferror (f))
339    goto error;
340  /* Add a NUL terminator.  */
341  buffer[len] = '\0';
342  fclose (f);
343
344  /* Parse files.  */
345  b = buffer;
346  while (1)
347    {
348      /* Skip empty lines.  */
349      while (*b == '\n' || *b == '\r')
350	b++;
351
352      /* Stop if end of buffer.  */
353      if (b == buffer + len)
354	break;
355
356      /* Eat any byte until end of line.  */
357      for (e = b; *e != '\0'; e++)
358	if (*e == '\n' || *e == '\r')
359	  break;
360
361      /* Replace end of line by nul.  */
362      if (*e != '\0')
363	*e++ = '\0';
364
365      if (b != e)
366	lang_add_input_file (b, lang_input_file_is_search_file_enum, NULL);
367      b = e;
368    }
369  return;
370
371 error:
372  einfo (_("%F%P: cannot read %s\n"), optarg);
373  fclose (f);
374}
375
376static bfd_boolean
377gld${EMULATION_NAME}_handle_option (int optc)
378{
379  bfd_signed_vma val;
380  const char *end;
381
382  switch (optc)
383    {
384    default:
385      return FALSE;
386
387    case 0:
388      /* Long option which just sets a flag.  */
389      break;
390
391    case 'f':
392      /* This overrides --auxiliary.  This option specifies a file containing
393	 a list of input files.  */
394      read_file_list (optarg);
395      break;
396
397    case 'D':
398      val = bfd_scan_vma (optarg, &end, 0);
399      if (*end != '\0')
400	einfo (_("%P: warning: ignoring invalid -D number %s\n"), optarg);
401      else if (val != -1)
402	lang_section_start (".data", exp_intop (val), NULL);
403      break;
404
405    case 'H':
406      val = bfd_scan_vma (optarg, &end, 0);
407      if (*end != '\0' || (val & (val - 1)) != 0)
408	einfo (_("%P: warning: ignoring invalid -H number %s\n"), optarg);
409      else
410	file_align = val;
411      break;
412
413    case 'K':
414    case 'z':
415      /* FIXME: This should use the page size for the target system.  */
416      file_align = 4096;
417      break;
418
419    case 'T':
420      /* On AIX this is the same as GNU ld -Ttext.  When we see -T
421	 number, we assume the AIX option is intended.  Otherwise, we
422	 assume the usual GNU ld -T option is intended.  We can't just
423	 ignore the AIX option, because gcc passes it to the linker.  */
424      val = bfd_scan_vma (optarg, &end, 0);
425      if (*end != '\0')
426	return FALSE;
427      lang_section_start (".text", exp_intop (val), NULL);
428      break;
429
430    case OPTION_IGNORE:
431      break;
432
433    case OPTION_INITFINI:
434      {
435	/*
436	 * The aix linker init fini has the format :
437	 *
438	 * -binitfini:[ Initial][:Termination][:Priority]
439	 *
440	 * it allows the Termination and Priority to be optional.
441	 *
442	 * Since we support only one init/fini pair, we ignore the Priority.
443	 *
444	 * Define the special symbol __rtinit.
445	 *
446	 * strtok does not correctly handle the case of -binitfini::fini: so
447	 * do it by hand
448	 */
449	char *t, *i, *f;
450
451	i = t = optarg;
452	while (*t && ':' != *t)
453	  t++;
454	if (*t)
455	  *t++ = 0;
456
457	if (0 != strlen (i))
458	  link_info.init_function = i;
459
460	f = t;
461	while (*t && ':' != *t)
462	  t++;
463	*t = 0;
464
465	if (0 != strlen (f))
466	  link_info.fini_function = f;
467      }
468      break;
469
470    case OPTION_AUTOIMP:
471      link_info.static_link = FALSE;
472      break;
473
474    case OPTION_ERNOTOK:
475      link_info.unresolved_syms_in_objects = RM_GENERATE_ERROR;
476      link_info.unresolved_syms_in_shared_libs = RM_GENERATE_ERROR;
477      break;
478
479    case OPTION_EROK:
480      link_info.unresolved_syms_in_objects = RM_IGNORE;
481      link_info.unresolved_syms_in_shared_libs = RM_IGNORE;
482      break;
483
484    case OPTION_EXPALL:
485      auto_export_flags |= XCOFF_EXPALL;
486      explicit_auto_export_flags |= XCOFF_EXPALL;
487      break;
488
489    case OPTION_EXPFULL:
490      auto_export_flags |= XCOFF_EXPFULL;
491      explicit_auto_export_flags |= XCOFF_EXPFULL;
492      break;
493
494    case OPTION_EXPORT:
495      gld${EMULATION_NAME}_read_file (optarg, FALSE);
496      break;
497
498    case OPTION_IMPORT:
499      {
500	struct filelist *n;
501	struct filelist **flpp;
502
503	n = (struct filelist *) xmalloc (sizeof (struct filelist));
504	n->next = NULL;
505	n->name = optarg;
506	flpp = &import_files;
507	while (*flpp != NULL)
508	  flpp = &(*flpp)->next;
509	*flpp = n;
510      }
511      break;
512
513    case OPTION_LOADMAP:
514      config.map_filename = optarg;
515      break;
516
517    case OPTION_MAXDATA:
518      val = bfd_scan_vma (optarg, &end, 0);
519      if (*end != '\0')
520	einfo (_("%P: warning: ignoring invalid -bmaxdata number %s\n"),
521	       optarg);
522      else
523	maxdata = val;
524      break;
525
526    case OPTION_MAXSTACK:
527      val = bfd_scan_vma (optarg, &end, 0);
528      if (*end != '\0')
529	einfo (_("%P: warning: ignoring invalid -bmaxstack number %s\n"),
530	       optarg);
531      else
532	maxstack = val;
533      break;
534
535    case OPTION_MODTYPE:
536      if (*optarg == 'S')
537	{
538	  link_info.type = type_dll;
539	  ++optarg;
540	}
541      if (*optarg == '\0' || optarg[1] == '\0')
542	einfo (_("%P: warning: ignoring invalid module type %s\n"), optarg);
543      else
544	modtype = (*optarg << 8) | optarg[1];
545      break;
546
547    case OPTION_NOAUTOIMP:
548      link_info.static_link = TRUE;
549      break;
550
551    case OPTION_NOEXPALL:
552      auto_export_flags &= ~XCOFF_EXPALL;
553      explicit_auto_export_flags |= XCOFF_EXPALL;
554      break;
555
556    case OPTION_NOEXPFULL:
557      auto_export_flags &= ~XCOFF_EXPFULL;
558      explicit_auto_export_flags |= XCOFF_EXPFULL;
559      break;
560
561    case OPTION_NOSTRCMPCT:
562      link_info.traditional_format = TRUE;
563      break;
564
565    case OPTION_PD:
566      /* This sets the page that the .data section is supposed to
567	 start on.  The offset within the page should still be the
568	 offset within the file, so we need to build an appropriate
569	 expression.  */
570      val = bfd_scan_vma (optarg, &end, 0);
571      if (*end != '\0')
572	einfo (_("%P: warning: ignoring invalid -pD number %s\n"), optarg);
573      else
574	{
575	  etree_type *t;
576
577	  t = exp_binop ('+',
578			 exp_intop (val),
579			 exp_binop ('&',
580				    exp_nameop (NAME, "."),
581				    exp_intop (0xfff)));
582	  t = exp_binop ('&',
583			 exp_binop ('+', t, exp_intop (31)),
584			 exp_intop (~(bfd_vma) 31));
585	  lang_section_start (".data", t, NULL);
586	}
587      break;
588
589    case OPTION_PT:
590      /* This set the page that the .text section is supposed to start
591	 on.  The offset within the page should still be the offset
592	 within the file.  */
593      val = bfd_scan_vma (optarg, &end, 0);
594      if (*end != '\0')
595	einfo (_("%P: warning: ignoring invalid -pT number %s\n"), optarg);
596      else
597	{
598	  etree_type *t;
599
600	  t = exp_binop ('+',
601			 exp_intop (val),
602			 exp_nameop (SIZEOF_HEADERS, NULL));
603	  t = exp_binop ('&',
604			 exp_binop ('+', t, exp_intop (31)),
605			 exp_intop (~(bfd_vma) 31));
606	  lang_section_start (".text", t, NULL);
607	}
608      break;
609
610    case OPTION_STRCMPCT:
611      link_info.traditional_format = FALSE;
612      break;
613
614    case OPTION_UNIX:
615      unix_ld = TRUE;
616      break;
617
618    case OPTION_32:
619      is_64bit = 0;
620      syscall_mask = 0x77;
621      symbol_mode_mask = 0x0d;
622      break;
623
624    case OPTION_64:
625      is_64bit = 1;
626      syscall_mask = 0xcc;
627      symbol_mode_mask = 0x0e;
628      break;
629
630    case OPTION_LIBPATH:
631      command_line_blibpath = optarg;
632      break;
633
634    case OPTION_NOLIBPATH:
635      command_line_blibpath = NULL;
636      break;
637
638    }
639
640  return TRUE;
641}
642
643/* This is called when an input file can not be recognized as a BFD
644   object or an archive.  If the file starts with #!, we must treat it
645   as an import file.  This is for AIX compatibility.  */
646
647static bfd_boolean
648gld${EMULATION_NAME}_unrecognized_file (lang_input_statement_type *entry)
649{
650  FILE *e;
651  bfd_boolean ret;
652
653  e = fopen (entry->filename, FOPEN_RT);
654  if (e == NULL)
655    return FALSE;
656
657  ret = FALSE;
658
659  if (getc (e) == '#' && getc (e) == '!')
660    {
661      struct filelist *n;
662      struct filelist **flpp;
663
664      n = (struct filelist *) xmalloc (sizeof (struct filelist));
665      n->next = NULL;
666      n->name = entry->filename;
667      flpp = &import_files;
668      while (*flpp != NULL)
669	flpp = &(*flpp)->next;
670      *flpp = n;
671
672      ret = TRUE;
673      entry->flags.loaded = TRUE;
674    }
675
676  fclose (e);
677
678  return ret;
679}
680
681/* This is called after the input files have been opened.  */
682
683static void
684gld${EMULATION_NAME}_after_open (void)
685{
686  enum output_type t;
687  struct set_info *p;
688
689  after_open_default ();
690
691  /* Call ldctor_build_sets, after pretending that this is a
692     relocatable link.  We do this because AIX requires relocation
693     entries for all references to symbols, even in a final
694     executable.  Of course, we only want to do this if we are
695     producing an XCOFF output file.  */
696  t = link_info.type;
697  if (strstr (bfd_get_target (link_info.output_bfd), "xcoff") != NULL)
698    link_info.type = type_relocatable;
699  ldctor_build_sets ();
700  link_info.type = t;
701
702  /* For each set, record the size, so that the XCOFF backend can
703     output the correct csect length.  */
704  for (p = sets; p != (struct set_info *) NULL; p = p->next)
705    {
706      bfd_size_type size;
707
708      /* If the symbol is defined, we may have been invoked from
709	 collect, and the sets may already have been built, so we do
710	 not do anything.  */
711      if (p->h->type == bfd_link_hash_defined
712	  || p->h->type == bfd_link_hash_defweak)
713	continue;
714
715      if (p->reloc != BFD_RELOC_CTOR)
716	{
717	  /* Handle this if we need to.  */
718	  abort ();
719	}
720
721      size = (p->count + 2) * 4;
722      if (!bfd_xcoff_link_record_set (link_info.output_bfd, &link_info,
723				      p->h, size))
724	einfo (_("%F%P: bfd_xcoff_link_record_set failed: %E\n"));
725    }
726}
727
728/* This is called after the sections have been attached to output
729   sections, but before any sizes or addresses have been set.  */
730
731static void
732gld${EMULATION_NAME}_before_allocation (void)
733{
734  struct filelist *fl;
735  struct export_symbol_list *el;
736  char *libpath;
737  asection *special_sections[XCOFF_NUMBER_OF_SPECIAL_SECTIONS];
738  static const char *const must_keep_sections[] = {
739    ".text",
740    ".data",
741    ".bss"
742  };
743  unsigned int i, flags;
744
745  /* Handle the import and export files, if any.  */
746  for (fl = import_files; fl != NULL; fl = fl->next)
747    gld${EMULATION_NAME}_read_file (fl->name, TRUE);
748  for (el = export_symbols; el != NULL; el = el->next)
749    {
750      struct bfd_link_hash_entry *h;
751
752      h = bfd_link_hash_lookup (link_info.hash, el->name, FALSE, FALSE, FALSE);
753      if (h == NULL)
754	einfo (_("%F%P: bfd_link_hash_lookup of export symbol failed: %E\n"));
755      if (!bfd_xcoff_export_symbol (link_info.output_bfd, &link_info, h))
756	einfo (_("%F%P: bfd_xcoff_export_symbol failed: %E\n"));
757    }
758
759  /* Track down all relocations called for by the linker script (these
760     are typically constructor/destructor entries created by
761     CONSTRUCTORS) and let the backend know it will need to create
762     .loader relocs for them.  */
763  lang_for_each_statement (gld${EMULATION_NAME}_find_relocs);
764
765  /* Precedence of LIBPATH
766     -blibpath:	 native support always first
767     -rpath:	 gnu extension
768     -L		 build from command line -L's */
769  if (command_line_blibpath != NULL)
770    libpath = command_line_blibpath;
771  else if (command_line.rpath != NULL)
772    libpath = command_line.rpath;
773  else if (search_head == NULL)
774    libpath = (char *) "";
775  else
776    {
777      size_t len;
778      search_dirs_type *search;
779
780      /* PR ld/4023: Strip sysroot prefix from any paths
781	 being inserted into the output binary's DT_RPATH.  */
782      if (ld_sysroot != NULL
783	  && * ld_sysroot != 0)
784	{
785	  const char * name = search_head->name;
786	  size_t ld_sysroot_len = strlen (ld_sysroot);
787
788	  if (strncmp (name, ld_sysroot, ld_sysroot_len) == 0)
789	    name += ld_sysroot_len;
790
791	  len = strlen (name);
792	  libpath = xmalloc (len + 1);
793	  strcpy (libpath, name);
794
795	  for (search = search_head->next; search != NULL; search = search->next)
796	    {
797	      size_t nlen;
798
799	      name = search->name;
800	      if (strncmp (name, ld_sysroot, ld_sysroot_len) == 0)
801		name += ld_sysroot_len;
802
803	      nlen = strlen (name);
804	      libpath = xrealloc (libpath, len + nlen + 2);
805	      libpath[len] = ':';
806	      strcpy (libpath + len + 1, name);
807	      len += nlen + 1;
808	    }
809	}
810      else
811	{
812	  len = strlen (search_head->name);
813	  libpath = xmalloc (len + 1);
814	  strcpy (libpath, search_head->name);
815
816	  for (search = search_head->next; search != NULL; search = search->next)
817	    {
818	      size_t nlen;
819
820	      nlen = strlen (search->name);
821	      libpath = xrealloc (libpath, len + nlen + 2);
822	      libpath[len] = ':';
823	      strcpy (libpath + len + 1, search->name);
824	      len += nlen + 1;
825	    }
826	}
827    }
828
829  /* Default to -bexpfull for SVR4-like semantics.  */
830  flags = (unix_ld ? XCOFF_EXPFULL : 0);
831  flags &= ~explicit_auto_export_flags;
832  flags |= auto_export_flags;
833
834  /* Let the XCOFF backend set up the .loader section.  */
835  if (!bfd_xcoff_size_dynamic_sections
836      (link_info.output_bfd, &link_info, libpath, entry_symbol.name, file_align,
837       maxstack, maxdata, gc && !unix_ld ? TRUE : FALSE,
838       modtype, textro ? TRUE : FALSE, flags, special_sections,
839       rtld ? TRUE : FALSE))
840    einfo (_("%F%P: failed to set dynamic section sizes: %E\n"));
841
842  /* Look through the special sections, and put them in the right
843     place in the link ordering.  This is especially magic.  */
844  for (i = 0; i < XCOFF_NUMBER_OF_SPECIAL_SECTIONS; i++)
845    {
846      asection *sec;
847      lang_output_section_statement_type *os;
848      lang_statement_union_type **pls;
849      lang_input_section_type *is;
850      const char *oname;
851      bfd_boolean start;
852
853      sec = special_sections[i];
854      if (sec == NULL)
855	continue;
856
857      /* Remove this section from the list of the output section.
858	 This assumes we know what the script looks like.  */
859      is = NULL;
860      os = lang_output_section_get (sec->output_section);
861      if (os == NULL)
862	einfo (_("%F%P: can't find output section %s\n"),
863	       sec->output_section->name);
864
865      for (pls = &os->children.head; *pls != NULL; pls = &(*pls)->header.next)
866	{
867	  if ((*pls)->header.type == lang_input_section_enum
868	      && (*pls)->input_section.section == sec)
869	    {
870	      is = (lang_input_section_type *) * pls;
871	      *pls = (*pls)->header.next;
872	      break;
873	    }
874
875	  if ((*pls)->header.type == lang_wild_statement_enum)
876	    {
877	      lang_statement_union_type **pwls;
878
879	      for (pwls = &(*pls)->wild_statement.children.head;
880		   *pwls != NULL; pwls = &(*pwls)->header.next)
881		{
882
883		  if ((*pwls)->header.type == lang_input_section_enum
884		      && (*pwls)->input_section.section == sec)
885		    {
886		      is = (lang_input_section_type *) * pwls;
887		      *pwls = (*pwls)->header.next;
888		      break;
889		    }
890		}
891
892	      if (is != NULL)
893		break;
894	    }
895	}
896
897      if (is == NULL)
898	{
899	  einfo (_("%F%P: can't find %s in output section\n"),
900		 bfd_section_name (sec));
901	}
902
903      /* Now figure out where the section should go.  */
904      switch (i)
905	{
906
907	default:		/* to avoid warnings */
908	case XCOFF_SPECIAL_SECTION_TEXT:
909	  /* _text */
910	  oname = ".text";
911	  start = TRUE;
912	  break;
913
914	case XCOFF_SPECIAL_SECTION_ETEXT:
915	  /* _etext */
916	  oname = ".text";
917	  start = FALSE;
918	  break;
919
920	case XCOFF_SPECIAL_SECTION_DATA:
921	  /* _data */
922	  oname = ".data";
923	  start = TRUE;
924	  break;
925
926	case XCOFF_SPECIAL_SECTION_EDATA:
927	  /* _edata */
928	  oname = ".data";
929	  start = FALSE;
930	  break;
931
932	case XCOFF_SPECIAL_SECTION_END:
933	case XCOFF_SPECIAL_SECTION_END2:
934	  /* _end and end */
935	  oname = ".bss";
936	  start = FALSE;
937	  break;
938	}
939
940      os = lang_output_section_find (oname);
941
942      if (start)
943	{
944	  is->header.next = os->children.head;
945	  os->children.head = (lang_statement_union_type *) is;
946	}
947      else
948	{
949	  is->header.next = NULL;
950	  *os->children.tail = (lang_statement_union_type *) is;
951	  os->children.tail = &is->header.next;
952	}
953    }
954
955  /* Executables and shared objects must always have .text, .data
956     and .bss output sections, so that the header can refer to them.
957     The kernel refuses to load objects that have missing sections.  */
958  if (!bfd_link_relocatable (&link_info))
959    for (i = 0; i < ARRAY_SIZE (must_keep_sections); i++)
960      {
961	asection *sec;
962
963	sec = bfd_get_section_by_name (link_info.output_bfd,
964				       must_keep_sections[i]);
965	if (sec == NULL)
966	  einfo (_("%P: can't find required output section %s\n"),
967		 must_keep_sections[i]);
968	else
969	  sec->flags |= SEC_KEEP;
970      }
971
972  before_allocation_default ();
973}
974
975static char *
976gld${EMULATION_NAME}_choose_target (int argc, char **argv)
977{
978  int i, j, jmax;
979  static char *from_outside;
980  static char *from_inside;
981  static char *argv_to_target[][2] = {
982    {NULL,   "${OUTPUT_FORMAT}"},
983    {"-b32", "${OUTPUT_FORMAT_32BIT}"},
984    {"-b64", "${OUTPUT_FORMAT_64BIT}"},
985  };
986
987  jmax = 3;
988
989  from_outside = getenv (TARGET_ENVIRON);
990  if (from_outside != (char *) NULL)
991    return from_outside;
992
993  /* Set to default. */
994  from_inside = argv_to_target[0][1];
995  for (i = 1; i < argc; i++)
996    {
997      for (j = 1; j < jmax; j++)
998	{
999	  if (0 == strcmp (argv[i], argv_to_target[j][0]))
1000	    from_inside = argv_to_target[j][1];
1001	}
1002    }
1003
1004  return from_inside;
1005}
1006
1007/* Returns
1008   1 : state changed
1009   0 : no change */
1010static int
1011change_symbol_mode (char *input)
1012{
1013  char *symbol_mode_string[] = {
1014    "# 32",			/* 0x01 */
1015    "# 64",			/* 0x02 */
1016    "# no32",			/* 0x04 */
1017    "# no64",			/* 0x08 */
1018    NULL,
1019  };
1020
1021  unsigned int bit;
1022  char *string;
1023
1024  for (bit = 0;; bit++)
1025    {
1026      string = symbol_mode_string[bit];
1027      if (string == NULL)
1028	return 0;
1029
1030      if (0 == strcmp (input, string))
1031	{
1032	  symbol_mode = (1 << bit);
1033	  return 1;
1034	}
1035    }
1036  /* should not be here */
1037  return 0;
1038}
1039
1040/* Returns
1041   1 : yes
1042   0 : ignore
1043   -1 : error, try something else */
1044static int
1045is_syscall (char *input, unsigned int *flag)
1046{
1047  unsigned int bit;
1048  char *string;
1049
1050  struct sc {
1051    char *syscall_string;
1052    unsigned int flag;
1053  } s [] = {
1054    { "svc"	    /* 0x01 */, XCOFF_SYSCALL32 },
1055    { "svc32"	    /* 0x02 */, XCOFF_SYSCALL32 },
1056    { "svc3264"     /* 0x04 */, XCOFF_SYSCALL32 | XCOFF_SYSCALL64 },
1057    { "svc64"	    /* 0x08 */, XCOFF_SYSCALL64 },
1058    { "syscall"     /* 0x10 */, XCOFF_SYSCALL32 },
1059    { "syscall32"   /* 0x20 */, XCOFF_SYSCALL32 },
1060    { "syscall3264" /* 0x40 */, XCOFF_SYSCALL32 | XCOFF_SYSCALL64 },
1061    { "syscall64"   /* 0x80 */, XCOFF_SYSCALL64 },
1062    { NULL, 0 },
1063  };
1064
1065  *flag = 0;
1066
1067  for (bit = 0;; bit++)
1068    {
1069      string = s[bit].syscall_string;
1070      if (string == NULL)
1071	return -1;
1072
1073      if (0 == strcmp (input, string))
1074	{
1075	  if (1 << bit & syscall_mask)
1076	    {
1077	      *flag = s[bit].flag;
1078	      return 1;
1079	    }
1080	  else
1081	    {
1082	      return 0;
1083	    }
1084	}
1085    }
1086  /* should not be here */
1087  return -1;
1088}
1089
1090/* Read an import or export file.  For an import file, this is called
1091   by the before_allocation emulation routine.  For an export file,
1092   this is called by the handle_option emulation routine.  */
1093
1094static void
1095gld${EMULATION_NAME}_read_file (const char *filename, bfd_boolean import)
1096{
1097  struct obstack *o;
1098  FILE *f;
1099  int lineno;
1100  int c;
1101  bfd_boolean keep;
1102  const char *imppath;
1103  const char *impfile;
1104  const char *impmember;
1105
1106  o = (struct obstack *) xmalloc (sizeof (struct obstack));
1107  obstack_specify_allocation (o, 0, 0, xmalloc, gld${EMULATION_NAME}_free);
1108
1109  f = fopen (filename, FOPEN_RT);
1110  if (f == NULL)
1111    {
1112      bfd_set_error (bfd_error_system_call);
1113      einfo ("%F%P: %s: %E\n", filename);
1114      return;
1115    }
1116
1117  keep = FALSE;
1118
1119  imppath = NULL;
1120  impfile = NULL;
1121  impmember = NULL;
1122
1123  lineno = 0;
1124
1125  /* Default to 32 and 64 bit mode
1126     symbols at top of /lib/syscalls.exp do not have a mode modifier and they
1127     are not repeated, assume 64 bit routines also want to use them.
1128     See the routine change_symbol_mode for more information.  */
1129
1130  symbol_mode = 0x04;
1131
1132  while ((c = getc (f)) != EOF)
1133    {
1134      char *s;
1135      char *symname;
1136      unsigned int syscall_flag = 0;
1137      bfd_vma address;
1138      struct bfd_link_hash_entry *h;
1139
1140      if (c != '\n')
1141	{
1142	  obstack_1grow (o, c);
1143	  continue;
1144	}
1145
1146      obstack_1grow (o, '\0');
1147      ++lineno;
1148
1149      s = (char *) obstack_base (o);
1150      while (ISSPACE (*s))
1151	++s;
1152      if (*s == '\0'
1153	  || *s == '*'
1154	  || change_symbol_mode (s)
1155	  || (*s == '#' && s[1] == ' ')
1156	  || (!import && *s == '#' && s[1] == '!'))
1157	{
1158	  obstack_free (o, obstack_base (o));
1159	  continue;
1160	}
1161
1162      if (*s == '#' && s[1] == '!')
1163	{
1164	  s += 2;
1165	  while (ISSPACE (*s))
1166	    ++s;
1167	  if (*s == '\0')
1168	    {
1169	      imppath = NULL;
1170	      impfile = NULL;
1171	      impmember = NULL;
1172	      obstack_free (o, obstack_base (o));
1173	    }
1174	  else if (*s == '(')
1175	    einfo (_("%F%P:%s:%d: #! ([member]) is not supported "
1176		     "in import files\n"),
1177		   filename, lineno);
1178	  else
1179	    {
1180	      char cs;
1181	      char *start;
1182
1183	      (void) obstack_finish (o);
1184	      keep = TRUE;
1185	      start = s;
1186	      while (!ISSPACE (*s) && *s != '(' && *s != '\0')
1187		++s;
1188	      cs = *s;
1189	      *s = '\0';
1190	      if (!bfd_xcoff_split_import_path (link_info.output_bfd,
1191						start, &imppath, &impfile))
1192		einfo (_("%F%P: could not parse import path: %E\n"));
1193	      while (ISSPACE (cs))
1194		{
1195		  ++s;
1196		  cs = *s;
1197		}
1198	      if (cs != '(')
1199		{
1200		  impmember = "";
1201		  if (cs != '\0')
1202		    einfo (_("%P:%s:%d: warning: syntax error in import file\n"),
1203			   filename, lineno);
1204		}
1205	      else
1206		{
1207		  ++s;
1208		  impmember = s;
1209		  while (*s != ')' && *s != '\0')
1210		    ++s;
1211		  if (*s == ')')
1212		    *s = '\0';
1213		  else
1214		    einfo (_("%P:%s:%d: warning: syntax error in import file\n"),
1215			   filename, lineno);
1216		}
1217	    }
1218
1219	  continue;
1220	}
1221
1222      if (symbol_mode & symbol_mode_mask)
1223	{
1224	  /* This is a symbol to be imported or exported.  */
1225	  symname = s;
1226	  syscall_flag = 0;
1227	  address = (bfd_vma) -1;
1228
1229	  while (!ISSPACE (*s) && *s != '\0')
1230	    ++s;
1231	  if (*s != '\0')
1232	    {
1233	      char *se;
1234
1235	      *s++ = '\0';
1236
1237	      while (ISSPACE (*s))
1238		++s;
1239
1240	      se = s;
1241	      while (!ISSPACE (*se) && *se != '\0')
1242		++se;
1243	      if (*se != '\0')
1244		{
1245		  *se++ = '\0';
1246		  while (ISSPACE (*se))
1247		    ++se;
1248		  if (*se != '\0')
1249		    einfo (_("%P:%s%d: warning: syntax error in "
1250			     "import/export file\n"),
1251			   filename, lineno);
1252		}
1253
1254	      if (s != se)
1255		{
1256		  int status;
1257		  const char *end;
1258
1259		  status = is_syscall (s, &syscall_flag);
1260
1261		  if (0 > status)
1262		    {
1263		      /* not a system call, check for address */
1264		      address = bfd_scan_vma (s, &end, 0);
1265		      if (*end != '\0')
1266			{
1267			  einfo (_("%P:%s:%d: warning: syntax error in "
1268				   "import/export file\n"),
1269				 filename, lineno);
1270
1271			}
1272		    }
1273		}
1274	    }
1275
1276	  if (!import)
1277	    {
1278	      struct export_symbol_list *n;
1279
1280	      ldlang_add_undef (symname, TRUE);
1281	      n = ((struct export_symbol_list *)
1282		   xmalloc (sizeof (struct export_symbol_list)));
1283	      n->next = export_symbols;
1284	      n->name = xstrdup (symname);
1285	      export_symbols = n;
1286	    }
1287	  else
1288	    {
1289	      h = bfd_link_hash_lookup (link_info.hash, symname, FALSE, FALSE,
1290					TRUE);
1291	      if (h == NULL || h->type == bfd_link_hash_new)
1292		{
1293		  /* We can just ignore attempts to import an unreferenced
1294		     symbol.  */
1295		}
1296	      else
1297		{
1298		  if (!bfd_xcoff_import_symbol (link_info.output_bfd,
1299						&link_info, h,
1300						address, imppath, impfile,
1301						impmember, syscall_flag))
1302		    einfo (_("%X%P:%s:%d: failed to import symbol %s: %E\n"),
1303			   filename, lineno, symname);
1304		}
1305	    }
1306	}
1307      obstack_free (o, obstack_base (o));
1308    }
1309
1310  if (obstack_object_size (o) > 0)
1311    {
1312      einfo (_("%P:%s:%d: warning: ignoring unterminated last line\n"),
1313	     filename, lineno);
1314      obstack_free (o, obstack_base (o));
1315    }
1316
1317  if (!keep)
1318    {
1319      obstack_free (o, NULL);
1320      free (o);
1321    }
1322
1323  fclose (f);
1324}
1325
1326/* This routine saves us from worrying about declaring free.  */
1327
1328static void
1329gld${EMULATION_NAME}_free (void *p)
1330{
1331  free (p);
1332}
1333
1334/* This is called by the before_allocation routine via
1335   lang_for_each_statement.  It looks for relocations and assignments
1336   to symbols.  */
1337
1338static void
1339gld${EMULATION_NAME}_find_relocs (lang_statement_union_type *s)
1340{
1341  if (s->header.type == lang_reloc_statement_enum)
1342    {
1343      lang_reloc_statement_type *rs;
1344
1345      rs = &s->reloc_statement;
1346      if (rs->name == NULL)
1347	einfo (_("%F%P: only relocations against symbols are permitted\n"));
1348      if (!bfd_xcoff_link_count_reloc (link_info.output_bfd, &link_info,
1349				       rs->name))
1350	einfo (_("%F%P: bfd_xcoff_link_count_reloc failed: %E\n"));
1351    }
1352
1353  if (s->header.type == lang_assignment_statement_enum)
1354    gld${EMULATION_NAME}_find_exp_assignment (s->assignment_statement.exp);
1355}
1356
1357/* Look through an expression for an assignment statement.  */
1358
1359static void
1360gld${EMULATION_NAME}_find_exp_assignment (etree_type *exp)
1361{
1362  struct bfd_link_hash_entry *h;
1363
1364  switch (exp->type.node_class)
1365    {
1366    case etree_provide:
1367      h = bfd_link_hash_lookup (link_info.hash, exp->assign.dst,
1368				FALSE, FALSE, FALSE);
1369      if (h == NULL)
1370	break;
1371      /* Fall through.  */
1372    case etree_assign:
1373      if (strcmp (exp->assign.dst, ".") != 0)
1374	{
1375	  if (!bfd_xcoff_record_link_assignment (link_info.output_bfd,
1376						 &link_info,
1377						 exp->assign.dst))
1378	    einfo (_("%F%P: failed to record assignment to %s: %E\n"),
1379		   exp->assign.dst);
1380	}
1381      gld${EMULATION_NAME}_find_exp_assignment (exp->assign.src);
1382      break;
1383
1384    case etree_binary:
1385      gld${EMULATION_NAME}_find_exp_assignment (exp->binary.lhs);
1386      gld${EMULATION_NAME}_find_exp_assignment (exp->binary.rhs);
1387      break;
1388
1389    case etree_trinary:
1390      gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.cond);
1391      gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.lhs);
1392      gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.rhs);
1393      break;
1394
1395    case etree_unary:
1396      gld${EMULATION_NAME}_find_exp_assignment (exp->unary.child);
1397      break;
1398
1399    default:
1400      break;
1401    }
1402}
1403
1404static char *
1405gld${EMULATION_NAME}_get_script (int *isfile)
1406EOF
1407
1408if test x"$COMPILE_IN" = xyes
1409then
1410# Scripts compiled in.
1411
1412# sed commands to quote an ld script as a C string.
1413sc="-f ${srcdir}/emultempl/ostring.sed"
1414
1415fragment <<EOF
1416{
1417  *isfile = 0;
1418
1419  if (bfd_link_relocatable (&link_info) && config.build_constructors)
1420    return
1421EOF
1422sed $sc ldscripts/${EMULATION_NAME}.xu		       >> e${EMULATION_NAME}.c
1423echo '  ; else if (bfd_link_relocatable (&link_info)) return' >> e${EMULATION_NAME}.c
1424sed $sc ldscripts/${EMULATION_NAME}.xr		       >> e${EMULATION_NAME}.c
1425echo '  ; else if (!config.text_read_only) return'     >> e${EMULATION_NAME}.c
1426sed $sc ldscripts/${EMULATION_NAME}.xbn		       >> e${EMULATION_NAME}.c
1427echo '  ; else if (!config.magic_demand_paged) return' >> e${EMULATION_NAME}.c
1428sed $sc ldscripts/${EMULATION_NAME}.xn		       >> e${EMULATION_NAME}.c
1429echo '  ; else return'				       >> e${EMULATION_NAME}.c
1430sed $sc ldscripts/${EMULATION_NAME}.x		       >> e${EMULATION_NAME}.c
1431echo '; }'					       >> e${EMULATION_NAME}.c
1432
1433else
1434# Scripts read from the filesystem.
1435
1436fragment <<EOF
1437{
1438  *isfile = 1;
1439
1440  if (bfd_link_relocatable (&link_info) && config.build_constructors)
1441    return "ldscripts/${EMULATION_NAME}.xu";
1442  else if (bfd_link_relocatable (&link_info))
1443    return "ldscripts/${EMULATION_NAME}.xr";
1444  else if (!config.text_read_only)
1445    return "ldscripts/${EMULATION_NAME}.xbn";
1446  else if (!config.magic_demand_paged)
1447    return "ldscripts/${EMULATION_NAME}.xn";
1448  else
1449    return "ldscripts/${EMULATION_NAME}.x";
1450}
1451EOF
1452
1453fi
1454
1455fragment <<EOF
1456
1457static void
1458gld${EMULATION_NAME}_create_output_section_statements (void)
1459{
1460  /* __rtinit */
1461  if ((bfd_get_flavour (link_info.output_bfd) == bfd_target_xcoff_flavour)
1462      && (link_info.init_function != NULL
1463	  || link_info.fini_function != NULL
1464	  || rtld))
1465    {
1466      initfini_file = lang_add_input_file ("initfini",
1467					   lang_input_file_is_file_enum,
1468					   NULL);
1469
1470      initfini_file->the_bfd = bfd_create ("initfini", link_info.output_bfd);
1471      if (initfini_file->the_bfd == NULL
1472	  || ! bfd_set_arch_mach (initfini_file->the_bfd,
1473				  bfd_get_arch (link_info.output_bfd),
1474				  bfd_get_mach (link_info.output_bfd)))
1475	{
1476	  einfo (_("%F%P: can not create BFD: %E\n"));
1477	  return;
1478	}
1479
1480      /* Call backend to fill in the rest */
1481      if (! bfd_xcoff_link_generate_rtinit (initfini_file->the_bfd,
1482					    link_info.init_function,
1483					    link_info.fini_function,
1484					    rtld))
1485	{
1486	  einfo (_("%F%P: can not create BFD: %E\n"));
1487	  return;
1488	}
1489
1490      /* __rtld defined in /lib/librtl.a */
1491      if (rtld)
1492	lang_add_input_file ("rtl", lang_input_file_is_l_enum, NULL);
1493    }
1494}
1495
1496static void
1497gld${EMULATION_NAME}_set_output_arch (void)
1498{
1499  bfd_set_arch_mach (link_info.output_bfd,
1500		     bfd_xcoff_architecture (link_info.output_bfd),
1501		     bfd_xcoff_machine (link_info.output_bfd));
1502
1503  ldfile_output_architecture = bfd_get_arch (link_info.output_bfd);
1504  ldfile_output_machine = bfd_get_mach (link_info.output_bfd);
1505  ldfile_output_machine_name = bfd_printable_name (link_info.output_bfd);
1506}
1507
1508static bfd_boolean
1509gld${EMULATION_NAME}_open_dynamic_archive (const char *arch,
1510					   search_dirs_type *search,
1511					   lang_input_statement_type *entry)
1512{
1513  char *path;
1514
1515  if (!entry->flags.maybe_archive)
1516    return FALSE;
1517
1518  if (entry->flags.full_name_provided)
1519    path = concat (search->name, "/", entry->filename,
1520		   (const char *) NULL);
1521  else
1522    path = concat (search->name, "/lib", entry->filename, arch, ".a",
1523		   (const char *) NULL);
1524
1525  if (!ldfile_try_open_bfd (path, entry))
1526    {
1527      free (path);
1528      return FALSE;
1529    }
1530  /* Don't include the searched directory in the import path.  */
1531  bfd_xcoff_set_archive_import_path (&link_info, entry->the_bfd,
1532				     path + strlen (search->name) + 1);
1533  entry->filename = path;
1534  return TRUE;
1535}
1536
1537struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation = {
1538  gld${EMULATION_NAME}_before_parse,
1539  syslib_default,
1540  hll_default,
1541  after_parse_default,
1542  gld${EMULATION_NAME}_after_open,
1543  after_check_relocs_default,
1544  after_allocation_default,
1545  gld${EMULATION_NAME}_set_output_arch,
1546  gld${EMULATION_NAME}_choose_target,
1547  gld${EMULATION_NAME}_before_allocation,
1548  gld${EMULATION_NAME}_get_script,
1549  "${EMULATION_NAME}",
1550  "${OUTPUT_FORMAT}",
1551  finish_default,
1552  gld${EMULATION_NAME}_create_output_section_statements,
1553  gld${EMULATION_NAME}_open_dynamic_archive,
1554  0,				/* place_orphan */
1555  0,				/* set_symbols */
1556  gld${EMULATION_NAME}_parse_args,
1557  gld${EMULATION_NAME}_add_options,
1558  gld${EMULATION_NAME}_handle_option,
1559  gld${EMULATION_NAME}_unrecognized_file,
1560  NULL,				/* list_options */
1561  NULL,				/* recognized_file */
1562  NULL,				/* find potential_libraries */
1563  NULL,				/* new_vers_pattern */
1564  NULL,				/* extra_map_file_text */
1565  ${LDEMUL_EMIT_CTF_EARLY-NULL},
1566  ${LDEMUL_EXAMINE_STRTAB_FOR_CTF-NULL}
1567};
1568EOF
1569