1 /* This file is is generated by a shell script. DO NOT EDIT! */
3 /* AIX emulation code for ppcmacos
4 Copyright (C) 1991, 1993, 1995 Free Software Foundation, Inc.
5 Written by Steve Chamberlain <sac@cygnus.com>
6 AIX support by Ian Lance Taylor <ian@cygnus.com>
8 This file is part of GLD, the Gnu Linker.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
24 #define TARGET_IS_ppcmacos
28 #include "libiberty.h"
44 static void gldppcmacos_before_parse
PARAMS ((void));
45 static int gldppcmacos_parse_args
PARAMS ((int, char **));
46 static void gldppcmacos_after_open
PARAMS ((void));
47 static void gldppcmacos_before_allocation
PARAMS ((void));
48 static void gldppcmacos_read_file
PARAMS ((const char *, boolean
));
49 static void gldppcmacos_free
PARAMS ((PTR
));
50 static void gldppcmacos_find_relocs
51 PARAMS ((lang_statement_union_type
*));
52 static void gldppcmacos_find_exp_assignment
PARAMS ((etree_type
*));
53 static char *gldppcmacos_get_script
PARAMS ((int *isfile
));
55 /* The file alignment required for each section. */
56 static unsigned long file_align
;
58 /* The maximum size the stack is permitted to grow. This is stored in
60 static unsigned long maxstack
;
62 /* The maximum data size. This is stored in the a.out header. */
63 static unsigned long maxdata
;
65 /* Whether to perform garbage collection. */
68 /* The module type to use. */
69 static unsigned short modtype
= ('1' << 8) | 'L';
71 /* Whether the .text section must be read-only (i.e., no relocs
75 /* Structure used to hold import file list. */
79 struct filelist
*next
;
83 /* List of import files. */
84 static struct filelist
*import_files
;
86 /* List of export symbols read from the export files. */
88 struct export_symbol_list
90 struct export_symbol_list
*next
;
95 static struct export_symbol_list
*export_symbols
;
97 /* This routine is called before anything else is done. */
100 gldppcmacos_before_parse()
102 #ifndef TARGET_ /* I.e., if not generic. */
103 ldfile_output_architecture
= bfd_arch_powerpc
;
104 #endif /* not TARGET_ */
107 /* Handle AIX specific options. */
110 gldppcmacos_parse_args (argc
, argv
)
114 int prevoptind
= optind
;
115 int prevopterr
= opterr
;
122 #define OPTION_IGNORE (300)
123 #define OPTION_AUTOIMP (OPTION_IGNORE + 1)
124 #define OPTION_ERNOTOK (OPTION_AUTOIMP + 1)
125 #define OPTION_EROK (OPTION_ERNOTOK + 1)
126 #define OPTION_EXPORT (OPTION_EROK + 1)
127 #define OPTION_IMPORT (OPTION_EXPORT + 1)
128 #define OPTION_LOADMAP (OPTION_IMPORT + 1)
129 #define OPTION_MAXDATA (OPTION_LOADMAP + 1)
130 #define OPTION_MAXSTACK (OPTION_MAXDATA + 1)
131 #define OPTION_MODTYPE (OPTION_MAXSTACK + 1)
132 #define OPTION_NOAUTOIMP (OPTION_MODTYPE + 1)
133 #define OPTION_NOSTRCMPCT (OPTION_NOAUTOIMP + 1)
134 #define OPTION_PD (OPTION_NOSTRCMPCT + 1)
135 #define OPTION_PT (OPTION_PD + 1)
136 #define OPTION_STRCMPCT (OPTION_PT + 1)
138 static struct option longopts
[] = {
139 {"basis", no_argument
, NULL
, OPTION_IGNORE
},
140 {"bautoimp", no_argument
, NULL
, OPTION_AUTOIMP
},
141 {"bcomprld", no_argument
, NULL
, OPTION_IGNORE
},
142 {"bcrld", no_argument
, NULL
, OPTION_IGNORE
},
143 {"bcror31", no_argument
, NULL
, OPTION_IGNORE
},
144 {"bD", required_argument
, NULL
, OPTION_MAXDATA
},
145 {"bE", required_argument
, NULL
, OPTION_EXPORT
},
146 {"bernotok", no_argument
, NULL
, OPTION_ERNOTOK
},
147 {"berok", no_argument
, NULL
, OPTION_EROK
},
148 {"berrmsg", no_argument
, NULL
, OPTION_IGNORE
},
149 {"bexport", required_argument
, NULL
, OPTION_EXPORT
},
150 {"bf", no_argument
, NULL
, OPTION_ERNOTOK
},
151 {"bgc", no_argument
, &gc
, 1},
152 {"bh", required_argument
, NULL
, OPTION_IGNORE
},
153 {"bhalt", required_argument
, NULL
, OPTION_IGNORE
},
154 {"bI", required_argument
, NULL
, OPTION_IMPORT
},
155 {"bimport", required_argument
, NULL
, OPTION_IMPORT
},
156 {"bl", required_argument
, NULL
, OPTION_LOADMAP
},
157 {"bloadmap", required_argument
, NULL
, OPTION_LOADMAP
},
158 {"bmaxdata", required_argument
, NULL
, OPTION_MAXDATA
},
159 {"bmaxstack", required_argument
, NULL
, OPTION_MAXSTACK
},
160 {"bM", required_argument
, NULL
, OPTION_MODTYPE
},
161 {"bmodtype", required_argument
, NULL
, OPTION_MODTYPE
},
162 {"bnoautoimp", no_argument
, NULL
, OPTION_NOAUTOIMP
},
163 {"bnodelcsect", no_argument
, NULL
, OPTION_IGNORE
},
164 {"bnogc", no_argument
, &gc
, 0},
165 {"bnso", no_argument
, NULL
, OPTION_NOAUTOIMP
},
166 {"bnostrcmpct", no_argument
, NULL
, OPTION_NOSTRCMPCT
},
167 {"bnotextro", no_argument
, &textro
, 0},
168 {"bnro", no_argument
, &textro
, 0},
169 {"bpD", required_argument
, NULL
, OPTION_PD
},
170 {"bpT", required_argument
, NULL
, OPTION_PT
},
171 {"bro", no_argument
, &textro
, 1},
172 {"bS", required_argument
, NULL
, OPTION_MAXSTACK
},
173 {"bso", no_argument
, NULL
, OPTION_AUTOIMP
},
174 {"bstrcmpct", no_argument
, NULL
, OPTION_STRCMPCT
},
175 {"btextro", no_argument
, &textro
, 1},
176 {"static", no_argument
, NULL
, OPTION_NOAUTOIMP
},
177 {NULL
, no_argument
, NULL
, 0}
180 /* Options supported by the AIX linker which we do not support: -f,
181 -S, -v, -Z, -bbindcmds, -bbinder, -bbindopts, -bcalls, -bcaps,
182 -bcror15, -bdebugopt, -bdbg, -bdelcsect, -bex?, -bfilelist, -bfl,
183 -bgcbypass, -bglink, -binsert, -bi, -bloadmap, -bl, -bmap, -bnl,
184 -bnobind, -bnocomprld, -bnocrld, -bnoerrmsg, -bnoglink,
185 -bnoloadmap, -bnl, -bnoobjreorder, -bnoquiet, -bnoreorder,
186 -bnotypchk, -bnox, -bquiet, -bR, -brename, -breorder, -btypchk,
189 /* If the current option starts with -b, change the first : to an =.
190 The AIX linker uses : to separate the option from the argument;
191 changing it to = lets us treat it as a getopt option. */
195 if (indx
< argc
&& strncmp (argv
[indx
], "-b", 2) == 0)
199 for (s
= argv
[indx
]; *s
!= '\0'; s
++)
210 optc
= getopt_long_only (argc
, argv
, "-D:H:KT:z", longopts
, &longind
);
220 /* Long option which just sets a flag. */
224 val
= strtol (optarg
, &end
, 0);
226 einfo ("%P: warning: ignoring invalid -D number %s\n", optarg
);
228 lang_section_start (".data", exp_intop (val
));
232 val
= strtoul (optarg
, &end
, 0);
234 || (val
& (val
- 1)) != 0)
235 einfo ("%P: warning: ignoring invalid -H number %s\n", optarg
);
242 /* FIXME: This should use the page size for the target system. */
247 /* On AIX this is the same as GNU ld -Ttext. When we see -T
248 number, we assume the AIX option is intended. Otherwise, we
249 assume the usual GNU ld -T option is intended. We can't just
250 ignore the AIX option, because gcc passes it to the linker. */
251 val
= strtoul (optarg
, &end
, 0);
257 lang_section_start (".text", exp_intop (val
));
264 link_info
.static_link
= false;
268 force_make_executable
= false;
272 force_make_executable
= true;
276 gldppcmacos_read_file (optarg
, false);
282 struct filelist
**flpp
;
284 n
= (struct filelist
*) xmalloc (sizeof (struct filelist
));
287 flpp
= &import_files
;
288 while (*flpp
!= NULL
)
289 flpp
= &(*flpp
)->next
;
295 config
.map_filename
= optarg
;
299 val
= strtoul (optarg
, &end
, 0);
301 einfo ("%P: warning: ignoring invalid -bmaxdata number %s\n",
307 case OPTION_MAXSTACK
:
308 val
= strtoul (optarg
, &end
, 0);
310 einfo ("%P: warning: ignoring invalid -bmaxstack number %s\n",
319 link_info
.shared
= true;
322 if (*optarg
== '\0' || optarg
[1] == '\0')
323 einfo ("%P: warning: ignoring invalid module type %s\n", optarg
);
325 modtype
= (*optarg
<< 8) | optarg
[1];
328 case OPTION_NOAUTOIMP
:
329 link_info
.static_link
= true;
332 case OPTION_NOSTRCMPCT
:
333 config
.traditional_format
= true;
337 /* This sets the page that the .data section is supposed to
338 start on. The offset within the page should still be the
339 offset within the file, so we need to build an appropriate
341 val
= strtoul (optarg
, &end
, 0);
343 einfo ("%P: warning: ignoring invalid -pD number %s\n", optarg
);
351 exp_nameop (NAME
, "."),
354 exp_binop ('+', t
, exp_intop (7)),
355 exp_intop (~ (bfd_vma
) 7));
356 lang_section_start (".data", t
);
361 /* This set the page that the .text section is supposed to start
362 on. The offset within the page should still be the offset
364 val
= strtoul (optarg
, &end
, 0);
366 einfo ("%P: warning: ignoring invalid -pT number %s\n", optarg
);
373 exp_nameop (SIZEOF_HEADERS
, NULL
));
375 exp_binop ('+', t
, exp_intop (7)),
376 exp_intop (~ (bfd_vma
) 7));
377 lang_section_start (".text", t
);
381 case OPTION_STRCMPCT
:
382 config
.traditional_format
= false;
389 /* This is called after the input files have been opened. */
392 gldppcmacos_after_open ()
397 /* Call ldctor_build_sets, after pretending that this is a
398 relocateable link. We do this because AIX requires relocation
399 entries for all references to symbols, even in a final
401 r
= link_info
.relocateable
;
402 link_info
.relocateable
= true;
403 ldctor_build_sets ();
404 link_info
.relocateable
= r
;
406 /* For each set, record the size, so that the XCOFF backend can
407 output the correct csect length. */
408 for (p
= sets
; p
!= (struct set_info
*) NULL
; p
= p
->next
)
412 /* If the symbol is defined, we may have been invoked from
413 collect, and the sets may already have been built, so we do
415 if (p
->h
->type
== bfd_link_hash_defined
416 || p
->h
->type
== bfd_link_hash_defweak
)
419 if (p
->reloc
!= BFD_RELOC_CTOR
)
421 /* Handle this if we need to. */
425 size
= (p
->count
+ 2) * 4;
426 if (! bfd_xcoff_link_record_set (output_bfd
, &link_info
, p
->h
, size
))
427 einfo ("%F%P: bfd_xcoff_link_record_set failed: %E\n");
431 /* This is called after the sections have been attached to output
432 sections, but before any sizes or addresses have been set. */
435 gldppcmacos_before_allocation ()
438 struct export_symbol_list
*el
;
440 asection
*special_sections
[6];
443 /* Handle the import and export files, if any. */
444 for (fl
= import_files
; fl
!= NULL
; fl
= fl
->next
)
445 gldppcmacos_read_file (fl
->name
, true);
446 for (el
= export_symbols
; el
!= NULL
; el
= el
->next
)
448 struct bfd_link_hash_entry
*h
;
450 h
= bfd_link_hash_lookup (link_info
.hash
, el
->name
, false, false, false);
452 einfo ("%P%F: bfd_link_hash_lookup of export symbol failed: %E\n");
453 if (! bfd_xcoff_export_symbol (output_bfd
, &link_info
, h
, el
->syscall
))
454 einfo ("%P%F: bfd_xcoff_export_symbol failed: %E\n");
457 /* Track down all relocations called for by the linker script (these
458 are typically constructor/destructor entries created by
459 CONSTRUCTORS) and let the backend know it will need to create
460 .loader relocs for them. */
461 lang_for_each_statement (gldppcmacos_find_relocs
);
463 /* We need to build LIBPATH from the -L arguments. If any -rpath
464 arguments were used, though, we use -rpath instead, as a GNU
466 if (command_line
.rpath
!= NULL
)
467 libpath
= command_line
.rpath
;
468 else if (search_head
== NULL
)
469 libpath
= (char *) "";
473 search_dirs_type
*search
;
475 len
= strlen (search_head
->name
);
476 libpath
= xmalloc (len
+ 1);
477 strcpy (libpath
, search_head
->name
);
478 for (search
= search_head
->next
; search
!= NULL
; search
= search
->next
)
482 nlen
= strlen (search
->name
);
483 libpath
= xrealloc (libpath
, len
+ nlen
+ 2);
485 strcpy (libpath
+ len
+ 1, search
->name
);
490 /* Let the XCOFF backend set up the .loader section. */
491 if (! bfd_xcoff_size_dynamic_sections (output_bfd
, &link_info
, libpath
,
492 entry_symbol
, file_align
,
496 textro
? true : false,
498 einfo ("%P%F: failed to set dynamic section sizes: %E\n");
500 /* Look through the special sections, and put them in the right
501 place in the link ordering. This is especially magic. */
502 for (i
= 0; i
< 6; i
++)
505 lang_output_section_statement_type
*os
;
506 lang_statement_union_type
**pls
;
507 lang_input_section_type
*is
;
511 sec
= special_sections
[i
];
515 /* Remove this section from the list of the output section.
516 This assumes we know what the script looks like. */
518 os
= lang_output_section_find (sec
->output_section
->name
);
520 einfo ("%P%F: can't find output section %s\n",
521 sec
->output_section
->name
);
522 for (pls
= &os
->children
.head
; *pls
!= NULL
; pls
= &(*pls
)->next
)
524 if ((*pls
)->header
.type
== lang_input_section_enum
525 && (*pls
)->input_section
.section
== sec
)
527 is
= (lang_input_section_type
*) *pls
;
531 if ((*pls
)->header
.type
== lang_wild_statement_enum
)
533 lang_statement_union_type
**pwls
;
535 for (pwls
= &(*pls
)->wild_statement
.children
.head
;
537 pwls
= &(*pwls
)->next
)
539 if ((*pwls
)->header
.type
== lang_input_section_enum
540 && (*pwls
)->input_section
.section
== sec
)
542 is
= (lang_input_section_type
*) *pwls
;
543 *pwls
= (*pwls
)->next
;
553 einfo ("%P%F: can't find %s in output section\n",
554 bfd_get_section_name (sec
->owner
, sec
));
556 /* Now figure out where the section should go. */
559 default: /* to avoid warnings */
588 os
= lang_output_section_find (oname
);
592 is
->header
.next
= os
->children
.head
;
593 os
->children
.head
= (lang_statement_union_type
*) is
;
597 is
->header
.next
= NULL
;
598 lang_statement_append (&os
->children
,
599 (lang_statement_union_type
*) is
,
605 /* Read an import or export file. For an import file, this is called
606 by the before_allocation emulation routine. For an export file,
607 this is called by the parse_args emulation routine. */
610 gldppcmacos_read_file (filename
, import
)
611 const char *filename
;
621 const char *impmember
;
623 o
= (struct obstack
*) xmalloc (sizeof (struct obstack
));
624 obstack_specify_allocation (o
, 0, 0, xmalloc
, gldppcmacos_free
);
626 f
= fopen (filename
, "r");
629 bfd_set_error (bfd_error_system_call
);
630 einfo ("%F%s: %E\n", filename
);
640 while ((c
= getc (f
)) != EOF
)
646 struct bfd_link_hash_entry
*h
;
650 obstack_1grow (o
, c
);
654 obstack_1grow (o
, '\0');
657 s
= (char *) obstack_base (o
);
658 while (isspace ((unsigned char) *s
))
662 || (*s
== '#' && s
[1] == ' ')
663 || (! import
&& *s
== '#' && s
[1] == '!'))
665 obstack_free (o
, obstack_base (o
));
669 if (*s
== '#' && s
[1] == '!')
672 while (isspace ((unsigned char) *s
))
679 obstack_free (o
, obstack_base (o
));
682 einfo ("%F%s%d: #! ([member]) is not supported in import files",
689 (void) obstack_finish (o
);
693 while (! isspace ((unsigned char) *s
) && *s
!= '(' && *s
!= '\0')
703 if (imppath
== file
- 1)
713 while (isspace ((unsigned char) cs
))
722 einfo ("%s:%d: warning: syntax error in import file\n",
729 while (*s
!= ')' && *s
!= '\0')
734 einfo ("%s:%d: warning: syntax error in import file\n",
742 /* This is a symbol to be imported or exported. */
745 address
= (bfd_vma
) -1;
747 while (! isspace ((unsigned char) *s
) && *s
!= '\0')
755 while (isspace ((unsigned char) *s
))
759 while (! isspace ((unsigned char) *se
) && *se
!= '\0')
764 while (isspace ((unsigned char) *se
))
767 einfo ("%s%d: warning: syntax error in import/export file\n",
771 if (strcasecmp (s
, "svc") == 0
772 || strcasecmp (s
, "syscall") == 0)
778 address
= strtoul (s
, &end
, 0);
780 einfo ("%s:%d: warning: syntax error in import/export file\n",
787 struct export_symbol_list
*n
;
789 ldlang_add_undef (symname
);
790 n
= ((struct export_symbol_list
*)
791 xmalloc (sizeof (struct export_symbol_list
)));
792 n
->next
= export_symbols
;
793 n
->name
= buystring (symname
);
794 n
->syscall
= syscall
;
799 h
= bfd_link_hash_lookup (link_info
.hash
, symname
, false, false,
801 if (h
== NULL
|| h
->type
== bfd_link_hash_new
)
803 /* We can just ignore attempts to import an unreferenced
808 if (! bfd_xcoff_import_symbol (output_bfd
, &link_info
, h
,
809 address
, imppath
, impfile
,
811 einfo ("%X%s:%d: failed to import symbol %s: %E\n",
812 filename
, lineno
, symname
);
816 obstack_free (o
, obstack_base (o
));
819 if (obstack_object_size (o
) > 0)
821 einfo ("%s:%d: warning: ignoring unterminated last line\n",
823 obstack_free (o
, obstack_base (o
));
828 obstack_free (o
, NULL
);
833 /* This routine saves us from worrying about declaring free. */
842 /* This is called by the before_allocation routine via
843 lang_for_each_statement. It looks for relocations and assignments
847 gldppcmacos_find_relocs (s
)
848 lang_statement_union_type
*s
;
850 if (s
->header
.type
== lang_reloc_statement_enum
)
852 lang_reloc_statement_type
*rs
;
854 rs
= &s
->reloc_statement
;
855 if (rs
->name
== NULL
)
856 einfo ("%F%P: only relocations against symbols are permitted\n");
857 if (! bfd_xcoff_link_count_reloc (output_bfd
, &link_info
, rs
->name
))
858 einfo ("%F%P: bfd_xcoff_link_count_reloc failed: %E\n");
861 if (s
->header
.type
== lang_assignment_statement_enum
)
862 gldppcmacos_find_exp_assignment (s
->assignment_statement
.exp
);
865 /* Look through an expression for an assignment statement. */
868 gldppcmacos_find_exp_assignment (exp
)
871 struct bfd_link_hash_entry
*h
;
873 switch (exp
->type
.node_class
)
876 h
= bfd_link_hash_lookup (link_info
.hash
, exp
->assign
.dst
,
877 false, false, false);
882 if (strcmp (exp
->assign
.dst
, ".") != 0)
884 if (! bfd_xcoff_record_link_assignment (output_bfd
, &link_info
,
886 einfo ("%P%F: failed to record assignment to %s: %E\n",
889 gldppcmacos_find_exp_assignment (exp
->assign
.src
);
893 gldppcmacos_find_exp_assignment (exp
->binary
.lhs
);
894 gldppcmacos_find_exp_assignment (exp
->binary
.rhs
);
898 gldppcmacos_find_exp_assignment (exp
->trinary
.cond
);
899 gldppcmacos_find_exp_assignment (exp
->trinary
.lhs
);
900 gldppcmacos_find_exp_assignment (exp
->trinary
.rhs
);
904 gldppcmacos_find_exp_assignment (exp
->unary
.child
);
913 gldppcmacos_get_script(isfile
)
918 if (link_info
.relocateable
== true && config
.build_constructors
== true)
920 "OUTPUT_FORMAT(\"xcoff-powermac\")\n\
921 OUTPUT_ARCH(powerpc)\n\
925 .pad 0 : { *(.pad) }\n\
961 ; else if (link_info
.relocateable
== true) return
962 "OUTPUT_FORMAT(\"xcoff-powermac\")\n\
963 OUTPUT_ARCH(powerpc)\n\
967 .pad 0 : { *(.pad) }\n\
1002 ; else if (!config
.text_read_only
) return
1003 "OUTPUT_FORMAT(\"xcoff-powermac\")\n\
1004 OUTPUT_ARCH(powerpc)\n\
1005 SEARCH_DIR(/usr/local/powerpc-apple-macos/lib);\n\
1009 .pad 0 : { *(.pad) }\n\
1011 PROVIDE (_text = .);\n\
1020 PROVIDE (_etext = .);\n\
1023 PROVIDE (_data = .);\n\
1034 PROVIDE (_edata = .);\n\
1041 PROVIDE (_end = .);\n\
1042 PROVIDE (end = .);\n\
1051 ; else if (!config
.magic_demand_paged
) return
1052 "OUTPUT_FORMAT(\"xcoff-powermac\")\n\
1053 OUTPUT_ARCH(powerpc)\n\
1054 SEARCH_DIR(/usr/local/powerpc-apple-macos/lib);\n\
1058 .pad 0 : { *(.pad) }\n\
1060 PROVIDE (_text = .);\n\
1069 PROVIDE (_etext = .);\n\
1072 PROVIDE (_data = .);\n\
1083 PROVIDE (_edata = .);\n\
1090 PROVIDE (_end = .);\n\
1091 PROVIDE (end = .);\n\
1101 "OUTPUT_FORMAT(\"xcoff-powermac\")\n\
1102 OUTPUT_ARCH(powerpc)\n\
1103 SEARCH_DIR(/usr/local/powerpc-apple-macos/lib);\n\
1107 .pad 0 : { *(.pad) }\n\
1109 PROVIDE (_text = .);\n\
1118 PROVIDE (_etext = .);\n\
1121 PROVIDE (_data = .);\n\
1132 PROVIDE (_edata = .);\n\
1139 PROVIDE (_end = .);\n\
1140 PROVIDE (end = .);\n\
1151 struct ld_emulation_xfer_struct ld_ppcmacos_emulation
=
1153 gldppcmacos_before_parse
,
1156 after_parse_default
,
1157 gldppcmacos_after_open
,
1158 after_allocation_default
,
1159 set_output_arch_default
,
1160 ldemul_default_target
,
1161 gldppcmacos_before_allocation
,
1162 gldppcmacos_get_script
,
1166 0, /* create_output_section_statements */
1167 0, /* open_dynamic_archive */
1168 0, /* place_orphan */
1169 0, /* set_symbols */
1170 gldppcmacos_parse_args
,