1 # This shell script emits a C file. -*- C -*-
2 # It does some substitutions.
3 cat >e${EMULATION_NAME}.c <<EOF
4 /* This file is is generated by a shell script. DO NOT EDIT! */
6 /* AIX emulation code for ${EMULATION_NAME}
7 Copyright (C) 1991, 1993, 1995 Free Software Foundation, Inc.
8 Written by Steve Chamberlain <sac@cygnus.com>
9 AIX support by Ian Lance Taylor <ian@cygnus.com>
11 This file is part of GLD, the Gnu Linker.
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
27 #define TARGET_IS_${EMULATION_NAME}
31 #include "libiberty.h"
46 static void gld${EMULATION_NAME}_before_parse PARAMS ((void));
47 static int gld${EMULATION_NAME}_parse_args PARAMS ((int, char **));
48 static void gld${EMULATION_NAME}_after_open PARAMS ((void));
49 static void gld${EMULATION_NAME}_before_allocation PARAMS ((void));
50 static void gld${EMULATION_NAME}_read_file PARAMS ((const char *, boolean));
51 static void gld${EMULATION_NAME}_free PARAMS ((PTR));
52 static void gld${EMULATION_NAME}_find_relocs
53 PARAMS ((lang_statement_union_type *));
54 static void gld${EMULATION_NAME}_find_exp_assignment PARAMS ((etree_type *));
55 static char *gld${EMULATION_NAME}_get_script PARAMS ((int *isfile));
57 /* The file alignment required for each section. */
58 static unsigned long file_align;
60 /* The maximum size the stack is permitted to grow. This is stored in
62 static unsigned long maxstack;
64 /* The maximum data size. This is stored in the a.out header. */
65 static unsigned long maxdata;
67 /* Whether to perform garbage collection. */
70 /* The module type to use. */
71 static unsigned short modtype = ('1' << 8) | 'L';
73 /* Whether the .text section must be read-only (i.e., no relocs
77 /* Structure used to hold import or export file list. */
81 struct filelist *next;
85 /* List of import files. */
86 struct filelist *import_files;
88 /* List of export files. */
89 struct filelist *export_files;
92 gld${EMULATION_NAME}_before_parse()
94 #ifndef TARGET_ /* I.e., if not generic. */
95 ldfile_output_architecture = bfd_arch_${ARCH};
96 #endif /* not TARGET_ */
99 /* Handle AIX specific options. */
102 gld${EMULATION_NAME}_parse_args (argc, argv)
106 int prevoptind = optind;
107 int prevopterr = opterr;
113 #define OPTION_IGNORE (300)
114 #define OPTION_AUTOIMP (OPTION_IGNORE + 1)
115 #define OPTION_ERNOTOK (OPTION_AUTOIMP + 1)
116 #define OPTION_EROK (OPTION_ERNOTOK + 1)
117 #define OPTION_EXPORT (OPTION_EROK + 1)
118 #define OPTION_IMPORT (OPTION_EXPORT + 1)
119 #define OPTION_MAXDATA (OPTION_IMPORT + 1)
120 #define OPTION_MAXSTACK (OPTION_MAXDATA + 1)
121 #define OPTION_MODTYPE (OPTION_MAXSTACK + 1)
122 #define OPTION_NOAUTOIMP (OPTION_MODTYPE + 1)
123 #define OPTION_NOSTRCMPCT (OPTION_NOAUTOIMP + 1)
124 #define OPTION_STRCMPCT (OPTION_NOSTRCMPCT + 1)
126 static struct option longopts[] = {
127 {"basis", no_argument, NULL, OPTION_IGNORE},
128 {"bautoimp", no_argument, NULL, OPTION_AUTOIMP},
129 {"bcomprld", no_argument, NULL, OPTION_IGNORE},
130 {"bcrld", no_argument, NULL, OPTION_IGNORE},
131 {"bcror31", no_argument, NULL, OPTION_IGNORE},
132 {"bD", required_argument, NULL, OPTION_MAXDATA},
133 {"bE", required_argument, NULL, OPTION_EXPORT},
134 {"bernotok", no_argument, NULL, OPTION_ERNOTOK},
135 {"berok", no_argument, NULL, OPTION_EROK},
136 {"berrmsg", no_argument, NULL, OPTION_IGNORE},
137 {"bexport", required_argument, NULL, OPTION_EXPORT},
138 {"bf", no_argument, NULL, OPTION_ERNOTOK},
139 {"bgc", no_argument, &gc, 1},
140 {"bh", required_argument, NULL, OPTION_IGNORE},
141 {"bhalt", required_argument, NULL, OPTION_IGNORE},
142 {"bI", required_argument, NULL, OPTION_IMPORT},
143 {"bimport", required_argument, NULL, OPTION_IMPORT},
144 {"bmaxdata", required_argument, NULL, OPTION_MAXDATA},
145 {"bmaxstack", required_argument, NULL, OPTION_MAXSTACK},
146 {"bM", required_argument, NULL, OPTION_MODTYPE},
147 {"bmodtype", required_argument, NULL, OPTION_MODTYPE},
148 {"bnoautoimp", no_argument, NULL, OPTION_NOAUTOIMP},
149 {"bnodelcsect", no_argument, NULL, OPTION_IGNORE},
150 {"bnogc", no_argument, &gc, 0},
151 {"bnso", no_argument, NULL, OPTION_NOAUTOIMP},
152 {"bnostrcmpct", no_argument, NULL, OPTION_NOSTRCMPCT},
153 {"bnotextro", no_argument, &textro, 0},
154 {"bnro", no_argument, &textro, 0},
155 {"bro", no_argument, &textro, 1},
156 {"bS", required_argument, NULL, OPTION_MAXSTACK},
157 {"bso", no_argument, NULL, OPTION_AUTOIMP},
158 {"bstrcmpct", no_argument, NULL, OPTION_STRCMPCT},
159 {"btextro", no_argument, &textro, 1},
160 {"static", no_argument, NULL, OPTION_NOAUTOIMP},
161 {NULL, no_argument, NULL, 0}
164 /* Options supported by the AIX linker which we do not support: -f,
165 -S, -v, -Z, -bbindcmds, -bbinder, -bbindopts, -bcalls, -bcaps,
166 -bcror15, -bdebugopt, -bdbg, -bdelcsect, -bex?, -bfilelist, -bfl,
167 -bgcbypass, -bglink, -binsert, -bi, -bloadmap, -bl, -bmap, -bnl,
168 -bnobind, -bnocomprld, -bnocrld, -bnoerrmsg, -bnoglink,
169 -bnoloadmap, -bnl, -bnoobjreorder, -bnoquiet, -bnoreorder,
170 -bnotypchk, -bnox, -bquiet, -bR, -brename, -breorder, -btypchk,
173 /* If the first option starts with -b, change the first : to an =.
174 The AIX linker uses : to separate the option from the argument;
175 changing it to = lets us treat it as a getopt option. */
176 if (optind < argc && strncmp (argv[optind], "-b", 2) == 0)
180 for (s = argv[optind]; *s != '\0'; s++)
191 optc = getopt_long_only (argc, argv, "-D:H:KT:z", longopts, &longind);
201 /* Long option which just sets a flag. */
205 val = strtol (optarg, &end, 0);
207 einfo ("%P: warning: ignoring invalid -D number %s\n", optarg);
209 lang_section_start (".data", exp_intop (val));
213 val = strtoul (optarg, &end, 0);
215 || (val & (val - 1)) != 0)
216 einfo ("%P: warning: ignoring invalid -H number %s\n", optarg);
223 /* FIXME: This should use the page size for the target system. */
228 /* On AIX this is the same as GNU ld -Ttext. When we see -T
229 number, we assume the AIX option is intended. Otherwise, we
230 assume the usual GNU ld -T option is intended. We can't just
231 ignore the AIX option, because gcc passes it to the linker. */
232 val = strtoul (optarg, &end, 0);
238 lang_section_start (".text", exp_intop (val));
245 link_info.static_link = false;
249 force_make_executable = false;
253 force_make_executable = true;
260 struct filelist **flpp;
262 n = (struct filelist *) xmalloc (sizeof (struct filelist));
265 if (optc == OPTION_EXPORT)
266 flpp = &export_files;
268 flpp = &import_files;
269 while (*flpp != NULL)
270 flpp = &(*flpp)->next;
276 val = strtoul (optarg, &end, 0);
278 einfo ("%P: warning: ignoring invalid -bmaxdata number %s\n",
284 case OPTION_MAXSTACK:
285 val = strtoul (optarg, &end, 0);
287 einfo ("%P: warning: ignoring invalid -bmaxstack number %s\n",
296 link_info.shared = true;
299 if (*optarg == '\0' || optarg[1] == '\0')
300 einfo ("%P: warning: ignoring invalid module type %s\n", optarg);
302 modtype = (*optarg << 8) | optarg[1];
305 case OPTION_NOAUTOIMP:
306 link_info.static_link = true;
309 case OPTION_NOSTRCMPCT:
310 config.traditional_format = true;
313 case OPTION_STRCMPCT:
314 config.traditional_format = false;
321 /* This is called after the input files have been opened. */
324 gld${EMULATION_NAME}_after_open ()
329 /* Call ldctor_build_sets, after pretending that this is a
330 relocateable link. We do this because AIX requires relocation
331 entries for all references to symbols, even in a final
333 r = link_info.relocateable;
334 link_info.relocateable = true;
335 ldctor_build_sets ();
336 link_info.relocateable = r;
338 /* For each set, record the size, so that the XCOFF backend can
339 output the correct csect length. */
340 for (p = sets; p != (struct set_info *) NULL; p = p->next)
344 /* If the symbol is defined, we may have been invoked from
345 collect, and the sets may already have been built, so we do
347 if (p->h->type == bfd_link_hash_defined
348 || p->h->type == bfd_link_hash_defweak)
351 if (p->reloc != BFD_RELOC_CTOR)
353 /* Handle this if we need to. */
357 size = (p->count + 2) * 4;
358 if (! bfd_xcoff_link_record_set (output_bfd, &link_info, p->h, size))
359 einfo ("%F%P: bfd_xcoff_link_record_set failed: %E\n");
363 /* This is called after the sections have been attached to output
364 sections, but before any sizes or addresses have been set. */
367 gld${EMULATION_NAME}_before_allocation ()
372 /* Handle the import and export files, if any. */
373 for (fl = import_files; fl != NULL; fl = fl->next)
374 gld${EMULATION_NAME}_read_file (fl->name, true);
375 for (fl = export_files; fl != NULL; fl = fl->next)
376 gld${EMULATION_NAME}_read_file (fl->name, false);
378 /* Track down all relocations called for by the linker script (these
379 are typically constructor/destructor entries created by
380 CONSTRUCTORS) and let the backend know it will need to create
381 .loader relocs for them. */
382 lang_for_each_statement (gld${EMULATION_NAME}_find_relocs);
384 /* We need to build LIBPATH from the -L arguments. If any -rpath
385 arguments were used, though, we use -rpath instead, as a GNU
387 if (command_line.rpath != NULL)
388 libpath = command_line.rpath;
389 else if (search_head == NULL)
390 libpath = (char *) "";
394 search_dirs_type *search;
396 len = strlen (search_head->name);
397 libpath = xmalloc (len + 1);
398 strcpy (libpath, search_head->name);
399 for (search = search_head->next; search != NULL; search = search->next)
403 nlen = strlen (search->name);
404 libpath = xrealloc (libpath, len + nlen + 2);
406 strcpy (libpath + len + 1, search->name);
411 /* Let the XCOFF backend set up the .loader section. */
412 if (! bfd_xcoff_size_dynamic_sections (output_bfd, &link_info, libpath,
413 entry_symbol, file_align,
417 textro ? true : false))
418 einfo ("%P%F: failed to set dynamic section sizes: %E\n");
421 /* Read an import or export file. */
424 gld${EMULATION_NAME}_read_file (filename, import)
425 const char *filename;
435 const char *impmember;
437 o = (struct obstack *) xmalloc (sizeof (struct obstack));
438 obstack_specify_allocation (o, 0, 0, xmalloc, gld${EMULATION_NAME}_free);
440 f = fopen (filename, "r");
443 bfd_set_error (bfd_error_system_call);
444 einfo ("%F%s: %E\n", filename);
454 while ((c = getc (f)) != EOF)
460 struct bfd_link_hash_entry *h;
464 obstack_1grow (o, c);
468 obstack_1grow (o, '\0');
471 s = (char *) obstack_base (o);
472 while (isspace ((unsigned char) *s))
476 || (*s == '#' && s[1] == ' ')
477 || (! import && *s == '#' && s[1] == '!'))
479 obstack_free (o, obstack_base (o));
483 if (*s == '#' && s[1] == '!')
486 while (isspace ((unsigned char) *s))
493 obstack_free (o, obstack_base (o));
496 einfo ("%F%s%d: #! ([member]) is not supported in import files",
503 (void) obstack_finish (o);
507 while (! isspace ((unsigned char) *s) && *s != '(' && *s != '\0')
517 if (imppath == file - 1)
527 while (isspace ((unsigned char) cs))
536 einfo ("%s:%d: warning: syntax error in import file\n",
543 while (*s != ')' && *s != '\0')
548 einfo ("%s:%d: warning: syntax error in import file\n",
556 /* This is a symbol to be imported or exported. */
559 address = (bfd_vma) -1;
561 while (! isspace ((unsigned char) *s) && *s != '\0')
569 while (isspace ((unsigned char) *s))
573 while (! isspace ((unsigned char) *se) && *se != '\0')
578 while (isspace ((unsigned char) *se))
581 einfo ("%s%d: warning: syntax error in import/export file\n",
585 if (strcasecmp (s, "svc") == 0
586 || strcasecmp (s, "syscall") == 0)
592 address = strtoul (s, &end, 0);
594 einfo ("%s:%d: warning: syntax error in import/export file\n",
599 h = bfd_link_hash_lookup (link_info.hash, symname, false, false, true);
600 if (h == NULL || h->type == bfd_link_hash_new)
602 /* We can just ignore attempts to import an unreferenced
605 einfo ("%X%s:%d: attempt to export undefined symbol %s\n",
606 filename, lineno, symname);
610 if (! bfd_xcoff_import_symbol (output_bfd, &link_info, h, address,
611 imppath, impfile, impmember))
612 einfo ("%X%s:%d: failed to import symbol %s: %E\n",
613 filename, lineno, symname);
617 if (! bfd_xcoff_export_symbol (output_bfd, &link_info, h, syscall))
618 einfo ("%X%s:%d: failed to export symbol %s: %E\n",
619 filename, lineno, symname);
622 obstack_free (o, obstack_base (o));
625 if (obstack_object_size (o) > 0)
627 einfo ("%s:%d: warning: ignoring unterminated last line\n",
629 obstack_free (o, obstack_base (o));
634 obstack_free (o, NULL);
639 /* This routine saves us from worrying about declaring free. */
642 gld${EMULATION_NAME}_free (p)
648 /* This is called by the before_allocation routine via
649 lang_for_each_statement. It looks for relocations and assignments
653 gld${EMULATION_NAME}_find_relocs (s)
654 lang_statement_union_type *s;
656 if (s->header.type == lang_reloc_statement_enum)
658 lang_reloc_statement_type *rs;
660 rs = &s->reloc_statement;
661 if (rs->name == NULL)
662 einfo ("%F%P: only relocations against symbols are permitted\n");
663 if (! bfd_xcoff_link_count_reloc (output_bfd, &link_info, rs->name))
664 einfo ("%F%P: bfd_xcoff_link_count_reloc failed: %E\n");
667 if (s->header.type == lang_assignment_statement_enum)
668 gld${EMULATION_NAME}_find_exp_assignment (s->assignment_statement.exp);
671 /* Look through an expression for an assignment statement. */
674 gld${EMULATION_NAME}_find_exp_assignment (exp)
677 struct bfd_link_hash_entry *h;
679 switch (exp->type.node_class)
682 h = bfd_link_hash_lookup (link_info.hash, exp->assign.dst,
683 false, false, false);
688 if (strcmp (exp->assign.dst, ".") != 0)
690 if (! bfd_xcoff_record_link_assignment (output_bfd, &link_info,
692 einfo ("%P%F: failed to record assignment to %s: %E\n",
695 gld${EMULATION_NAME}_find_exp_assignment (exp->assign.src);
699 gld${EMULATION_NAME}_find_exp_assignment (exp->binary.lhs);
700 gld${EMULATION_NAME}_find_exp_assignment (exp->binary.rhs);
704 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.cond);
705 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.lhs);
706 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.rhs);
710 gld${EMULATION_NAME}_find_exp_assignment (exp->unary.child);
719 gld${EMULATION_NAME}_get_script(isfile)
723 if test -n "$COMPILE_IN"
725 # Scripts compiled in.
727 # sed commands to quote an ld script as a C string.
728 sc="-f ${srcdir}/emultempl/stringify.sed"
730 cat >>e${EMULATION_NAME}.c <<EOF
734 if (link_info.relocateable == true && config.build_constructors == true)
737 sed $sc ldscripts/${EMULATION_NAME}.xu >> e${EMULATION_NAME}.c
738 echo ' ; else if (link_info.relocateable == true) return' >> e${EMULATION_NAME}.c
739 sed $sc ldscripts/${EMULATION_NAME}.xr >> e${EMULATION_NAME}.c
740 echo ' ; else if (!config.text_read_only) return' >> e${EMULATION_NAME}.c
741 sed $sc ldscripts/${EMULATION_NAME}.xbn >> e${EMULATION_NAME}.c
742 echo ' ; else if (!config.magic_demand_paged) return' >> e${EMULATION_NAME}.c
743 sed $sc ldscripts/${EMULATION_NAME}.xn >> e${EMULATION_NAME}.c
744 echo ' ; else return' >> e${EMULATION_NAME}.c
745 sed $sc ldscripts/${EMULATION_NAME}.x >> e${EMULATION_NAME}.c
746 echo '; }' >> e${EMULATION_NAME}.c
749 # Scripts read from the filesystem.
751 cat >>e${EMULATION_NAME}.c <<EOF
755 if (link_info.relocateable == true && config.build_constructors == true)
756 return "ldscripts/${EMULATION_NAME}.xu";
757 else if (link_info.relocateable == true)
758 return "ldscripts/${EMULATION_NAME}.xr";
759 else if (!config.text_read_only)
760 return "ldscripts/${EMULATION_NAME}.xbn";
761 else if (!config.magic_demand_paged)
762 return "ldscripts/${EMULATION_NAME}.xn";
764 return "ldscripts/${EMULATION_NAME}.x";
770 cat >>e${EMULATION_NAME}.c <<EOF
772 struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
774 gld${EMULATION_NAME}_before_parse,
778 gld${EMULATION_NAME}_after_open,
779 after_allocation_default,
780 set_output_arch_default,
781 ldemul_default_target,
782 gld${EMULATION_NAME}_before_allocation,
783 gld${EMULATION_NAME}_get_script,
787 0, /* create_output_section_statements */
788 0, /* open_dynamic_archive */
789 0, /* place_orphan */
791 gld${EMULATION_NAME}_parse_args,