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-2024 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"
56 #include "coff/internal.h"
57 #include "coff/xcoff.h"
60 #include "xcofflink.h"
62 static void gld${EMULATION_NAME}_read_file (const char *, bool);
63 static void gld${EMULATION_NAME}_free (void *);
64 static void gld${EMULATION_NAME}_find_relocs (lang_statement_union_type *);
65 static void gld${EMULATION_NAME}_find_exp_assignment (etree_type *);
67 static asection *xcoff_add_stub_section (const char *, asection *);
68 static void xcoff_layout_sections_again (void);
70 static struct bfd_xcoff_link_params params = {
72 &xcoff_add_stub_section,
73 &xcoff_layout_sections_again
77 /* The file alignment required for each section. */
78 static unsigned long file_align;
80 /* The maximum size the stack is permitted to grow. This is stored in
82 static unsigned long maxstack;
84 /* The maximum data size. This is stored in the a.out header. */
85 static unsigned long maxdata;
87 /* Whether to perform garbage collection. */
90 /* The module type to use. */
91 static unsigned short modtype = ('1' << 8) | 'L';
93 /* Whether the .text section must be read-only (i.e., no relocs
97 /* A mask of XCOFF_EXPALL and XCOFF_EXPFULL flags, as set by their
98 associated -b and -bno options. */
99 static unsigned int auto_export_flags;
101 /* A mask of auto_export_flags bits that were explicitly set on the
103 static unsigned int explicit_auto_export_flags;
105 /* Whether to implement Unix like linker semantics. */
108 /* Structure used to hold import file list. */
112 struct filelist *next;
116 /* List of import files. */
117 static struct filelist *import_files;
119 /* List of export symbols read from the export files. */
121 struct export_symbol_list
123 struct export_symbol_list *next;
127 static struct export_symbol_list *export_symbols;
129 /* Maintains the 32 or 64 bit mode state of import file */
130 static unsigned int symbol_mode = 0x04;
132 /* Which symbol modes are valid */
133 static unsigned int symbol_mode_mask = 0x0d;
135 /* Whether this is a 64 bit link */
136 static int is_64bit = 0;
138 /* Which syscalls from import file are valid */
139 static unsigned int syscall_mask = 0x77;
141 /* fake file for -binitfini support */
142 static lang_input_statement_type *initfini_file;
144 /* Whether to do run time linking
145 -brtl enables, -bnortl and -bnortllib disable. */
148 /* Explicit command line library path, -blibpath */
149 static char *command_line_blibpath = NULL;
151 /* Fake input file for stubs. */
152 static lang_input_statement_type *stub_file;
154 /* This routine is called before anything else is done. */
157 gld${EMULATION_NAME}_before_parse (void)
159 ldfile_set_output_arch ("${OUTPUT_ARCH}", bfd_arch_`echo ${ARCH} | sed -e 's/:.*//'`);
161 input_flags.dynamic = true;
162 config.has_shared = true;
164 /* The link_info.[init|fini]_functions are initialized in ld/lexsup.c.
165 Override them here so we can use the link_info.init_function as a
166 state flag that lets the backend know that -binitfini has been done. */
168 link_info.init_function = NULL;
169 link_info.fini_function = NULL;
173 /* Handle AIX specific options. */
176 gld${EMULATION_NAME}_add_options
177 (int ns, char **shortopts, int nl, struct option **longopts,
178 int nrl ATTRIBUTE_UNUSED, struct option **really_longopts ATTRIBUTE_UNUSED)
180 static const char xtra_short[] = "D:H:KT:z";
181 static const struct option xtra_long[] = {
182 /* -binitfini has special handling in the linker backend. The native linker
183 uses the arguemnts to generate a table of init and fini functions for
184 the executable. The important use for this option is to support aix 4.2+
185 c++ constructors and destructors. This is tied into gcc via collect2.c.
187 The function table is accessed by the runtime linker/loader by checking if
188 the first symbol in the loader symbol table is __rtinit. The gnu linker
189 generates this symbol and makes it the first loader symbol. */
191 {"basis", no_argument, NULL, OPTION_IGNORE},
192 {"bautoimp", no_argument, NULL, OPTION_AUTOIMP},
193 {"bcomprld", no_argument, NULL, OPTION_IGNORE},
194 {"bcrld", no_argument, NULL, OPTION_IGNORE},
195 {"bcror31", no_argument, NULL, OPTION_IGNORE},
196 {"bD", required_argument, NULL, OPTION_MAXDATA},
197 {"bE", required_argument, NULL, OPTION_EXPORT},
198 {"bernotok", no_argument, NULL, OPTION_ERNOTOK},
199 {"berok", no_argument, NULL, OPTION_EROK},
200 {"berrmsg", no_argument, NULL, OPTION_IGNORE},
201 {"bexpall", no_argument, NULL, OPTION_EXPALL},
202 {"bexpfull", no_argument, NULL, OPTION_EXPFULL},
203 {"bexport", required_argument, NULL, OPTION_EXPORT},
204 {"bbigtoc", no_argument, NULL, OPTION_IGNORE},
205 {"bf", no_argument, NULL, OPTION_ERNOTOK},
206 {"bgc", no_argument, &gc, 1},
207 {"bh", required_argument, NULL, OPTION_IGNORE},
208 {"bhalt", required_argument, NULL, OPTION_IGNORE},
209 {"bI", required_argument, NULL, OPTION_IMPORT},
210 {"bimport", required_argument, NULL, OPTION_IMPORT},
211 {"binitfini", required_argument, NULL, OPTION_INITFINI},
212 {"bl", required_argument, NULL, OPTION_LOADMAP},
213 {"bloadmap", required_argument, NULL, OPTION_LOADMAP},
214 {"bmaxdata", required_argument, NULL, OPTION_MAXDATA},
215 {"bmaxstack", required_argument, NULL, OPTION_MAXSTACK},
216 {"bM", required_argument, NULL, OPTION_MODTYPE},
217 {"bmodtype", required_argument, NULL, OPTION_MODTYPE},
218 {"bnoautoimp", no_argument, NULL, OPTION_NOAUTOIMP},
219 {"bnoexpall", no_argument, NULL, OPTION_NOEXPALL},
220 {"bnoexpfull", no_argument, NULL, OPTION_NOEXPFULL},
221 {"bnodelcsect", no_argument, NULL, OPTION_IGNORE},
222 {"bnoentry", no_argument, NULL, OPTION_IGNORE},
223 {"bnogc", no_argument, &gc, 0},
224 {"bnso", no_argument, NULL, OPTION_NOAUTOIMP},
225 {"bnostrcmpct", no_argument, NULL, OPTION_NOSTRCMPCT},
226 {"bnotextro", no_argument, &textro, 0},
227 {"bnro", no_argument, &textro, 0},
228 {"bpD", required_argument, NULL, OPTION_PD},
229 {"bpT", required_argument, NULL, OPTION_PT},
230 {"bro", no_argument, &textro, 1},
231 {"brtl", no_argument, &rtld, 1},
232 {"bnortl", no_argument, &rtld, 0},
233 {"bnortllib", no_argument, &rtld, 0},
234 {"bS", required_argument, NULL, OPTION_MAXSTACK},
235 {"bso", no_argument, NULL, OPTION_AUTOIMP},
236 {"bstrcmpct", no_argument, NULL, OPTION_STRCMPCT},
237 {"btextro", no_argument, &textro, 1},
238 {"b32", no_argument, NULL, OPTION_32},
239 {"b64", no_argument, NULL, OPTION_64},
240 {"static", no_argument, NULL, OPTION_NOAUTOIMP},
241 {"unix", no_argument, NULL, OPTION_UNIX},
242 {"blibpath", required_argument, NULL, OPTION_LIBPATH},
243 {"bnolibpath", required_argument, NULL, OPTION_NOLIBPATH},
244 {NULL, no_argument, NULL, 0}
247 /* Options supported by the AIX linker which we do not support:
248 -S, -v, -Z, -bbindcmds, -bbinder, -bbindopts, -bcalls, -bcaps,
249 -bcror15, -bdebugopt, -bdbg, -bdelcsect, -bex?, -bfilelist, -bfl,
250 -bgcbypass, -bglink, -binsert, -bi, -bloadmap, -bl, -bmap, -bnl,
251 -bnobind, -bnocomprld, -bnocrld, -bnoerrmsg, -bnoglink,
252 -bnoloadmap, -bnl, -bnoobjreorder, -bnoquiet, -bnoreorder,
253 -bnotypchk, -bnox, -bquiet, -bR, -brename, -breorder, -btypchk,
256 *shortopts = (char *) xrealloc (*shortopts, ns + sizeof (xtra_short));
257 memcpy (*shortopts + ns, &xtra_short, sizeof (xtra_short));
258 *longopts = xrealloc (*longopts,
259 nl * sizeof (struct option) + sizeof (xtra_long));
260 memcpy (*longopts + nl, &xtra_long, sizeof (xtra_long));
264 gld${EMULATION_NAME}_parse_args (int argc, char **argv)
268 /* If the current option starts with -b, change the first : to an =.
269 The AIX linker uses : to separate the option from the argument;
270 changing it to = lets us treat it as a getopt option. */
275 if (indx < argc && startswith (argv[indx], "-b"))
279 for (s = argv[indx]; *s != '\0'; s++)
291 /* Helper for option '-f', which specify a list of input files.
292 Contrary to the native linker, we don't support shell patterns
293 (simply because glob isn't always available). */
296 read_file_list (const char *filename)
299 /* An upper bound on the number of characters in the file. */
301 /* File in memory. */
307 f = fopen (filename, FOPEN_RT);
310 einfo (_("%F%P: cannot open %s\n"), filename);
313 if (fseek (f, 0L, SEEK_END) == -1)
318 if (fseek (f, 0L, SEEK_SET) == -1)
321 buffer = (char *) xmalloc (pos + 1);
322 len = fread (buffer, sizeof (char), pos, f);
323 if (len != (size_t) pos && ferror (f))
325 /* Add a NUL terminator. */
333 /* Skip empty lines. */
334 while (*b == '\n' || *b == '\r')
337 /* Stop if end of buffer. */
338 if (b == buffer + len)
341 /* Eat any byte until end of line. */
342 for (e = b; *e != '\0'; e++)
343 if (*e == '\n' || *e == '\r')
346 /* Replace end of line by nul. */
351 lang_add_input_file (b, lang_input_file_is_search_file_enum, NULL);
357 einfo (_("%F%P: cannot read %s\n"), optarg);
362 gld${EMULATION_NAME}_handle_option (int optc)
373 /* Long option which just sets a flag. */
377 /* This overrides --auxiliary. This option specifies a file containing
378 a list of input files. */
379 read_file_list (optarg);
383 val = bfd_scan_vma (optarg, &end, 0);
385 einfo (_("%P: warning: ignoring invalid -D number %s\n"), optarg);
387 lang_section_start (".data", exp_intop (val), NULL);
391 val = bfd_scan_vma (optarg, &end, 0);
392 if (*end != '\0' || (val & (val - 1)) != 0)
393 einfo (_("%P: warning: ignoring invalid -H number %s\n"), optarg);
400 /* FIXME: This should use the page size for the target system. */
405 /* On AIX this is the same as GNU ld -Ttext. When we see -T
406 number, we assume the AIX option is intended. Otherwise, we
407 assume the usual GNU ld -T option is intended. We can't just
408 ignore the AIX option, because gcc passes it to the linker. */
409 val = bfd_scan_vma (optarg, &end, 0);
412 lang_section_start (".text", exp_intop (val), NULL);
418 case OPTION_INITFINI:
421 * The aix linker init fini has the format :
423 * -binitfini:[ Initial][:Termination][:Priority]
425 * it allows the Termination and Priority to be optional.
427 * Since we support only one init/fini pair, we ignore the Priority.
429 * Define the special symbol __rtinit.
431 * strtok does not correctly handle the case of -binitfini::fini: so
437 while (*t && ':' != *t)
443 link_info.init_function = i;
446 while (*t && ':' != *t)
451 link_info.fini_function = f;
456 link_info.static_link = false;
460 link_info.unresolved_syms_in_objects = RM_DIAGNOSE;
461 link_info.unresolved_syms_in_shared_libs = RM_DIAGNOSE;
465 link_info.unresolved_syms_in_objects = RM_IGNORE;
466 link_info.unresolved_syms_in_shared_libs = RM_IGNORE;
470 auto_export_flags |= XCOFF_EXPALL;
471 explicit_auto_export_flags |= XCOFF_EXPALL;
475 auto_export_flags |= XCOFF_EXPFULL;
476 explicit_auto_export_flags |= XCOFF_EXPFULL;
480 gld${EMULATION_NAME}_read_file (optarg, false);
486 struct filelist **flpp;
488 n = (struct filelist *) xmalloc (sizeof (struct filelist));
491 flpp = &import_files;
492 while (*flpp != NULL)
493 flpp = &(*flpp)->next;
499 config.map_filename = optarg;
503 val = bfd_scan_vma (optarg, &end, 0);
505 einfo (_("%P: warning: ignoring invalid -bmaxdata number %s\n"),
511 case OPTION_MAXSTACK:
512 val = bfd_scan_vma (optarg, &end, 0);
514 einfo (_("%P: warning: ignoring invalid -bmaxstack number %s\n"),
523 link_info.type = type_dll;
526 if (*optarg == '\0' || optarg[1] == '\0')
527 einfo (_("%P: warning: ignoring invalid module type %s\n"), optarg);
529 modtype = (*optarg << 8) | optarg[1];
532 case OPTION_NOAUTOIMP:
533 link_info.static_link = true;
536 case OPTION_NOEXPALL:
537 auto_export_flags &= ~XCOFF_EXPALL;
538 explicit_auto_export_flags |= XCOFF_EXPALL;
541 case OPTION_NOEXPFULL:
542 auto_export_flags &= ~XCOFF_EXPFULL;
543 explicit_auto_export_flags |= XCOFF_EXPFULL;
546 case OPTION_NOSTRCMPCT:
547 link_info.traditional_format = true;
551 /* This sets the page that the .data section is supposed to
552 start on. The offset within the page should still be the
553 offset within the file, so we need to build an appropriate
555 val = bfd_scan_vma (optarg, &end, 0);
557 einfo (_("%P: warning: ignoring invalid -pD number %s\n"), optarg);
565 exp_nameop (NAME, "."),
568 exp_binop ('+', t, exp_intop (31)),
569 exp_intop (~(bfd_vma) 31));
570 lang_section_start (".data", t, NULL);
575 /* This set the page that the .text section is supposed to start
576 on. The offset within the page should still be the offset
578 val = bfd_scan_vma (optarg, &end, 0);
580 einfo (_("%P: warning: ignoring invalid -pT number %s\n"), optarg);
587 exp_nameop (SIZEOF_HEADERS, NULL));
589 exp_binop ('+', t, exp_intop (31)),
590 exp_intop (~(bfd_vma) 31));
591 lang_section_start (".text", t, NULL);
595 case OPTION_STRCMPCT:
596 link_info.traditional_format = false;
606 symbol_mode_mask = 0x0d;
612 symbol_mode_mask = 0x0e;
616 command_line_blibpath = optarg;
619 case OPTION_NOLIBPATH:
620 command_line_blibpath = NULL;
628 /* This is called when an input file can not be recognized as a BFD
629 object or an archive. If the file starts with #!, we must treat it
630 as an import file. This is for AIX compatibility. */
633 gld${EMULATION_NAME}_unrecognized_file (lang_input_statement_type *entry)
638 e = fopen (entry->filename, FOPEN_RT);
644 if (getc (e) == '#' && getc (e) == '!')
647 struct filelist **flpp;
649 n = (struct filelist *) xmalloc (sizeof (struct filelist));
651 n->name = entry->filename;
652 flpp = &import_files;
653 while (*flpp != NULL)
654 flpp = &(*flpp)->next;
658 entry->flags.loaded = true;
666 /* This is called after the input files have been opened. */
669 gld${EMULATION_NAME}_after_open (void)
674 after_open_default ();
676 /* Call ldctor_build_sets, after pretending that this is a
677 relocatable link. We do this because AIX requires relocation
678 entries for all references to symbols, even in a final
679 executable. Of course, we only want to do this if we are
680 producing an XCOFF output file. */
682 if (strstr (bfd_get_target (link_info.output_bfd), "xcoff") != NULL)
683 link_info.type = type_relocatable;
684 ldctor_build_sets ();
687 /* For each set, record the size, so that the XCOFF backend can
688 output the correct csect length. */
689 for (p = sets; p != (struct set_info *) NULL; p = p->next)
693 /* If the symbol is defined, we may have been invoked from
694 collect, and the sets may already have been built, so we do
696 if (p->h->type == bfd_link_hash_defined
697 || p->h->type == bfd_link_hash_defweak)
700 if (p->reloc != BFD_RELOC_CTOR)
702 /* Handle this if we need to. */
706 size = (p->count + 2) * 4;
707 if (!bfd_xcoff_link_record_set (link_info.output_bfd, &link_info,
709 einfo (_("%F%P: bfd_xcoff_link_record_set failed: %E\n"));
713 /* This is called after the sections have been attached to output
714 sections, but before any sizes or addresses have been set. */
717 gld${EMULATION_NAME}_before_allocation (void)
720 struct export_symbol_list *el;
722 asection *special_sections[XCOFF_NUMBER_OF_SPECIAL_SECTIONS];
723 static const char *const must_keep_sections[] = {
728 unsigned int i, flags;
730 /* Handle the import and export files, if any. */
731 for (fl = import_files; fl != NULL; fl = fl->next)
732 gld${EMULATION_NAME}_read_file (fl->name, true);
733 for (el = export_symbols; el != NULL; el = el->next)
735 struct bfd_link_hash_entry *h;
737 h = bfd_link_hash_lookup (link_info.hash, el->name, false, false, false);
739 einfo (_("%F%P: bfd_link_hash_lookup of export symbol failed: %E\n"));
740 if (!bfd_xcoff_export_symbol (link_info.output_bfd, &link_info, h))
741 einfo (_("%F%P: bfd_xcoff_export_symbol failed: %E\n"));
744 /* Track down all relocations called for by the linker script (these
745 are typically constructor/destructor entries created by
746 CONSTRUCTORS) and let the backend know it will need to create
747 .loader relocs for them. */
748 lang_for_each_statement (gld${EMULATION_NAME}_find_relocs);
750 /* Precedence of LIBPATH
751 -blibpath: native support always first
752 -rpath: gnu extension
753 -L build from command line -L's */
754 if (command_line_blibpath != NULL)
755 libpath = command_line_blibpath;
756 else if (command_line.rpath != NULL)
757 libpath = command_line.rpath;
758 else if (search_head == NULL)
759 libpath = (char *) "";
763 search_dirs_type *search;
765 /* PR ld/4023: Strip sysroot prefix from any paths
766 being inserted into the output binary's DT_RPATH. */
767 if (ld_sysroot != NULL
768 && * ld_sysroot != 0)
770 const char * name = search_head->name;
771 size_t ld_sysroot_len = strlen (ld_sysroot);
773 if (strncmp (name, ld_sysroot, ld_sysroot_len) == 0)
774 name += ld_sysroot_len;
777 libpath = xmalloc (len + 1);
778 strcpy (libpath, name);
780 for (search = search_head->next; search != NULL; search = search->next)
785 if (strncmp (name, ld_sysroot, ld_sysroot_len) == 0)
786 name += ld_sysroot_len;
788 nlen = strlen (name);
789 libpath = xrealloc (libpath, len + nlen + 2);
791 strcpy (libpath + len + 1, name);
797 len = strlen (search_head->name);
798 libpath = xmalloc (len + 1);
799 strcpy (libpath, search_head->name);
801 for (search = search_head->next; search != NULL; search = search->next)
805 nlen = strlen (search->name);
806 libpath = xrealloc (libpath, len + nlen + 2);
808 strcpy (libpath + len + 1, search->name);
814 /* Default to -bexpfull for SVR4-like semantics. */
815 flags = (unix_ld ? XCOFF_EXPFULL : 0);
816 flags &= ~explicit_auto_export_flags;
817 flags |= auto_export_flags;
819 /* Let the XCOFF backend set up the .loader section. */
820 if (!bfd_xcoff_size_dynamic_sections
821 (link_info.output_bfd, &link_info, libpath, entry_symbol.name,
822 file_align, maxstack, maxdata, gc && !unix_ld,
823 modtype, textro, flags, special_sections, rtld))
824 einfo (_("%F%P: failed to set dynamic section sizes: %E\n"));
826 /* Look through the special sections, and put them in the right
827 place in the link ordering. This is especially magic. */
828 for (i = 0; i < XCOFF_NUMBER_OF_SPECIAL_SECTIONS; i++)
831 lang_output_section_statement_type *os;
832 lang_statement_union_type **pls;
833 lang_input_section_type *is;
837 sec = special_sections[i];
841 /* Remove this section from the list of the output section.
842 This assumes we know what the script looks like. */
844 os = lang_output_section_get (sec->output_section);
846 einfo (_("%F%P: can't find output section %s\n"),
847 sec->output_section->name);
849 for (pls = &os->children.head; *pls != NULL; pls = &(*pls)->header.next)
851 if ((*pls)->header.type == lang_input_section_enum
852 && (*pls)->input_section.section == sec)
854 is = (lang_input_section_type *) * pls;
855 *pls = (*pls)->header.next;
859 if ((*pls)->header.type == lang_wild_statement_enum)
861 lang_statement_union_type **pwls;
863 for (pwls = &(*pls)->wild_statement.children.head;
864 *pwls != NULL; pwls = &(*pwls)->header.next)
867 if ((*pwls)->header.type == lang_input_section_enum
868 && (*pwls)->input_section.section == sec)
870 is = (lang_input_section_type *) * pwls;
871 *pwls = (*pwls)->header.next;
883 einfo (_("%F%P: can't find %s in output section\n"),
884 bfd_section_name (sec));
887 /* Now figure out where the section should go. */
891 default: /* to avoid warnings */
892 case XCOFF_SPECIAL_SECTION_TEXT:
898 case XCOFF_SPECIAL_SECTION_ETEXT:
904 case XCOFF_SPECIAL_SECTION_DATA:
910 case XCOFF_SPECIAL_SECTION_EDATA:
916 case XCOFF_SPECIAL_SECTION_END:
917 case XCOFF_SPECIAL_SECTION_END2:
924 os = lang_output_section_find (oname);
928 is->header.next = os->children.head;
929 os->children.head = (lang_statement_union_type *) is;
933 is->header.next = NULL;
934 *os->children.tail = (lang_statement_union_type *) is;
935 os->children.tail = &is->header.next;
939 /* Executables and shared objects must always have .text, .data
940 and .bss output sections, so that the header can refer to them.
941 The kernel refuses to load objects that have missing sections. */
942 if (!bfd_link_relocatable (&link_info))
943 for (i = 0; i < ARRAY_SIZE (must_keep_sections); i++)
947 sec = bfd_get_section_by_name (link_info.output_bfd,
948 must_keep_sections[i]);
950 einfo (_("%P: can't find required output section %s\n"),
951 must_keep_sections[i]);
953 sec->flags |= SEC_KEEP;
956 /* Make sure .tdata is removed if empty, even with -r flag.
957 .tdata is always being generated because its size is needed
958 to cumpute .data address. */
959 if (bfd_link_relocatable (&link_info))
961 static const char *const thread_sections[] = {
966 /* Run lang_size_sections (if not already done). */
967 if (expld.phase != lang_mark_phase_enum)
969 expld.phase = lang_mark_phase_enum;
970 expld.dataseg.phase = exp_seg_none;
971 one_lang_size_sections_pass (NULL, false);
972 lang_reset_memory_regions ();
975 for (i = 0; i < ARRAY_SIZE (thread_sections); i++)
979 sec = bfd_get_section_by_name (link_info.output_bfd,
982 if (sec != NULL && sec->rawsize == 0
983 && (sec->flags & SEC_KEEP) == 0
984 && !bfd_section_removed_from_list (link_info.output_bfd,
987 sec->flags |= SEC_EXCLUDE;
988 bfd_section_list_remove (link_info.output_bfd, sec);
989 link_info.output_bfd->section_count--;
994 before_allocation_default ();
997 struct hook_stub_info
999 lang_statement_list_type add;
1000 asection *input_section;
1003 /* Traverse the linker tree to find the spot where the stub goes. */
1006 hook_in_stub (struct hook_stub_info *info, lang_statement_union_type **lp)
1008 lang_statement_union_type *l;
1011 for (; (l = *lp) != NULL; lp = &l->header.next)
1013 switch (l->header.type)
1015 case lang_constructors_statement_enum:
1016 ret = hook_in_stub (info, &constructor_list.head);
1021 case lang_output_section_statement_enum:
1022 ret = hook_in_stub (info,
1023 &l->output_section_statement.children.head);
1028 case lang_wild_statement_enum:
1029 ret = hook_in_stub (info, &l->wild_statement.children.head);
1034 case lang_group_statement_enum:
1035 ret = hook_in_stub (info, &l->group_statement.children.head);
1040 case lang_input_section_enum:
1041 if (l->input_section.section == info->input_section)
1043 /* We've found our section. Insert the stub immediately
1044 after its associated input section. */
1045 *(info->add.tail) = l->header.next;
1046 l->header.next = info->add.head;
1051 case lang_data_statement_enum:
1052 case lang_reloc_statement_enum:
1053 case lang_object_symbols_statement_enum:
1054 case lang_output_statement_enum:
1055 case lang_target_statement_enum:
1056 case lang_input_statement_enum:
1057 case lang_assignment_statement_enum:
1058 case lang_padding_statement_enum:
1059 case lang_address_statement_enum:
1060 case lang_fill_statement_enum:
1071 /* Call-back for bfd_xcoff_link_relocations.
1072 Create a new stub section, and arrange for it to be linked
1073 immediately before INPUT_SECTION. */
1076 xcoff_add_stub_section (const char *stub_sec_name, asection *input_section)
1080 asection *output_section;
1081 lang_output_section_statement_type *os;
1082 struct hook_stub_info info;
1084 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE
1085 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_KEEP);
1086 stub_sec = bfd_make_section_anyway_with_flags (stub_file->the_bfd,
1087 stub_sec_name, flags);
1088 if (stub_sec == NULL)
1091 output_section = input_section->output_section;
1092 os = lang_output_section_get (output_section);
1094 info.input_section = input_section;
1095 lang_list_init (&info.add);
1096 lang_add_section (&info.add, stub_sec, NULL, NULL, os);
1098 if (info.add.head == NULL)
1101 if (hook_in_stub (&info, &os->children.head))
1105 einfo (_("%X%P: can not make stub section: %E\n"));
1109 /* Another call-back for bfd_xcoff_link_relocations. */
1112 xcoff_layout_sections_again (void)
1114 /* If we have changed sizes of the stub sections, then we need
1115 to recalculate all the section offsets. This may mean we need to
1116 add even more stubs. */
1117 lang_relax_sections (true);
1120 /* Call the back-end to verify relocations. */
1123 gld${EMULATION_NAME}_after_allocation (void)
1126 /* If generating a relocatable output file, then we don't have any
1128 if (stub_file != NULL && !bfd_link_relocatable (&link_info))
1130 /* Call into the BFD backend to do the real work. */
1131 if (!bfd_xcoff_size_stubs (&link_info))
1132 einfo (_("%X%P: can not size stub sections: %E\n"));
1135 /* Now that everything is in place, finalize the dynamic sections. */
1136 if (!bfd_xcoff_build_dynamic_sections (link_info.output_bfd, &link_info))
1137 einfo (_("%F%P: failed to layout dynamic sections: %E\n"));
1139 if (!bfd_link_relocatable (&link_info))
1141 /* Now build the linker stubs. */
1142 if (stub_file != NULL && stub_file->the_bfd->sections != NULL)
1144 if (! bfd_xcoff_build_stubs (&link_info))
1145 einfo (_("%X%P: can not build stubs: %E\n"));
1151 gld${EMULATION_NAME}_choose_target (int argc, char **argv)
1154 static char *from_outside;
1155 static char *from_inside;
1156 static char *argv_to_target[][2] = {
1157 {NULL, "${OUTPUT_FORMAT}"},
1158 {"-b32", "${OUTPUT_FORMAT_32BIT}"},
1159 {"-b64", "${OUTPUT_FORMAT_64BIT}"},
1164 from_outside = getenv (TARGET_ENVIRON);
1165 if (from_outside != (char *) NULL)
1166 return from_outside;
1168 /* Set to default. */
1169 from_inside = argv_to_target[0][1];
1170 for (i = 1; i < argc; i++)
1172 for (j = 1; j < jmax; j++)
1174 if (0 == strcmp (argv[i], argv_to_target[j][0]))
1175 from_inside = argv_to_target[j][1];
1186 change_symbol_mode (char *input)
1188 char *symbol_mode_string[] = {
1191 "# no32", /* 0x04 */
1192 "# no64", /* 0x08 */
1199 for (bit = 0;; bit++)
1201 string = symbol_mode_string[bit];
1205 if (0 == strcmp (input, string))
1207 symbol_mode = (1 << bit);
1211 /* should not be here */
1218 -1 : error, try something else */
1220 is_syscall (char *input, unsigned int *flag)
1226 char *syscall_string;
1229 { "svc" /* 0x01 */, XCOFF_SYSCALL32 },
1230 { "svc32" /* 0x02 */, XCOFF_SYSCALL32 },
1231 { "svc3264" /* 0x04 */, XCOFF_SYSCALL32 | XCOFF_SYSCALL64 },
1232 { "svc64" /* 0x08 */, XCOFF_SYSCALL64 },
1233 { "syscall" /* 0x10 */, XCOFF_SYSCALL32 },
1234 { "syscall32" /* 0x20 */, XCOFF_SYSCALL32 },
1235 { "syscall3264" /* 0x40 */, XCOFF_SYSCALL32 | XCOFF_SYSCALL64 },
1236 { "syscall64" /* 0x80 */, XCOFF_SYSCALL64 },
1242 for (bit = 0;; bit++)
1244 string = s[bit].syscall_string;
1248 if (0 == strcmp (input, string))
1250 if (1 << bit & syscall_mask)
1252 *flag = s[bit].flag;
1261 /* should not be here */
1265 /* Read an import or export file. For an import file, this is called
1266 by the before_allocation emulation routine. For an export file,
1267 this is called by the handle_option emulation routine. */
1270 gld${EMULATION_NAME}_read_file (const char *filename, bool import)
1277 const char *imppath;
1278 const char *impfile;
1279 const char *impmember;
1281 o = (struct obstack *) xmalloc (sizeof (struct obstack));
1282 obstack_specify_allocation (o, 0, 0, xmalloc, gld${EMULATION_NAME}_free);
1284 f = fopen (filename, FOPEN_RT);
1287 bfd_set_error (bfd_error_system_call);
1288 einfo ("%F%P: %s: %E\n", filename);
1300 /* Default to 32 and 64 bit mode
1301 symbols at top of /lib/syscalls.exp do not have a mode modifier and they
1302 are not repeated, assume 64 bit routines also want to use them.
1303 See the routine change_symbol_mode for more information. */
1307 while ((c = getc (f)) != EOF)
1311 unsigned int syscall_flag = 0;
1313 struct bfd_link_hash_entry *h;
1317 obstack_1grow (o, c);
1321 obstack_1grow (o, '\0');
1324 s = (char *) obstack_base (o);
1325 while (ISSPACE (*s))
1329 || change_symbol_mode (s)
1330 || (*s == '#' && s[1] == ' ')
1331 || (!import && *s == '#' && s[1] == '!'))
1333 obstack_free (o, obstack_base (o));
1337 if (*s == '#' && s[1] == '!')
1340 while (ISSPACE (*s))
1347 obstack_free (o, obstack_base (o));
1350 einfo (_("%F%P:%s:%d: #! ([member]) is not supported "
1351 "in import files\n"),
1352 filename, linenumber);
1358 (void) obstack_finish (o);
1361 while (!ISSPACE (*s) && *s != '(' && *s != '\0')
1365 if (!bfd_xcoff_split_import_path (link_info.output_bfd,
1366 start, &imppath, &impfile))
1367 einfo (_("%F%P: could not parse import path: %E\n"));
1368 while (ISSPACE (cs))
1377 einfo (_("%P:%s:%d: warning: syntax error in import file\n"),
1378 filename, linenumber);
1384 while (*s != ')' && *s != '\0')
1389 einfo (_("%P:%s:%d: warning: syntax error in import file\n"),
1390 filename, linenumber);
1397 if (symbol_mode & symbol_mode_mask)
1399 /* This is a symbol to be imported or exported. */
1402 address = (bfd_vma) -1;
1404 while (!ISSPACE (*s) && *s != '\0')
1412 while (ISSPACE (*s))
1416 while (!ISSPACE (*se) && *se != '\0')
1421 while (ISSPACE (*se))
1424 einfo (_("%P:%s%d: warning: syntax error in "
1425 "import/export file\n"),
1426 filename, linenumber);
1434 status = is_syscall (s, &syscall_flag);
1438 /* not a system call, check for address */
1439 address = bfd_scan_vma (s, &end, 0);
1442 einfo (_("%P:%s:%d: warning: syntax error in "
1443 "import/export file\n"),
1444 filename, linenumber);
1453 struct export_symbol_list *n;
1455 ldlang_add_undef (symname, true);
1456 n = ((struct export_symbol_list *)
1457 xmalloc (sizeof (struct export_symbol_list)));
1458 n->next = export_symbols;
1459 n->name = xstrdup (symname);
1464 h = bfd_link_hash_lookup (link_info.hash, symname, false, false,
1466 if (h == NULL || h->type == bfd_link_hash_new)
1468 /* We can just ignore attempts to import an unreferenced
1473 if (!bfd_xcoff_import_symbol (link_info.output_bfd,
1475 address, imppath, impfile,
1476 impmember, syscall_flag))
1477 einfo (_("%X%P:%s:%d: failed to import symbol %s: %E\n"),
1478 filename, linenumber, symname);
1482 obstack_free (o, obstack_base (o));
1485 if (obstack_object_size (o) > 0)
1487 einfo (_("%P:%s:%d: warning: ignoring unterminated last line\n"),
1488 filename, linenumber);
1489 obstack_free (o, obstack_base (o));
1494 obstack_free (o, NULL);
1501 /* This routine saves us from worrying about declaring free. */
1504 gld${EMULATION_NAME}_free (void *p)
1509 /* This is called by the before_allocation routine via
1510 lang_for_each_statement. It looks for relocations and assignments
1514 gld${EMULATION_NAME}_find_relocs (lang_statement_union_type *s)
1516 if (s->header.type == lang_reloc_statement_enum)
1518 lang_reloc_statement_type *rs;
1520 rs = &s->reloc_statement;
1521 if (rs->name == NULL)
1522 einfo (_("%F%P: only relocations against symbols are permitted\n"));
1523 if (!bfd_xcoff_link_count_reloc (link_info.output_bfd, &link_info,
1525 einfo (_("%F%P: bfd_xcoff_link_count_reloc failed: %E\n"));
1528 if (s->header.type == lang_assignment_statement_enum)
1529 gld${EMULATION_NAME}_find_exp_assignment (s->assignment_statement.exp);
1532 /* Look through an expression for an assignment statement. */
1535 gld${EMULATION_NAME}_find_exp_assignment (etree_type *exp)
1537 struct bfd_link_hash_entry *h;
1539 switch (exp->type.node_class)
1542 case etree_provided:
1543 h = bfd_link_hash_lookup (link_info.hash, exp->assign.dst,
1544 false, false, false);
1549 if (strcmp (exp->assign.dst, ".") != 0)
1551 if (!bfd_xcoff_record_link_assignment (link_info.output_bfd,
1554 einfo (_("%F%P: failed to record assignment to %s: %E\n"),
1557 gld${EMULATION_NAME}_find_exp_assignment (exp->assign.src);
1561 gld${EMULATION_NAME}_find_exp_assignment (exp->binary.lhs);
1562 gld${EMULATION_NAME}_find_exp_assignment (exp->binary.rhs);
1566 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.cond);
1567 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.lhs);
1568 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.rhs);
1572 gld${EMULATION_NAME}_find_exp_assignment (exp->unary.child);
1581 gld${EMULATION_NAME}_get_script (int *isfile)
1584 if test x"$COMPILE_IN" = xyes
1586 # Scripts compiled in.
1588 # sed commands to quote an ld script as a C string.
1589 sc="-f ${srcdir}/emultempl/stringify.sed"
1595 if (bfd_link_relocatable (&link_info) && config.build_constructors)
1598 sed $sc ldscripts/${EMULATION_NAME}.xu >> e${EMULATION_NAME}.c
1599 echo ' ; else if (bfd_link_relocatable (&link_info)) return' >> e${EMULATION_NAME}.c
1600 sed $sc ldscripts/${EMULATION_NAME}.xr >> e${EMULATION_NAME}.c
1601 echo ' ; else if (!config.text_read_only) return' >> e${EMULATION_NAME}.c
1602 sed $sc ldscripts/${EMULATION_NAME}.xbn >> e${EMULATION_NAME}.c
1603 echo ' ; else if (!config.magic_demand_paged) return' >> e${EMULATION_NAME}.c
1604 sed $sc ldscripts/${EMULATION_NAME}.xn >> e${EMULATION_NAME}.c
1605 echo ' ; else return' >> e${EMULATION_NAME}.c
1606 sed $sc ldscripts/${EMULATION_NAME}.x >> e${EMULATION_NAME}.c
1607 echo '; }' >> e${EMULATION_NAME}.c
1610 # Scripts read from the filesystem.
1616 if (bfd_link_relocatable (&link_info) && config.build_constructors)
1617 return "ldscripts/${EMULATION_NAME}.xu";
1618 else if (bfd_link_relocatable (&link_info))
1619 return "ldscripts/${EMULATION_NAME}.xr";
1620 else if (!config.text_read_only)
1621 return "ldscripts/${EMULATION_NAME}.xbn";
1622 else if (!config.magic_demand_paged)
1623 return "ldscripts/${EMULATION_NAME}.xn";
1625 return "ldscripts/${EMULATION_NAME}.x";
1634 gld${EMULATION_NAME}_create_output_section_statements (void)
1636 if ((bfd_get_flavour (link_info.output_bfd) != bfd_target_xcoff_flavour))
1640 stub_file = lang_add_input_file ("linker stubs",
1641 lang_input_file_is_fake_enum,
1643 stub_file->the_bfd = bfd_create ("linker stubs", link_info.output_bfd);
1644 if (stub_file->the_bfd == NULL
1645 || !bfd_set_arch_mach (stub_file->the_bfd,
1646 bfd_get_arch (link_info.output_bfd),
1647 bfd_get_mach (link_info.output_bfd)))
1649 einfo (_("%F%P: can not create stub BFD: %E\n"));
1653 stub_file->the_bfd->flags |= BFD_LINKER_CREATED;
1654 ldlang_add_file (stub_file);
1655 params.stub_bfd = stub_file->the_bfd;
1657 /* Pass linker params to the back-end. */
1658 if (!bfd_xcoff_link_init (&link_info, ¶ms))
1659 einfo (_("%F%P: can not init BFD: %E\n"));
1662 if (link_info.init_function != NULL
1663 || link_info.fini_function != NULL
1666 initfini_file = lang_add_input_file ("initfini",
1667 lang_input_file_is_file_enum,
1670 initfini_file->the_bfd = bfd_create ("initfini", link_info.output_bfd);
1671 if (initfini_file->the_bfd == NULL
1672 || ! bfd_set_arch_mach (initfini_file->the_bfd,
1673 bfd_get_arch (link_info.output_bfd),
1674 bfd_get_mach (link_info.output_bfd)))
1676 einfo (_("%F%P: can not create BFD: %E\n"));
1680 /* Call backend to fill in the rest */
1681 if (! bfd_xcoff_link_generate_rtinit (initfini_file->the_bfd,
1682 link_info.init_function,
1683 link_info.fini_function,
1686 einfo (_("%F%P: can not create BFD: %E\n"));
1690 /* __rtld defined in /lib/librtl.a */
1692 lang_add_input_file ("rtl", lang_input_file_is_l_enum, NULL);
1697 gld${EMULATION_NAME}_set_output_arch (void)
1699 bfd_set_arch_mach (link_info.output_bfd,
1700 bfd_xcoff_architecture (link_info.output_bfd),
1701 bfd_xcoff_machine (link_info.output_bfd));
1703 ldfile_output_architecture = bfd_get_arch (link_info.output_bfd);
1704 ldfile_output_machine = bfd_get_mach (link_info.output_bfd);
1705 ldfile_output_machine_name = bfd_printable_name (link_info.output_bfd);
1709 gld${EMULATION_NAME}_open_dynamic_archive (const char *arch,
1710 search_dirs_type *search,
1711 lang_input_statement_type *entry)
1715 if (!entry->flags.maybe_archive)
1718 if (entry->flags.full_name_provided)
1719 path = concat (search->name, "/", entry->filename,
1720 (const char *) NULL);
1722 path = concat (search->name, "/lib", entry->filename, arch, ".a",
1723 (const char *) NULL);
1725 if (!ldfile_try_open_bfd (path, entry))
1730 /* Don't include the searched directory in the import path. */
1731 bfd_xcoff_set_archive_import_path (&link_info, entry->the_bfd,
1732 path + strlen (search->name) + 1);
1733 entry->filename = path;
1738 gld${EMULATION_NAME}_print_symbol (struct bfd_link_hash_entry *hash_entry,
1741 asection *sec = (asection *) ptr;
1743 if ((hash_entry->type == bfd_link_hash_defined
1744 || hash_entry->type == bfd_link_hash_defweak)
1745 && sec == hash_entry->u.def.section)
1747 struct xcoff_link_hash_entry *h;
1749 print_spaces (SECTION_NAME_MAP_LENGTH);
1751 (hash_entry->u.def.value
1752 + hash_entry->u.def.section->output_offset
1753 + hash_entry->u.def.section->output_section->vma));
1755 /* Flag symbol if it has been garbage collected. */
1756 h = (struct xcoff_link_hash_entry *) hash_entry;
1757 if ((h != NULL) && !(h->flags & XCOFF_MARK))
1759 minfo (" %pT\n", hash_entry->root.string);
1766 LDEMUL_AFTER_OPEN=gld${EMULATION_NAME}_after_open
1767 LDEMUL_SET_OUTPUT_ARCH=gld${EMULATION_NAME}_set_output_arch
1768 LDEMUL_CHOOSE_TARGET=gld${EMULATION_NAME}_choose_target
1769 LDEMUL_BEFORE_ALLOCATION=gld${EMULATION_NAME}_before_allocation
1770 LDEMUL_AFTER_ALLOCATION=gld${EMULATION_NAME}_after_allocation
1771 LDEMUL_CREATE_OUTPUT_SECTION_STATEMENTS=gld${EMULATION_NAME}_create_output_section_statements
1772 LDEMUL_OPEN_DYNAMIC_ARCHIVE=gld${EMULATION_NAME}_open_dynamic_archive
1773 LDEMUL_PARSE_ARGS=gld${EMULATION_NAME}_parse_args
1774 LDEMUL_ADD_OPTIONS=gld${EMULATION_NAME}_add_options
1775 LDEMUL_HANDLE_OPTION=gld${EMULATION_NAME}_handle_option
1776 LDEMUL_UNRECOGNIZED_FILE=gld${EMULATION_NAME}_unrecognized_file
1777 LDEMUL_PRINT_SYMBOL=gld${EMULATION_NAME}_print_symbol
1779 source_em ${srcdir}/emultempl/emulation.em