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.
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 /* Whether to implement Unix like linker semantics. */
81 /* Structure used to hold import file list. */
85 struct filelist *next;
89 /* List of import files. */
90 static struct filelist *import_files;
92 /* List of export symbols read from the export files. */
94 struct export_symbol_list
96 struct export_symbol_list *next;
101 static struct export_symbol_list *export_symbols;
103 /* This routine is called before anything else is done. */
106 gld${EMULATION_NAME}_before_parse()
108 #ifndef TARGET_ /* I.e., if not generic. */
109 ldfile_output_architecture = bfd_arch_${ARCH};
110 #endif /* not TARGET_ */
113 /* Handle AIX specific options. */
116 gld${EMULATION_NAME}_parse_args (argc, argv)
120 int prevoptind = optind;
121 int prevopterr = opterr;
128 #define OPTION_IGNORE (300)
129 #define OPTION_AUTOIMP (OPTION_IGNORE + 1)
130 #define OPTION_ERNOTOK (OPTION_AUTOIMP + 1)
131 #define OPTION_EROK (OPTION_ERNOTOK + 1)
132 #define OPTION_EXPORT (OPTION_EROK + 1)
133 #define OPTION_IMPORT (OPTION_EXPORT + 1)
134 #define OPTION_LOADMAP (OPTION_IMPORT + 1)
135 #define OPTION_MAXDATA (OPTION_LOADMAP + 1)
136 #define OPTION_MAXSTACK (OPTION_MAXDATA + 1)
137 #define OPTION_MODTYPE (OPTION_MAXSTACK + 1)
138 #define OPTION_NOAUTOIMP (OPTION_MODTYPE + 1)
139 #define OPTION_NOSTRCMPCT (OPTION_NOAUTOIMP + 1)
140 #define OPTION_PD (OPTION_NOSTRCMPCT + 1)
141 #define OPTION_PT (OPTION_PD + 1)
142 #define OPTION_STRCMPCT (OPTION_PT + 1)
143 #define OPTION_UNIX (OPTION_STRCMPCT + 1)
145 static struct option longopts[] = {
146 {"basis", no_argument, NULL, OPTION_IGNORE},
147 {"bautoimp", no_argument, NULL, OPTION_AUTOIMP},
148 {"bcomprld", no_argument, NULL, OPTION_IGNORE},
149 {"bcrld", no_argument, NULL, OPTION_IGNORE},
150 {"bcror31", no_argument, NULL, OPTION_IGNORE},
151 {"bD", required_argument, NULL, OPTION_MAXDATA},
152 {"bE", required_argument, NULL, OPTION_EXPORT},
153 {"bernotok", no_argument, NULL, OPTION_ERNOTOK},
154 {"berok", no_argument, NULL, OPTION_EROK},
155 {"berrmsg", no_argument, NULL, OPTION_IGNORE},
156 {"bexport", required_argument, NULL, OPTION_EXPORT},
157 {"bf", no_argument, NULL, OPTION_ERNOTOK},
158 {"bgc", no_argument, &gc, 1},
159 {"bh", required_argument, NULL, OPTION_IGNORE},
160 {"bhalt", required_argument, NULL, OPTION_IGNORE},
161 {"bI", required_argument, NULL, OPTION_IMPORT},
162 {"bimport", required_argument, NULL, OPTION_IMPORT},
163 {"bl", required_argument, NULL, OPTION_LOADMAP},
164 {"bloadmap", required_argument, NULL, OPTION_LOADMAP},
165 {"bmaxdata", required_argument, NULL, OPTION_MAXDATA},
166 {"bmaxstack", required_argument, NULL, OPTION_MAXSTACK},
167 {"bM", required_argument, NULL, OPTION_MODTYPE},
168 {"bmodtype", required_argument, NULL, OPTION_MODTYPE},
169 {"bnoautoimp", no_argument, NULL, OPTION_NOAUTOIMP},
170 {"bnodelcsect", no_argument, NULL, OPTION_IGNORE},
171 {"bnogc", no_argument, &gc, 0},
172 {"bnso", no_argument, NULL, OPTION_NOAUTOIMP},
173 {"bnostrcmpct", no_argument, NULL, OPTION_NOSTRCMPCT},
174 {"bnotextro", no_argument, &textro, 0},
175 {"bnro", no_argument, &textro, 0},
176 {"bpD", required_argument, NULL, OPTION_PD},
177 {"bpT", required_argument, NULL, OPTION_PT},
178 {"bro", no_argument, &textro, 1},
179 {"bS", required_argument, NULL, OPTION_MAXSTACK},
180 {"bso", no_argument, NULL, OPTION_AUTOIMP},
181 {"bstrcmpct", no_argument, NULL, OPTION_STRCMPCT},
182 {"btextro", no_argument, &textro, 1},
183 {"static", no_argument, NULL, OPTION_NOAUTOIMP},
184 {"unix", no_argument, NULL, OPTION_UNIX},
185 {NULL, no_argument, NULL, 0}
188 /* Options supported by the AIX linker which we do not support: -f,
189 -S, -v, -Z, -bbindcmds, -bbinder, -bbindopts, -bcalls, -bcaps,
190 -bcror15, -bdebugopt, -bdbg, -bdelcsect, -bex?, -bfilelist, -bfl,
191 -bgcbypass, -bglink, -binsert, -bi, -bloadmap, -bl, -bmap, -bnl,
192 -bnobind, -bnocomprld, -bnocrld, -bnoerrmsg, -bnoglink,
193 -bnoloadmap, -bnl, -bnoobjreorder, -bnoquiet, -bnoreorder,
194 -bnotypchk, -bnox, -bquiet, -bR, -brename, -breorder, -btypchk,
197 /* If the current option starts with -b, change the first : to an =.
198 The AIX linker uses : to separate the option from the argument;
199 changing it to = lets us treat it as a getopt option. */
203 if (indx < argc && strncmp (argv[indx], "-b", 2) == 0)
207 for (s = argv[indx]; *s != '\0'; s++)
218 optc = getopt_long_only (argc, argv, "-D:H:KT:z", longopts, &longind);
228 /* Long option which just sets a flag. */
232 val = strtol (optarg, &end, 0);
234 einfo ("%P: warning: ignoring invalid -D number %s\n", optarg);
236 lang_section_start (".data", exp_intop (val));
240 val = strtoul (optarg, &end, 0);
242 || (val & (val - 1)) != 0)
243 einfo ("%P: warning: ignoring invalid -H number %s\n", optarg);
250 /* FIXME: This should use the page size for the target system. */
255 /* On AIX this is the same as GNU ld -Ttext. When we see -T
256 number, we assume the AIX option is intended. Otherwise, we
257 assume the usual GNU ld -T option is intended. We can't just
258 ignore the AIX option, because gcc passes it to the linker. */
259 val = strtoul (optarg, &end, 0);
265 lang_section_start (".text", exp_intop (val));
272 link_info.static_link = false;
276 force_make_executable = false;
280 force_make_executable = true;
284 gld${EMULATION_NAME}_read_file (optarg, false);
290 struct filelist **flpp;
292 n = (struct filelist *) xmalloc (sizeof (struct filelist));
295 flpp = &import_files;
296 while (*flpp != NULL)
297 flpp = &(*flpp)->next;
303 config.map_filename = optarg;
307 val = strtoul (optarg, &end, 0);
309 einfo ("%P: warning: ignoring invalid -bmaxdata number %s\n",
315 case OPTION_MAXSTACK:
316 val = strtoul (optarg, &end, 0);
318 einfo ("%P: warning: ignoring invalid -bmaxstack number %s\n",
327 link_info.shared = true;
330 if (*optarg == '\0' || optarg[1] == '\0')
331 einfo ("%P: warning: ignoring invalid module type %s\n", optarg);
333 modtype = (*optarg << 8) | optarg[1];
336 case OPTION_NOAUTOIMP:
337 link_info.static_link = true;
340 case OPTION_NOSTRCMPCT:
341 config.traditional_format = true;
345 /* This sets the page that the .data section is supposed to
346 start on. The offset within the page should still be the
347 offset within the file, so we need to build an appropriate
349 val = strtoul (optarg, &end, 0);
351 einfo ("%P: warning: ignoring invalid -pD number %s\n", optarg);
359 exp_nameop (NAME, "."),
362 exp_binop ('+', t, exp_intop (7)),
363 exp_intop (~ (bfd_vma) 7));
364 lang_section_start (".data", t);
369 /* This set the page that the .text section is supposed to start
370 on. The offset within the page should still be the offset
372 val = strtoul (optarg, &end, 0);
374 einfo ("%P: warning: ignoring invalid -pT number %s\n", optarg);
381 exp_nameop (SIZEOF_HEADERS, NULL));
383 exp_binop ('+', t, exp_intop (7)),
384 exp_intop (~ (bfd_vma) 7));
385 lang_section_start (".text", t);
389 case OPTION_STRCMPCT:
390 config.traditional_format = false;
401 /* This is called after the input files have been opened. */
404 gld${EMULATION_NAME}_after_open ()
409 /* Call ldctor_build_sets, after pretending that this is a
410 relocateable link. We do this because AIX requires relocation
411 entries for all references to symbols, even in a final
412 executable. Of course, we only want to do this if we are
413 producing an XCOFF output file. */
414 r = link_info.relocateable;
415 if (strstr (bfd_get_target (output_bfd), "xcoff") != NULL)
416 link_info.relocateable = true;
417 ldctor_build_sets ();
418 link_info.relocateable = r;
420 /* For each set, record the size, so that the XCOFF backend can
421 output the correct csect length. */
422 for (p = sets; p != (struct set_info *) NULL; p = p->next)
426 /* If the symbol is defined, we may have been invoked from
427 collect, and the sets may already have been built, so we do
429 if (p->h->type == bfd_link_hash_defined
430 || p->h->type == bfd_link_hash_defweak)
433 if (p->reloc != BFD_RELOC_CTOR)
435 /* Handle this if we need to. */
439 size = (p->count + 2) * 4;
440 if (! bfd_xcoff_link_record_set (output_bfd, &link_info, p->h, size))
441 einfo ("%F%P: bfd_xcoff_link_record_set failed: %E\n");
445 /* This is called after the sections have been attached to output
446 sections, but before any sizes or addresses have been set. */
449 gld${EMULATION_NAME}_before_allocation ()
452 struct export_symbol_list *el;
454 boolean export_defineds;
455 asection *special_sections[6];
458 /* Handle the import and export files, if any. */
459 for (fl = import_files; fl != NULL; fl = fl->next)
460 gld${EMULATION_NAME}_read_file (fl->name, true);
461 for (el = export_symbols; el != NULL; el = el->next)
463 struct bfd_link_hash_entry *h;
465 h = bfd_link_hash_lookup (link_info.hash, el->name, false, false, false);
467 einfo ("%P%F: bfd_link_hash_lookup of export symbol failed: %E\n");
468 if (! bfd_xcoff_export_symbol (output_bfd, &link_info, h, el->syscall))
469 einfo ("%P%F: bfd_xcoff_export_symbol failed: %E\n");
472 /* Track down all relocations called for by the linker script (these
473 are typically constructor/destructor entries created by
474 CONSTRUCTORS) and let the backend know it will need to create
475 .loader relocs for them. */
476 lang_for_each_statement (gld${EMULATION_NAME}_find_relocs);
478 /* We need to build LIBPATH from the -L arguments. If any -rpath
479 arguments were used, though, we use -rpath instead, as a GNU
481 if (command_line.rpath != NULL)
482 libpath = command_line.rpath;
483 else if (search_head == NULL)
484 libpath = (char *) "";
488 search_dirs_type *search;
490 len = strlen (search_head->name);
491 libpath = xmalloc (len + 1);
492 strcpy (libpath, search_head->name);
493 for (search = search_head->next; search != NULL; search = search->next)
497 nlen = strlen (search->name);
498 libpath = xrealloc (libpath, len + nlen + 2);
500 strcpy (libpath + len + 1, search->name);
505 /* If we are emulating the Unix linker, we want to export all
506 defined symbols, unless an explicit -bE option was used. */
507 export_defineds = false;
508 if (unix_ld && export_symbols == NULL)
509 export_defineds = true;
511 /* Let the XCOFF backend set up the .loader section. */
512 if (! bfd_xcoff_size_dynamic_sections (output_bfd, &link_info, libpath,
513 entry_symbol, file_align,
515 gc && ! unix_ld ? true : false,
517 textro ? true : false,
520 einfo ("%P%F: failed to set dynamic section sizes: %E\n");
522 /* Look through the special sections, and put them in the right
523 place in the link ordering. This is especially magic. */
524 for (i = 0; i < 6; i++)
527 lang_output_section_statement_type *os;
528 lang_statement_union_type **pls;
529 lang_input_section_type *is;
533 sec = special_sections[i];
537 /* Remove this section from the list of the output section.
538 This assumes we know what the script looks like. */
540 os = lang_output_section_find (sec->output_section->name);
542 einfo ("%P%F: can't find output section %s\n",
543 sec->output_section->name);
544 for (pls = &os->children.head; *pls != NULL; pls = &(*pls)->next)
546 if ((*pls)->header.type == lang_input_section_enum
547 && (*pls)->input_section.section == sec)
549 is = (lang_input_section_type *) *pls;
553 if ((*pls)->header.type == lang_wild_statement_enum)
555 lang_statement_union_type **pwls;
557 for (pwls = &(*pls)->wild_statement.children.head;
559 pwls = &(*pwls)->next)
561 if ((*pwls)->header.type == lang_input_section_enum
562 && (*pwls)->input_section.section == sec)
564 is = (lang_input_section_type *) *pwls;
565 *pwls = (*pwls)->next;
575 einfo ("%P%F: can't find %s in output section\n",
576 bfd_get_section_name (sec->owner, sec));
578 /* Now figure out where the section should go. */
581 default: /* to avoid warnings */
610 os = lang_output_section_find (oname);
614 is->header.next = os->children.head;
615 os->children.head = (lang_statement_union_type *) is;
619 is->header.next = NULL;
620 lang_statement_append (&os->children,
621 (lang_statement_union_type *) is,
627 /* Read an import or export file. For an import file, this is called
628 by the before_allocation emulation routine. For an export file,
629 this is called by the parse_args emulation routine. */
632 gld${EMULATION_NAME}_read_file (filename, import)
633 const char *filename;
643 const char *impmember;
645 o = (struct obstack *) xmalloc (sizeof (struct obstack));
646 obstack_specify_allocation (o, 0, 0, xmalloc, gld${EMULATION_NAME}_free);
648 f = fopen (filename, "r");
651 bfd_set_error (bfd_error_system_call);
652 einfo ("%F%s: %E\n", filename);
662 while ((c = getc (f)) != EOF)
668 struct bfd_link_hash_entry *h;
672 obstack_1grow (o, c);
676 obstack_1grow (o, '\0');
679 s = (char *) obstack_base (o);
680 while (isspace ((unsigned char) *s))
684 || (*s == '#' && s[1] == ' ')
685 || (! import && *s == '#' && s[1] == '!'))
687 obstack_free (o, obstack_base (o));
691 if (*s == '#' && s[1] == '!')
694 while (isspace ((unsigned char) *s))
701 obstack_free (o, obstack_base (o));
704 einfo ("%F%s%d: #! ([member]) is not supported in import files",
711 (void) obstack_finish (o);
715 while (! isspace ((unsigned char) *s) && *s != '(' && *s != '\0')
725 if (imppath == file - 1)
735 while (isspace ((unsigned char) cs))
744 einfo ("%s:%d: warning: syntax error in import file\n",
751 while (*s != ')' && *s != '\0')
756 einfo ("%s:%d: warning: syntax error in import file\n",
764 /* This is a symbol to be imported or exported. */
767 address = (bfd_vma) -1;
769 while (! isspace ((unsigned char) *s) && *s != '\0')
777 while (isspace ((unsigned char) *s))
781 while (! isspace ((unsigned char) *se) && *se != '\0')
786 while (isspace ((unsigned char) *se))
789 einfo ("%s%d: warning: syntax error in import/export file\n",
793 if (strcasecmp (s, "svc") == 0
794 || strcasecmp (s, "syscall") == 0)
800 address = strtoul (s, &end, 0);
802 einfo ("%s:%d: warning: syntax error in import/export file\n",
809 struct export_symbol_list *n;
811 ldlang_add_undef (symname);
812 n = ((struct export_symbol_list *)
813 xmalloc (sizeof (struct export_symbol_list)));
814 n->next = export_symbols;
815 n->name = buystring (symname);
816 n->syscall = syscall;
821 h = bfd_link_hash_lookup (link_info.hash, symname, false, false,
823 if (h == NULL || h->type == bfd_link_hash_new)
825 /* We can just ignore attempts to import an unreferenced
830 if (! bfd_xcoff_import_symbol (output_bfd, &link_info, h,
831 address, imppath, impfile,
833 einfo ("%X%s:%d: failed to import symbol %s: %E\n",
834 filename, lineno, symname);
838 obstack_free (o, obstack_base (o));
841 if (obstack_object_size (o) > 0)
843 einfo ("%s:%d: warning: ignoring unterminated last line\n",
845 obstack_free (o, obstack_base (o));
850 obstack_free (o, NULL);
855 /* This routine saves us from worrying about declaring free. */
858 gld${EMULATION_NAME}_free (p)
864 /* This is called by the before_allocation routine via
865 lang_for_each_statement. It looks for relocations and assignments
869 gld${EMULATION_NAME}_find_relocs (s)
870 lang_statement_union_type *s;
872 if (s->header.type == lang_reloc_statement_enum)
874 lang_reloc_statement_type *rs;
876 rs = &s->reloc_statement;
877 if (rs->name == NULL)
878 einfo ("%F%P: only relocations against symbols are permitted\n");
879 if (! bfd_xcoff_link_count_reloc (output_bfd, &link_info, rs->name))
880 einfo ("%F%P: bfd_xcoff_link_count_reloc failed: %E\n");
883 if (s->header.type == lang_assignment_statement_enum)
884 gld${EMULATION_NAME}_find_exp_assignment (s->assignment_statement.exp);
887 /* Look through an expression for an assignment statement. */
890 gld${EMULATION_NAME}_find_exp_assignment (exp)
893 struct bfd_link_hash_entry *h;
895 switch (exp->type.node_class)
898 h = bfd_link_hash_lookup (link_info.hash, exp->assign.dst,
899 false, false, false);
904 if (strcmp (exp->assign.dst, ".") != 0)
906 if (! bfd_xcoff_record_link_assignment (output_bfd, &link_info,
908 einfo ("%P%F: failed to record assignment to %s: %E\n",
911 gld${EMULATION_NAME}_find_exp_assignment (exp->assign.src);
915 gld${EMULATION_NAME}_find_exp_assignment (exp->binary.lhs);
916 gld${EMULATION_NAME}_find_exp_assignment (exp->binary.rhs);
920 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.cond);
921 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.lhs);
922 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.rhs);
926 gld${EMULATION_NAME}_find_exp_assignment (exp->unary.child);
935 gld${EMULATION_NAME}_get_script(isfile)
939 if test -n "$COMPILE_IN"
941 # Scripts compiled in.
943 # sed commands to quote an ld script as a C string.
944 sc="-f ${srcdir}/emultempl/stringify.sed"
946 cat >>e${EMULATION_NAME}.c <<EOF
950 if (link_info.relocateable == true && config.build_constructors == true)
953 sed $sc ldscripts/${EMULATION_NAME}.xu >> e${EMULATION_NAME}.c
954 echo ' ; else if (link_info.relocateable == true) return' >> e${EMULATION_NAME}.c
955 sed $sc ldscripts/${EMULATION_NAME}.xr >> e${EMULATION_NAME}.c
956 echo ' ; else if (!config.text_read_only) return' >> e${EMULATION_NAME}.c
957 sed $sc ldscripts/${EMULATION_NAME}.xbn >> e${EMULATION_NAME}.c
958 echo ' ; else if (!config.magic_demand_paged) return' >> e${EMULATION_NAME}.c
959 sed $sc ldscripts/${EMULATION_NAME}.xn >> e${EMULATION_NAME}.c
960 echo ' ; else return' >> e${EMULATION_NAME}.c
961 sed $sc ldscripts/${EMULATION_NAME}.x >> e${EMULATION_NAME}.c
962 echo '; }' >> e${EMULATION_NAME}.c
965 # Scripts read from the filesystem.
967 cat >>e${EMULATION_NAME}.c <<EOF
971 if (link_info.relocateable == true && config.build_constructors == true)
972 return "ldscripts/${EMULATION_NAME}.xu";
973 else if (link_info.relocateable == true)
974 return "ldscripts/${EMULATION_NAME}.xr";
975 else if (!config.text_read_only)
976 return "ldscripts/${EMULATION_NAME}.xbn";
977 else if (!config.magic_demand_paged)
978 return "ldscripts/${EMULATION_NAME}.xn";
980 return "ldscripts/${EMULATION_NAME}.x";
986 cat >>e${EMULATION_NAME}.c <<EOF
988 struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
990 gld${EMULATION_NAME}_before_parse,
994 gld${EMULATION_NAME}_after_open,
995 after_allocation_default,
996 set_output_arch_default,
997 ldemul_default_target,
998 gld${EMULATION_NAME}_before_allocation,
999 gld${EMULATION_NAME}_get_script,
1000 "${EMULATION_NAME}",
1003 0, /* create_output_section_statements */
1004 0, /* open_dynamic_archive */
1005 0, /* place_orphan */
1006 0, /* set_symbols */
1007 gld${EMULATION_NAME}_parse_args,