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;
436 asection *special_sections[6];
439 /* Handle the import and export files, if any. */
440 for (fl = import_files; fl != NULL; fl = fl->next)
441 gld${EMULATION_NAME}_read_file (fl->name, true);
442 for (el = export_symbols; el != NULL; el = el->next)
444 struct bfd_link_hash_entry *h;
446 h = bfd_link_hash_lookup (link_info.hash, el->name, false, false, false);
448 einfo ("%P%F: bfd_link_hash_lookup of export symbol failed: %E\n");
449 if (! bfd_xcoff_export_symbol (output_bfd, &link_info, h, el->syscall))
450 einfo ("%P%F: bfd_xcoff_export_symbol failed: %E\n");
453 /* Track down all relocations called for by the linker script (these
454 are typically constructor/destructor entries created by
455 CONSTRUCTORS) and let the backend know it will need to create
456 .loader relocs for them. */
457 lang_for_each_statement (gld${EMULATION_NAME}_find_relocs);
459 /* We need to build LIBPATH from the -L arguments. If any -rpath
460 arguments were used, though, we use -rpath instead, as a GNU
462 if (command_line.rpath != NULL)
463 libpath = command_line.rpath;
464 else if (search_head == NULL)
465 libpath = (char *) "";
469 search_dirs_type *search;
471 len = strlen (search_head->name);
472 libpath = xmalloc (len + 1);
473 strcpy (libpath, search_head->name);
474 for (search = search_head->next; search != NULL; search = search->next)
478 nlen = strlen (search->name);
479 libpath = xrealloc (libpath, len + nlen + 2);
481 strcpy (libpath + len + 1, search->name);
486 /* Let the XCOFF backend set up the .loader section. */
487 if (! bfd_xcoff_size_dynamic_sections (output_bfd, &link_info, libpath,
488 entry_symbol, file_align,
492 textro ? true : false,
494 einfo ("%P%F: failed to set dynamic section sizes: %E\n");
496 /* Look through the special sections, and put them in the right
497 place in the link ordering. This is especially magic. */
498 for (i = 0; i < 6; i++)
501 lang_output_section_statement_type *os;
502 lang_statement_union_type **pls;
503 lang_input_section_type *is;
507 sec = special_sections[i];
511 /* Remove this section from the list of the output section.
512 This assumes we know what the script looks like. */
514 os = lang_output_section_find (sec->output_section->name);
516 einfo ("%P%F: can't find output section %s\n",
517 sec->output_section->name);
518 for (pls = &os->children.head; *pls != NULL; pls = &(*pls)->next)
520 if ((*pls)->header.type == lang_input_section_enum
521 && (*pls)->input_section.section == sec)
523 is = (lang_input_section_type *) *pls;
527 if ((*pls)->header.type == lang_wild_statement_enum)
529 lang_statement_union_type **pwls;
531 for (pwls = &(*pls)->wild_statement.children.head;
533 pwls = &(*pwls)->next)
535 if ((*pwls)->header.type == lang_input_section_enum
536 && (*pwls)->input_section.section == sec)
538 is = (lang_input_section_type *) *pwls;
539 *pwls = (*pwls)->next;
549 einfo ("%P%F: can't find %s in output section\n",
550 bfd_get_section_name (sec->owner, sec));
552 /* Now figure out where the section should go. */
555 default: /* to avoid warnings */
584 os = lang_output_section_find (oname);
588 is->header.next = os->children.head;
589 os->children.head = (lang_statement_union_type *) is;
593 is->header.next = NULL;
594 lang_statement_append (&os->children,
595 (lang_statement_union_type *) is,
601 /* Read an import or export file. For an import file, this is called
602 by the before_allocation emulation routine. For an export file,
603 this is called by the parse_args emulation routine. */
606 gld${EMULATION_NAME}_read_file (filename, import)
607 const char *filename;
617 const char *impmember;
619 o = (struct obstack *) xmalloc (sizeof (struct obstack));
620 obstack_specify_allocation (o, 0, 0, xmalloc, gld${EMULATION_NAME}_free);
622 f = fopen (filename, "r");
625 bfd_set_error (bfd_error_system_call);
626 einfo ("%F%s: %E\n", filename);
636 while ((c = getc (f)) != EOF)
642 struct bfd_link_hash_entry *h;
646 obstack_1grow (o, c);
650 obstack_1grow (o, '\0');
653 s = (char *) obstack_base (o);
654 while (isspace ((unsigned char) *s))
658 || (*s == '#' && s[1] == ' ')
659 || (! import && *s == '#' && s[1] == '!'))
661 obstack_free (o, obstack_base (o));
665 if (*s == '#' && s[1] == '!')
668 while (isspace ((unsigned char) *s))
675 obstack_free (o, obstack_base (o));
678 einfo ("%F%s%d: #! ([member]) is not supported in import files",
685 (void) obstack_finish (o);
689 while (! isspace ((unsigned char) *s) && *s != '(' && *s != '\0')
699 if (imppath == file - 1)
709 while (isspace ((unsigned char) cs))
718 einfo ("%s:%d: warning: syntax error in import file\n",
725 while (*s != ')' && *s != '\0')
730 einfo ("%s:%d: warning: syntax error in import file\n",
738 /* This is a symbol to be imported or exported. */
741 address = (bfd_vma) -1;
743 while (! isspace ((unsigned char) *s) && *s != '\0')
751 while (isspace ((unsigned char) *s))
755 while (! isspace ((unsigned char) *se) && *se != '\0')
760 while (isspace ((unsigned char) *se))
763 einfo ("%s%d: warning: syntax error in import/export file\n",
767 if (strcasecmp (s, "svc") == 0
768 || strcasecmp (s, "syscall") == 0)
774 address = strtoul (s, &end, 0);
776 einfo ("%s:%d: warning: syntax error in import/export file\n",
783 struct export_symbol_list *n;
785 ldlang_add_undef (symname);
786 n = ((struct export_symbol_list *)
787 xmalloc (sizeof (struct export_symbol_list)));
788 n->next = export_symbols;
789 n->name = buystring (symname);
790 n->syscall = syscall;
795 h = bfd_link_hash_lookup (link_info.hash, symname, false, false,
797 if (h == NULL || h->type == bfd_link_hash_new)
799 /* We can just ignore attempts to import an unreferenced
804 if (! bfd_xcoff_import_symbol (output_bfd, &link_info, h,
805 address, imppath, impfile,
807 einfo ("%X%s:%d: failed to import symbol %s: %E\n",
808 filename, lineno, symname);
812 obstack_free (o, obstack_base (o));
815 if (obstack_object_size (o) > 0)
817 einfo ("%s:%d: warning: ignoring unterminated last line\n",
819 obstack_free (o, obstack_base (o));
824 obstack_free (o, NULL);
829 /* This routine saves us from worrying about declaring free. */
832 gld${EMULATION_NAME}_free (p)
838 /* This is called by the before_allocation routine via
839 lang_for_each_statement. It looks for relocations and assignments
843 gld${EMULATION_NAME}_find_relocs (s)
844 lang_statement_union_type *s;
846 if (s->header.type == lang_reloc_statement_enum)
848 lang_reloc_statement_type *rs;
850 rs = &s->reloc_statement;
851 if (rs->name == NULL)
852 einfo ("%F%P: only relocations against symbols are permitted\n");
853 if (! bfd_xcoff_link_count_reloc (output_bfd, &link_info, rs->name))
854 einfo ("%F%P: bfd_xcoff_link_count_reloc failed: %E\n");
857 if (s->header.type == lang_assignment_statement_enum)
858 gld${EMULATION_NAME}_find_exp_assignment (s->assignment_statement.exp);
861 /* Look through an expression for an assignment statement. */
864 gld${EMULATION_NAME}_find_exp_assignment (exp)
867 struct bfd_link_hash_entry *h;
869 switch (exp->type.node_class)
872 h = bfd_link_hash_lookup (link_info.hash, exp->assign.dst,
873 false, false, false);
878 if (strcmp (exp->assign.dst, ".") != 0)
880 if (! bfd_xcoff_record_link_assignment (output_bfd, &link_info,
882 einfo ("%P%F: failed to record assignment to %s: %E\n",
885 gld${EMULATION_NAME}_find_exp_assignment (exp->assign.src);
889 gld${EMULATION_NAME}_find_exp_assignment (exp->binary.lhs);
890 gld${EMULATION_NAME}_find_exp_assignment (exp->binary.rhs);
894 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.cond);
895 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.lhs);
896 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.rhs);
900 gld${EMULATION_NAME}_find_exp_assignment (exp->unary.child);
909 gld${EMULATION_NAME}_get_script(isfile)
913 if test -n "$COMPILE_IN"
915 # Scripts compiled in.
917 # sed commands to quote an ld script as a C string.
918 sc="-f ${srcdir}/emultempl/stringify.sed"
920 cat >>e${EMULATION_NAME}.c <<EOF
924 if (link_info.relocateable == true && config.build_constructors == true)
927 sed $sc ldscripts/${EMULATION_NAME}.xu >> e${EMULATION_NAME}.c
928 echo ' ; else if (link_info.relocateable == true) return' >> e${EMULATION_NAME}.c
929 sed $sc ldscripts/${EMULATION_NAME}.xr >> e${EMULATION_NAME}.c
930 echo ' ; else if (!config.text_read_only) return' >> e${EMULATION_NAME}.c
931 sed $sc ldscripts/${EMULATION_NAME}.xbn >> e${EMULATION_NAME}.c
932 echo ' ; else if (!config.magic_demand_paged) return' >> e${EMULATION_NAME}.c
933 sed $sc ldscripts/${EMULATION_NAME}.xn >> e${EMULATION_NAME}.c
934 echo ' ; else return' >> e${EMULATION_NAME}.c
935 sed $sc ldscripts/${EMULATION_NAME}.x >> e${EMULATION_NAME}.c
936 echo '; }' >> e${EMULATION_NAME}.c
939 # Scripts read from the filesystem.
941 cat >>e${EMULATION_NAME}.c <<EOF
945 if (link_info.relocateable == true && config.build_constructors == true)
946 return "ldscripts/${EMULATION_NAME}.xu";
947 else if (link_info.relocateable == true)
948 return "ldscripts/${EMULATION_NAME}.xr";
949 else if (!config.text_read_only)
950 return "ldscripts/${EMULATION_NAME}.xbn";
951 else if (!config.magic_demand_paged)
952 return "ldscripts/${EMULATION_NAME}.xn";
954 return "ldscripts/${EMULATION_NAME}.x";
960 cat >>e${EMULATION_NAME}.c <<EOF
962 struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
964 gld${EMULATION_NAME}_before_parse,
968 gld${EMULATION_NAME}_after_open,
969 after_allocation_default,
970 set_output_arch_default,
971 ldemul_default_target,
972 gld${EMULATION_NAME}_before_allocation,
973 gld${EMULATION_NAME}_get_script,
977 0, /* create_output_section_statements */
978 0, /* open_dynamic_archive */
979 0, /* place_orphan */
981 gld${EMULATION_NAME}_parse_args,