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-2021 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 *, bfd_boolean);
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 *);
67 /* The file alignment required for each section. */
68 static unsigned long file_align;
70 /* The maximum size the stack is permitted to grow. This is stored in
72 static unsigned long maxstack;
74 /* The maximum data size. This is stored in the a.out header. */
75 static unsigned long maxdata;
77 /* Whether to perform garbage collection. */
80 /* The module type to use. */
81 static unsigned short modtype = ('1' << 8) | 'L';
83 /* Whether the .text section must be read-only (i.e., no relocs
87 /* A mask of XCOFF_EXPALL and XCOFF_EXPFULL flags, as set by their
88 associated -b and -bno options. */
89 static unsigned int auto_export_flags;
91 /* A mask of auto_export_flags bits that were explicitly set on the
93 static unsigned int explicit_auto_export_flags;
95 /* Whether to implement Unix like linker semantics. */
98 /* Structure used to hold import file list. */
102 struct filelist *next;
106 /* List of import files. */
107 static struct filelist *import_files;
109 /* List of export symbols read from the export files. */
111 struct export_symbol_list
113 struct export_symbol_list *next;
117 static struct export_symbol_list *export_symbols;
119 /* Maintains the 32 or 64 bit mode state of import file */
120 static unsigned int symbol_mode = 0x04;
122 /* Which symbol modes are valid */
123 static unsigned int symbol_mode_mask = 0x0d;
125 /* Whether this is a 64 bit link */
126 static int is_64bit = 0;
128 /* Which syscalls from import file are valid */
129 static unsigned int syscall_mask = 0x77;
131 /* fake file for -binitfini support */
132 static lang_input_statement_type *initfini_file;
134 /* Whether to do run time linking
135 -brtl enables, -bnortl and -bnortllib disable. */
138 /* Explicit command line library path, -blibpath */
139 static char *command_line_blibpath = NULL;
141 /* This routine is called before anything else is done. */
144 gld${EMULATION_NAME}_before_parse (void)
146 ldfile_set_output_arch ("${OUTPUT_ARCH}", bfd_arch_`echo ${ARCH} | sed -e 's/:.*//'`);
148 input_flags.dynamic = TRUE;
149 config.has_shared = TRUE;
151 /* The link_info.[init|fini]_functions are initialized in ld/lexsup.c.
152 Override them here so we can use the link_info.init_function as a
153 state flag that lets the backend know that -binitfini has been done. */
155 link_info.init_function = NULL;
156 link_info.fini_function = NULL;
159 /* Handle AIX specific options. */
191 gld${EMULATION_NAME}_add_options
192 (int ns, char **shortopts, int nl, struct option **longopts,
193 int nrl ATTRIBUTE_UNUSED, struct option **really_longopts ATTRIBUTE_UNUSED)
195 static const char xtra_short[] = "D:H:KT:z";
196 static const struct option xtra_long[] = {
197 /* -binitfini has special handling in the linker backend. The native linker
198 uses the arguemnts to generate a table of init and fini functions for
199 the executable. The important use for this option is to support aix 4.2+
200 c++ constructors and destructors. This is tied into gcc via collect2.c.
202 The function table is accessed by the runtime linker/loader by checking if
203 the first symbol in the loader symbol table is __rtinit. The gnu linker
204 generates this symbol and makes it the first loader symbol. */
206 {"basis", no_argument, NULL, OPTION_IGNORE},
207 {"bautoimp", no_argument, NULL, OPTION_AUTOIMP},
208 {"bcomprld", no_argument, NULL, OPTION_IGNORE},
209 {"bcrld", no_argument, NULL, OPTION_IGNORE},
210 {"bcror31", no_argument, NULL, OPTION_IGNORE},
211 {"bD", required_argument, NULL, OPTION_MAXDATA},
212 {"bE", required_argument, NULL, OPTION_EXPORT},
213 {"bernotok", no_argument, NULL, OPTION_ERNOTOK},
214 {"berok", no_argument, NULL, OPTION_EROK},
215 {"berrmsg", no_argument, NULL, OPTION_IGNORE},
216 {"bexpall", no_argument, NULL, OPTION_EXPALL},
217 {"bexpfull", no_argument, NULL, OPTION_EXPFULL},
218 {"bexport", required_argument, NULL, OPTION_EXPORT},
219 {"bbigtoc", no_argument, NULL, OPTION_IGNORE},
220 {"bf", no_argument, NULL, OPTION_ERNOTOK},
221 {"bgc", no_argument, &gc, 1},
222 {"bh", required_argument, NULL, OPTION_IGNORE},
223 {"bhalt", required_argument, NULL, OPTION_IGNORE},
224 {"bI", required_argument, NULL, OPTION_IMPORT},
225 {"bimport", required_argument, NULL, OPTION_IMPORT},
226 {"binitfini", required_argument, NULL, OPTION_INITFINI},
227 {"bl", required_argument, NULL, OPTION_LOADMAP},
228 {"bloadmap", required_argument, NULL, OPTION_LOADMAP},
229 {"bmaxdata", required_argument, NULL, OPTION_MAXDATA},
230 {"bmaxstack", required_argument, NULL, OPTION_MAXSTACK},
231 {"bM", required_argument, NULL, OPTION_MODTYPE},
232 {"bmodtype", required_argument, NULL, OPTION_MODTYPE},
233 {"bnoautoimp", no_argument, NULL, OPTION_NOAUTOIMP},
234 {"bnoexpall", no_argument, NULL, OPTION_NOEXPALL},
235 {"bnoexpfull", no_argument, NULL, OPTION_NOEXPFULL},
236 {"bnodelcsect", no_argument, NULL, OPTION_IGNORE},
237 {"bnoentry", no_argument, NULL, OPTION_IGNORE},
238 {"bnogc", no_argument, &gc, 0},
239 {"bnso", no_argument, NULL, OPTION_NOAUTOIMP},
240 {"bnostrcmpct", no_argument, NULL, OPTION_NOSTRCMPCT},
241 {"bnotextro", no_argument, &textro, 0},
242 {"bnro", no_argument, &textro, 0},
243 {"bpD", required_argument, NULL, OPTION_PD},
244 {"bpT", required_argument, NULL, OPTION_PT},
245 {"bro", no_argument, &textro, 1},
246 {"brtl", no_argument, &rtld, 1},
247 {"bnortl", no_argument, &rtld, 0},
248 {"bnortllib", no_argument, &rtld, 0},
249 {"bS", required_argument, NULL, OPTION_MAXSTACK},
250 {"bso", no_argument, NULL, OPTION_AUTOIMP},
251 {"bstrcmpct", no_argument, NULL, OPTION_STRCMPCT},
252 {"btextro", no_argument, &textro, 1},
253 {"b32", no_argument, NULL, OPTION_32},
254 {"b64", no_argument, NULL, OPTION_64},
255 {"static", no_argument, NULL, OPTION_NOAUTOIMP},
256 {"unix", no_argument, NULL, OPTION_UNIX},
257 {"blibpath", required_argument, NULL, OPTION_LIBPATH},
258 {"bnolibpath", required_argument, NULL, OPTION_NOLIBPATH},
259 {NULL, no_argument, NULL, 0}
262 /* Options supported by the AIX linker which we do not support:
263 -S, -v, -Z, -bbindcmds, -bbinder, -bbindopts, -bcalls, -bcaps,
264 -bcror15, -bdebugopt, -bdbg, -bdelcsect, -bex?, -bfilelist, -bfl,
265 -bgcbypass, -bglink, -binsert, -bi, -bloadmap, -bl, -bmap, -bnl,
266 -bnobind, -bnocomprld, -bnocrld, -bnoerrmsg, -bnoglink,
267 -bnoloadmap, -bnl, -bnoobjreorder, -bnoquiet, -bnoreorder,
268 -bnotypchk, -bnox, -bquiet, -bR, -brename, -breorder, -btypchk,
271 *shortopts = (char *) xrealloc (*shortopts, ns + sizeof (xtra_short));
272 memcpy (*shortopts + ns, &xtra_short, sizeof (xtra_short));
273 *longopts = xrealloc (*longopts,
274 nl * sizeof (struct option) + sizeof (xtra_long));
275 memcpy (*longopts + nl, &xtra_long, sizeof (xtra_long));
279 gld${EMULATION_NAME}_parse_args (int argc, char **argv)
283 /* If the current option starts with -b, change the first : to an =.
284 The AIX linker uses : to separate the option from the argument;
285 changing it to = lets us treat it as a getopt option. */
290 if (indx < argc && startswith (argv[indx], "-b"))
294 for (s = argv[indx]; *s != '\0'; s++)
306 /* Helper for option '-f', which specify a list of input files.
307 Contrary to the native linker, we don't support shell patterns
308 (simply because glob isn't always available). */
311 read_file_list (const char *filename)
314 /* An upper bound on the number of characters in the file. */
316 /* File in memory. */
322 f = fopen (filename, FOPEN_RT);
325 einfo (_("%F%P: cannot open %s\n"), filename);
328 if (fseek (f, 0L, SEEK_END) == -1)
333 if (fseek (f, 0L, SEEK_SET) == -1)
336 buffer = (char *) xmalloc (pos + 1);
337 len = fread (buffer, sizeof (char), pos, f);
338 if (len != (size_t) pos && ferror (f))
340 /* Add a NUL terminator. */
348 /* Skip empty lines. */
349 while (*b == '\n' || *b == '\r')
352 /* Stop if end of buffer. */
353 if (b == buffer + len)
356 /* Eat any byte until end of line. */
357 for (e = b; *e != '\0'; e++)
358 if (*e == '\n' || *e == '\r')
361 /* Replace end of line by nul. */
366 lang_add_input_file (b, lang_input_file_is_search_file_enum, NULL);
372 einfo (_("%F%P: cannot read %s\n"), optarg);
377 gld${EMULATION_NAME}_handle_option (int optc)
388 /* Long option which just sets a flag. */
392 /* This overrides --auxiliary. This option specifies a file containing
393 a list of input files. */
394 read_file_list (optarg);
398 val = bfd_scan_vma (optarg, &end, 0);
400 einfo (_("%P: warning: ignoring invalid -D number %s\n"), optarg);
402 lang_section_start (".data", exp_intop (val), NULL);
406 val = bfd_scan_vma (optarg, &end, 0);
407 if (*end != '\0' || (val & (val - 1)) != 0)
408 einfo (_("%P: warning: ignoring invalid -H number %s\n"), optarg);
415 /* FIXME: This should use the page size for the target system. */
420 /* On AIX this is the same as GNU ld -Ttext. When we see -T
421 number, we assume the AIX option is intended. Otherwise, we
422 assume the usual GNU ld -T option is intended. We can't just
423 ignore the AIX option, because gcc passes it to the linker. */
424 val = bfd_scan_vma (optarg, &end, 0);
427 lang_section_start (".text", exp_intop (val), NULL);
433 case OPTION_INITFINI:
436 * The aix linker init fini has the format :
438 * -binitfini:[ Initial][:Termination][:Priority]
440 * it allows the Termination and Priority to be optional.
442 * Since we support only one init/fini pair, we ignore the Priority.
444 * Define the special symbol __rtinit.
446 * strtok does not correctly handle the case of -binitfini::fini: so
452 while (*t && ':' != *t)
458 link_info.init_function = i;
461 while (*t && ':' != *t)
466 link_info.fini_function = f;
471 link_info.static_link = FALSE;
475 link_info.unresolved_syms_in_objects = RM_DIAGNOSE;
476 link_info.unresolved_syms_in_shared_libs = RM_DIAGNOSE;
480 link_info.unresolved_syms_in_objects = RM_IGNORE;
481 link_info.unresolved_syms_in_shared_libs = RM_IGNORE;
485 auto_export_flags |= XCOFF_EXPALL;
486 explicit_auto_export_flags |= XCOFF_EXPALL;
490 auto_export_flags |= XCOFF_EXPFULL;
491 explicit_auto_export_flags |= XCOFF_EXPFULL;
495 gld${EMULATION_NAME}_read_file (optarg, FALSE);
501 struct filelist **flpp;
503 n = (struct filelist *) xmalloc (sizeof (struct filelist));
506 flpp = &import_files;
507 while (*flpp != NULL)
508 flpp = &(*flpp)->next;
514 config.map_filename = optarg;
518 val = bfd_scan_vma (optarg, &end, 0);
520 einfo (_("%P: warning: ignoring invalid -bmaxdata number %s\n"),
526 case OPTION_MAXSTACK:
527 val = bfd_scan_vma (optarg, &end, 0);
529 einfo (_("%P: warning: ignoring invalid -bmaxstack number %s\n"),
538 link_info.type = type_dll;
541 if (*optarg == '\0' || optarg[1] == '\0')
542 einfo (_("%P: warning: ignoring invalid module type %s\n"), optarg);
544 modtype = (*optarg << 8) | optarg[1];
547 case OPTION_NOAUTOIMP:
548 link_info.static_link = TRUE;
551 case OPTION_NOEXPALL:
552 auto_export_flags &= ~XCOFF_EXPALL;
553 explicit_auto_export_flags |= XCOFF_EXPALL;
556 case OPTION_NOEXPFULL:
557 auto_export_flags &= ~XCOFF_EXPFULL;
558 explicit_auto_export_flags |= XCOFF_EXPFULL;
561 case OPTION_NOSTRCMPCT:
562 link_info.traditional_format = TRUE;
566 /* This sets the page that the .data section is supposed to
567 start on. The offset within the page should still be the
568 offset within the file, so we need to build an appropriate
570 val = bfd_scan_vma (optarg, &end, 0);
572 einfo (_("%P: warning: ignoring invalid -pD number %s\n"), optarg);
580 exp_nameop (NAME, "."),
583 exp_binop ('+', t, exp_intop (31)),
584 exp_intop (~(bfd_vma) 31));
585 lang_section_start (".data", t, NULL);
590 /* This set the page that the .text section is supposed to start
591 on. The offset within the page should still be the offset
593 val = bfd_scan_vma (optarg, &end, 0);
595 einfo (_("%P: warning: ignoring invalid -pT number %s\n"), optarg);
602 exp_nameop (SIZEOF_HEADERS, NULL));
604 exp_binop ('+', t, exp_intop (31)),
605 exp_intop (~(bfd_vma) 31));
606 lang_section_start (".text", t, NULL);
610 case OPTION_STRCMPCT:
611 link_info.traditional_format = FALSE;
621 symbol_mode_mask = 0x0d;
627 symbol_mode_mask = 0x0e;
631 command_line_blibpath = optarg;
634 case OPTION_NOLIBPATH:
635 command_line_blibpath = NULL;
643 /* This is called when an input file can not be recognized as a BFD
644 object or an archive. If the file starts with #!, we must treat it
645 as an import file. This is for AIX compatibility. */
648 gld${EMULATION_NAME}_unrecognized_file (lang_input_statement_type *entry)
653 e = fopen (entry->filename, FOPEN_RT);
659 if (getc (e) == '#' && getc (e) == '!')
662 struct filelist **flpp;
664 n = (struct filelist *) xmalloc (sizeof (struct filelist));
666 n->name = entry->filename;
667 flpp = &import_files;
668 while (*flpp != NULL)
669 flpp = &(*flpp)->next;
673 entry->flags.loaded = TRUE;
681 /* This is called after the input files have been opened. */
684 gld${EMULATION_NAME}_after_open (void)
689 after_open_default ();
691 /* Call ldctor_build_sets, after pretending that this is a
692 relocatable link. We do this because AIX requires relocation
693 entries for all references to symbols, even in a final
694 executable. Of course, we only want to do this if we are
695 producing an XCOFF output file. */
697 if (strstr (bfd_get_target (link_info.output_bfd), "xcoff") != NULL)
698 link_info.type = type_relocatable;
699 ldctor_build_sets ();
702 /* For each set, record the size, so that the XCOFF backend can
703 output the correct csect length. */
704 for (p = sets; p != (struct set_info *) NULL; p = p->next)
708 /* If the symbol is defined, we may have been invoked from
709 collect, and the sets may already have been built, so we do
711 if (p->h->type == bfd_link_hash_defined
712 || p->h->type == bfd_link_hash_defweak)
715 if (p->reloc != BFD_RELOC_CTOR)
717 /* Handle this if we need to. */
721 size = (p->count + 2) * 4;
722 if (!bfd_xcoff_link_record_set (link_info.output_bfd, &link_info,
724 einfo (_("%F%P: bfd_xcoff_link_record_set failed: %E\n"));
728 /* This is called after the sections have been attached to output
729 sections, but before any sizes or addresses have been set. */
732 gld${EMULATION_NAME}_before_allocation (void)
735 struct export_symbol_list *el;
737 asection *special_sections[XCOFF_NUMBER_OF_SPECIAL_SECTIONS];
738 static const char *const must_keep_sections[] = {
743 unsigned int i, flags;
745 /* Handle the import and export files, if any. */
746 for (fl = import_files; fl != NULL; fl = fl->next)
747 gld${EMULATION_NAME}_read_file (fl->name, TRUE);
748 for (el = export_symbols; el != NULL; el = el->next)
750 struct bfd_link_hash_entry *h;
752 h = bfd_link_hash_lookup (link_info.hash, el->name, FALSE, FALSE, FALSE);
754 einfo (_("%F%P: bfd_link_hash_lookup of export symbol failed: %E\n"));
755 if (!bfd_xcoff_export_symbol (link_info.output_bfd, &link_info, h))
756 einfo (_("%F%P: bfd_xcoff_export_symbol failed: %E\n"));
759 /* Track down all relocations called for by the linker script (these
760 are typically constructor/destructor entries created by
761 CONSTRUCTORS) and let the backend know it will need to create
762 .loader relocs for them. */
763 lang_for_each_statement (gld${EMULATION_NAME}_find_relocs);
765 /* Precedence of LIBPATH
766 -blibpath: native support always first
767 -rpath: gnu extension
768 -L build from command line -L's */
769 if (command_line_blibpath != NULL)
770 libpath = command_line_blibpath;
771 else if (command_line.rpath != NULL)
772 libpath = command_line.rpath;
773 else if (search_head == NULL)
774 libpath = (char *) "";
778 search_dirs_type *search;
780 /* PR ld/4023: Strip sysroot prefix from any paths
781 being inserted into the output binary's DT_RPATH. */
782 if (ld_sysroot != NULL
783 && * ld_sysroot != 0)
785 const char * name = search_head->name;
786 size_t ld_sysroot_len = strlen (ld_sysroot);
788 if (strncmp (name, ld_sysroot, ld_sysroot_len) == 0)
789 name += ld_sysroot_len;
792 libpath = xmalloc (len + 1);
793 strcpy (libpath, name);
795 for (search = search_head->next; search != NULL; search = search->next)
800 if (strncmp (name, ld_sysroot, ld_sysroot_len) == 0)
801 name += ld_sysroot_len;
803 nlen = strlen (name);
804 libpath = xrealloc (libpath, len + nlen + 2);
806 strcpy (libpath + len + 1, name);
812 len = strlen (search_head->name);
813 libpath = xmalloc (len + 1);
814 strcpy (libpath, search_head->name);
816 for (search = search_head->next; search != NULL; search = search->next)
820 nlen = strlen (search->name);
821 libpath = xrealloc (libpath, len + nlen + 2);
823 strcpy (libpath + len + 1, search->name);
829 /* Default to -bexpfull for SVR4-like semantics. */
830 flags = (unix_ld ? XCOFF_EXPFULL : 0);
831 flags &= ~explicit_auto_export_flags;
832 flags |= auto_export_flags;
834 /* Let the XCOFF backend set up the .loader section. */
835 if (!bfd_xcoff_size_dynamic_sections
836 (link_info.output_bfd, &link_info, libpath, entry_symbol.name, file_align,
837 maxstack, maxdata, gc && !unix_ld,
838 modtype, textro, flags, special_sections, rtld))
839 einfo (_("%F%P: failed to set dynamic section sizes: %E\n"));
841 /* Look through the special sections, and put them in the right
842 place in the link ordering. This is especially magic. */
843 for (i = 0; i < XCOFF_NUMBER_OF_SPECIAL_SECTIONS; i++)
846 lang_output_section_statement_type *os;
847 lang_statement_union_type **pls;
848 lang_input_section_type *is;
852 sec = special_sections[i];
856 /* Remove this section from the list of the output section.
857 This assumes we know what the script looks like. */
859 os = lang_output_section_get (sec->output_section);
861 einfo (_("%F%P: can't find output section %s\n"),
862 sec->output_section->name);
864 for (pls = &os->children.head; *pls != NULL; pls = &(*pls)->header.next)
866 if ((*pls)->header.type == lang_input_section_enum
867 && (*pls)->input_section.section == sec)
869 is = (lang_input_section_type *) * pls;
870 *pls = (*pls)->header.next;
874 if ((*pls)->header.type == lang_wild_statement_enum)
876 lang_statement_union_type **pwls;
878 for (pwls = &(*pls)->wild_statement.children.head;
879 *pwls != NULL; pwls = &(*pwls)->header.next)
882 if ((*pwls)->header.type == lang_input_section_enum
883 && (*pwls)->input_section.section == sec)
885 is = (lang_input_section_type *) * pwls;
886 *pwls = (*pwls)->header.next;
898 einfo (_("%F%P: can't find %s in output section\n"),
899 bfd_section_name (sec));
902 /* Now figure out where the section should go. */
906 default: /* to avoid warnings */
907 case XCOFF_SPECIAL_SECTION_TEXT:
913 case XCOFF_SPECIAL_SECTION_ETEXT:
919 case XCOFF_SPECIAL_SECTION_DATA:
925 case XCOFF_SPECIAL_SECTION_EDATA:
931 case XCOFF_SPECIAL_SECTION_END:
932 case XCOFF_SPECIAL_SECTION_END2:
939 os = lang_output_section_find (oname);
943 is->header.next = os->children.head;
944 os->children.head = (lang_statement_union_type *) is;
948 is->header.next = NULL;
949 *os->children.tail = (lang_statement_union_type *) is;
950 os->children.tail = &is->header.next;
954 /* Executables and shared objects must always have .text, .data
955 and .bss output sections, so that the header can refer to them.
956 The kernel refuses to load objects that have missing sections. */
957 if (!bfd_link_relocatable (&link_info))
958 for (i = 0; i < ARRAY_SIZE (must_keep_sections); i++)
962 sec = bfd_get_section_by_name (link_info.output_bfd,
963 must_keep_sections[i]);
965 einfo (_("%P: can't find required output section %s\n"),
966 must_keep_sections[i]);
968 sec->flags |= SEC_KEEP;
971 /* Make sure .tdata is removed if empty, even with -r flag.
972 .tdata is always being generated because its size is needed
973 to cumpute .data address. */
974 if (bfd_link_relocatable (&link_info))
976 static const char *const thread_sections[] = {
981 /* Run lang_size_sections (if not already done). */
982 if (expld.phase != lang_mark_phase_enum)
984 expld.phase = lang_mark_phase_enum;
985 expld.dataseg.phase = exp_seg_none;
986 one_lang_size_sections_pass (NULL, FALSE);
987 lang_reset_memory_regions ();
990 for (i = 0; i < ARRAY_SIZE (thread_sections); i++)
994 sec = bfd_get_section_by_name (link_info.output_bfd,
997 if (sec != NULL && sec->rawsize == 0
998 && (sec->flags & SEC_KEEP) == 0
999 && !bfd_section_removed_from_list (link_info.output_bfd,
1002 sec->flags |= SEC_EXCLUDE;
1003 bfd_section_list_remove (link_info.output_bfd, sec);
1004 link_info.output_bfd->section_count--;
1009 before_allocation_default ();
1013 gld${EMULATION_NAME}_choose_target (int argc, char **argv)
1016 static char *from_outside;
1017 static char *from_inside;
1018 static char *argv_to_target[][2] = {
1019 {NULL, "${OUTPUT_FORMAT}"},
1020 {"-b32", "${OUTPUT_FORMAT_32BIT}"},
1021 {"-b64", "${OUTPUT_FORMAT_64BIT}"},
1026 from_outside = getenv (TARGET_ENVIRON);
1027 if (from_outside != (char *) NULL)
1028 return from_outside;
1030 /* Set to default. */
1031 from_inside = argv_to_target[0][1];
1032 for (i = 1; i < argc; i++)
1034 for (j = 1; j < jmax; j++)
1036 if (0 == strcmp (argv[i], argv_to_target[j][0]))
1037 from_inside = argv_to_target[j][1];
1048 change_symbol_mode (char *input)
1050 char *symbol_mode_string[] = {
1053 "# no32", /* 0x04 */
1054 "# no64", /* 0x08 */
1061 for (bit = 0;; bit++)
1063 string = symbol_mode_string[bit];
1067 if (0 == strcmp (input, string))
1069 symbol_mode = (1 << bit);
1073 /* should not be here */
1080 -1 : error, try something else */
1082 is_syscall (char *input, unsigned int *flag)
1088 char *syscall_string;
1091 { "svc" /* 0x01 */, XCOFF_SYSCALL32 },
1092 { "svc32" /* 0x02 */, XCOFF_SYSCALL32 },
1093 { "svc3264" /* 0x04 */, XCOFF_SYSCALL32 | XCOFF_SYSCALL64 },
1094 { "svc64" /* 0x08 */, XCOFF_SYSCALL64 },
1095 { "syscall" /* 0x10 */, XCOFF_SYSCALL32 },
1096 { "syscall32" /* 0x20 */, XCOFF_SYSCALL32 },
1097 { "syscall3264" /* 0x40 */, XCOFF_SYSCALL32 | XCOFF_SYSCALL64 },
1098 { "syscall64" /* 0x80 */, XCOFF_SYSCALL64 },
1104 for (bit = 0;; bit++)
1106 string = s[bit].syscall_string;
1110 if (0 == strcmp (input, string))
1112 if (1 << bit & syscall_mask)
1114 *flag = s[bit].flag;
1123 /* should not be here */
1127 /* Read an import or export file. For an import file, this is called
1128 by the before_allocation emulation routine. For an export file,
1129 this is called by the handle_option emulation routine. */
1132 gld${EMULATION_NAME}_read_file (const char *filename, bfd_boolean import)
1139 const char *imppath;
1140 const char *impfile;
1141 const char *impmember;
1143 o = (struct obstack *) xmalloc (sizeof (struct obstack));
1144 obstack_specify_allocation (o, 0, 0, xmalloc, gld${EMULATION_NAME}_free);
1146 f = fopen (filename, FOPEN_RT);
1149 bfd_set_error (bfd_error_system_call);
1150 einfo ("%F%P: %s: %E\n", filename);
1162 /* Default to 32 and 64 bit mode
1163 symbols at top of /lib/syscalls.exp do not have a mode modifier and they
1164 are not repeated, assume 64 bit routines also want to use them.
1165 See the routine change_symbol_mode for more information. */
1169 while ((c = getc (f)) != EOF)
1173 unsigned int syscall_flag = 0;
1175 struct bfd_link_hash_entry *h;
1179 obstack_1grow (o, c);
1183 obstack_1grow (o, '\0');
1186 s = (char *) obstack_base (o);
1187 while (ISSPACE (*s))
1191 || change_symbol_mode (s)
1192 || (*s == '#' && s[1] == ' ')
1193 || (!import && *s == '#' && s[1] == '!'))
1195 obstack_free (o, obstack_base (o));
1199 if (*s == '#' && s[1] == '!')
1202 while (ISSPACE (*s))
1209 obstack_free (o, obstack_base (o));
1212 einfo (_("%F%P:%s:%d: #! ([member]) is not supported "
1213 "in import files\n"),
1220 (void) obstack_finish (o);
1223 while (!ISSPACE (*s) && *s != '(' && *s != '\0')
1227 if (!bfd_xcoff_split_import_path (link_info.output_bfd,
1228 start, &imppath, &impfile))
1229 einfo (_("%F%P: could not parse import path: %E\n"));
1230 while (ISSPACE (cs))
1239 einfo (_("%P:%s:%d: warning: syntax error in import file\n"),
1246 while (*s != ')' && *s != '\0')
1251 einfo (_("%P:%s:%d: warning: syntax error in import file\n"),
1259 if (symbol_mode & symbol_mode_mask)
1261 /* This is a symbol to be imported or exported. */
1264 address = (bfd_vma) -1;
1266 while (!ISSPACE (*s) && *s != '\0')
1274 while (ISSPACE (*s))
1278 while (!ISSPACE (*se) && *se != '\0')
1283 while (ISSPACE (*se))
1286 einfo (_("%P:%s%d: warning: syntax error in "
1287 "import/export file\n"),
1296 status = is_syscall (s, &syscall_flag);
1300 /* not a system call, check for address */
1301 address = bfd_scan_vma (s, &end, 0);
1304 einfo (_("%P:%s:%d: warning: syntax error in "
1305 "import/export file\n"),
1315 struct export_symbol_list *n;
1317 ldlang_add_undef (symname, TRUE);
1318 n = ((struct export_symbol_list *)
1319 xmalloc (sizeof (struct export_symbol_list)));
1320 n->next = export_symbols;
1321 n->name = xstrdup (symname);
1326 h = bfd_link_hash_lookup (link_info.hash, symname, FALSE, FALSE,
1328 if (h == NULL || h->type == bfd_link_hash_new)
1330 /* We can just ignore attempts to import an unreferenced
1335 if (!bfd_xcoff_import_symbol (link_info.output_bfd,
1337 address, imppath, impfile,
1338 impmember, syscall_flag))
1339 einfo (_("%X%P:%s:%d: failed to import symbol %s: %E\n"),
1340 filename, lineno, symname);
1344 obstack_free (o, obstack_base (o));
1347 if (obstack_object_size (o) > 0)
1349 einfo (_("%P:%s:%d: warning: ignoring unterminated last line\n"),
1351 obstack_free (o, obstack_base (o));
1356 obstack_free (o, NULL);
1363 /* This routine saves us from worrying about declaring free. */
1366 gld${EMULATION_NAME}_free (void *p)
1371 /* This is called by the before_allocation routine via
1372 lang_for_each_statement. It looks for relocations and assignments
1376 gld${EMULATION_NAME}_find_relocs (lang_statement_union_type *s)
1378 if (s->header.type == lang_reloc_statement_enum)
1380 lang_reloc_statement_type *rs;
1382 rs = &s->reloc_statement;
1383 if (rs->name == NULL)
1384 einfo (_("%F%P: only relocations against symbols are permitted\n"));
1385 if (!bfd_xcoff_link_count_reloc (link_info.output_bfd, &link_info,
1387 einfo (_("%F%P: bfd_xcoff_link_count_reloc failed: %E\n"));
1390 if (s->header.type == lang_assignment_statement_enum)
1391 gld${EMULATION_NAME}_find_exp_assignment (s->assignment_statement.exp);
1394 /* Look through an expression for an assignment statement. */
1397 gld${EMULATION_NAME}_find_exp_assignment (etree_type *exp)
1399 struct bfd_link_hash_entry *h;
1401 switch (exp->type.node_class)
1404 case etree_provided:
1405 h = bfd_link_hash_lookup (link_info.hash, exp->assign.dst,
1406 FALSE, FALSE, FALSE);
1411 if (strcmp (exp->assign.dst, ".") != 0)
1413 if (!bfd_xcoff_record_link_assignment (link_info.output_bfd,
1416 einfo (_("%F%P: failed to record assignment to %s: %E\n"),
1419 gld${EMULATION_NAME}_find_exp_assignment (exp->assign.src);
1423 gld${EMULATION_NAME}_find_exp_assignment (exp->binary.lhs);
1424 gld${EMULATION_NAME}_find_exp_assignment (exp->binary.rhs);
1428 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.cond);
1429 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.lhs);
1430 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.rhs);
1434 gld${EMULATION_NAME}_find_exp_assignment (exp->unary.child);
1443 gld${EMULATION_NAME}_get_script (int *isfile)
1446 if test x"$COMPILE_IN" = xyes
1448 # Scripts compiled in.
1450 # sed commands to quote an ld script as a C string.
1451 sc="-f ${srcdir}/emultempl/ostring.sed"
1457 if (bfd_link_relocatable (&link_info) && config.build_constructors)
1460 sed $sc ldscripts/${EMULATION_NAME}.xu >> e${EMULATION_NAME}.c
1461 echo ' ; else if (bfd_link_relocatable (&link_info)) return' >> e${EMULATION_NAME}.c
1462 sed $sc ldscripts/${EMULATION_NAME}.xr >> e${EMULATION_NAME}.c
1463 echo ' ; else if (!config.text_read_only) return' >> e${EMULATION_NAME}.c
1464 sed $sc ldscripts/${EMULATION_NAME}.xbn >> e${EMULATION_NAME}.c
1465 echo ' ; else if (!config.magic_demand_paged) return' >> e${EMULATION_NAME}.c
1466 sed $sc ldscripts/${EMULATION_NAME}.xn >> e${EMULATION_NAME}.c
1467 echo ' ; else return' >> e${EMULATION_NAME}.c
1468 sed $sc ldscripts/${EMULATION_NAME}.x >> e${EMULATION_NAME}.c
1469 echo '; }' >> e${EMULATION_NAME}.c
1472 # Scripts read from the filesystem.
1478 if (bfd_link_relocatable (&link_info) && config.build_constructors)
1479 return "ldscripts/${EMULATION_NAME}.xu";
1480 else if (bfd_link_relocatable (&link_info))
1481 return "ldscripts/${EMULATION_NAME}.xr";
1482 else if (!config.text_read_only)
1483 return "ldscripts/${EMULATION_NAME}.xbn";
1484 else if (!config.magic_demand_paged)
1485 return "ldscripts/${EMULATION_NAME}.xn";
1487 return "ldscripts/${EMULATION_NAME}.x";
1496 gld${EMULATION_NAME}_create_output_section_statements (void)
1499 if ((bfd_get_flavour (link_info.output_bfd) == bfd_target_xcoff_flavour)
1500 && (link_info.init_function != NULL
1501 || link_info.fini_function != NULL
1504 initfini_file = lang_add_input_file ("initfini",
1505 lang_input_file_is_file_enum,
1508 initfini_file->the_bfd = bfd_create ("initfini", link_info.output_bfd);
1509 if (initfini_file->the_bfd == NULL
1510 || ! bfd_set_arch_mach (initfini_file->the_bfd,
1511 bfd_get_arch (link_info.output_bfd),
1512 bfd_get_mach (link_info.output_bfd)))
1514 einfo (_("%F%P: can not create BFD: %E\n"));
1518 /* Call backend to fill in the rest */
1519 if (! bfd_xcoff_link_generate_rtinit (initfini_file->the_bfd,
1520 link_info.init_function,
1521 link_info.fini_function,
1524 einfo (_("%F%P: can not create BFD: %E\n"));
1528 /* __rtld defined in /lib/librtl.a */
1530 lang_add_input_file ("rtl", lang_input_file_is_l_enum, NULL);
1535 gld${EMULATION_NAME}_set_output_arch (void)
1537 bfd_set_arch_mach (link_info.output_bfd,
1538 bfd_xcoff_architecture (link_info.output_bfd),
1539 bfd_xcoff_machine (link_info.output_bfd));
1541 ldfile_output_architecture = bfd_get_arch (link_info.output_bfd);
1542 ldfile_output_machine = bfd_get_mach (link_info.output_bfd);
1543 ldfile_output_machine_name = bfd_printable_name (link_info.output_bfd);
1547 gld${EMULATION_NAME}_open_dynamic_archive (const char *arch,
1548 search_dirs_type *search,
1549 lang_input_statement_type *entry)
1553 if (!entry->flags.maybe_archive)
1556 if (entry->flags.full_name_provided)
1557 path = concat (search->name, "/", entry->filename,
1558 (const char *) NULL);
1560 path = concat (search->name, "/lib", entry->filename, arch, ".a",
1561 (const char *) NULL);
1563 if (!ldfile_try_open_bfd (path, entry))
1568 /* Don't include the searched directory in the import path. */
1569 bfd_xcoff_set_archive_import_path (&link_info, entry->the_bfd,
1570 path + strlen (search->name) + 1);
1571 entry->filename = path;
1576 gld${EMULATION_NAME}_print_symbol (struct bfd_link_hash_entry *hash_entry,
1579 asection *sec = (asection *) ptr;
1581 if ((hash_entry->type == bfd_link_hash_defined
1582 || hash_entry->type == bfd_link_hash_defweak)
1583 && sec == hash_entry->u.def.section)
1586 struct xcoff_link_hash_entry *h;
1588 for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
1591 (hash_entry->u.def.value
1592 + hash_entry->u.def.section->output_offset
1593 + hash_entry->u.def.section->output_section->vma));
1595 /* Flag symbol if it has been garbage collected. */
1596 h = (struct xcoff_link_hash_entry *) hash_entry;
1597 if ((h != NULL) && !(h->flags & XCOFF_MARK))
1599 minfo (" %pT\n", hash_entry->root.string);
1605 struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation = {
1606 gld${EMULATION_NAME}_before_parse,
1609 after_parse_default,
1610 gld${EMULATION_NAME}_after_open,
1611 after_check_relocs_default,
1612 before_place_orphans_default,
1613 after_allocation_default,
1614 gld${EMULATION_NAME}_set_output_arch,
1615 gld${EMULATION_NAME}_choose_target,
1616 gld${EMULATION_NAME}_before_allocation,
1617 gld${EMULATION_NAME}_get_script,
1618 "${EMULATION_NAME}",
1621 gld${EMULATION_NAME}_create_output_section_statements,
1622 gld${EMULATION_NAME}_open_dynamic_archive,
1623 0, /* place_orphan */
1624 0, /* set_symbols */
1625 gld${EMULATION_NAME}_parse_args,
1626 gld${EMULATION_NAME}_add_options,
1627 gld${EMULATION_NAME}_handle_option,
1628 gld${EMULATION_NAME}_unrecognized_file,
1629 NULL, /* list_options */
1630 NULL, /* recognized_file */
1631 NULL, /* find potential_libraries */
1632 NULL, /* new_vers_pattern */
1633 NULL, /* extra_map_file_text */
1634 ${LDEMUL_EMIT_CTF_EARLY-NULL},
1635 ${LDEMUL_ACQUIRE_STRINGS_FOR_CTF-NULL},
1636 ${LDEMUL_NEW_DYNSYM_FOR_CTF-NULL},
1637 gld${EMULATION_NAME}_print_symbol