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 file list. */
81 struct filelist *next;
85 /* List of import files. */
86 struct filelist *import_files;
88 /* List of export symbols read from the export files. */
90 struct export_symbol_list
92 struct export_symbol_list *next;
97 static struct export_symbol_list *export_symbols;
99 /* This routine is called before anything else is done. */
102 gld${EMULATION_NAME}_before_parse()
104 #ifndef TARGET_ /* I.e., if not generic. */
105 ldfile_output_architecture = bfd_arch_${ARCH};
106 #endif /* not TARGET_ */
109 /* Handle AIX specific options. */
112 gld${EMULATION_NAME}_parse_args (argc, argv)
116 int prevoptind = optind;
117 int prevopterr = opterr;
124 #define OPTION_IGNORE (300)
125 #define OPTION_AUTOIMP (OPTION_IGNORE + 1)
126 #define OPTION_ERNOTOK (OPTION_AUTOIMP + 1)
127 #define OPTION_EROK (OPTION_ERNOTOK + 1)
128 #define OPTION_EXPORT (OPTION_EROK + 1)
129 #define OPTION_IMPORT (OPTION_EXPORT + 1)
130 #define OPTION_MAXDATA (OPTION_IMPORT + 1)
131 #define OPTION_MAXSTACK (OPTION_MAXDATA + 1)
132 #define OPTION_MODTYPE (OPTION_MAXSTACK + 1)
133 #define OPTION_NOAUTOIMP (OPTION_MODTYPE + 1)
134 #define OPTION_NOSTRCMPCT (OPTION_NOAUTOIMP + 1)
135 #define OPTION_STRCMPCT (OPTION_NOSTRCMPCT + 1)
137 static struct option longopts[] = {
138 {"basis", no_argument, NULL, OPTION_IGNORE},
139 {"bautoimp", no_argument, NULL, OPTION_AUTOIMP},
140 {"bcomprld", no_argument, NULL, OPTION_IGNORE},
141 {"bcrld", no_argument, NULL, OPTION_IGNORE},
142 {"bcror31", no_argument, NULL, OPTION_IGNORE},
143 {"bD", required_argument, NULL, OPTION_MAXDATA},
144 {"bE", required_argument, NULL, OPTION_EXPORT},
145 {"bernotok", no_argument, NULL, OPTION_ERNOTOK},
146 {"berok", no_argument, NULL, OPTION_EROK},
147 {"berrmsg", no_argument, NULL, OPTION_IGNORE},
148 {"bexport", required_argument, NULL, OPTION_EXPORT},
149 {"bf", no_argument, NULL, OPTION_ERNOTOK},
150 {"bgc", no_argument, &gc, 1},
151 {"bh", required_argument, NULL, OPTION_IGNORE},
152 {"bhalt", required_argument, NULL, OPTION_IGNORE},
153 {"bI", required_argument, NULL, OPTION_IMPORT},
154 {"bimport", required_argument, NULL, OPTION_IMPORT},
155 {"bmaxdata", required_argument, NULL, OPTION_MAXDATA},
156 {"bmaxstack", required_argument, NULL, OPTION_MAXSTACK},
157 {"bM", required_argument, NULL, OPTION_MODTYPE},
158 {"bmodtype", required_argument, NULL, OPTION_MODTYPE},
159 {"bnoautoimp", no_argument, NULL, OPTION_NOAUTOIMP},
160 {"bnodelcsect", no_argument, NULL, OPTION_IGNORE},
161 {"bnogc", no_argument, &gc, 0},
162 {"bnso", no_argument, NULL, OPTION_NOAUTOIMP},
163 {"bnostrcmpct", no_argument, NULL, OPTION_NOSTRCMPCT},
164 {"bnotextro", no_argument, &textro, 0},
165 {"bnro", no_argument, &textro, 0},
166 {"bro", no_argument, &textro, 1},
167 {"bS", required_argument, NULL, OPTION_MAXSTACK},
168 {"bso", no_argument, NULL, OPTION_AUTOIMP},
169 {"bstrcmpct", no_argument, NULL, OPTION_STRCMPCT},
170 {"btextro", no_argument, &textro, 1},
171 {"static", no_argument, NULL, OPTION_NOAUTOIMP},
172 {NULL, no_argument, NULL, 0}
175 /* Options supported by the AIX linker which we do not support: -f,
176 -S, -v, -Z, -bbindcmds, -bbinder, -bbindopts, -bcalls, -bcaps,
177 -bcror15, -bdebugopt, -bdbg, -bdelcsect, -bex?, -bfilelist, -bfl,
178 -bgcbypass, -bglink, -binsert, -bi, -bloadmap, -bl, -bmap, -bnl,
179 -bnobind, -bnocomprld, -bnocrld, -bnoerrmsg, -bnoglink,
180 -bnoloadmap, -bnl, -bnoobjreorder, -bnoquiet, -bnoreorder,
181 -bnotypchk, -bnox, -bquiet, -bR, -brename, -breorder, -btypchk,
184 /* If the current option starts with -b, change the first : to an =.
185 The AIX linker uses : to separate the option from the argument;
186 changing it to = lets us treat it as a getopt option. */
190 if (indx < argc && strncmp (argv[indx], "-b", 2) == 0)
194 for (s = argv[indx]; *s != '\0'; s++)
205 optc = getopt_long_only (argc, argv, "-D:H:KT:z", longopts, &longind);
215 /* Long option which just sets a flag. */
219 val = strtol (optarg, &end, 0);
221 einfo ("%P: warning: ignoring invalid -D number %s\n", optarg);
223 lang_section_start (".data", exp_intop (val));
227 val = strtoul (optarg, &end, 0);
229 || (val & (val - 1)) != 0)
230 einfo ("%P: warning: ignoring invalid -H number %s\n", optarg);
237 /* FIXME: This should use the page size for the target system. */
242 /* On AIX this is the same as GNU ld -Ttext. When we see -T
243 number, we assume the AIX option is intended. Otherwise, we
244 assume the usual GNU ld -T option is intended. We can't just
245 ignore the AIX option, because gcc passes it to the linker. */
246 val = strtoul (optarg, &end, 0);
252 lang_section_start (".text", exp_intop (val));
259 link_info.static_link = false;
263 force_make_executable = false;
267 force_make_executable = true;
271 gld${EMULATION_NAME}_read_file (optarg, false);
277 struct filelist **flpp;
279 n = (struct filelist *) xmalloc (sizeof (struct filelist));
282 flpp = &import_files;
283 while (*flpp != NULL)
284 flpp = &(*flpp)->next;
290 val = strtoul (optarg, &end, 0);
292 einfo ("%P: warning: ignoring invalid -bmaxdata number %s\n",
298 case OPTION_MAXSTACK:
299 val = strtoul (optarg, &end, 0);
301 einfo ("%P: warning: ignoring invalid -bmaxstack number %s\n",
310 link_info.shared = true;
313 if (*optarg == '\0' || optarg[1] == '\0')
314 einfo ("%P: warning: ignoring invalid module type %s\n", optarg);
316 modtype = (*optarg << 8) | optarg[1];
319 case OPTION_NOAUTOIMP:
320 link_info.static_link = true;
323 case OPTION_NOSTRCMPCT:
324 config.traditional_format = true;
327 case OPTION_STRCMPCT:
328 config.traditional_format = false;
335 /* This is called after the input files have been opened. */
338 gld${EMULATION_NAME}_after_open ()
343 /* Call ldctor_build_sets, after pretending that this is a
344 relocateable link. We do this because AIX requires relocation
345 entries for all references to symbols, even in a final
347 r = link_info.relocateable;
348 link_info.relocateable = true;
349 ldctor_build_sets ();
350 link_info.relocateable = r;
352 /* For each set, record the size, so that the XCOFF backend can
353 output the correct csect length. */
354 for (p = sets; p != (struct set_info *) NULL; p = p->next)
358 /* If the symbol is defined, we may have been invoked from
359 collect, and the sets may already have been built, so we do
361 if (p->h->type == bfd_link_hash_defined
362 || p->h->type == bfd_link_hash_defweak)
365 if (p->reloc != BFD_RELOC_CTOR)
367 /* Handle this if we need to. */
371 size = (p->count + 2) * 4;
372 if (! bfd_xcoff_link_record_set (output_bfd, &link_info, p->h, size))
373 einfo ("%F%P: bfd_xcoff_link_record_set failed: %E\n");
377 /* This is called after the sections have been attached to output
378 sections, but before any sizes or addresses have been set. */
381 gld${EMULATION_NAME}_before_allocation ()
384 struct export_symbol_list *el;
387 /* Handle the import and export files, if any. */
388 for (fl = import_files; fl != NULL; fl = fl->next)
389 gld${EMULATION_NAME}_read_file (fl->name, true);
390 for (el = export_symbols; el != NULL; el = el->next)
392 struct bfd_link_hash_entry *h;
394 h = bfd_link_hash_lookup (link_info.hash, el->name, false, false, false);
396 einfo ("%P%F: bfd_link_hash_lookup of export symbol failed: %E\n");
397 if (! bfd_xcoff_export_symbol (output_bfd, &link_info, h, el->syscall))
398 einfo ("%P%F: bfd_xcoff_export_symbol failed: %E\n");
401 /* Track down all relocations called for by the linker script (these
402 are typically constructor/destructor entries created by
403 CONSTRUCTORS) and let the backend know it will need to create
404 .loader relocs for them. */
405 lang_for_each_statement (gld${EMULATION_NAME}_find_relocs);
407 /* We need to build LIBPATH from the -L arguments. If any -rpath
408 arguments were used, though, we use -rpath instead, as a GNU
410 if (command_line.rpath != NULL)
411 libpath = command_line.rpath;
412 else if (search_head == NULL)
413 libpath = (char *) "";
417 search_dirs_type *search;
419 len = strlen (search_head->name);
420 libpath = xmalloc (len + 1);
421 strcpy (libpath, search_head->name);
422 for (search = search_head->next; search != NULL; search = search->next)
426 nlen = strlen (search->name);
427 libpath = xrealloc (libpath, len + nlen + 2);
429 strcpy (libpath + len + 1, search->name);
434 /* Let the XCOFF backend set up the .loader section. */
435 if (! bfd_xcoff_size_dynamic_sections (output_bfd, &link_info, libpath,
436 entry_symbol, file_align,
440 textro ? true : false))
441 einfo ("%P%F: failed to set dynamic section sizes: %E\n");
444 /* Read an import or export file. For an import file, this is called
445 by the before_allocation emulation routine. For an export file,
446 this is called by the parse_args emulation routine. */
449 gld${EMULATION_NAME}_read_file (filename, import)
450 const char *filename;
460 const char *impmember;
462 o = (struct obstack *) xmalloc (sizeof (struct obstack));
463 obstack_specify_allocation (o, 0, 0, xmalloc, gld${EMULATION_NAME}_free);
465 f = fopen (filename, "r");
468 bfd_set_error (bfd_error_system_call);
469 einfo ("%F%s: %E\n", filename);
479 while ((c = getc (f)) != EOF)
485 struct bfd_link_hash_entry *h;
489 obstack_1grow (o, c);
493 obstack_1grow (o, '\0');
496 s = (char *) obstack_base (o);
497 while (isspace ((unsigned char) *s))
501 || (*s == '#' && s[1] == ' ')
502 || (! import && *s == '#' && s[1] == '!'))
504 obstack_free (o, obstack_base (o));
508 if (*s == '#' && s[1] == '!')
511 while (isspace ((unsigned char) *s))
518 obstack_free (o, obstack_base (o));
521 einfo ("%F%s%d: #! ([member]) is not supported in import files",
528 (void) obstack_finish (o);
532 while (! isspace ((unsigned char) *s) && *s != '(' && *s != '\0')
542 if (imppath == file - 1)
552 while (isspace ((unsigned char) cs))
561 einfo ("%s:%d: warning: syntax error in import file\n",
568 while (*s != ')' && *s != '\0')
573 einfo ("%s:%d: warning: syntax error in import file\n",
581 /* This is a symbol to be imported or exported. */
584 address = (bfd_vma) -1;
586 while (! isspace ((unsigned char) *s) && *s != '\0')
594 while (isspace ((unsigned char) *s))
598 while (! isspace ((unsigned char) *se) && *se != '\0')
603 while (isspace ((unsigned char) *se))
606 einfo ("%s%d: warning: syntax error in import/export file\n",
610 if (strcasecmp (s, "svc") == 0
611 || strcasecmp (s, "syscall") == 0)
617 address = strtoul (s, &end, 0);
619 einfo ("%s:%d: warning: syntax error in import/export file\n",
626 struct export_symbol_list *n;
628 ldlang_add_undef (symname);
629 n = ((struct export_symbol_list *)
630 xmalloc (sizeof (struct export_symbol_list)));
631 n->next = export_symbols;
632 n->name = buystring (symname);
633 n->syscall = syscall;
638 h = bfd_link_hash_lookup (link_info.hash, symname, false, false,
640 if (h == NULL || h->type == bfd_link_hash_new)
642 /* We can just ignore attempts to import an unreferenced
647 if (! bfd_xcoff_import_symbol (output_bfd, &link_info, h,
648 address, imppath, impfile,
650 einfo ("%X%s:%d: failed to import symbol %s: %E\n",
651 filename, lineno, symname);
655 obstack_free (o, obstack_base (o));
658 if (obstack_object_size (o) > 0)
660 einfo ("%s:%d: warning: ignoring unterminated last line\n",
662 obstack_free (o, obstack_base (o));
667 obstack_free (o, NULL);
672 /* This routine saves us from worrying about declaring free. */
675 gld${EMULATION_NAME}_free (p)
681 /* This is called by the before_allocation routine via
682 lang_for_each_statement. It looks for relocations and assignments
686 gld${EMULATION_NAME}_find_relocs (s)
687 lang_statement_union_type *s;
689 if (s->header.type == lang_reloc_statement_enum)
691 lang_reloc_statement_type *rs;
693 rs = &s->reloc_statement;
694 if (rs->name == NULL)
695 einfo ("%F%P: only relocations against symbols are permitted\n");
696 if (! bfd_xcoff_link_count_reloc (output_bfd, &link_info, rs->name))
697 einfo ("%F%P: bfd_xcoff_link_count_reloc failed: %E\n");
700 if (s->header.type == lang_assignment_statement_enum)
701 gld${EMULATION_NAME}_find_exp_assignment (s->assignment_statement.exp);
704 /* Look through an expression for an assignment statement. */
707 gld${EMULATION_NAME}_find_exp_assignment (exp)
710 struct bfd_link_hash_entry *h;
712 switch (exp->type.node_class)
715 h = bfd_link_hash_lookup (link_info.hash, exp->assign.dst,
716 false, false, false);
721 if (strcmp (exp->assign.dst, ".") != 0)
723 if (! bfd_xcoff_record_link_assignment (output_bfd, &link_info,
725 einfo ("%P%F: failed to record assignment to %s: %E\n",
728 gld${EMULATION_NAME}_find_exp_assignment (exp->assign.src);
732 gld${EMULATION_NAME}_find_exp_assignment (exp->binary.lhs);
733 gld${EMULATION_NAME}_find_exp_assignment (exp->binary.rhs);
737 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.cond);
738 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.lhs);
739 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.rhs);
743 gld${EMULATION_NAME}_find_exp_assignment (exp->unary.child);
752 gld${EMULATION_NAME}_get_script(isfile)
756 if test -n "$COMPILE_IN"
758 # Scripts compiled in.
760 # sed commands to quote an ld script as a C string.
761 sc="-f ${srcdir}/emultempl/stringify.sed"
763 cat >>e${EMULATION_NAME}.c <<EOF
767 if (link_info.relocateable == true && config.build_constructors == true)
770 sed $sc ldscripts/${EMULATION_NAME}.xu >> e${EMULATION_NAME}.c
771 echo ' ; else if (link_info.relocateable == true) return' >> e${EMULATION_NAME}.c
772 sed $sc ldscripts/${EMULATION_NAME}.xr >> e${EMULATION_NAME}.c
773 echo ' ; else if (!config.text_read_only) return' >> e${EMULATION_NAME}.c
774 sed $sc ldscripts/${EMULATION_NAME}.xbn >> e${EMULATION_NAME}.c
775 echo ' ; else if (!config.magic_demand_paged) return' >> e${EMULATION_NAME}.c
776 sed $sc ldscripts/${EMULATION_NAME}.xn >> e${EMULATION_NAME}.c
777 echo ' ; else return' >> e${EMULATION_NAME}.c
778 sed $sc ldscripts/${EMULATION_NAME}.x >> e${EMULATION_NAME}.c
779 echo '; }' >> e${EMULATION_NAME}.c
782 # Scripts read from the filesystem.
784 cat >>e${EMULATION_NAME}.c <<EOF
788 if (link_info.relocateable == true && config.build_constructors == true)
789 return "ldscripts/${EMULATION_NAME}.xu";
790 else if (link_info.relocateable == true)
791 return "ldscripts/${EMULATION_NAME}.xr";
792 else if (!config.text_read_only)
793 return "ldscripts/${EMULATION_NAME}.xbn";
794 else if (!config.magic_demand_paged)
795 return "ldscripts/${EMULATION_NAME}.xn";
797 return "ldscripts/${EMULATION_NAME}.x";
803 cat >>e${EMULATION_NAME}.c <<EOF
805 struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
807 gld${EMULATION_NAME}_before_parse,
811 gld${EMULATION_NAME}_after_open,
812 after_allocation_default,
813 set_output_arch_default,
814 ldemul_default_target,
815 gld${EMULATION_NAME}_before_allocation,
816 gld${EMULATION_NAME}_get_script,
820 0, /* create_output_section_statements */
821 0, /* open_dynamic_archive */
822 0, /* place_orphan */
824 gld${EMULATION_NAME}_parse_args,