2001-05-24 H.J. Lu <hjl@gnu.org>
[binutils.git] / ld / emultempl / aix.em
blobb88960ceb42d61daca52a38840d5933e1ce6817d
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 cat >e${EMULATION_NAME}.c <<EOF
9 /* This file is is generated by a shell script.  DO NOT EDIT! */
11 /* AIX emulation code for ${EMULATION_NAME}
12    Copyright (C) 1991, 93, 95, 96, 97, 1998 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>
17 This file is part of GLD, the Gnu Linker.
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 2 of the License, or
22 (at your option) any later version.
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.
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
33 #define TARGET_IS_${EMULATION_NAME}
35 #include "bfd.h"
36 #include "sysdep.h"
37 #include "libiberty.h"
38 #include "getopt.h"
39 #include "obstack.h"
40 #include "bfdlink.h"
42 #include <ctype.h>
44 #include "ld.h"
45 #include "ldmain.h"
46 #include "ldemul.h"
47 #include "ldfile.h"
48 #include "ldmisc.h"
49 #include "ldexp.h"
50 #include "ldlang.h"
51 #include "ldctor.h"
52 #include "ldgram.h"
54 #include "coff/internal.h"
55 #include "coff/xcoff.h"
57 static void gld${EMULATION_NAME}_before_parse PARAMS ((void));
58 static int gld${EMULATION_NAME}_parse_args PARAMS ((int, char **));
59 static void gld${EMULATION_NAME}_after_open PARAMS ((void));
60 static void gld${EMULATION_NAME}_before_allocation PARAMS ((void));
61 static void gld${EMULATION_NAME}_read_file PARAMS ((const char *, boolean));
62 static void gld${EMULATION_NAME}_free PARAMS ((PTR));
63 static void gld${EMULATION_NAME}_find_relocs
64   PARAMS ((lang_statement_union_type *));
65 static void gld${EMULATION_NAME}_find_exp_assignment PARAMS ((etree_type *));
66 static char *gld${EMULATION_NAME}_get_script PARAMS ((int *isfile));
68 /* The file alignment required for each section.  */
69 static unsigned long file_align;
71 /* The maximum size the stack is permitted to grow.  This is stored in
72    the a.out header.  */
73 static unsigned long maxstack;
75 /* The maximum data size.  This is stored in the a.out header.  */
76 static unsigned long maxdata;
78 /* Whether to perform garbage collection.  */
79 static int gc = 1;
81 /* The module type to use.  */
82 static unsigned short modtype = ('1' << 8) | 'L';
84 /* Whether the .text section must be read-only (i.e., no relocs
85    permitted).  */
86 static int textro;
88 /* Whether to implement Unix like linker semantics.  */
89 static int unix_ld;
91 /* Structure used to hold import file list.  */
93 struct filelist
95   struct filelist *next;
96   const char *name;
99 /* List of import files.  */
100 static struct filelist *import_files;
102 /* List of export symbols read from the export files.  */
104 struct export_symbol_list
106   struct export_symbol_list *next;
107   const char *name;
108   boolean syscall;
111 static struct export_symbol_list *export_symbols;
113 /* Maintains the 32 or 64 bit mode state of import file */
114 static unsigned int symbol_mode = 0x04;
116 /* This routine is called before anything else is done.  */
118 static void
119 gld${EMULATION_NAME}_before_parse()
121 #ifndef TARGET_                 /* I.e., if not generic.  */
122   const bfd_arch_info_type *arch = bfd_scan_arch ("${OUTPUT_ARCH}");
123   if (arch)
124     {
125       ldfile_output_architecture = arch->arch;
126       ldfile_output_machine = arch->mach;
127       ldfile_output_machine_name = arch->printable_name;
128     }
129   else
130     ldfile_output_architecture = bfd_arch_${ARCH};
131 #endif /* not TARGET_ */
132   config.has_shared = true;
134   /*
135    * The link_info.[init|fini]_functions are initialized in ld/lexsup.c.
136    * Override them here so we can use the link_info.init_function as a
137    * state flag that lets the backend know that -binitfini has been done.
138    */ 
139   link_info.init_function = NULL;
140   link_info.fini_function = NULL;
144 /* Handle AIX specific options.  */
146 static int
147 gld${EMULATION_NAME}_parse_args (argc, argv)
148      int argc;
149      char **argv;
151   int prevoptind = optind;
152   int prevopterr = opterr;
153   int indx;
154   int longind;
155   int optc;
156   bfd_signed_vma val;
157   char *end;
159   enum {
160     OPTION_IGNORE = 300,
161     OPTION_AUTOIMP, 
162     OPTION_ERNOTOK, 
163     OPTION_EROK, 
164     OPTION_EXPORT, 
165     OPTION_IMPORT, 
166     OPTION_INITFINI, 
167     OPTION_LOADMAP, 
168     OPTION_MAXDATA, 
169     OPTION_MAXSTACK, 
170     OPTION_MODTYPE, 
171     OPTION_NOAUTOIMP, 
172     OPTION_NOSTRCMPCT, 
173     OPTION_PD, 
174     OPTION_PT, 
175     OPTION_STRCMPCT, 
176     OPTION_UNIX
177   };
179   /*
180     b64 is an empty option.  The native linker uses -b64 for xcoff64 support
181     Our linker uses -m aixppc64 for xcoff64 support. The choice for the 
182     correct emulation is done in collect2.c via the environmental varible
183     LDEMULATION.
184         
185     binitfini has special handling in the linker backend.  The native linker
186     uses the arguemnts to generate a table of init and fini functions for
187     the executable.  The important use for this option is to support aix 4.2+
188     c++ constructors and destructors.  This is tied into gcc via collect2.c.
189         The function table is accessed by the runtime linker/loader by checking if
190         the first symbol in the loader symbol table is "__rtinit".  The native
191         linker generates this table and the loader symbol.  The gnu linker looks
192         for the symbol "__rtinit" and makes it the first loader symbol.  It is the
193         responsiblity of the user to define the __rtinit symbol.  The format for
194         __rtinit is given by the aix system file /usr/include/rtinit.h.  You can
195         look at collect2.c to see an example of how this is done for 32 and 64 bit.
196         Below is an exmaple of a 32 bit assembly file that defines __rtinit.
198         .file   "my_rtinit.s"
199         
200         .csect .data[RW],3
201         .globl __rtinit
202         .extern init_function
203         .extern fini_function
204         
205         __rtinit:
206                 .long 0
207                 .long f1i - __rtinit
208                 .long f1f - __rtinit
209                 .long f2i - f1i
210                 .align 3
211         f1i:    .long init_function
212                 .long s1i - __rtinit
213                 .long 0
214         f2i:    .long 0
215                 .long 0
216                 .long 0
217         f1f:    .long fini_function
218                 .long s1f - __rtinit
219                 .long 0
220         f2f:    .long 0
221                 .long 0
222                 .long 0
223                 .align 3
224         s1i:    .string "init_function"
225                 .align 3
226         s1f:    .string "fini_function"
228    */
230   static const struct option longopts[] = {
231     {"basis", no_argument, NULL, OPTION_IGNORE},
232     {"bautoimp", no_argument, NULL, OPTION_AUTOIMP},
233     {"bcomprld", no_argument, NULL, OPTION_IGNORE},
234     {"bcrld", no_argument, NULL, OPTION_IGNORE},
235     {"bcror31", no_argument, NULL, OPTION_IGNORE},
236     {"bD", required_argument, NULL, OPTION_MAXDATA},
237     {"bE", required_argument, NULL, OPTION_EXPORT},
238     {"bernotok", no_argument, NULL, OPTION_ERNOTOK},
239     {"berok", no_argument, NULL, OPTION_EROK},
240     {"berrmsg", no_argument, NULL, OPTION_IGNORE},
241     {"bexport", required_argument, NULL, OPTION_EXPORT},
242     {"bf", no_argument, NULL, OPTION_ERNOTOK},
243     {"bgc", no_argument, &gc, 1},
244     {"bh", required_argument, NULL, OPTION_IGNORE},
245     {"bhalt", required_argument, NULL, OPTION_IGNORE},
246     {"bI", required_argument, NULL, OPTION_IMPORT},
247     {"bimport", required_argument, NULL, OPTION_IMPORT},
248     {"binitfini", required_argument, NULL, OPTION_INITFINI},
249     {"bl", required_argument, NULL, OPTION_LOADMAP},
250     {"bloadmap", required_argument, NULL, OPTION_LOADMAP},
251     {"bmaxdata", required_argument, NULL, OPTION_MAXDATA},
252     {"bmaxstack", required_argument, NULL, OPTION_MAXSTACK},
253     {"bM", required_argument, NULL, OPTION_MODTYPE},
254     {"bmodtype", required_argument, NULL, OPTION_MODTYPE},
255     {"bnoautoimp", no_argument, NULL, OPTION_NOAUTOIMP},
256     {"bnodelcsect", no_argument, NULL, OPTION_IGNORE},
257     {"bnoentry", no_argument, NULL, OPTION_IGNORE},
258     {"bnogc", no_argument, &gc, 0},
259     {"bnso", no_argument, NULL, OPTION_NOAUTOIMP},
260     {"bnostrcmpct", no_argument, NULL, OPTION_NOSTRCMPCT},
261     {"bnotextro", no_argument, &textro, 0},
262     {"bnro", no_argument, &textro, 0},
263     {"bpD", required_argument, NULL, OPTION_PD},
264     {"bpT", required_argument, NULL, OPTION_PT},
265     {"bro", no_argument, &textro, 1},
266     {"bS", required_argument, NULL, OPTION_MAXSTACK},
267     {"bso", no_argument, NULL, OPTION_AUTOIMP},
268     {"bstrcmpct", no_argument, NULL, OPTION_STRCMPCT},
269     {"btextro", no_argument, &textro, 1},
270     {"b64", no_argument, NULL, 0},
271     {"static", no_argument, NULL, OPTION_NOAUTOIMP},
272     {"unix", no_argument, NULL, OPTION_UNIX},
273     {NULL, no_argument, NULL, 0}
274   };
276   /* Options supported by the AIX linker which we do not support: -f,
277      -S, -v, -Z, -bbindcmds, -bbinder, -bbindopts, -bcalls, -bcaps,
278      -bcror15, -bdebugopt, -bdbg, -bdelcsect, -bex?, -bfilelist, -bfl,
279      -bgcbypass, -bglink, -binsert, -bi, -bloadmap, -bl, -bmap, -bnl,
280      -bnobind, -bnocomprld, -bnocrld, -bnoerrmsg, -bnoglink,
281      -bnoloadmap, -bnl, -bnoobjreorder, -bnoquiet, -bnoreorder,
282      -bnotypchk, -bnox, -bquiet, -bR, -brename, -breorder, -btypchk,
283      -bx, -bX, -bxref.  */
285   /* If the current option starts with -b, change the first : to an =.
286      The AIX linker uses : to separate the option from the argument;
287      changing it to = lets us treat it as a getopt option.  */
288   indx = optind;
289   if (indx == 0) {
290     indx = 1;
291   }
293   if (indx < argc && strncmp (argv[indx], "-b", 2) == 0) {
294     char *s;
295     
296     for (s = argv[indx]; *s != '\0'; s++) {
297       if (*s == ':') {
298         *s = '=';
299         break;
300       }
301     }
302   }
305   /* We add s and u so to the short options list so that -s and -u on
306      the command line do not match -static and -unix.  */
308   opterr = 0;
309   optc = getopt_long_only (argc, argv, "-D:H:KT:zsu", longopts, &longind);
310   opterr = prevopterr;
312   switch (optc)
313     {
314     case 's':
315     case 'u':
316     default:
317       optind = prevoptind;
318       return 0;
320     case 0:
321       /* Long option which just sets a flag.  */
322       break;
324     case 'D':
325       val = strtoll (optarg, &end, 0);
326       if (*end != '\0')
327         einfo ("%P: warning: ignoring invalid -D number %s\n", optarg);
328       else if (val != -1)
329         lang_section_start (".data", exp_intop (val));
330       break;
332     case 'H':
333       val = strtoul (optarg, &end, 0);
334       if (*end != '\0'
335           || (val & (val - 1)) != 0)
336         einfo ("%P: warning: ignoring invalid -H number %s\n", optarg);
337       else
338         file_align = val;
339       break;
341     case 'K':
342     case 'z':
343       /* FIXME: This should use the page size for the target system.  */
344       file_align = 4096;
345       break;
347     case 'T':
348       /* On AIX this is the same as GNU ld -Ttext.  When we see -T
349          number, we assume the AIX option is intended.  Otherwise, we
350          assume the usual GNU ld -T option is intended.  We can't just
351          ignore the AIX option, because gcc passes it to the linker.  */
352       val = strtoull (optarg, &end, 0);
353       if (*end != '\0')
354         {
355           optind = prevoptind;
356           return 0;
357         }
358       lang_section_start (".text", exp_intop (val));
359       break;
361     case OPTION_IGNORE:
362       break;
364     case OPTION_INITFINI: {
365       /* 
366        * The aix linker init fini has the format :
367        *
368        * -binitfini:[ Initial][:Termination][:Priority]
369        *
370        * it allows the Termination and Priority to be optional.
371        *
372        * Since we support only one init/fini pair, we ignore the Priority.
373        * 
374        * Define the special symbol __rtinit.
375        *
376        * strtok does not correctly handle the case of -binitfini::fini: so
377        * do it by hand
378        */
379       char *t, *i, *f;
381       i = t = optarg;
382       while (*t && ':' != *t) t++;
383       if (*t) *t++ = 0;
385       if (0 != strlen(i)) {
386         link_info.init_function = i;
387       }
389       f = t;
390       while (*t && ':' != *t) t++;
391       *t = 0;
393       if (0 != strlen(f)) {
394         link_info.fini_function = f;
395       }
396     }
397     break;
399     case OPTION_AUTOIMP:
400       link_info.static_link = false;
401       break;
403     case OPTION_ERNOTOK:
404       force_make_executable = false;
405       break;
407     case OPTION_EROK:
408       force_make_executable = true;
409       break;
411     case OPTION_EXPORT:
412       gld${EMULATION_NAME}_read_file (optarg, false);
413       break;
415     case OPTION_IMPORT:
416       {
417         struct filelist *n;
418         struct filelist **flpp;
420         n = (struct filelist *) xmalloc (sizeof (struct filelist));
421         n->next = NULL;
422         n->name = optarg;
423         flpp = &import_files;
424         while (*flpp != NULL)
425           flpp = &(*flpp)->next;
426         *flpp = n;
427       }
428       break;
430     case OPTION_LOADMAP:
431       config.map_filename = optarg;
432       break;
434     case OPTION_MAXDATA:
435       val = strtoull (optarg, &end, 0);
436       if (*end != '\0')
437         einfo ("%P: warning: ignoring invalid -bmaxdata number %s\n",
438                optarg);
439       else
440         maxdata = val;
441       break;
443     case OPTION_MAXSTACK:
444       val = strtoull (optarg, &end, 0);
445       if (*end != '\0')
446         einfo ("%P: warning: ignoring invalid -bmaxstack number %s\n",
447                optarg);
448       else
449         maxstack = val;
450       break;
452     case OPTION_MODTYPE:
453       if (*optarg == 'S')
454         {
455           link_info.shared = true;
456           ++optarg;
457         }
458       if (*optarg == '\0' || optarg[1] == '\0')
459         einfo ("%P: warning: ignoring invalid module type %s\n", optarg);
460       else
461         modtype = (*optarg << 8) | optarg[1];
462       break;
464     case OPTION_NOAUTOIMP:
465       link_info.static_link = true;
466       break;
468     case OPTION_NOSTRCMPCT:
469       link_info.traditional_format = true;
470       break;
472     case OPTION_PD:
473       /* This sets the page that the .data section is supposed to
474          start on.  The offset within the page should still be the
475          offset within the file, so we need to build an appropriate
476          expression.  */
477       val = strtoull (optarg, &end, 0);
478       if (*end != '\0')
479         einfo ("%P: warning: ignoring invalid -pD number %s\n", optarg);
480       else
481         {
482           etree_type *t;
484           t = exp_binop ('+',
485                          exp_intop (val),
486                          exp_binop ('&',
487                                     exp_nameop (NAME, "."),
488                                     exp_intop (0xfff)));
489           t = exp_binop ('&',
490                          exp_binop ('+', t, exp_intop (31)),
491                          exp_intop (~ (bfd_vma) 31));
492           lang_section_start (".data", t);
493         }
494       break;
496     case OPTION_PT:
497       /* This set the page that the .text section is supposed to start
498          on.  The offset within the page should still be the offset
499          within the file.  */
500       val = strtoull (optarg, &end, 0);
501       if (*end != '\0')
502         einfo ("%P: warning: ignoring invalid -pT number %s\n", optarg);
503       else
504         {
505           etree_type *t;
507           t = exp_binop ('+',
508                          exp_intop (val),
509                          exp_nameop (SIZEOF_HEADERS, NULL));
510           t = exp_binop ('&',
511                          exp_binop ('+', t, exp_intop (31)),
512                          exp_intop (~ (bfd_vma) 31));
513           lang_section_start (".text", t);
514         }
515       break;
517     case OPTION_STRCMPCT:
518       link_info.traditional_format = false;
519       break;
521     case OPTION_UNIX:
522       unix_ld = true;
523       break;
524     }
526   return 1;
529 /* This is called when an input file can not be recognized as a BFD
530    object or an archive.  If the file starts with #!, we must treat it
531    as an import file.  This is for AIX compatibility.  */
533 static boolean
534 gld${EMULATION_NAME}_unrecognized_file (entry)
535      lang_input_statement_type *entry;
537   FILE *e;
538   boolean ret;
540   e = fopen (entry->filename, FOPEN_RT);
541   if (e == NULL)
542     return false;
544   ret = false;
546   if (getc (e) == '#' && getc (e) == '!')
547     {
548       struct filelist *n;
549       struct filelist **flpp;
551       n = (struct filelist *) xmalloc (sizeof (struct filelist));
552       n->next = NULL;
553       n->name = entry->filename;
554       flpp = &import_files;
555       while (*flpp != NULL)
556         flpp = &(*flpp)->next;
557       *flpp = n;
559       ret = true;
560       entry->loaded = true;
561     }
563   fclose (e);
565   return ret;
568 /* This is called after the input files have been opened.  */
570 static void
571 gld${EMULATION_NAME}_after_open ()
573   boolean r;
574   struct set_info *p;
576   /* Call ldctor_build_sets, after pretending that this is a
577      relocateable link.  We do this because AIX requires relocation
578      entries for all references to symbols, even in a final
579      executable.  Of course, we only want to do this if we are
580      producing an XCOFF output file.  */
581   r = link_info.relocateable;
582   if (strstr (bfd_get_target (output_bfd), "xcoff") != NULL)
583     link_info.relocateable = true;
584   ldctor_build_sets ();
585   link_info.relocateable = r;
587   /* For each set, record the size, so that the XCOFF backend can
588      output the correct csect length.  */
589   for (p = sets; p != (struct set_info *) NULL; p = p->next)
590     {
591       bfd_size_type size;
593       /* If the symbol is defined, we may have been invoked from
594          collect, and the sets may already have been built, so we do
595          not do anything.  */
596       if (p->h->type == bfd_link_hash_defined
597           || p->h->type == bfd_link_hash_defweak)
598         continue;
600       if (p->reloc != BFD_RELOC_CTOR)
601         {
602           /* Handle this if we need to.  */
603           abort ();
604         }
606       size = (p->count + 2) * 4;
607       if (! bfd_xcoff_link_record_set (output_bfd, &link_info, p->h, size))
608         einfo ("%F%P: bfd_xcoff_link_record_set failed: %E\n");
609     }
612 /* This is called after the sections have been attached to output
613    sections, but before any sizes or addresses have been set.  */
615 static void
616 gld${EMULATION_NAME}_before_allocation ()
618   struct filelist *fl;
619   struct export_symbol_list *el;
620   char *libpath;
621   asection *special_sections[XCOFF_NUMBER_OF_SPECIAL_SECTIONS];
622   int i;
624   /* Handle the import and export files, if any.  */
625   for (fl = import_files; fl != NULL; fl = fl->next)
626     gld${EMULATION_NAME}_read_file (fl->name, true);
627   for (el = export_symbols; el != NULL; el = el->next) {
628     struct bfd_link_hash_entry *h;
629     
630     h = bfd_link_hash_lookup (link_info.hash, el->name, false, false, false);
631     if (h == NULL)
632       einfo ("%P%F: bfd_link_hash_lookup of export symbol failed: %E\n");
633     if (! bfd_xcoff_export_symbol (output_bfd, &link_info, h, el->syscall))
634       einfo ("%P%F: bfd_xcoff_export_symbol failed: %E\n");
635   }
637   /* Track down all relocations called for by the linker script (these
638      are typically constructor/destructor entries created by
639      CONSTRUCTORS) and let the backend know it will need to create
640      .loader relocs for them.  */
641   lang_for_each_statement (gld${EMULATION_NAME}_find_relocs);
643   /* We need to build LIBPATH from the -L arguments.  If any -rpath
644      arguments were used, though, we use -rpath instead, as a GNU
645      extension.  */
646   if (command_line.rpath != NULL)
647     libpath = command_line.rpath;
648   else if (search_head == NULL)
649     libpath = (char *) "";
650   else
651     {
652       size_t len;
653       search_dirs_type *search;
655       len = strlen (search_head->name);
656       libpath = xmalloc (len + 1);
657       strcpy (libpath, search_head->name);
658       for (search = search_head->next; search != NULL; search = search->next)
659         {
660           size_t nlen;
662           nlen = strlen (search->name);
663           libpath = xrealloc (libpath, len + nlen + 2);
664           libpath[len] = ':';
665           strcpy (libpath + len + 1, search->name);
666           len += nlen + 1;
667         }
668     }
670   /* Let the XCOFF backend set up the .loader section.  */
671   if (! bfd_xcoff_size_dynamic_sections (output_bfd, &link_info, libpath,
672                                          entry_symbol, file_align,
673                                          maxstack, maxdata,
674                                          gc && ! unix_ld ? true : false,
675                                          modtype,
676                                          textro ? true : false,
677                                          unix_ld,
678                                          special_sections))
679     einfo ("%P%F: failed to set dynamic section sizes: %E\n");
681   /* Look through the special sections, and put them in the right
682      place in the link ordering.  This is especially magic.  */
683   for (i = 0; i < XCOFF_NUMBER_OF_SPECIAL_SECTIONS; i++) {
684     asection *sec;
685     lang_output_section_statement_type *os;
686     lang_statement_union_type **pls;
687     lang_input_section_type *is;
688     const char *oname;
689     boolean start;
691     sec = special_sections[i];
692     if (sec == NULL)
693       continue;
695     /* Remove this section from the list of the output section.
696        This assumes we know what the script looks like.  */
697     is = NULL;
698     os = lang_output_section_find (sec->output_section->name);
699     if (os == NULL) {
700       einfo ("%P%F: can't find output section %s\n",
701              sec->output_section->name);
702     }
704     for (pls = &os->children.head; *pls != NULL; pls = &(*pls)->next) {
706       if ((*pls)->header.type == lang_input_section_enum && 
707           (*pls)->input_section.section == sec) {
708         is = (lang_input_section_type *) *pls;
709         *pls = (*pls)->next;
710         break;
711       }
712         
713       if ((*pls)->header.type == lang_wild_statement_enum) {
714         lang_statement_union_type **pwls;
715         
716         for (pwls = &(*pls)->wild_statement.children.head;
717              *pwls != NULL;
718              pwls = &(*pwls)->next) {
720           if ((*pwls)->header.type == lang_input_section_enum && 
721               (*pwls)->input_section.section == sec) {
723               is = (lang_input_section_type *) *pwls;
724               *pwls = (*pwls)->next;
725               break;
726           }
727         }
728         
729         if (is != NULL)
730           break;
731       }
732     }   
734     if (is == NULL) {
735       einfo ("%P%F: can't find %s in output section\n",
736              bfd_get_section_name (sec->owner, sec));
737     }
738      
739     /* Now figure out where the section should go.  */
740     switch (i) {
742     default: /* to avoid warnings */
743     case XCOFF_SPECIAL_SECTION_TEXT:
744       /* _text */
745       oname = ".text";
746       start = true;
747       break;
748     
749     case XCOFF_SPECIAL_SECTION_ETEXT:
750       /* _etext */
751       oname = ".text";
752       start = false;
753       break;
755     case XCOFF_SPECIAL_SECTION_DATA:
756       /* _data */
757       oname = ".data";
758       start = true;
759       break;
760     
761     case XCOFF_SPECIAL_SECTION_EDATA:
762       /* _edata */
763       oname = ".data";
764       start = false;
765       break;
767     case XCOFF_SPECIAL_SECTION_END:
768     case XCOFF_SPECIAL_SECTION_END2:
769       /* _end and end */
770       oname = ".bss";
771       start = false;
772       break;
773     }
775     os = lang_output_section_find (oname);
777     if (start) {
778       is->header.next = os->children.head;
779       os->children.head = (lang_statement_union_type *) is;
780     } else {
781       is->header.next = NULL;
782       lang_statement_append (&os->children,
783                              (lang_statement_union_type *) is,
784                              &is->header.next);
785     }
786   }
789 static int change_symbol_mode (char *input) 
791   /*
792    * 1 : state changed
793    * 0 : no change
794    */
796   char *symbol_mode_string[] = {
797     "# 32",   /* 0x01 */
798     "# 64",   /* 0x02 */
799     "# no32", /* 0x04 */
800     "# no64", /* 0x08 */
801     NULL,
802   };
803   unsigned int bit;
804   char *string;
806   for (bit = 0; ; bit++) {
807     string = symbol_mode_string[bit];
808     if (NULL == string)
809       return 0;
810     
811     if (0 == strcmp(input, string)) {
812       symbol_mode = (1 << bit);
813       return 1;
814     }
815   }
816   /* should not be here */
817   return 0;
821 static int is_syscall(char *input)
823   /*
824    * 1 : yes
825    * 0 : ignore
826    * -1 : error, try something else
827    */
828   unsigned int bit;
829   char *string;
830   
831   char *syscall_string[] = {
832     "svc",           /* 0x01 */
833     "svc32",         /* 0x02 */
834     "svc3264",       /* 0x04 */
835     "svc64",         /* 0x08 */
836     "syscall",       /* 0x10 */
837     "syscall32",     /* 0x20 */
838     "syscall3264",   /* 0x40 */
839     "syscall64",     /* 0x80 */
840     NULL
841   };
843   for (bit = 0; ;bit++) {
844     
845     string = syscall_string[bit];
846     if (NULL == string) {
847       return -1;
848     }
850     if (0 == strcmp(input, string)) {
851       if (1 << bit & ${SYSCALL_MASK}) {
852         return 1;
853       } else {
854         return 0;
855       }
856     }
857   }
858   /* should not be here */
859   return -1;
863 /* Read an import or export file.  For an import file, this is called
864    by the before_allocation emulation routine.  For an export file,
865    this is called by the parse_args emulation routine.  */
867 static void
868 gld${EMULATION_NAME}_read_file (filename, import)
869      const char *filename;
870      boolean import;
872   struct obstack *o;
873   FILE *f;
874   int lineno;
875   int c;
876   boolean keep;
877   const char *imppath;
878   const char *impfile;
879   const char *impmember;
881   o = (struct obstack *) xmalloc (sizeof (struct obstack));
882   obstack_specify_allocation (o, 0, 0, xmalloc, gld${EMULATION_NAME}_free);
884   f = fopen (filename, FOPEN_RT);
885   if (f == NULL)
886     {
887       bfd_set_error (bfd_error_system_call);
888       einfo ("%F%s: %E\n", filename);
889     }
891   keep = false;
893   imppath = NULL;
894   impfile = NULL;
895   impmember = NULL;
897   lineno = 0;
899   /* 
900    * default to 32 and 64 bit mode
901    * symbols at top of /lib/syscalls.exp do not have a mode modifier and they
902    * are not repeated, assume 64 bit routines also want to use them.
903    * See the routine change_symbol_mode for more information.
904    */
905   symbol_mode = 0x04;
907   while ((c = getc (f)) != EOF)
908     {
909       char *s;
910       char *symname;
911       boolean syscall;
912       bfd_vma address;
913       struct bfd_link_hash_entry *h;
915       if (c != '\n')
916         {
917           obstack_1grow (o, c);
918           continue;
919         }
921       obstack_1grow (o, '\0');
922       ++lineno;
924       s = (char *) obstack_base (o);
925       while (isspace ((unsigned char) *s))
926         ++s;
927       if (*s == '\0'  || 
928           *s == '*' ||
929           change_symbol_mode (s) ||
930           (*s == '#' && s[1] == ' ') ||
931           (! import && *s == '#' && s[1] == '!'))
932         {
933           obstack_free (o, obstack_base (o));
934           continue;
935         }
937       if (*s == '#' && s[1] == '!')
938         {
939           s += 2;
940           while (isspace ((unsigned char) *s))
941             ++s;
942           if (*s == '\0')
943             {
944               imppath = NULL;
945               impfile = NULL;
946               impmember = NULL;
947               obstack_free (o, obstack_base (o));
948             }
949           else if (*s == '(')
950             einfo ("%F%s%d: #! ([member]) is not supported in import files\n",
951                    filename, lineno);
952           else
953             {
954               char cs;
955               char *file;
957               (void) obstack_finish (o);
958               keep = true;
959               imppath = s;
960               file = NULL;
961               while (! isspace ((unsigned char) *s) && *s != '(' && *s != '\0')
962                 {
963                   if (*s == '/')
964                     file = s + 1;
965                   ++s;
966                 }
967               if (file != NULL)
968                 {
969                   file[-1] = '\0';
970                   impfile = file;
971                   if (imppath == file - 1)
972                     imppath = "/";
973                 }
974               else
975                 {
976                   impfile = imppath;
977                   imppath = "";
978                 }
979               cs = *s;
980               *s = '\0';
981               while (isspace ((unsigned char) cs))
982                 {
983                   ++s;
984                   cs = *s;
985                 }
986               if (cs != '(')
987                 {
988                   impmember = "";
989                   if (cs != '\0')
990                     einfo ("%s:%d: warning: syntax error in import file\n",
991                            filename, lineno);
992                 }
993               else
994                 {
995                   ++s;
996                   impmember = s;
997                   while (*s != ')' && *s != '\0')
998                     ++s;
999                   if (*s == ')')
1000                     *s = '\0';
1001                   else
1002                     einfo ("%s:%d: warning: syntax error in import file\n",
1003                            filename, lineno);
1004                 }
1005             }
1007           continue;
1008         }
1010       if (symbol_mode & ${SYMBOL_MODE_MASK}) { 
1012         /* This is a symbol to be imported or exported.  */
1013         symname = s;
1014         syscall = false;
1015         address = (bfd_vma) -1;
1017         while (! isspace ((unsigned char) *s) && *s != '\0')
1018           ++s;
1019         if (*s != '\0')
1020           {
1021             char *se;
1022             
1023             *s++ = '\0';
1025             while (isspace ((unsigned char) *s))
1026               ++s;
1028             se = s;
1029             while (! isspace ((unsigned char) *se) && *se != '\0')
1030               ++se;
1031             if (*se != '\0')
1032               {
1033                 *se++ = '\0';
1034                 while (isspace ((unsigned char) *se))
1035                   ++se;
1036                 if (*se != '\0')
1037                   einfo ("%s%d: warning: syntax error in import/export file\n",
1038                          filename, lineno);
1039               }
1041             if (s != se) {
1042               int status;
1043               char *end;
1044                           
1045               status = is_syscall(s);
1046                           
1047               switch (status) {
1048               case 1:
1049                 /* this is a system call */
1050                 syscall = true;
1051                 break;
1052                                           
1053               case 0:
1054                 /* ignore this system call */
1055                 break;
1056                                           
1057               default:
1058                 /* not a system call, check for address */
1059                 address = strtoul (s, &end, 0);
1060                 if (*end != '\0') {
1061                   einfo ("%s:%d: warning: syntax error in import/export file\n", filename, lineno);
1062                          
1063                 }
1064               }
1065             }
1066           }
1068         if (! import)
1069           {
1070             struct export_symbol_list *n;
1072             ldlang_add_undef (symname);
1073             n = ((struct export_symbol_list *)
1074                  xmalloc (sizeof (struct export_symbol_list)));
1075             n->next = export_symbols;
1076             n->name = xstrdup (symname);
1077             n->syscall = syscall;
1078             export_symbols = n;
1079           }
1080         else
1081           {
1082             h = bfd_link_hash_lookup (link_info.hash, symname, false, false,
1083                                       true);
1084             if (h == NULL || h->type == bfd_link_hash_new)
1085               {
1086                 /* We can just ignore attempts to import an unreferenced
1087                    symbol.  */
1088               }
1089             else
1090               {
1091                 if (! bfd_xcoff_import_symbol (output_bfd, &link_info, h,
1092                                                address, imppath, impfile,
1093                                                impmember))
1094                   einfo ("%X%s:%d: failed to import symbol %s: %E\n",
1095                          filename, lineno, symname);
1096               }
1097           }
1098       }
1099       obstack_free (o, obstack_base (o));
1100     }
1101   
1102   if (obstack_object_size (o) > 0)
1103     {
1104           einfo ("%s:%d: warning: ignoring unterminated last line\n",
1105                  filename, lineno);
1106           obstack_free (o, obstack_base (o));
1107         }
1109       if (! keep)
1110         {
1111           obstack_free (o, NULL);
1112           free (o);
1113         }
1114     }
1116 /* This routine saves us from worrying about declaring free.  */
1118 static void
1119 gld${EMULATION_NAME}_free (p)
1120      PTR p;
1122   free (p);
1125 /* This is called by the before_allocation routine via
1126    lang_for_each_statement.  It looks for relocations and assignments
1127    to symbols.  */
1129 static void
1130 gld${EMULATION_NAME}_find_relocs (s)
1131      lang_statement_union_type *s;
1133   if (s->header.type == lang_reloc_statement_enum)
1134     {
1135       lang_reloc_statement_type *rs;
1137       rs = &s->reloc_statement;
1138       if (rs->name == NULL)
1139         einfo ("%F%P: only relocations against symbols are permitted\n");
1140       if (! bfd_xcoff_link_count_reloc (output_bfd, &link_info, rs->name))
1141         einfo ("%F%P: bfd_xcoff_link_count_reloc failed: %E\n");
1142     }
1144   if (s->header.type == lang_assignment_statement_enum)
1145     gld${EMULATION_NAME}_find_exp_assignment (s->assignment_statement.exp);
1148 /* Look through an expression for an assignment statement.  */
1150 static void
1151 gld${EMULATION_NAME}_find_exp_assignment (exp)
1152      etree_type *exp;
1154   struct bfd_link_hash_entry *h;
1156   switch (exp->type.node_class)
1157     {
1158     case etree_provide:
1159       h = bfd_link_hash_lookup (link_info.hash, exp->assign.dst,
1160                                 false, false, false);
1161       if (h == NULL)
1162         break;
1163       /* Fall through.  */
1164     case etree_assign:
1165       if (strcmp (exp->assign.dst, ".") != 0)
1166         {
1167           if (! bfd_xcoff_record_link_assignment (output_bfd, &link_info,
1168                                                   exp->assign.dst))
1169             einfo ("%P%F: failed to record assignment to %s: %E\n",
1170                    exp->assign.dst);
1171         }
1172       gld${EMULATION_NAME}_find_exp_assignment (exp->assign.src);
1173       break;
1175     case etree_binary:
1176       gld${EMULATION_NAME}_find_exp_assignment (exp->binary.lhs);
1177       gld${EMULATION_NAME}_find_exp_assignment (exp->binary.rhs);
1178       break;
1180     case etree_trinary:
1181       gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.cond);
1182       gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.lhs);
1183       gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.rhs);
1184       break;
1186     case etree_unary:
1187       gld${EMULATION_NAME}_find_exp_assignment (exp->unary.child);
1188       break;
1190     default:
1191       break;
1192     }
1195 static char *
1196 gld${EMULATION_NAME}_get_script(isfile)
1197      int *isfile;
1200 if test -n "$COMPILE_IN"
1201 then
1202 # Scripts compiled in.
1204 # sed commands to quote an ld script as a C string.
1205 sc="-f ${srcdir}/emultempl/stringify.sed"
1207 cat >>e${EMULATION_NAME}.c <<EOF
1208 {                            
1209   *isfile = 0;
1211   if (link_info.relocateable == true && config.build_constructors == true)
1212     return
1214 sed $sc ldscripts/${EMULATION_NAME}.xu                     >> e${EMULATION_NAME}.c
1215 echo '  ; else if (link_info.relocateable == true) return' >> e${EMULATION_NAME}.c
1216 sed $sc ldscripts/${EMULATION_NAME}.xr                     >> e${EMULATION_NAME}.c
1217 echo '  ; else if (!config.text_read_only) return'         >> e${EMULATION_NAME}.c
1218 sed $sc ldscripts/${EMULATION_NAME}.xbn                    >> e${EMULATION_NAME}.c
1219 echo '  ; else if (!config.magic_demand_paged) return'     >> e${EMULATION_NAME}.c
1220 sed $sc ldscripts/${EMULATION_NAME}.xn                     >> e${EMULATION_NAME}.c
1221 echo '  ; else return'                                     >> e${EMULATION_NAME}.c
1222 sed $sc ldscripts/${EMULATION_NAME}.x                      >> e${EMULATION_NAME}.c
1223 echo '; }'                                                 >> e${EMULATION_NAME}.c
1225 else
1226 # Scripts read from the filesystem.
1228 cat >>e${EMULATION_NAME}.c <<EOF
1229 {                            
1230   *isfile = 1;
1232   if (link_info.relocateable == true && config.build_constructors == true)
1233     return "ldscripts/${EMULATION_NAME}.xu";
1234   else if (link_info.relocateable == true)
1235     return "ldscripts/${EMULATION_NAME}.xr";
1236   else if (!config.text_read_only)
1237     return "ldscripts/${EMULATION_NAME}.xbn";
1238   else if (!config.magic_demand_paged)
1239     return "ldscripts/${EMULATION_NAME}.xn";
1240   else
1241     return "ldscripts/${EMULATION_NAME}.x";
1247 cat >>e${EMULATION_NAME}.c <<EOF
1249 struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation = 
1251   gld${EMULATION_NAME}_before_parse,
1252   syslib_default,
1253   hll_default,
1254   after_parse_default,
1255   gld${EMULATION_NAME}_after_open,
1256   after_allocation_default,
1257   set_output_arch_default,
1258   ldemul_default_target,
1259   gld${EMULATION_NAME}_before_allocation,
1260   gld${EMULATION_NAME}_get_script,
1261   "${EMULATION_NAME}",
1262   "${OUTPUT_FORMAT}",
1263   0,    /* finish */
1264   0,    /* create_output_section_statements */
1265   0,    /* open_dynamic_archive */
1266   0,    /* place_orphan */
1267   0,    /* set_symbols */
1268   gld${EMULATION_NAME}_parse_args,
1269   gld${EMULATION_NAME}_unrecognized_file,
1270   NULL, /* list_options */
1271   NULL, /* recognized_file */
1272   NULL, /* find potential_libraries */