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"
47 static void gld${EMULATION_NAME}_before_parse PARAMS ((void));
48 static int gld${EMULATION_NAME}_parse_args PARAMS ((int, char **));
49 static void gld${EMULATION_NAME}_after_open PARAMS ((void));
50 static void gld${EMULATION_NAME}_before_allocation PARAMS ((void));
51 static void gld${EMULATION_NAME}_read_file PARAMS ((const char *, boolean));
52 static void gld${EMULATION_NAME}_free PARAMS ((PTR));
53 static void gld${EMULATION_NAME}_find_relocs
54 PARAMS ((lang_statement_union_type *));
55 static void gld${EMULATION_NAME}_find_exp_assignment PARAMS ((etree_type *));
56 static char *gld${EMULATION_NAME}_get_script PARAMS ((int *isfile));
58 /* The file alignment required for each section. */
59 static unsigned long file_align;
61 /* The maximum size the stack is permitted to grow. This is stored in
63 static unsigned long maxstack;
65 /* The maximum data size. This is stored in the a.out header. */
66 static unsigned long maxdata;
68 /* Whether to perform garbage collection. */
71 /* The module type to use. */
72 static unsigned short modtype = ('1' << 8) | 'L';
74 /* Whether the .text section must be read-only (i.e., no relocs
78 /* Structure used to hold import file list. */
82 struct filelist *next;
86 /* List of import files. */
87 static struct filelist *import_files;
89 /* List of export symbols read from the export files. */
91 struct export_symbol_list
93 struct export_symbol_list *next;
98 static struct export_symbol_list *export_symbols;
100 /* This routine is called before anything else is done. */
103 gld${EMULATION_NAME}_before_parse()
105 #ifndef TARGET_ /* I.e., if not generic. */
106 ldfile_output_architecture = bfd_arch_${ARCH};
107 #endif /* not TARGET_ */
110 /* Handle AIX specific options. */
113 gld${EMULATION_NAME}_parse_args (argc, argv)
117 int prevoptind = optind;
118 int prevopterr = opterr;
125 #define OPTION_IGNORE (300)
126 #define OPTION_AUTOIMP (OPTION_IGNORE + 1)
127 #define OPTION_ERNOTOK (OPTION_AUTOIMP + 1)
128 #define OPTION_EROK (OPTION_ERNOTOK + 1)
129 #define OPTION_EXPORT (OPTION_EROK + 1)
130 #define OPTION_IMPORT (OPTION_EXPORT + 1)
131 #define OPTION_MAXDATA (OPTION_IMPORT + 1)
132 #define OPTION_MAXSTACK (OPTION_MAXDATA + 1)
133 #define OPTION_MODTYPE (OPTION_MAXSTACK + 1)
134 #define OPTION_NOAUTOIMP (OPTION_MODTYPE + 1)
135 #define OPTION_NOSTRCMPCT (OPTION_NOAUTOIMP + 1)
136 #define OPTION_PD (OPTION_NOSTRCMPCT + 1)
137 #define OPTION_PT (OPTION_PD + 1)
138 #define OPTION_STRCMPCT (OPTION_PT + 1)
140 static struct option longopts[] = {
141 {"basis", no_argument, NULL, OPTION_IGNORE},
142 {"bautoimp", no_argument, NULL, OPTION_AUTOIMP},
143 {"bcomprld", no_argument, NULL, OPTION_IGNORE},
144 {"bcrld", no_argument, NULL, OPTION_IGNORE},
145 {"bcror31", no_argument, NULL, OPTION_IGNORE},
146 {"bD", required_argument, NULL, OPTION_MAXDATA},
147 {"bE", required_argument, NULL, OPTION_EXPORT},
148 {"bernotok", no_argument, NULL, OPTION_ERNOTOK},
149 {"berok", no_argument, NULL, OPTION_EROK},
150 {"berrmsg", no_argument, NULL, OPTION_IGNORE},
151 {"bexport", required_argument, NULL, OPTION_EXPORT},
152 {"bf", no_argument, NULL, OPTION_ERNOTOK},
153 {"bgc", no_argument, &gc, 1},
154 {"bh", required_argument, NULL, OPTION_IGNORE},
155 {"bhalt", required_argument, NULL, OPTION_IGNORE},
156 {"bI", required_argument, NULL, OPTION_IMPORT},
157 {"bimport", required_argument, NULL, OPTION_IMPORT},
158 {"bmaxdata", required_argument, NULL, OPTION_MAXDATA},
159 {"bmaxstack", required_argument, NULL, OPTION_MAXSTACK},
160 {"bM", required_argument, NULL, OPTION_MODTYPE},
161 {"bmodtype", required_argument, NULL, OPTION_MODTYPE},
162 {"bnoautoimp", no_argument, NULL, OPTION_NOAUTOIMP},
163 {"bnodelcsect", no_argument, NULL, OPTION_IGNORE},
164 {"bnogc", no_argument, &gc, 0},
165 {"bnso", no_argument, NULL, OPTION_NOAUTOIMP},
166 {"bnostrcmpct", no_argument, NULL, OPTION_NOSTRCMPCT},
167 {"bnotextro", no_argument, &textro, 0},
168 {"bnro", no_argument, &textro, 0},
169 {"bpD", required_argument, NULL, OPTION_PD},
170 {"bpT", required_argument, NULL, OPTION_PT},
171 {"bro", no_argument, &textro, 1},
172 {"bS", required_argument, NULL, OPTION_MAXSTACK},
173 {"bso", no_argument, NULL, OPTION_AUTOIMP},
174 {"bstrcmpct", no_argument, NULL, OPTION_STRCMPCT},
175 {"btextro", no_argument, &textro, 1},
176 {"static", no_argument, NULL, OPTION_NOAUTOIMP},
177 {NULL, no_argument, NULL, 0}
180 /* Options supported by the AIX linker which we do not support: -f,
181 -S, -v, -Z, -bbindcmds, -bbinder, -bbindopts, -bcalls, -bcaps,
182 -bcror15, -bdebugopt, -bdbg, -bdelcsect, -bex?, -bfilelist, -bfl,
183 -bgcbypass, -bglink, -binsert, -bi, -bloadmap, -bl, -bmap, -bnl,
184 -bnobind, -bnocomprld, -bnocrld, -bnoerrmsg, -bnoglink,
185 -bnoloadmap, -bnl, -bnoobjreorder, -bnoquiet, -bnoreorder,
186 -bnotypchk, -bnox, -bquiet, -bR, -brename, -breorder, -btypchk,
189 /* If the current option starts with -b, change the first : to an =.
190 The AIX linker uses : to separate the option from the argument;
191 changing it to = lets us treat it as a getopt option. */
195 if (indx < argc && strncmp (argv[indx], "-b", 2) == 0)
199 for (s = argv[indx]; *s != '\0'; s++)
210 optc = getopt_long_only (argc, argv, "-D:H:KT:z", longopts, &longind);
220 /* Long option which just sets a flag. */
224 val = strtol (optarg, &end, 0);
226 einfo ("%P: warning: ignoring invalid -D number %s\n", optarg);
228 lang_section_start (".data", exp_intop (val));
232 val = strtoul (optarg, &end, 0);
234 || (val & (val - 1)) != 0)
235 einfo ("%P: warning: ignoring invalid -H number %s\n", optarg);
242 /* FIXME: This should use the page size for the target system. */
247 /* On AIX this is the same as GNU ld -Ttext. When we see -T
248 number, we assume the AIX option is intended. Otherwise, we
249 assume the usual GNU ld -T option is intended. We can't just
250 ignore the AIX option, because gcc passes it to the linker. */
251 val = strtoul (optarg, &end, 0);
257 lang_section_start (".text", exp_intop (val));
264 link_info.static_link = false;
268 force_make_executable = false;
272 force_make_executable = true;
276 gld${EMULATION_NAME}_read_file (optarg, false);
282 struct filelist **flpp;
284 n = (struct filelist *) xmalloc (sizeof (struct filelist));
287 flpp = &import_files;
288 while (*flpp != NULL)
289 flpp = &(*flpp)->next;
295 val = strtoul (optarg, &end, 0);
297 einfo ("%P: warning: ignoring invalid -bmaxdata number %s\n",
303 case OPTION_MAXSTACK:
304 val = strtoul (optarg, &end, 0);
306 einfo ("%P: warning: ignoring invalid -bmaxstack number %s\n",
315 link_info.shared = true;
318 if (*optarg == '\0' || optarg[1] == '\0')
319 einfo ("%P: warning: ignoring invalid module type %s\n", optarg);
321 modtype = (*optarg << 8) | optarg[1];
324 case OPTION_NOAUTOIMP:
325 link_info.static_link = true;
328 case OPTION_NOSTRCMPCT:
329 config.traditional_format = true;
333 /* This sets the page that the .data section is supposed to
334 start on. The offset within the page should still be the
335 offset within the file, so we need to build an appropriate
337 val = strtoul (optarg, &end, 0);
339 einfo ("%P: warning: ignoring invalid -pD number %s\n", optarg);
347 exp_nameop (NAME, "."),
350 exp_binop ('+', t, exp_intop (7)),
351 exp_intop (~ (bfd_vma) 7));
352 lang_section_start (".data", t);
357 /* This set the page that the .text section is supposed to start
358 on. The offset within the page should still be the offset
360 val = strtoul (optarg, &end, 0);
362 einfo ("%P: warning: ignoring invalid -pT number %s\n", optarg);
369 exp_nameop (SIZEOF_HEADERS, NULL));
371 exp_binop ('+', t, exp_intop (7)),
372 exp_intop (~ (bfd_vma) 7));
373 lang_section_start (".text", t);
377 case OPTION_STRCMPCT:
378 config.traditional_format = false;
385 /* This is called after the input files have been opened. */
388 gld${EMULATION_NAME}_after_open ()
393 /* Call ldctor_build_sets, after pretending that this is a
394 relocateable link. We do this because AIX requires relocation
395 entries for all references to symbols, even in a final
397 r = link_info.relocateable;
398 link_info.relocateable = true;
399 ldctor_build_sets ();
400 link_info.relocateable = r;
402 /* For each set, record the size, so that the XCOFF backend can
403 output the correct csect length. */
404 for (p = sets; p != (struct set_info *) NULL; p = p->next)
408 /* If the symbol is defined, we may have been invoked from
409 collect, and the sets may already have been built, so we do
411 if (p->h->type == bfd_link_hash_defined
412 || p->h->type == bfd_link_hash_defweak)
415 if (p->reloc != BFD_RELOC_CTOR)
417 /* Handle this if we need to. */
421 size = (p->count + 2) * 4;
422 if (! bfd_xcoff_link_record_set (output_bfd, &link_info, p->h, size))
423 einfo ("%F%P: bfd_xcoff_link_record_set failed: %E\n");
427 /* This is called after the sections have been attached to output
428 sections, but before any sizes or addresses have been set. */
431 gld${EMULATION_NAME}_before_allocation ()
434 struct export_symbol_list *el;
437 /* Handle the import and export files, if any. */
438 for (fl = import_files; fl != NULL; fl = fl->next)
439 gld${EMULATION_NAME}_read_file (fl->name, true);
440 for (el = export_symbols; el != NULL; el = el->next)
442 struct bfd_link_hash_entry *h;
444 h = bfd_link_hash_lookup (link_info.hash, el->name, false, false, false);
446 einfo ("%P%F: bfd_link_hash_lookup of export symbol failed: %E\n");
447 if (! bfd_xcoff_export_symbol (output_bfd, &link_info, h, el->syscall))
448 einfo ("%P%F: bfd_xcoff_export_symbol failed: %E\n");
451 /* Track down all relocations called for by the linker script (these
452 are typically constructor/destructor entries created by
453 CONSTRUCTORS) and let the backend know it will need to create
454 .loader relocs for them. */
455 lang_for_each_statement (gld${EMULATION_NAME}_find_relocs);
457 /* We need to build LIBPATH from the -L arguments. If any -rpath
458 arguments were used, though, we use -rpath instead, as a GNU
460 if (command_line.rpath != NULL)
461 libpath = command_line.rpath;
462 else if (search_head == NULL)
463 libpath = (char *) "";
467 search_dirs_type *search;
469 len = strlen (search_head->name);
470 libpath = xmalloc (len + 1);
471 strcpy (libpath, search_head->name);
472 for (search = search_head->next; search != NULL; search = search->next)
476 nlen = strlen (search->name);
477 libpath = xrealloc (libpath, len + nlen + 2);
479 strcpy (libpath + len + 1, search->name);
484 /* Let the XCOFF backend set up the .loader section. */
485 if (! bfd_xcoff_size_dynamic_sections (output_bfd, &link_info, libpath,
486 entry_symbol, file_align,
490 textro ? true : false))
491 einfo ("%P%F: failed to set dynamic section sizes: %E\n");
494 /* Read an import or export file. For an import file, this is called
495 by the before_allocation emulation routine. For an export file,
496 this is called by the parse_args emulation routine. */
499 gld${EMULATION_NAME}_read_file (filename, import)
500 const char *filename;
510 const char *impmember;
512 o = (struct obstack *) xmalloc (sizeof (struct obstack));
513 obstack_specify_allocation (o, 0, 0, xmalloc, gld${EMULATION_NAME}_free);
515 f = fopen (filename, "r");
518 bfd_set_error (bfd_error_system_call);
519 einfo ("%F%s: %E\n", filename);
529 while ((c = getc (f)) != EOF)
535 struct bfd_link_hash_entry *h;
539 obstack_1grow (o, c);
543 obstack_1grow (o, '\0');
546 s = (char *) obstack_base (o);
547 while (isspace ((unsigned char) *s))
551 || (*s == '#' && s[1] == ' ')
552 || (! import && *s == '#' && s[1] == '!'))
554 obstack_free (o, obstack_base (o));
558 if (*s == '#' && s[1] == '!')
561 while (isspace ((unsigned char) *s))
568 obstack_free (o, obstack_base (o));
571 einfo ("%F%s%d: #! ([member]) is not supported in import files",
578 (void) obstack_finish (o);
582 while (! isspace ((unsigned char) *s) && *s != '(' && *s != '\0')
592 if (imppath == file - 1)
602 while (isspace ((unsigned char) cs))
611 einfo ("%s:%d: warning: syntax error in import file\n",
618 while (*s != ')' && *s != '\0')
623 einfo ("%s:%d: warning: syntax error in import file\n",
631 /* This is a symbol to be imported or exported. */
634 address = (bfd_vma) -1;
636 while (! isspace ((unsigned char) *s) && *s != '\0')
644 while (isspace ((unsigned char) *s))
648 while (! isspace ((unsigned char) *se) && *se != '\0')
653 while (isspace ((unsigned char) *se))
656 einfo ("%s%d: warning: syntax error in import/export file\n",
660 if (strcasecmp (s, "svc") == 0
661 || strcasecmp (s, "syscall") == 0)
667 address = strtoul (s, &end, 0);
669 einfo ("%s:%d: warning: syntax error in import/export file\n",
676 struct export_symbol_list *n;
678 ldlang_add_undef (symname);
679 n = ((struct export_symbol_list *)
680 xmalloc (sizeof (struct export_symbol_list)));
681 n->next = export_symbols;
682 n->name = buystring (symname);
683 n->syscall = syscall;
688 h = bfd_link_hash_lookup (link_info.hash, symname, false, false,
690 if (h == NULL || h->type == bfd_link_hash_new)
692 /* We can just ignore attempts to import an unreferenced
697 if (! bfd_xcoff_import_symbol (output_bfd, &link_info, h,
698 address, imppath, impfile,
700 einfo ("%X%s:%d: failed to import symbol %s: %E\n",
701 filename, lineno, symname);
705 obstack_free (o, obstack_base (o));
708 if (obstack_object_size (o) > 0)
710 einfo ("%s:%d: warning: ignoring unterminated last line\n",
712 obstack_free (o, obstack_base (o));
717 obstack_free (o, NULL);
722 /* This routine saves us from worrying about declaring free. */
725 gld${EMULATION_NAME}_free (p)
731 /* This is called by the before_allocation routine via
732 lang_for_each_statement. It looks for relocations and assignments
736 gld${EMULATION_NAME}_find_relocs (s)
737 lang_statement_union_type *s;
739 if (s->header.type == lang_reloc_statement_enum)
741 lang_reloc_statement_type *rs;
743 rs = &s->reloc_statement;
744 if (rs->name == NULL)
745 einfo ("%F%P: only relocations against symbols are permitted\n");
746 if (! bfd_xcoff_link_count_reloc (output_bfd, &link_info, rs->name))
747 einfo ("%F%P: bfd_xcoff_link_count_reloc failed: %E\n");
750 if (s->header.type == lang_assignment_statement_enum)
751 gld${EMULATION_NAME}_find_exp_assignment (s->assignment_statement.exp);
754 /* Look through an expression for an assignment statement. */
757 gld${EMULATION_NAME}_find_exp_assignment (exp)
760 struct bfd_link_hash_entry *h;
762 switch (exp->type.node_class)
765 h = bfd_link_hash_lookup (link_info.hash, exp->assign.dst,
766 false, false, false);
771 if (strcmp (exp->assign.dst, ".") != 0)
773 if (! bfd_xcoff_record_link_assignment (output_bfd, &link_info,
775 einfo ("%P%F: failed to record assignment to %s: %E\n",
778 gld${EMULATION_NAME}_find_exp_assignment (exp->assign.src);
782 gld${EMULATION_NAME}_find_exp_assignment (exp->binary.lhs);
783 gld${EMULATION_NAME}_find_exp_assignment (exp->binary.rhs);
787 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.cond);
788 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.lhs);
789 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.rhs);
793 gld${EMULATION_NAME}_find_exp_assignment (exp->unary.child);
802 gld${EMULATION_NAME}_get_script(isfile)
806 if test -n "$COMPILE_IN"
808 # Scripts compiled in.
810 # sed commands to quote an ld script as a C string.
811 sc="-f ${srcdir}/emultempl/stringify.sed"
813 cat >>e${EMULATION_NAME}.c <<EOF
817 if (link_info.relocateable == true && config.build_constructors == true)
820 sed $sc ldscripts/${EMULATION_NAME}.xu >> e${EMULATION_NAME}.c
821 echo ' ; else if (link_info.relocateable == true) return' >> e${EMULATION_NAME}.c
822 sed $sc ldscripts/${EMULATION_NAME}.xr >> e${EMULATION_NAME}.c
823 echo ' ; else if (!config.text_read_only) return' >> e${EMULATION_NAME}.c
824 sed $sc ldscripts/${EMULATION_NAME}.xbn >> e${EMULATION_NAME}.c
825 echo ' ; else if (!config.magic_demand_paged) return' >> e${EMULATION_NAME}.c
826 sed $sc ldscripts/${EMULATION_NAME}.xn >> e${EMULATION_NAME}.c
827 echo ' ; else return' >> e${EMULATION_NAME}.c
828 sed $sc ldscripts/${EMULATION_NAME}.x >> e${EMULATION_NAME}.c
829 echo '; }' >> e${EMULATION_NAME}.c
832 # Scripts read from the filesystem.
834 cat >>e${EMULATION_NAME}.c <<EOF
838 if (link_info.relocateable == true && config.build_constructors == true)
839 return "ldscripts/${EMULATION_NAME}.xu";
840 else if (link_info.relocateable == true)
841 return "ldscripts/${EMULATION_NAME}.xr";
842 else if (!config.text_read_only)
843 return "ldscripts/${EMULATION_NAME}.xbn";
844 else if (!config.magic_demand_paged)
845 return "ldscripts/${EMULATION_NAME}.xn";
847 return "ldscripts/${EMULATION_NAME}.x";
853 cat >>e${EMULATION_NAME}.c <<EOF
855 struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
857 gld${EMULATION_NAME}_before_parse,
861 gld${EMULATION_NAME}_after_open,
862 after_allocation_default,
863 set_output_arch_default,
864 ldemul_default_target,
865 gld${EMULATION_NAME}_before_allocation,
866 gld${EMULATION_NAME}_get_script,
870 0, /* create_output_section_statements */
871 0, /* open_dynamic_archive */
872 0, /* place_orphan */
874 gld${EMULATION_NAME}_parse_args,