1 # This shell script emits a C file. -*- C -*-
2 # It does some substitutions.
3 if [ -z "$MACHINE" ]; then
6 OUTPUT_ARCH=${ARCH}:${MACHINE}
9 /* This file is is generated by a shell script. DO NOT EDIT! */
11 /* AIX emulation code for ${EMULATION_NAME}
12 Copyright (C) 1991-2022 Free Software Foundation, Inc.
13 Written by Steve Chamberlain <sac@cygnus.com>
14 AIX support by Ian Lance Taylor <ian@cygnus.com>
15 AIX 64 bit support by Tom Rix <trix@redhat.com>
17 This file is part of the GNU Binutils.
19 This program is free software; you can redistribute it and/or modify
20 it under the terms of the GNU General Public License as published by
21 the Free Software Foundation; either version 3 of the License, or
22 (at your option) any later version.
24 This program is distributed in the hope that it will be useful,
25 but WITHOUT ANY WARRANTY; without even the implied warranty of
26 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 GNU General Public License for more details.
29 You should have received a copy of the GNU General Public License
30 along with this program; if not, write to the Free Software
31 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
32 MA 02110-1301, USA. */
34 #define TARGET_IS_${EMULATION_NAME}
38 #include "libiberty.h"
39 #include "safe-ctype.h"
55 #include "coff/internal.h"
56 #include "coff/xcoff.h"
59 #include "xcofflink.h"
61 static void gld${EMULATION_NAME}_read_file (const char *, bool);
62 static void gld${EMULATION_NAME}_free (void *);
63 static void gld${EMULATION_NAME}_find_relocs (lang_statement_union_type *);
64 static void gld${EMULATION_NAME}_find_exp_assignment (etree_type *);
66 static asection *xcoff_add_stub_section (const char *, asection *);
67 static void xcoff_layout_sections_again (void);
69 static struct bfd_xcoff_link_params params = {
71 &xcoff_add_stub_section,
72 &xcoff_layout_sections_again
76 /* The file alignment required for each section. */
77 static unsigned long file_align;
79 /* The maximum size the stack is permitted to grow. This is stored in
81 static unsigned long maxstack;
83 /* The maximum data size. This is stored in the a.out header. */
84 static unsigned long maxdata;
86 /* Whether to perform garbage collection. */
89 /* The module type to use. */
90 static unsigned short modtype = ('1' << 8) | 'L';
92 /* Whether the .text section must be read-only (i.e., no relocs
96 /* A mask of XCOFF_EXPALL and XCOFF_EXPFULL flags, as set by their
97 associated -b and -bno options. */
98 static unsigned int auto_export_flags;
100 /* A mask of auto_export_flags bits that were explicitly set on the
102 static unsigned int explicit_auto_export_flags;
104 /* Whether to implement Unix like linker semantics. */
107 /* Structure used to hold import file list. */
111 struct filelist *next;
115 /* List of import files. */
116 static struct filelist *import_files;
118 /* List of export symbols read from the export files. */
120 struct export_symbol_list
122 struct export_symbol_list *next;
126 static struct export_symbol_list *export_symbols;
128 /* Maintains the 32 or 64 bit mode state of import file */
129 static unsigned int symbol_mode = 0x04;
131 /* Which symbol modes are valid */
132 static unsigned int symbol_mode_mask = 0x0d;
134 /* Whether this is a 64 bit link */
135 static int is_64bit = 0;
137 /* Which syscalls from import file are valid */
138 static unsigned int syscall_mask = 0x77;
140 /* fake file for -binitfini support */
141 static lang_input_statement_type *initfini_file;
143 /* Whether to do run time linking
144 -brtl enables, -bnortl and -bnortllib disable. */
147 /* Explicit command line library path, -blibpath */
148 static char *command_line_blibpath = NULL;
150 /* Fake input file for stubs. */
151 static lang_input_statement_type *stub_file;
153 /* This routine is called before anything else is done. */
156 gld${EMULATION_NAME}_before_parse (void)
158 ldfile_set_output_arch ("${OUTPUT_ARCH}", bfd_arch_`echo ${ARCH} | sed -e 's/:.*//'`);
160 input_flags.dynamic = true;
161 config.has_shared = true;
163 /* The link_info.[init|fini]_functions are initialized in ld/lexsup.c.
164 Override them here so we can use the link_info.init_function as a
165 state flag that lets the backend know that -binitfini has been done. */
167 link_info.init_function = NULL;
168 link_info.fini_function = NULL;
172 /* Handle AIX specific options. */
204 gld${EMULATION_NAME}_add_options
205 (int ns, char **shortopts, int nl, struct option **longopts,
206 int nrl ATTRIBUTE_UNUSED, struct option **really_longopts ATTRIBUTE_UNUSED)
208 static const char xtra_short[] = "D:H:KT:z";
209 static const struct option xtra_long[] = {
210 /* -binitfini has special handling in the linker backend. The native linker
211 uses the arguemnts to generate a table of init and fini functions for
212 the executable. The important use for this option is to support aix 4.2+
213 c++ constructors and destructors. This is tied into gcc via collect2.c.
215 The function table is accessed by the runtime linker/loader by checking if
216 the first symbol in the loader symbol table is __rtinit. The gnu linker
217 generates this symbol and makes it the first loader symbol. */
219 {"basis", no_argument, NULL, OPTION_IGNORE},
220 {"bautoimp", no_argument, NULL, OPTION_AUTOIMP},
221 {"bcomprld", no_argument, NULL, OPTION_IGNORE},
222 {"bcrld", no_argument, NULL, OPTION_IGNORE},
223 {"bcror31", no_argument, NULL, OPTION_IGNORE},
224 {"bD", required_argument, NULL, OPTION_MAXDATA},
225 {"bE", required_argument, NULL, OPTION_EXPORT},
226 {"bernotok", no_argument, NULL, OPTION_ERNOTOK},
227 {"berok", no_argument, NULL, OPTION_EROK},
228 {"berrmsg", no_argument, NULL, OPTION_IGNORE},
229 {"bexpall", no_argument, NULL, OPTION_EXPALL},
230 {"bexpfull", no_argument, NULL, OPTION_EXPFULL},
231 {"bexport", required_argument, NULL, OPTION_EXPORT},
232 {"bbigtoc", no_argument, NULL, OPTION_IGNORE},
233 {"bf", no_argument, NULL, OPTION_ERNOTOK},
234 {"bgc", no_argument, &gc, 1},
235 {"bh", required_argument, NULL, OPTION_IGNORE},
236 {"bhalt", required_argument, NULL, OPTION_IGNORE},
237 {"bI", required_argument, NULL, OPTION_IMPORT},
238 {"bimport", required_argument, NULL, OPTION_IMPORT},
239 {"binitfini", required_argument, NULL, OPTION_INITFINI},
240 {"bl", required_argument, NULL, OPTION_LOADMAP},
241 {"bloadmap", required_argument, NULL, OPTION_LOADMAP},
242 {"bmaxdata", required_argument, NULL, OPTION_MAXDATA},
243 {"bmaxstack", required_argument, NULL, OPTION_MAXSTACK},
244 {"bM", required_argument, NULL, OPTION_MODTYPE},
245 {"bmodtype", required_argument, NULL, OPTION_MODTYPE},
246 {"bnoautoimp", no_argument, NULL, OPTION_NOAUTOIMP},
247 {"bnoexpall", no_argument, NULL, OPTION_NOEXPALL},
248 {"bnoexpfull", no_argument, NULL, OPTION_NOEXPFULL},
249 {"bnodelcsect", no_argument, NULL, OPTION_IGNORE},
250 {"bnoentry", no_argument, NULL, OPTION_IGNORE},
251 {"bnogc", no_argument, &gc, 0},
252 {"bnso", no_argument, NULL, OPTION_NOAUTOIMP},
253 {"bnostrcmpct", no_argument, NULL, OPTION_NOSTRCMPCT},
254 {"bnotextro", no_argument, &textro, 0},
255 {"bnro", no_argument, &textro, 0},
256 {"bpD", required_argument, NULL, OPTION_PD},
257 {"bpT", required_argument, NULL, OPTION_PT},
258 {"bro", no_argument, &textro, 1},
259 {"brtl", no_argument, &rtld, 1},
260 {"bnortl", no_argument, &rtld, 0},
261 {"bnortllib", no_argument, &rtld, 0},
262 {"bS", required_argument, NULL, OPTION_MAXSTACK},
263 {"bso", no_argument, NULL, OPTION_AUTOIMP},
264 {"bstrcmpct", no_argument, NULL, OPTION_STRCMPCT},
265 {"btextro", no_argument, &textro, 1},
266 {"b32", no_argument, NULL, OPTION_32},
267 {"b64", no_argument, NULL, OPTION_64},
268 {"static", no_argument, NULL, OPTION_NOAUTOIMP},
269 {"unix", no_argument, NULL, OPTION_UNIX},
270 {"blibpath", required_argument, NULL, OPTION_LIBPATH},
271 {"bnolibpath", required_argument, NULL, OPTION_NOLIBPATH},
272 {NULL, no_argument, NULL, 0}
275 /* Options supported by the AIX linker which we do not support:
276 -S, -v, -Z, -bbindcmds, -bbinder, -bbindopts, -bcalls, -bcaps,
277 -bcror15, -bdebugopt, -bdbg, -bdelcsect, -bex?, -bfilelist, -bfl,
278 -bgcbypass, -bglink, -binsert, -bi, -bloadmap, -bl, -bmap, -bnl,
279 -bnobind, -bnocomprld, -bnocrld, -bnoerrmsg, -bnoglink,
280 -bnoloadmap, -bnl, -bnoobjreorder, -bnoquiet, -bnoreorder,
281 -bnotypchk, -bnox, -bquiet, -bR, -brename, -breorder, -btypchk,
284 *shortopts = (char *) xrealloc (*shortopts, ns + sizeof (xtra_short));
285 memcpy (*shortopts + ns, &xtra_short, sizeof (xtra_short));
286 *longopts = xrealloc (*longopts,
287 nl * sizeof (struct option) + sizeof (xtra_long));
288 memcpy (*longopts + nl, &xtra_long, sizeof (xtra_long));
292 gld${EMULATION_NAME}_parse_args (int argc, char **argv)
296 /* If the current option starts with -b, change the first : to an =.
297 The AIX linker uses : to separate the option from the argument;
298 changing it to = lets us treat it as a getopt option. */
303 if (indx < argc && startswith (argv[indx], "-b"))
307 for (s = argv[indx]; *s != '\0'; s++)
319 /* Helper for option '-f', which specify a list of input files.
320 Contrary to the native linker, we don't support shell patterns
321 (simply because glob isn't always available). */
324 read_file_list (const char *filename)
327 /* An upper bound on the number of characters in the file. */
329 /* File in memory. */
335 f = fopen (filename, FOPEN_RT);
338 einfo (_("%F%P: cannot open %s\n"), filename);
341 if (fseek (f, 0L, SEEK_END) == -1)
346 if (fseek (f, 0L, SEEK_SET) == -1)
349 buffer = (char *) xmalloc (pos + 1);
350 len = fread (buffer, sizeof (char), pos, f);
351 if (len != (size_t) pos && ferror (f))
353 /* Add a NUL terminator. */
361 /* Skip empty lines. */
362 while (*b == '\n' || *b == '\r')
365 /* Stop if end of buffer. */
366 if (b == buffer + len)
369 /* Eat any byte until end of line. */
370 for (e = b; *e != '\0'; e++)
371 if (*e == '\n' || *e == '\r')
374 /* Replace end of line by nul. */
379 lang_add_input_file (b, lang_input_file_is_search_file_enum, NULL);
385 einfo (_("%F%P: cannot read %s\n"), optarg);
390 gld${EMULATION_NAME}_handle_option (int optc)
401 /* Long option which just sets a flag. */
405 /* This overrides --auxiliary. This option specifies a file containing
406 a list of input files. */
407 read_file_list (optarg);
411 val = bfd_scan_vma (optarg, &end, 0);
413 einfo (_("%P: warning: ignoring invalid -D number %s\n"), optarg);
415 lang_section_start (".data", exp_intop (val), NULL);
419 val = bfd_scan_vma (optarg, &end, 0);
420 if (*end != '\0' || (val & (val - 1)) != 0)
421 einfo (_("%P: warning: ignoring invalid -H number %s\n"), optarg);
428 /* FIXME: This should use the page size for the target system. */
433 /* On AIX this is the same as GNU ld -Ttext. When we see -T
434 number, we assume the AIX option is intended. Otherwise, we
435 assume the usual GNU ld -T option is intended. We can't just
436 ignore the AIX option, because gcc passes it to the linker. */
437 val = bfd_scan_vma (optarg, &end, 0);
440 lang_section_start (".text", exp_intop (val), NULL);
446 case OPTION_INITFINI:
449 * The aix linker init fini has the format :
451 * -binitfini:[ Initial][:Termination][:Priority]
453 * it allows the Termination and Priority to be optional.
455 * Since we support only one init/fini pair, we ignore the Priority.
457 * Define the special symbol __rtinit.
459 * strtok does not correctly handle the case of -binitfini::fini: so
465 while (*t && ':' != *t)
471 link_info.init_function = i;
474 while (*t && ':' != *t)
479 link_info.fini_function = f;
484 link_info.static_link = false;
488 link_info.unresolved_syms_in_objects = RM_DIAGNOSE;
489 link_info.unresolved_syms_in_shared_libs = RM_DIAGNOSE;
493 link_info.unresolved_syms_in_objects = RM_IGNORE;
494 link_info.unresolved_syms_in_shared_libs = RM_IGNORE;
498 auto_export_flags |= XCOFF_EXPALL;
499 explicit_auto_export_flags |= XCOFF_EXPALL;
503 auto_export_flags |= XCOFF_EXPFULL;
504 explicit_auto_export_flags |= XCOFF_EXPFULL;
508 gld${EMULATION_NAME}_read_file (optarg, false);
514 struct filelist **flpp;
516 n = (struct filelist *) xmalloc (sizeof (struct filelist));
519 flpp = &import_files;
520 while (*flpp != NULL)
521 flpp = &(*flpp)->next;
527 config.map_filename = optarg;
531 val = bfd_scan_vma (optarg, &end, 0);
533 einfo (_("%P: warning: ignoring invalid -bmaxdata number %s\n"),
539 case OPTION_MAXSTACK:
540 val = bfd_scan_vma (optarg, &end, 0);
542 einfo (_("%P: warning: ignoring invalid -bmaxstack number %s\n"),
551 link_info.type = type_dll;
554 if (*optarg == '\0' || optarg[1] == '\0')
555 einfo (_("%P: warning: ignoring invalid module type %s\n"), optarg);
557 modtype = (*optarg << 8) | optarg[1];
560 case OPTION_NOAUTOIMP:
561 link_info.static_link = true;
564 case OPTION_NOEXPALL:
565 auto_export_flags &= ~XCOFF_EXPALL;
566 explicit_auto_export_flags |= XCOFF_EXPALL;
569 case OPTION_NOEXPFULL:
570 auto_export_flags &= ~XCOFF_EXPFULL;
571 explicit_auto_export_flags |= XCOFF_EXPFULL;
574 case OPTION_NOSTRCMPCT:
575 link_info.traditional_format = true;
579 /* This sets the page that the .data section is supposed to
580 start on. The offset within the page should still be the
581 offset within the file, so we need to build an appropriate
583 val = bfd_scan_vma (optarg, &end, 0);
585 einfo (_("%P: warning: ignoring invalid -pD number %s\n"), optarg);
593 exp_nameop (NAME, "."),
596 exp_binop ('+', t, exp_intop (31)),
597 exp_intop (~(bfd_vma) 31));
598 lang_section_start (".data", t, NULL);
603 /* This set the page that the .text section is supposed to start
604 on. The offset within the page should still be the offset
606 val = bfd_scan_vma (optarg, &end, 0);
608 einfo (_("%P: warning: ignoring invalid -pT number %s\n"), optarg);
615 exp_nameop (SIZEOF_HEADERS, NULL));
617 exp_binop ('+', t, exp_intop (31)),
618 exp_intop (~(bfd_vma) 31));
619 lang_section_start (".text", t, NULL);
623 case OPTION_STRCMPCT:
624 link_info.traditional_format = false;
634 symbol_mode_mask = 0x0d;
640 symbol_mode_mask = 0x0e;
644 command_line_blibpath = optarg;
647 case OPTION_NOLIBPATH:
648 command_line_blibpath = NULL;
656 /* This is called when an input file can not be recognized as a BFD
657 object or an archive. If the file starts with #!, we must treat it
658 as an import file. This is for AIX compatibility. */
661 gld${EMULATION_NAME}_unrecognized_file (lang_input_statement_type *entry)
666 e = fopen (entry->filename, FOPEN_RT);
672 if (getc (e) == '#' && getc (e) == '!')
675 struct filelist **flpp;
677 n = (struct filelist *) xmalloc (sizeof (struct filelist));
679 n->name = entry->filename;
680 flpp = &import_files;
681 while (*flpp != NULL)
682 flpp = &(*flpp)->next;
686 entry->flags.loaded = true;
694 /* This is called after the input files have been opened. */
697 gld${EMULATION_NAME}_after_open (void)
702 after_open_default ();
704 /* Call ldctor_build_sets, after pretending that this is a
705 relocatable link. We do this because AIX requires relocation
706 entries for all references to symbols, even in a final
707 executable. Of course, we only want to do this if we are
708 producing an XCOFF output file. */
710 if (strstr (bfd_get_target (link_info.output_bfd), "xcoff") != NULL)
711 link_info.type = type_relocatable;
712 ldctor_build_sets ();
715 /* For each set, record the size, so that the XCOFF backend can
716 output the correct csect length. */
717 for (p = sets; p != (struct set_info *) NULL; p = p->next)
721 /* If the symbol is defined, we may have been invoked from
722 collect, and the sets may already have been built, so we do
724 if (p->h->type == bfd_link_hash_defined
725 || p->h->type == bfd_link_hash_defweak)
728 if (p->reloc != BFD_RELOC_CTOR)
730 /* Handle this if we need to. */
734 size = (p->count + 2) * 4;
735 if (!bfd_xcoff_link_record_set (link_info.output_bfd, &link_info,
737 einfo (_("%F%P: bfd_xcoff_link_record_set failed: %E\n"));
741 /* This is called after the sections have been attached to output
742 sections, but before any sizes or addresses have been set. */
745 gld${EMULATION_NAME}_before_allocation (void)
748 struct export_symbol_list *el;
750 asection *special_sections[XCOFF_NUMBER_OF_SPECIAL_SECTIONS];
751 static const char *const must_keep_sections[] = {
756 unsigned int i, flags;
758 /* Handle the import and export files, if any. */
759 for (fl = import_files; fl != NULL; fl = fl->next)
760 gld${EMULATION_NAME}_read_file (fl->name, true);
761 for (el = export_symbols; el != NULL; el = el->next)
763 struct bfd_link_hash_entry *h;
765 h = bfd_link_hash_lookup (link_info.hash, el->name, false, false, false);
767 einfo (_("%F%P: bfd_link_hash_lookup of export symbol failed: %E\n"));
768 if (!bfd_xcoff_export_symbol (link_info.output_bfd, &link_info, h))
769 einfo (_("%F%P: bfd_xcoff_export_symbol failed: %E\n"));
772 /* Track down all relocations called for by the linker script (these
773 are typically constructor/destructor entries created by
774 CONSTRUCTORS) and let the backend know it will need to create
775 .loader relocs for them. */
776 lang_for_each_statement (gld${EMULATION_NAME}_find_relocs);
778 /* Precedence of LIBPATH
779 -blibpath: native support always first
780 -rpath: gnu extension
781 -L build from command line -L's */
782 if (command_line_blibpath != NULL)
783 libpath = command_line_blibpath;
784 else if (command_line.rpath != NULL)
785 libpath = command_line.rpath;
786 else if (search_head == NULL)
787 libpath = (char *) "";
791 search_dirs_type *search;
793 /* PR ld/4023: Strip sysroot prefix from any paths
794 being inserted into the output binary's DT_RPATH. */
795 if (ld_sysroot != NULL
796 && * ld_sysroot != 0)
798 const char * name = search_head->name;
799 size_t ld_sysroot_len = strlen (ld_sysroot);
801 if (strncmp (name, ld_sysroot, ld_sysroot_len) == 0)
802 name += ld_sysroot_len;
805 libpath = xmalloc (len + 1);
806 strcpy (libpath, name);
808 for (search = search_head->next; search != NULL; search = search->next)
813 if (strncmp (name, ld_sysroot, ld_sysroot_len) == 0)
814 name += ld_sysroot_len;
816 nlen = strlen (name);
817 libpath = xrealloc (libpath, len + nlen + 2);
819 strcpy (libpath + len + 1, name);
825 len = strlen (search_head->name);
826 libpath = xmalloc (len + 1);
827 strcpy (libpath, search_head->name);
829 for (search = search_head->next; search != NULL; search = search->next)
833 nlen = strlen (search->name);
834 libpath = xrealloc (libpath, len + nlen + 2);
836 strcpy (libpath + len + 1, search->name);
842 /* Default to -bexpfull for SVR4-like semantics. */
843 flags = (unix_ld ? XCOFF_EXPFULL : 0);
844 flags &= ~explicit_auto_export_flags;
845 flags |= auto_export_flags;
847 /* Let the XCOFF backend set up the .loader section. */
848 if (!bfd_xcoff_size_dynamic_sections
849 (link_info.output_bfd, &link_info, libpath, entry_symbol.name,
850 file_align, maxstack, maxdata, gc && !unix_ld,
851 modtype, textro, flags, special_sections, rtld))
852 einfo (_("%F%P: failed to set dynamic section sizes: %E\n"));
854 /* Look through the special sections, and put them in the right
855 place in the link ordering. This is especially magic. */
856 for (i = 0; i < XCOFF_NUMBER_OF_SPECIAL_SECTIONS; i++)
859 lang_output_section_statement_type *os;
860 lang_statement_union_type **pls;
861 lang_input_section_type *is;
865 sec = special_sections[i];
869 /* Remove this section from the list of the output section.
870 This assumes we know what the script looks like. */
872 os = lang_output_section_get (sec->output_section);
874 einfo (_("%F%P: can't find output section %s\n"),
875 sec->output_section->name);
877 for (pls = &os->children.head; *pls != NULL; pls = &(*pls)->header.next)
879 if ((*pls)->header.type == lang_input_section_enum
880 && (*pls)->input_section.section == sec)
882 is = (lang_input_section_type *) * pls;
883 *pls = (*pls)->header.next;
887 if ((*pls)->header.type == lang_wild_statement_enum)
889 lang_statement_union_type **pwls;
891 for (pwls = &(*pls)->wild_statement.children.head;
892 *pwls != NULL; pwls = &(*pwls)->header.next)
895 if ((*pwls)->header.type == lang_input_section_enum
896 && (*pwls)->input_section.section == sec)
898 is = (lang_input_section_type *) * pwls;
899 *pwls = (*pwls)->header.next;
911 einfo (_("%F%P: can't find %s in output section\n"),
912 bfd_section_name (sec));
915 /* Now figure out where the section should go. */
919 default: /* to avoid warnings */
920 case XCOFF_SPECIAL_SECTION_TEXT:
926 case XCOFF_SPECIAL_SECTION_ETEXT:
932 case XCOFF_SPECIAL_SECTION_DATA:
938 case XCOFF_SPECIAL_SECTION_EDATA:
944 case XCOFF_SPECIAL_SECTION_END:
945 case XCOFF_SPECIAL_SECTION_END2:
952 os = lang_output_section_find (oname);
956 is->header.next = os->children.head;
957 os->children.head = (lang_statement_union_type *) is;
961 is->header.next = NULL;
962 *os->children.tail = (lang_statement_union_type *) is;
963 os->children.tail = &is->header.next;
967 /* Executables and shared objects must always have .text, .data
968 and .bss output sections, so that the header can refer to them.
969 The kernel refuses to load objects that have missing sections. */
970 if (!bfd_link_relocatable (&link_info))
971 for (i = 0; i < ARRAY_SIZE (must_keep_sections); i++)
975 sec = bfd_get_section_by_name (link_info.output_bfd,
976 must_keep_sections[i]);
978 einfo (_("%P: can't find required output section %s\n"),
979 must_keep_sections[i]);
981 sec->flags |= SEC_KEEP;
984 /* Make sure .tdata is removed if empty, even with -r flag.
985 .tdata is always being generated because its size is needed
986 to cumpute .data address. */
987 if (bfd_link_relocatable (&link_info))
989 static const char *const thread_sections[] = {
994 /* Run lang_size_sections (if not already done). */
995 if (expld.phase != lang_mark_phase_enum)
997 expld.phase = lang_mark_phase_enum;
998 expld.dataseg.phase = exp_seg_none;
999 one_lang_size_sections_pass (NULL, false);
1000 lang_reset_memory_regions ();
1003 for (i = 0; i < ARRAY_SIZE (thread_sections); i++)
1007 sec = bfd_get_section_by_name (link_info.output_bfd,
1008 thread_sections[i]);
1010 if (sec != NULL && sec->rawsize == 0
1011 && (sec->flags & SEC_KEEP) == 0
1012 && !bfd_section_removed_from_list (link_info.output_bfd,
1015 sec->flags |= SEC_EXCLUDE;
1016 bfd_section_list_remove (link_info.output_bfd, sec);
1017 link_info.output_bfd->section_count--;
1022 before_allocation_default ();
1025 struct hook_stub_info
1027 lang_statement_list_type add;
1028 asection *input_section;
1031 /* Traverse the linker tree to find the spot where the stub goes. */
1034 hook_in_stub (struct hook_stub_info *info, lang_statement_union_type **lp)
1036 lang_statement_union_type *l;
1039 for (; (l = *lp) != NULL; lp = &l->header.next)
1041 switch (l->header.type)
1043 case lang_constructors_statement_enum:
1044 ret = hook_in_stub (info, &constructor_list.head);
1049 case lang_output_section_statement_enum:
1050 ret = hook_in_stub (info,
1051 &l->output_section_statement.children.head);
1056 case lang_wild_statement_enum:
1057 ret = hook_in_stub (info, &l->wild_statement.children.head);
1062 case lang_group_statement_enum:
1063 ret = hook_in_stub (info, &l->group_statement.children.head);
1068 case lang_input_section_enum:
1069 if (l->input_section.section == info->input_section)
1071 /* We've found our section. Insert the stub immediately
1072 after its associated input section. */
1073 *(info->add.tail) = l->header.next;
1074 l->header.next = info->add.head;
1079 case lang_data_statement_enum:
1080 case lang_reloc_statement_enum:
1081 case lang_object_symbols_statement_enum:
1082 case lang_output_statement_enum:
1083 case lang_target_statement_enum:
1084 case lang_input_statement_enum:
1085 case lang_assignment_statement_enum:
1086 case lang_padding_statement_enum:
1087 case lang_address_statement_enum:
1088 case lang_fill_statement_enum:
1099 /* Call-back for bfd_xcoff_link_relocations.
1100 Create a new stub section, and arrange for it to be linked
1101 immediately before INPUT_SECTION. */
1104 xcoff_add_stub_section (const char *stub_sec_name, asection *input_section)
1108 asection *output_section;
1109 lang_output_section_statement_type *os;
1110 struct hook_stub_info info;
1112 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE
1113 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_KEEP);
1114 stub_sec = bfd_make_section_anyway_with_flags (stub_file->the_bfd,
1115 stub_sec_name, flags);
1116 if (stub_sec == NULL)
1119 output_section = input_section->output_section;
1120 os = lang_output_section_get (output_section);
1122 info.input_section = input_section;
1123 lang_list_init (&info.add);
1124 lang_add_section (&info.add, stub_sec, NULL, NULL, os);
1126 if (info.add.head == NULL)
1129 if (hook_in_stub (&info, &os->children.head))
1133 einfo (_("%X%P: can not make stub section: %E\n"));
1137 /* Another call-back for bfd_xcoff_link_relocations. */
1140 xcoff_layout_sections_again (void)
1142 /* If we have changed sizes of the stub sections, then we need
1143 to recalculate all the section offsets. This may mean we need to
1144 add even more stubs. */
1145 lang_relax_sections (true);
1148 /* Call the back-end to verify relocations. */
1151 gld${EMULATION_NAME}_after_allocation (void)
1154 /* If generating a relocatable output file, then we don't have any
1156 if (stub_file != NULL && !bfd_link_relocatable (&link_info))
1158 /* Call into the BFD backend to do the real work. */
1159 if (!bfd_xcoff_size_stubs (&link_info))
1160 einfo (_("%X%P: can not size stub sections: %E\n"));
1163 /* Now that everything is in place, finalize the dynamic sections. */
1164 if (!bfd_xcoff_build_dynamic_sections (link_info.output_bfd, &link_info))
1165 einfo (_("%F%P: failed to layout dynamic sections: %E\n"));
1167 if (!bfd_link_relocatable (&link_info))
1169 /* Now build the linker stubs. */
1170 if (stub_file != NULL && stub_file->the_bfd->sections != NULL)
1172 if (! bfd_xcoff_build_stubs (&link_info))
1173 einfo (_("%X%P: can not build stubs: %E\n"));
1179 gld${EMULATION_NAME}_choose_target (int argc, char **argv)
1182 static char *from_outside;
1183 static char *from_inside;
1184 static char *argv_to_target[][2] = {
1185 {NULL, "${OUTPUT_FORMAT}"},
1186 {"-b32", "${OUTPUT_FORMAT_32BIT}"},
1187 {"-b64", "${OUTPUT_FORMAT_64BIT}"},
1192 from_outside = getenv (TARGET_ENVIRON);
1193 if (from_outside != (char *) NULL)
1194 return from_outside;
1196 /* Set to default. */
1197 from_inside = argv_to_target[0][1];
1198 for (i = 1; i < argc; i++)
1200 for (j = 1; j < jmax; j++)
1202 if (0 == strcmp (argv[i], argv_to_target[j][0]))
1203 from_inside = argv_to_target[j][1];
1214 change_symbol_mode (char *input)
1216 char *symbol_mode_string[] = {
1219 "# no32", /* 0x04 */
1220 "# no64", /* 0x08 */
1227 for (bit = 0;; bit++)
1229 string = symbol_mode_string[bit];
1233 if (0 == strcmp (input, string))
1235 symbol_mode = (1 << bit);
1239 /* should not be here */
1246 -1 : error, try something else */
1248 is_syscall (char *input, unsigned int *flag)
1254 char *syscall_string;
1257 { "svc" /* 0x01 */, XCOFF_SYSCALL32 },
1258 { "svc32" /* 0x02 */, XCOFF_SYSCALL32 },
1259 { "svc3264" /* 0x04 */, XCOFF_SYSCALL32 | XCOFF_SYSCALL64 },
1260 { "svc64" /* 0x08 */, XCOFF_SYSCALL64 },
1261 { "syscall" /* 0x10 */, XCOFF_SYSCALL32 },
1262 { "syscall32" /* 0x20 */, XCOFF_SYSCALL32 },
1263 { "syscall3264" /* 0x40 */, XCOFF_SYSCALL32 | XCOFF_SYSCALL64 },
1264 { "syscall64" /* 0x80 */, XCOFF_SYSCALL64 },
1270 for (bit = 0;; bit++)
1272 string = s[bit].syscall_string;
1276 if (0 == strcmp (input, string))
1278 if (1 << bit & syscall_mask)
1280 *flag = s[bit].flag;
1289 /* should not be here */
1293 /* Read an import or export file. For an import file, this is called
1294 by the before_allocation emulation routine. For an export file,
1295 this is called by the handle_option emulation routine. */
1298 gld${EMULATION_NAME}_read_file (const char *filename, bool import)
1305 const char *imppath;
1306 const char *impfile;
1307 const char *impmember;
1309 o = (struct obstack *) xmalloc (sizeof (struct obstack));
1310 obstack_specify_allocation (o, 0, 0, xmalloc, gld${EMULATION_NAME}_free);
1312 f = fopen (filename, FOPEN_RT);
1315 bfd_set_error (bfd_error_system_call);
1316 einfo ("%F%P: %s: %E\n", filename);
1328 /* Default to 32 and 64 bit mode
1329 symbols at top of /lib/syscalls.exp do not have a mode modifier and they
1330 are not repeated, assume 64 bit routines also want to use them.
1331 See the routine change_symbol_mode for more information. */
1335 while ((c = getc (f)) != EOF)
1339 unsigned int syscall_flag = 0;
1341 struct bfd_link_hash_entry *h;
1345 obstack_1grow (o, c);
1349 obstack_1grow (o, '\0');
1352 s = (char *) obstack_base (o);
1353 while (ISSPACE (*s))
1357 || change_symbol_mode (s)
1358 || (*s == '#' && s[1] == ' ')
1359 || (!import && *s == '#' && s[1] == '!'))
1361 obstack_free (o, obstack_base (o));
1365 if (*s == '#' && s[1] == '!')
1368 while (ISSPACE (*s))
1375 obstack_free (o, obstack_base (o));
1378 einfo (_("%F%P:%s:%d: #! ([member]) is not supported "
1379 "in import files\n"),
1386 (void) obstack_finish (o);
1389 while (!ISSPACE (*s) && *s != '(' && *s != '\0')
1393 if (!bfd_xcoff_split_import_path (link_info.output_bfd,
1394 start, &imppath, &impfile))
1395 einfo (_("%F%P: could not parse import path: %E\n"));
1396 while (ISSPACE (cs))
1405 einfo (_("%P:%s:%d: warning: syntax error in import file\n"),
1412 while (*s != ')' && *s != '\0')
1417 einfo (_("%P:%s:%d: warning: syntax error in import file\n"),
1425 if (symbol_mode & symbol_mode_mask)
1427 /* This is a symbol to be imported or exported. */
1430 address = (bfd_vma) -1;
1432 while (!ISSPACE (*s) && *s != '\0')
1440 while (ISSPACE (*s))
1444 while (!ISSPACE (*se) && *se != '\0')
1449 while (ISSPACE (*se))
1452 einfo (_("%P:%s%d: warning: syntax error in "
1453 "import/export file\n"),
1462 status = is_syscall (s, &syscall_flag);
1466 /* not a system call, check for address */
1467 address = bfd_scan_vma (s, &end, 0);
1470 einfo (_("%P:%s:%d: warning: syntax error in "
1471 "import/export file\n"),
1481 struct export_symbol_list *n;
1483 ldlang_add_undef (symname, true);
1484 n = ((struct export_symbol_list *)
1485 xmalloc (sizeof (struct export_symbol_list)));
1486 n->next = export_symbols;
1487 n->name = xstrdup (symname);
1492 h = bfd_link_hash_lookup (link_info.hash, symname, false, false,
1494 if (h == NULL || h->type == bfd_link_hash_new)
1496 /* We can just ignore attempts to import an unreferenced
1501 if (!bfd_xcoff_import_symbol (link_info.output_bfd,
1503 address, imppath, impfile,
1504 impmember, syscall_flag))
1505 einfo (_("%X%P:%s:%d: failed to import symbol %s: %E\n"),
1506 filename, lineno, symname);
1510 obstack_free (o, obstack_base (o));
1513 if (obstack_object_size (o) > 0)
1515 einfo (_("%P:%s:%d: warning: ignoring unterminated last line\n"),
1517 obstack_free (o, obstack_base (o));
1522 obstack_free (o, NULL);
1529 /* This routine saves us from worrying about declaring free. */
1532 gld${EMULATION_NAME}_free (void *p)
1537 /* This is called by the before_allocation routine via
1538 lang_for_each_statement. It looks for relocations and assignments
1542 gld${EMULATION_NAME}_find_relocs (lang_statement_union_type *s)
1544 if (s->header.type == lang_reloc_statement_enum)
1546 lang_reloc_statement_type *rs;
1548 rs = &s->reloc_statement;
1549 if (rs->name == NULL)
1550 einfo (_("%F%P: only relocations against symbols are permitted\n"));
1551 if (!bfd_xcoff_link_count_reloc (link_info.output_bfd, &link_info,
1553 einfo (_("%F%P: bfd_xcoff_link_count_reloc failed: %E\n"));
1556 if (s->header.type == lang_assignment_statement_enum)
1557 gld${EMULATION_NAME}_find_exp_assignment (s->assignment_statement.exp);
1560 /* Look through an expression for an assignment statement. */
1563 gld${EMULATION_NAME}_find_exp_assignment (etree_type *exp)
1565 struct bfd_link_hash_entry *h;
1567 switch (exp->type.node_class)
1570 case etree_provided:
1571 h = bfd_link_hash_lookup (link_info.hash, exp->assign.dst,
1572 false, false, false);
1577 if (strcmp (exp->assign.dst, ".") != 0)
1579 if (!bfd_xcoff_record_link_assignment (link_info.output_bfd,
1582 einfo (_("%F%P: failed to record assignment to %s: %E\n"),
1585 gld${EMULATION_NAME}_find_exp_assignment (exp->assign.src);
1589 gld${EMULATION_NAME}_find_exp_assignment (exp->binary.lhs);
1590 gld${EMULATION_NAME}_find_exp_assignment (exp->binary.rhs);
1594 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.cond);
1595 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.lhs);
1596 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.rhs);
1600 gld${EMULATION_NAME}_find_exp_assignment (exp->unary.child);
1609 gld${EMULATION_NAME}_get_script (int *isfile)
1612 if test x"$COMPILE_IN" = xyes
1614 # Scripts compiled in.
1616 # sed commands to quote an ld script as a C string.
1617 sc="-f ${srcdir}/emultempl/ostring.sed"
1623 if (bfd_link_relocatable (&link_info) && config.build_constructors)
1626 sed $sc ldscripts/${EMULATION_NAME}.xu >> e${EMULATION_NAME}.c
1627 echo ' ; else if (bfd_link_relocatable (&link_info)) return' >> e${EMULATION_NAME}.c
1628 sed $sc ldscripts/${EMULATION_NAME}.xr >> e${EMULATION_NAME}.c
1629 echo ' ; else if (!config.text_read_only) return' >> e${EMULATION_NAME}.c
1630 sed $sc ldscripts/${EMULATION_NAME}.xbn >> e${EMULATION_NAME}.c
1631 echo ' ; else if (!config.magic_demand_paged) return' >> e${EMULATION_NAME}.c
1632 sed $sc ldscripts/${EMULATION_NAME}.xn >> e${EMULATION_NAME}.c
1633 echo ' ; else return' >> e${EMULATION_NAME}.c
1634 sed $sc ldscripts/${EMULATION_NAME}.x >> e${EMULATION_NAME}.c
1635 echo '; }' >> e${EMULATION_NAME}.c
1638 # Scripts read from the filesystem.
1644 if (bfd_link_relocatable (&link_info) && config.build_constructors)
1645 return "ldscripts/${EMULATION_NAME}.xu";
1646 else if (bfd_link_relocatable (&link_info))
1647 return "ldscripts/${EMULATION_NAME}.xr";
1648 else if (!config.text_read_only)
1649 return "ldscripts/${EMULATION_NAME}.xbn";
1650 else if (!config.magic_demand_paged)
1651 return "ldscripts/${EMULATION_NAME}.xn";
1653 return "ldscripts/${EMULATION_NAME}.x";
1662 gld${EMULATION_NAME}_create_output_section_statements (void)
1664 if ((bfd_get_flavour (link_info.output_bfd) != bfd_target_xcoff_flavour))
1668 stub_file = lang_add_input_file ("linker stubs",
1669 lang_input_file_is_fake_enum,
1671 stub_file->the_bfd = bfd_create ("linker stubs", link_info.output_bfd);
1672 if (stub_file->the_bfd == NULL
1673 || !bfd_set_arch_mach (stub_file->the_bfd,
1674 bfd_get_arch (link_info.output_bfd),
1675 bfd_get_mach (link_info.output_bfd)))
1677 einfo (_("%F%P: can not create stub BFD: %E\n"));
1681 stub_file->the_bfd->flags |= BFD_LINKER_CREATED;
1682 ldlang_add_file (stub_file);
1683 params.stub_bfd = stub_file->the_bfd;
1685 /* Pass linker params to the back-end. */
1686 if (!bfd_xcoff_link_init (&link_info, ¶ms))
1687 einfo (_("%F%P: can not init BFD: %E\n"));
1690 if (link_info.init_function != NULL
1691 || link_info.fini_function != NULL
1694 initfini_file = lang_add_input_file ("initfini",
1695 lang_input_file_is_file_enum,
1698 initfini_file->the_bfd = bfd_create ("initfini", link_info.output_bfd);
1699 if (initfini_file->the_bfd == NULL
1700 || ! bfd_set_arch_mach (initfini_file->the_bfd,
1701 bfd_get_arch (link_info.output_bfd),
1702 bfd_get_mach (link_info.output_bfd)))
1704 einfo (_("%F%P: can not create BFD: %E\n"));
1708 /* Call backend to fill in the rest */
1709 if (! bfd_xcoff_link_generate_rtinit (initfini_file->the_bfd,
1710 link_info.init_function,
1711 link_info.fini_function,
1714 einfo (_("%F%P: can not create BFD: %E\n"));
1718 /* __rtld defined in /lib/librtl.a */
1720 lang_add_input_file ("rtl", lang_input_file_is_l_enum, NULL);
1725 gld${EMULATION_NAME}_set_output_arch (void)
1727 bfd_set_arch_mach (link_info.output_bfd,
1728 bfd_xcoff_architecture (link_info.output_bfd),
1729 bfd_xcoff_machine (link_info.output_bfd));
1731 ldfile_output_architecture = bfd_get_arch (link_info.output_bfd);
1732 ldfile_output_machine = bfd_get_mach (link_info.output_bfd);
1733 ldfile_output_machine_name = bfd_printable_name (link_info.output_bfd);
1737 gld${EMULATION_NAME}_open_dynamic_archive (const char *arch,
1738 search_dirs_type *search,
1739 lang_input_statement_type *entry)
1743 if (!entry->flags.maybe_archive)
1746 if (entry->flags.full_name_provided)
1747 path = concat (search->name, "/", entry->filename,
1748 (const char *) NULL);
1750 path = concat (search->name, "/lib", entry->filename, arch, ".a",
1751 (const char *) NULL);
1753 if (!ldfile_try_open_bfd (path, entry))
1758 /* Don't include the searched directory in the import path. */
1759 bfd_xcoff_set_archive_import_path (&link_info, entry->the_bfd,
1760 path + strlen (search->name) + 1);
1761 entry->filename = path;
1766 gld${EMULATION_NAME}_print_symbol (struct bfd_link_hash_entry *hash_entry,
1769 asection *sec = (asection *) ptr;
1771 if ((hash_entry->type == bfd_link_hash_defined
1772 || hash_entry->type == bfd_link_hash_defweak)
1773 && sec == hash_entry->u.def.section)
1775 struct xcoff_link_hash_entry *h;
1777 print_spaces (SECTION_NAME_MAP_LENGTH);
1779 (hash_entry->u.def.value
1780 + hash_entry->u.def.section->output_offset
1781 + hash_entry->u.def.section->output_section->vma));
1783 /* Flag symbol if it has been garbage collected. */
1784 h = (struct xcoff_link_hash_entry *) hash_entry;
1785 if ((h != NULL) && !(h->flags & XCOFF_MARK))
1787 minfo (" %pT\n", hash_entry->root.string);
1794 LDEMUL_AFTER_OPEN=gld${EMULATION_NAME}_after_open
1795 LDEMUL_SET_OUTPUT_ARCH=gld${EMULATION_NAME}_set_output_arch
1796 LDEMUL_CHOOSE_TARGET=gld${EMULATION_NAME}_choose_target
1797 LDEMUL_BEFORE_ALLOCATION=gld${EMULATION_NAME}_before_allocation
1798 LDEMUL_AFTER_ALLOCATION=gld${EMULATION_NAME}_after_allocation
1799 LDEMUL_CREATE_OUTPUT_SECTION_STATEMENTS=gld${EMULATION_NAME}_create_output_section_statements
1800 LDEMUL_OPEN_DYNAMIC_ARCHIVE=gld${EMULATION_NAME}_open_dynamic_archive
1801 LDEMUL_PARSE_ARGS=gld${EMULATION_NAME}_parse_args
1802 LDEMUL_ADD_OPTIONS=gld${EMULATION_NAME}_add_options
1803 LDEMUL_HANDLE_OPTION=gld${EMULATION_NAME}_handle_option
1804 LDEMUL_UNRECOGNIZED_FILE=gld${EMULATION_NAME}_unrecognized_file
1805 LDEMUL_PRINT_SYMBOL=gld${EMULATION_NAME}_print_symbol
1807 source_em ${srcdir}/emultempl/emulation.em