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