-binitfini emulation, put __rtinit symbol generation into linker.
[deliverable/binutils-gdb.git] / ld / emultempl / aix.em
1 # This shell script emits a C file. -*- C -*-
2 # It does some substitutions.
3 if [ -z "$MACHINE" ]; then
4 OUTPUT_ARCH=${ARCH}
5 else
6 OUTPUT_ARCH=${ARCH}:${MACHINE}
7 fi
8 cat >e${EMULATION_NAME}.c <<EOF
9 /* This file is is generated by a shell script. DO NOT EDIT! */
10
11 /* AIX emulation code for ${EMULATION_NAME}
12 Copyright 1991, 1993, 1995, 1996, 1997, 1998, 2000, 2001
13 Free Software Foundation, Inc.
14 Written by Steve Chamberlain <sac@cygnus.com>
15 AIX support by Ian Lance Taylor <ian@cygnus.com>
16 AIX 64 bit support by Tom Rix <trix@redhat.com>
17
18 This file is part of GLD, the Gnu Linker.
19
20 This program is free software; you can redistribute it and/or modify
21 it under the terms of the GNU General Public License as published by
22 the Free Software Foundation; either version 2 of the License, or
23 (at your option) any later version.
24
25 This program is distributed in the hope that it will be useful,
26 but WITHOUT ANY WARRANTY; without even the implied warranty of
27 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 GNU General Public License for more details.
29
30 You should have received a copy of the GNU General Public License
31 along with this program; if not, write to the Free Software
32 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
33
34 #define TARGET_IS_${EMULATION_NAME}
35
36 #include "bfd.h"
37 #include "sysdep.h"
38 #include "libiberty.h"
39 #include "safe-ctype.h"
40 #include "getopt.h"
41 #include "obstack.h"
42 #include "bfdlink.h"
43
44 #include "ld.h"
45 #include "ldmain.h"
46 #include "ldmisc.h"
47 #include "ldexp.h"
48 #include "ldlang.h"
49 #include "ldfile.h"
50 #include "ldemul.h"
51 #include "ldctor.h"
52 #include "ldgram.h"
53
54 #include "coff/internal.h"
55 #include "coff/xcoff.h"
56
57 static void gld${EMULATION_NAME}_before_parse PARAMS ((void));
58 static int gld${EMULATION_NAME}_parse_args PARAMS ((int, char **));
59 static void gld${EMULATION_NAME}_after_open PARAMS ((void));
60 static char *choose_target PARAMS ((int, char **));
61 static void gld${EMULATION_NAME}_before_allocation PARAMS ((void));
62 static void gld${EMULATION_NAME}_read_file PARAMS ((const char *, boolean));
63 static void gld${EMULATION_NAME}_free PARAMS ((PTR));
64 static void gld${EMULATION_NAME}_find_relocs
65 PARAMS ((lang_statement_union_type *));
66 static void gld${EMULATION_NAME}_find_exp_assignment PARAMS ((etree_type *));
67 static char *gld${EMULATION_NAME}_get_script PARAMS ((int *isfile));
68 static boolean gld${EMULATION_NAME}_unrecognized_file
69 PARAMS ((lang_input_statement_type *));
70 static void gld${EMULATION_NAME}_create_output_section_statements
71 PARAMS((void));
72 static int is_syscall PARAMS ((char *, unsigned int *));
73 static int change_symbol_mode PARAMS ((char *));
74
75 /* The file alignment required for each section. */
76 static unsigned long file_align;
77
78 /* The maximum size the stack is permitted to grow. This is stored in
79 the a.out header. */
80 static unsigned long maxstack;
81
82 /* The maximum data size. This is stored in the a.out header. */
83 static unsigned long maxdata;
84
85 /* Whether to perform garbage collection. */
86 static int gc = 1;
87
88 /* The module type to use. */
89 static unsigned short modtype = ('1' << 8) | 'L';
90
91 /* Whether the .text section must be read-only (i.e., no relocs
92 permitted). */
93 static int textro;
94
95 /* Whether to implement Unix like linker semantics. */
96 static int unix_ld;
97
98 /* Structure used to hold import file list. */
99
100 struct filelist
101 {
102 struct filelist *next;
103 const char *name;
104 };
105
106 /* List of import files. */
107 static struct filelist *import_files;
108
109 /* List of export symbols read from the export files. */
110
111 struct export_symbol_list
112 {
113 struct export_symbol_list *next;
114 const char *name;
115 };
116
117 static struct export_symbol_list *export_symbols;
118
119 /* Maintains the 32 or 64 bit mode state of import file */
120 static unsigned int symbol_mode = 0x04;
121
122 /* Which symbol modes are valid */
123 static unsigned int symbol_mode_mask = 0x0d;
124
125 /* Whether this is a 64 bit link */
126 static int is_64bit = 0;
127
128 /* Which syscalls from import file are valid */
129 static unsigned int syscall_mask = 0x77;
130
131 /* fake file for -binitfini support */
132 static lang_input_statement_type *initfini_file;
133
134 /* This routine is called before anything else is done. */
135
136 static void
137 gld${EMULATION_NAME}_before_parse ()
138 {
139 #ifndef TARGET_ /* I.e., if not generic. */
140 const bfd_arch_info_type *arch = bfd_scan_arch ("${OUTPUT_ARCH}");
141 if (arch)
142 {
143 ldfile_output_architecture = arch->arch;
144 ldfile_output_machine = arch->mach;
145 ldfile_output_machine_name = arch->printable_name;
146 }
147 else
148 ldfile_output_architecture = bfd_arch_${ARCH};
149 #endif /* not TARGET_ */
150 config.has_shared = true;
151
152 /* The link_info.[init|fini]_functions are initialized in ld/lexsup.c.
153 Override them here so we can use the link_info.init_function as a
154 state flag that lets the backend know that -binitfini has been done. */
155
156 link_info.init_function = NULL;
157 link_info.fini_function = NULL;
158
159 }
160
161 /* Handle AIX specific options. */
162
163 static int
164 gld${EMULATION_NAME}_parse_args (argc, argv)
165 int argc;
166 char **argv;
167 {
168 int prevoptind = optind;
169 int prevopterr = opterr;
170 int indx;
171 int longind;
172 int optc;
173 bfd_signed_vma val;
174 char *end;
175
176 enum
177 {
178 OPTION_IGNORE = 300,
179 OPTION_AUTOIMP,
180 OPTION_ERNOTOK,
181 OPTION_EROK,
182 OPTION_EXPORT,
183 OPTION_IMPORT,
184 OPTION_INITFINI,
185 OPTION_LOADMAP,
186 OPTION_MAXDATA,
187 OPTION_MAXSTACK,
188 OPTION_MODTYPE,
189 OPTION_NOAUTOIMP,
190 OPTION_NOSTRCMPCT,
191 OPTION_PD,
192 OPTION_PT,
193 OPTION_STRCMPCT,
194 OPTION_UNIX,
195 OPTION_32,
196 OPTION_64,
197 };
198
199 /* -binitfini has special handling in the linker backend. The native linker
200 uses the arguemnts to generate a table of init and fini functions for
201 the executable. The important use for this option is to support aix 4.2+
202 c++ constructors and destructors. This is tied into gcc via collect2.c.
203
204 The function table is accessed by the runtime linker/loader by checking if
205 the first symbol in the loader symbol table is __rtinit. The gnu linker
206 generates this symbol and makes it the first loader symbol. */
207
208 static const struct option longopts[] = {
209 {"basis", no_argument, NULL, OPTION_IGNORE},
210 {"bautoimp", no_argument, NULL, OPTION_AUTOIMP},
211 {"bcomprld", no_argument, NULL, OPTION_IGNORE},
212 {"bcrld", no_argument, NULL, OPTION_IGNORE},
213 {"bcror31", no_argument, NULL, OPTION_IGNORE},
214 {"bD", required_argument, NULL, OPTION_MAXDATA},
215 {"bE", required_argument, NULL, OPTION_EXPORT},
216 {"bernotok", no_argument, NULL, OPTION_ERNOTOK},
217 {"berok", no_argument, NULL, OPTION_EROK},
218 {"berrmsg", no_argument, NULL, OPTION_IGNORE},
219 {"bexport", required_argument, NULL, OPTION_EXPORT},
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 {"bnodelcsect", no_argument, NULL, OPTION_IGNORE},
235 {"bnoentry", no_argument, NULL, OPTION_IGNORE},
236 {"bnogc", no_argument, &gc, 0},
237 {"bnso", no_argument, NULL, OPTION_NOAUTOIMP},
238 {"bnostrcmpct", no_argument, NULL, OPTION_NOSTRCMPCT},
239 {"bnotextro", no_argument, &textro, 0},
240 {"bnro", no_argument, &textro, 0},
241 {"bpD", required_argument, NULL, OPTION_PD},
242 {"bpT", required_argument, NULL, OPTION_PT},
243 {"bro", no_argument, &textro, 1},
244 {"bS", required_argument, NULL, OPTION_MAXSTACK},
245 {"bso", no_argument, NULL, OPTION_AUTOIMP},
246 {"bstrcmpct", no_argument, NULL, OPTION_STRCMPCT},
247 {"btextro", no_argument, &textro, 1},
248 {"b32", no_argument, NULL, OPTION_32},
249 {"b64", no_argument, NULL, OPTION_64},
250 {"static", no_argument, NULL, OPTION_NOAUTOIMP},
251 {"unix", no_argument, NULL, OPTION_UNIX},
252 {NULL, no_argument, NULL, 0}
253 };
254
255 /* Options supported by the AIX linker which we do not support: -f,
256 -S, -v, -Z, -bbindcmds, -bbinder, -bbindopts, -bcalls, -bcaps,
257 -bcror15, -bdebugopt, -bdbg, -bdelcsect, -bex?, -bfilelist, -bfl,
258 -bgcbypass, -bglink, -binsert, -bi, -bloadmap, -bl, -bmap, -bnl,
259 -bnobind, -bnocomprld, -bnocrld, -bnoerrmsg, -bnoglink,
260 -bnoloadmap, -bnl, -bnoobjreorder, -bnoquiet, -bnoreorder,
261 -bnotypchk, -bnox, -bquiet, -bR, -brename, -breorder, -btypchk,
262 -bx, -bX, -bxref. */
263
264 /* If the current option starts with -b, change the first : to an =.
265 The AIX linker uses : to separate the option from the argument;
266 changing it to = lets us treat it as a getopt option. */
267 indx = optind;
268 if (indx == 0)
269 indx = 1;
270
271 if (indx < argc && strncmp (argv[indx], "-b", 2) == 0)
272 {
273 char *s;
274
275 for (s = argv[indx]; *s != '\0'; s++)
276 {
277 if (*s == ':')
278 {
279 *s = '=';
280 break;
281 }
282 }
283 }
284
285
286 /* We add s and u so to the short options list so that -s and -u on
287 the command line do not match -static and -unix. */
288
289 opterr = 0;
290 optc = getopt_long_only (argc, argv, "-D:H:KT:zsu", longopts, &longind);
291 opterr = prevopterr;
292
293 switch (optc)
294 {
295 case 's':
296 case 'u':
297 default:
298 optind = prevoptind;
299 return 0;
300
301 case 0:
302 /* Long option which just sets a flag. */
303 break;
304
305 case 'D':
306 val = strtoll (optarg, &end, 0);
307 if (*end != '\0')
308 einfo ("%P: warning: ignoring invalid -D number %s\n", optarg);
309 else if (val != -1)
310 lang_section_start (".data", exp_intop (val));
311 break;
312
313 case 'H':
314 val = strtoul (optarg, &end, 0);
315 if (*end != '\0' || (val & (val - 1)) != 0)
316 einfo ("%P: warning: ignoring invalid -H number %s\n", optarg);
317 else
318 file_align = val;
319 break;
320
321 case 'K':
322 case 'z':
323 /* FIXME: This should use the page size for the target system. */
324 file_align = 4096;
325 break;
326
327 case 'T':
328 /* On AIX this is the same as GNU ld -Ttext. When we see -T
329 number, we assume the AIX option is intended. Otherwise, we
330 assume the usual GNU ld -T option is intended. We can't just
331 ignore the AIX option, because gcc passes it to the linker. */
332 val = strtoull (optarg, &end, 0);
333 if (*end != '\0')
334 {
335 optind = prevoptind;
336 return 0;
337 }
338 lang_section_start (".text", exp_intop (val));
339 break;
340
341 case OPTION_IGNORE:
342 break;
343
344 case OPTION_INITFINI:
345 {
346 /*
347 * The aix linker init fini has the format :
348 *
349 * -binitfini:[ Initial][:Termination][:Priority]
350 *
351 * it allows the Termination and Priority to be optional.
352 *
353 * Since we support only one init/fini pair, we ignore the Priority.
354 *
355 * Define the special symbol __rtinit.
356 *
357 * strtok does not correctly handle the case of -binitfini::fini: so
358 * do it by hand
359 */
360 char *t, *i, *f;
361
362 i = t = optarg;
363 while (*t && ':' != *t)
364 t++;
365 if (*t)
366 *t++ = 0;
367
368 if (0 != strlen (i))
369 link_info.init_function = i;
370
371 f = t;
372 while (*t && ':' != *t)
373 t++;
374 *t = 0;
375
376 if (0 != strlen (f))
377 link_info.fini_function = f;
378 }
379 break;
380
381 case OPTION_AUTOIMP:
382 link_info.static_link = false;
383 break;
384
385 case OPTION_ERNOTOK:
386 force_make_executable = false;
387 break;
388
389 case OPTION_EROK:
390 force_make_executable = true;
391 break;
392
393 case OPTION_EXPORT:
394 gld${EMULATION_NAME}_read_file (optarg, false);
395 break;
396
397 case OPTION_IMPORT:
398 {
399 struct filelist *n;
400 struct filelist **flpp;
401
402 n = (struct filelist *) xmalloc (sizeof (struct filelist));
403 n->next = NULL;
404 n->name = optarg;
405 flpp = &import_files;
406 while (*flpp != NULL)
407 flpp = &(*flpp)->next;
408 *flpp = n;
409 }
410 break;
411
412 case OPTION_LOADMAP:
413 config.map_filename = optarg;
414 break;
415
416 case OPTION_MAXDATA:
417 val = strtoull (optarg, &end, 0);
418 if (*end != '\0')
419 einfo ("%P: warning: ignoring invalid -bmaxdata number %s\n", optarg);
420 else
421 maxdata = val;
422 break;
423
424 case OPTION_MAXSTACK:
425 val = strtoull (optarg, &end, 0);
426 if (*end != '\0')
427 einfo ("%P: warning: ignoring invalid -bmaxstack number %s\n",
428 optarg);
429 else
430 maxstack = val;
431 break;
432
433 case OPTION_MODTYPE:
434 if (*optarg == 'S')
435 {
436 link_info.shared = true;
437 ++optarg;
438 }
439 if (*optarg == '\0' || optarg[1] == '\0')
440 einfo ("%P: warning: ignoring invalid module type %s\n", optarg);
441 else
442 modtype = (*optarg << 8) | optarg[1];
443 break;
444
445 case OPTION_NOAUTOIMP:
446 link_info.static_link = true;
447 break;
448
449 case OPTION_NOSTRCMPCT:
450 link_info.traditional_format = true;
451 break;
452
453 case OPTION_PD:
454 /* This sets the page that the .data section is supposed to
455 start on. The offset within the page should still be the
456 offset within the file, so we need to build an appropriate
457 expression. */
458 val = strtoull (optarg, &end, 0);
459 if (*end != '\0')
460 einfo ("%P: warning: ignoring invalid -pD number %s\n", optarg);
461 else
462 {
463 etree_type *t;
464
465 t = exp_binop ('+',
466 exp_intop (val),
467 exp_binop ('&',
468 exp_nameop (NAME, "."),
469 exp_intop (0xfff)));
470 t = exp_binop ('&',
471 exp_binop ('+', t, exp_intop (31)),
472 exp_intop (~(bfd_vma) 31));
473 lang_section_start (".data", t);
474 }
475 break;
476
477 case OPTION_PT:
478 /* This set the page that the .text section is supposed to start
479 on. The offset within the page should still be the offset
480 within the file. */
481 val = strtoull (optarg, &end, 0);
482 if (*end != '\0')
483 einfo ("%P: warning: ignoring invalid -pT number %s\n", optarg);
484 else
485 {
486 etree_type *t;
487
488 t = exp_binop ('+',
489 exp_intop (val),
490 exp_nameop (SIZEOF_HEADERS, NULL));
491 t = exp_binop ('&',
492 exp_binop ('+', t, exp_intop (31)),
493 exp_intop (~(bfd_vma) 31));
494 lang_section_start (".text", t);
495 }
496 break;
497
498 case OPTION_STRCMPCT:
499 link_info.traditional_format = false;
500 break;
501
502 case OPTION_UNIX:
503 unix_ld = true;
504 break;
505
506 case OPTION_32:
507 is_64bit = 0;
508 syscall_mask = 0x77;
509 symbol_mode_mask = 0x0d;
510 break;
511
512 case OPTION_64:
513 is_64bit = 1;
514 syscall_mask = 0xcc;
515 symbol_mode_mask = 0x0e;
516 break;
517
518 }
519
520 return 1;
521 }
522
523 /* This is called when an input file can not be recognized as a BFD
524 object or an archive. If the file starts with #!, we must treat it
525 as an import file. This is for AIX compatibility. */
526
527 static boolean
528 gld${EMULATION_NAME}_unrecognized_file (entry)
529 lang_input_statement_type *entry;
530 {
531 FILE *e;
532 boolean ret;
533
534 e = fopen (entry->filename, FOPEN_RT);
535 if (e == NULL)
536 return false;
537
538 ret = false;
539
540 if (getc (e) == '#' && getc (e) == '!')
541 {
542 struct filelist *n;
543 struct filelist **flpp;
544
545 n = (struct filelist *) xmalloc (sizeof (struct filelist));
546 n->next = NULL;
547 n->name = entry->filename;
548 flpp = &import_files;
549 while (*flpp != NULL)
550 flpp = &(*flpp)->next;
551 *flpp = n;
552
553 ret = true;
554 entry->loaded = true;
555 }
556
557 fclose (e);
558
559 return ret;
560 }
561
562 /* This is called after the input files have been opened. */
563
564 static void
565 gld${EMULATION_NAME}_after_open ()
566 {
567 boolean r;
568 struct set_info *p;
569
570 /* Call ldctor_build_sets, after pretending that this is a
571 relocateable link. We do this because AIX requires relocation
572 entries for all references to symbols, even in a final
573 executable. Of course, we only want to do this if we are
574 producing an XCOFF output file. */
575 r = link_info.relocateable;
576 if (strstr (bfd_get_target (output_bfd), "xcoff") != NULL)
577 link_info.relocateable = true;
578 ldctor_build_sets ();
579 link_info.relocateable = r;
580
581 /* For each set, record the size, so that the XCOFF backend can
582 output the correct csect length. */
583 for (p = sets; p != (struct set_info *) NULL; p = p->next)
584 {
585 bfd_size_type size;
586
587 /* If the symbol is defined, we may have been invoked from
588 collect, and the sets may already have been built, so we do
589 not do anything. */
590 if (p->h->type == bfd_link_hash_defined
591 || p->h->type == bfd_link_hash_defweak)
592 continue;
593
594 if (p->reloc != BFD_RELOC_CTOR)
595 {
596 /* Handle this if we need to. */
597 abort ();
598 }
599
600 size = (p->count + 2) * 4;
601 if (!bfd_xcoff_link_record_set (output_bfd, &link_info, p->h, size))
602 einfo ("%F%P: bfd_xcoff_link_record_set failed: %E\n");
603 }
604 }
605
606 /* This is called after the sections have been attached to output
607 sections, but before any sizes or addresses have been set. */
608
609 static void
610 gld${EMULATION_NAME}_before_allocation ()
611 {
612 struct filelist *fl;
613 struct export_symbol_list *el;
614 char *libpath;
615 asection *special_sections[XCOFF_NUMBER_OF_SPECIAL_SECTIONS];
616 int i;
617
618 /* Handle the import and export files, if any. */
619 for (fl = import_files; fl != NULL; fl = fl->next)
620 gld${EMULATION_NAME}_read_file (fl->name, true);
621 for (el = export_symbols; el != NULL; el = el->next)
622 {
623 struct bfd_link_hash_entry *h;
624
625 h = bfd_link_hash_lookup (link_info.hash, el->name, false, false, false);
626 if (h == NULL)
627 einfo ("%P%F: bfd_link_hash_lookup of export symbol failed: %E\n");
628 if (!bfd_xcoff_export_symbol (output_bfd, &link_info, h))
629 einfo ("%P%F: bfd_xcoff_export_symbol failed: %E\n");
630 }
631
632 /* Track down all relocations called for by the linker script (these
633 are typically constructor/destructor entries created by
634 CONSTRUCTORS) and let the backend know it will need to create
635 .loader relocs for them. */
636 lang_for_each_statement (gld${EMULATION_NAME}_find_relocs);
637
638 /* We need to build LIBPATH from the -L arguments. If any -rpath
639 arguments were used, though, we use -rpath instead, as a GNU
640 extension. */
641 if (command_line.rpath != NULL)
642 libpath = command_line.rpath;
643 else if (search_head == NULL)
644 libpath = (char *) "";
645 else
646 {
647 size_t len;
648 search_dirs_type *search;
649
650 len = strlen (search_head->name);
651 libpath = xmalloc (len + 1);
652 strcpy (libpath, search_head->name);
653 for (search = search_head->next; search != NULL; search = search->next)
654 {
655 size_t nlen;
656
657 nlen = strlen (search->name);
658 libpath = xrealloc (libpath, len + nlen + 2);
659 libpath[len] = ':';
660 strcpy (libpath + len + 1, search->name);
661 len += nlen + 1;
662 }
663 }
664
665 /* Let the XCOFF backend set up the .loader section. */
666 if (!bfd_xcoff_size_dynamic_sections (output_bfd, &link_info, libpath,
667 entry_symbol, file_align,
668 maxstack, maxdata,
669 gc && !unix_ld ? true : false,
670 modtype,
671 textro ? true : false,
672 unix_ld, special_sections))
673 einfo ("%P%F: failed to set dynamic section sizes: %E\n");
674
675 /* Look through the special sections, and put them in the right
676 place in the link ordering. This is especially magic. */
677 for (i = 0; i < XCOFF_NUMBER_OF_SPECIAL_SECTIONS; i++)
678 {
679 asection *sec;
680 lang_output_section_statement_type *os;
681 lang_statement_union_type **pls;
682 lang_input_section_type *is;
683 const char *oname;
684 boolean start;
685
686 sec = special_sections[i];
687 if (sec == NULL)
688 continue;
689
690 /* Remove this section from the list of the output section.
691 This assumes we know what the script looks like. */
692 is = NULL;
693 os = lang_output_section_find (sec->output_section->name);
694 if (os == NULL)
695 einfo ("%P%F: can't find output section %s\n",
696 sec->output_section->name);
697
698 for (pls = &os->children.head; *pls != NULL; pls = &(*pls)->header.next)
699 {
700 if ((*pls)->header.type == lang_input_section_enum
701 && (*pls)->input_section.section == sec)
702 {
703 is = (lang_input_section_type *) * pls;
704 *pls = (*pls)->header.next;
705 break;
706 }
707
708 if ((*pls)->header.type == lang_wild_statement_enum)
709 {
710 lang_statement_union_type **pwls;
711
712 for (pwls = &(*pls)->wild_statement.children.head;
713 *pwls != NULL; pwls = &(*pwls)->header.next)
714 {
715
716 if ((*pwls)->header.type == lang_input_section_enum
717 && (*pwls)->input_section.section == sec)
718 {
719 is = (lang_input_section_type *) * pwls;
720 *pwls = (*pwls)->header.next;
721 break;
722 }
723 }
724
725 if (is != NULL)
726 break;
727 }
728 }
729
730 if (is == NULL)
731 {
732 einfo ("%P%F: can't find %s in output section\n",
733 bfd_get_section_name (sec->owner, sec));
734 }
735
736 /* Now figure out where the section should go. */
737 switch (i)
738 {
739
740 default: /* to avoid warnings */
741 case XCOFF_SPECIAL_SECTION_TEXT:
742 /* _text */
743 oname = ".text";
744 start = true;
745 break;
746
747 case XCOFF_SPECIAL_SECTION_ETEXT:
748 /* _etext */
749 oname = ".text";
750 start = false;
751 break;
752
753 case XCOFF_SPECIAL_SECTION_DATA:
754 /* _data */
755 oname = ".data";
756 start = true;
757 break;
758
759 case XCOFF_SPECIAL_SECTION_EDATA:
760 /* _edata */
761 oname = ".data";
762 start = false;
763 break;
764
765 case XCOFF_SPECIAL_SECTION_END:
766 case XCOFF_SPECIAL_SECTION_END2:
767 /* _end and end */
768 oname = ".bss";
769 start = false;
770 break;
771 }
772
773 os = lang_output_section_find (oname);
774
775 if (start)
776 {
777 is->header.next = os->children.head;
778 os->children.head = (lang_statement_union_type *) is;
779 }
780 else
781 {
782 is->header.next = NULL;
783 lang_statement_append (&os->children,
784 (lang_statement_union_type *) is,
785 &is->header.next);
786 }
787 }
788 }
789
790 static char *
791 choose_target (argc, argv)
792 int argc;
793 char **argv;
794 {
795 int i, j, jmax;
796 static char *from_outside;
797 static char *from_inside;
798 static char *argv_to_target[][2] = {
799 {NULL, "${OUTPUT_FORMAT}"},
800 {"-b32", "aixcoff-rs6000"},
801 {"-b64", "aixcoff64-rs6000"},
802 };
803
804 jmax = 3;
805
806 from_outside = getenv (TARGET_ENVIRON);
807 if (from_outside != (char *) NULL)
808 return from_outside;
809
810 /* Set to default. */
811 from_inside = argv_to_target[0][1];
812 for (i = 1; i < argc; i++)
813 {
814 for (j = 1; j < jmax; j++)
815 {
816 if (0 == strcmp (argv[i], argv_to_target[j][0]))
817 from_inside = argv_to_target[j][1];
818 }
819 }
820
821 return from_inside;
822 }
823
824 /* Returns
825 1 : state changed
826 0 : no change */
827 static int
828 change_symbol_mode (input)
829 char *input;
830 {
831 char *symbol_mode_string[] = {
832 "# 32", /* 0x01 */
833 "# 64", /* 0x02 */
834 "# no32", /* 0x04 */
835 "# no64", /* 0x08 */
836 NULL,
837 };
838
839 unsigned int bit;
840 char *string;
841
842 for (bit = 0;; bit++)
843 {
844 string = symbol_mode_string[bit];
845 if (string == NULL)
846 return 0;
847
848 if (0 == strcmp (input, string))
849 {
850 symbol_mode = (1 << bit);
851 return 1;
852 }
853 }
854 /* should not be here */
855 return 0;
856 }
857
858 /* Returns
859 1 : yes
860 0 : ignore
861 -1 : error, try something else */
862 static int
863 is_syscall (input, flag)
864 char *input;
865 unsigned int *flag;
866 {
867 unsigned int bit;
868 char *string;
869
870 struct sc {
871 char *syscall_string;
872 unsigned int flag;
873 } s [] = {
874 { "svc" /* 0x01 */, XCOFF_SYSCALL32 },
875 { "svc32" /* 0x02 */, XCOFF_SYSCALL32 },
876 { "svc3264" /* 0x04 */, XCOFF_SYSCALL32 | XCOFF_SYSCALL64 },
877 { "svc64" /* 0x08 */, XCOFF_SYSCALL64 },
878 { "syscall" /* 0x10 */, XCOFF_SYSCALL32 },
879 { "syscall32" /* 0x20 */, XCOFF_SYSCALL32 },
880 { "syscall3264" /* 0x40 */, XCOFF_SYSCALL32 | XCOFF_SYSCALL64 },
881 { "syscall64" /* 0x80 */, XCOFF_SYSCALL64 },
882 { NULL, 0 },
883 };
884
885 *flag = 0;
886
887 for (bit = 0;; bit++)
888 {
889 string = s[bit].syscall_string;
890 if (string == NULL)
891 return -1;
892
893 if (0 == strcmp (input, string))
894 {
895 if (1 << bit & syscall_mask)
896 {
897 *flag = s[bit].flag;
898 return 1;
899 }
900 else
901 {
902 return 0;
903 }
904 }
905 }
906 /* should not be here */
907 return -1;
908 }
909
910 /* Read an import or export file. For an import file, this is called
911 by the before_allocation emulation routine. For an export file,
912 this is called by the parse_args emulation routine. */
913
914 static void
915 gld${EMULATION_NAME}_read_file (filename, import)
916 const char *filename;
917 boolean import;
918 {
919 struct obstack *o;
920 FILE *f;
921 int lineno;
922 int c;
923 boolean keep;
924 const char *imppath;
925 const char *impfile;
926 const char *impmember;
927
928 o = (struct obstack *) xmalloc (sizeof (struct obstack));
929 obstack_specify_allocation (o, 0, 0, xmalloc, gld${EMULATION_NAME}_free);
930
931 f = fopen (filename, FOPEN_RT);
932 if (f == NULL)
933 {
934 bfd_set_error (bfd_error_system_call);
935 einfo ("%F%s: %E\n", filename);
936 }
937
938 keep = false;
939
940 imppath = NULL;
941 impfile = NULL;
942 impmember = NULL;
943
944 lineno = 0;
945
946 /*
947 * default to 32 and 64 bit mode
948 * symbols at top of /lib/syscalls.exp do not have a mode modifier and they
949 * are not repeated, assume 64 bit routines also want to use them.
950 * See the routine change_symbol_mode for more information.
951 */
952 symbol_mode = 0x04;
953
954 while ((c = getc (f)) != EOF)
955 {
956 char *s;
957 char *symname;
958 unsigned int syscall_flag = 0;
959 bfd_vma address;
960 struct bfd_link_hash_entry *h;
961
962 if (c != '\n')
963 {
964 obstack_1grow (o, c);
965 continue;
966 }
967
968 obstack_1grow (o, '\0');
969 ++lineno;
970
971 s = (char *) obstack_base (o);
972 while (ISSPACE (*s))
973 ++s;
974 if (*s == '\0'
975 || *s == '*'
976 || change_symbol_mode (s)
977 || (*s == '#' && s[1] == ' ')
978 || (!import && *s == '#' && s[1] == '!'))
979 {
980 obstack_free (o, obstack_base (o));
981 continue;
982 }
983
984 if (*s == '#' && s[1] == '!')
985 {
986 s += 2;
987 while (ISSPACE (*s))
988 ++s;
989 if (*s == '\0')
990 {
991 imppath = NULL;
992 impfile = NULL;
993 impmember = NULL;
994 obstack_free (o, obstack_base (o));
995 }
996 else if (*s == '(')
997 einfo ("%F%s%d: #! ([member]) is not supported in import files\n",
998 filename, lineno);
999 else
1000 {
1001 char cs;
1002 char *file;
1003
1004 (void) obstack_finish (o);
1005 keep = true;
1006 imppath = s;
1007 file = NULL;
1008 while (!ISSPACE (*s) && *s != '(' && *s != '\0')
1009 {
1010 if (*s == '/')
1011 file = s + 1;
1012 ++s;
1013 }
1014 if (file != NULL)
1015 {
1016 file[-1] = '\0';
1017 impfile = file;
1018 if (imppath == file - 1)
1019 imppath = "/";
1020 }
1021 else
1022 {
1023 impfile = imppath;
1024 imppath = "";
1025 }
1026 cs = *s;
1027 *s = '\0';
1028 while (ISSPACE (cs))
1029 {
1030 ++s;
1031 cs = *s;
1032 }
1033 if (cs != '(')
1034 {
1035 impmember = "";
1036 if (cs != '\0')
1037 einfo ("%s:%d: warning: syntax error in import file\n",
1038 filename, lineno);
1039 }
1040 else
1041 {
1042 ++s;
1043 impmember = s;
1044 while (*s != ')' && *s != '\0')
1045 ++s;
1046 if (*s == ')')
1047 *s = '\0';
1048 else
1049 einfo ("%s:%d: warning: syntax error in import file\n",
1050 filename, lineno);
1051 }
1052 }
1053
1054 continue;
1055 }
1056
1057 if (symbol_mode & symbol_mode_mask)
1058 {
1059 /* This is a symbol to be imported or exported. */
1060 symname = s;
1061 syscall_flag = 0;
1062 address = (bfd_vma) -1;
1063
1064 while (!ISSPACE (*s) && *s != '\0')
1065 ++s;
1066 if (*s != '\0')
1067 {
1068 char *se;
1069
1070 *s++ = '\0';
1071
1072 while (ISSPACE (*s))
1073 ++s;
1074
1075 se = s;
1076 while (!ISSPACE (*se) && *se != '\0')
1077 ++se;
1078 if (*se != '\0')
1079 {
1080 *se++ = '\0';
1081 while (ISSPACE (*se))
1082 ++se;
1083 if (*se != '\0')
1084 einfo ("%s%d: warning: syntax error in import/export file\n",
1085 filename, lineno);
1086 }
1087
1088 if (s != se)
1089 {
1090 int status;
1091 char *end;
1092
1093 status = is_syscall (s, &syscall_flag);
1094
1095 if (0 > status)
1096 {
1097 /* not a system call, check for address */
1098 address = strtoul (s, &end, 0);
1099 if (*end != '\0')
1100 {
1101 einfo ("%s:%d: warning: syntax error in import/export file\n",
1102 filename, lineno);
1103
1104 }
1105 }
1106 }
1107 }
1108
1109 if (!import)
1110 {
1111 struct export_symbol_list *n;
1112
1113 ldlang_add_undef (symname);
1114 n = ((struct export_symbol_list *)
1115 xmalloc (sizeof (struct export_symbol_list)));
1116 n->next = export_symbols;
1117 n->name = xstrdup (symname);
1118 export_symbols = n;
1119 }
1120 else
1121 {
1122 h = bfd_link_hash_lookup (link_info.hash, symname, false, false,
1123 true);
1124 if (h == NULL || h->type == bfd_link_hash_new)
1125 {
1126 /* We can just ignore attempts to import an unreferenced
1127 symbol. */
1128 }
1129 else
1130 {
1131 if (!bfd_xcoff_import_symbol (output_bfd, &link_info, h,
1132 address, imppath, impfile,
1133 impmember, syscall_flag))
1134 einfo ("%X%s:%d: failed to import symbol %s: %E\n",
1135 filename, lineno, symname);
1136 }
1137 }
1138 }
1139 obstack_free (o, obstack_base (o));
1140 }
1141
1142 if (obstack_object_size (o) > 0)
1143 {
1144 einfo ("%s:%d: warning: ignoring unterminated last line\n",
1145 filename, lineno);
1146 obstack_free (o, obstack_base (o));
1147 }
1148
1149 if (!keep)
1150 {
1151 obstack_free (o, NULL);
1152 free (o);
1153 }
1154 }
1155
1156 /* This routine saves us from worrying about declaring free. */
1157
1158 static void
1159 gld${EMULATION_NAME}_free (p)
1160 PTR p;
1161 {
1162 free (p);
1163 }
1164
1165 /* This is called by the before_allocation routine via
1166 lang_for_each_statement. It looks for relocations and assignments
1167 to symbols. */
1168
1169 static void
1170 gld${EMULATION_NAME}_find_relocs (s)
1171 lang_statement_union_type *s;
1172 {
1173 if (s->header.type == lang_reloc_statement_enum)
1174 {
1175 lang_reloc_statement_type *rs;
1176
1177 rs = &s->reloc_statement;
1178 if (rs->name == NULL)
1179 einfo ("%F%P: only relocations against symbols are permitted\n");
1180 if (!bfd_xcoff_link_count_reloc (output_bfd, &link_info, rs->name))
1181 einfo ("%F%P: bfd_xcoff_link_count_reloc failed: %E\n");
1182 }
1183
1184 if (s->header.type == lang_assignment_statement_enum)
1185 gld${EMULATION_NAME}_find_exp_assignment (s->assignment_statement.exp);
1186 }
1187
1188 /* Look through an expression for an assignment statement. */
1189
1190 static void
1191 gld${EMULATION_NAME}_find_exp_assignment (exp)
1192 etree_type *exp;
1193 {
1194 struct bfd_link_hash_entry *h;
1195
1196 switch (exp->type.node_class)
1197 {
1198 case etree_provide:
1199 h = bfd_link_hash_lookup (link_info.hash, exp->assign.dst,
1200 false, false, false);
1201 if (h == NULL)
1202 break;
1203 /* Fall through. */
1204 case etree_assign:
1205 if (strcmp (exp->assign.dst, ".") != 0)
1206 {
1207 if (!bfd_xcoff_record_link_assignment (output_bfd, &link_info,
1208 exp->assign.dst))
1209 einfo ("%P%F: failed to record assignment to %s: %E\n",
1210 exp->assign.dst);
1211 }
1212 gld${EMULATION_NAME}_find_exp_assignment (exp->assign.src);
1213 break;
1214
1215 case etree_binary:
1216 gld${EMULATION_NAME}_find_exp_assignment (exp->binary.lhs);
1217 gld${EMULATION_NAME}_find_exp_assignment (exp->binary.rhs);
1218 break;
1219
1220 case etree_trinary:
1221 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.cond);
1222 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.lhs);
1223 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.rhs);
1224 break;
1225
1226 case etree_unary:
1227 gld${EMULATION_NAME}_find_exp_assignment (exp->unary.child);
1228 break;
1229
1230 default:
1231 break;
1232 }
1233 }
1234
1235 static char *
1236 gld${EMULATION_NAME}_get_script (isfile)
1237 int *isfile;
1238 EOF
1239
1240 if test -n "$COMPILE_IN"
1241 then
1242 # Scripts compiled in.
1243
1244 # sed commands to quote an ld script as a C string.
1245 sc="-f ${srcdir}/emultempl/ostring.sed"
1246
1247 cat >>e${EMULATION_NAME}.c <<EOF
1248 {
1249 *isfile = 0;
1250
1251 if (link_info.relocateable == true && config.build_constructors == true)
1252 return
1253 EOF
1254 sed $sc ldscripts/${EMULATION_NAME}.xu >> e${EMULATION_NAME}.c
1255 echo ' ; else if (link_info.relocateable == true) return' >> e${EMULATION_NAME}.c
1256 sed $sc ldscripts/${EMULATION_NAME}.xr >> e${EMULATION_NAME}.c
1257 echo ' ; else if (!config.text_read_only) return' >> e${EMULATION_NAME}.c
1258 sed $sc ldscripts/${EMULATION_NAME}.xbn >> e${EMULATION_NAME}.c
1259 echo ' ; else if (!config.magic_demand_paged) return' >> e${EMULATION_NAME}.c
1260 sed $sc ldscripts/${EMULATION_NAME}.xn >> e${EMULATION_NAME}.c
1261 echo ' ; else return' >> e${EMULATION_NAME}.c
1262 sed $sc ldscripts/${EMULATION_NAME}.x >> e${EMULATION_NAME}.c
1263 echo '; }' >> e${EMULATION_NAME}.c
1264
1265 else
1266 # Scripts read from the filesystem.
1267
1268 cat >>e${EMULATION_NAME}.c <<EOF
1269 {
1270 *isfile = 1;
1271
1272 if (link_info.relocateable == true && config.build_constructors == true)
1273 return "ldscripts/${EMULATION_NAME}.xu";
1274 else if (link_info.relocateable == true)
1275 return "ldscripts/${EMULATION_NAME}.xr";
1276 else if (!config.text_read_only)
1277 return "ldscripts/${EMULATION_NAME}.xbn";
1278 else if (!config.magic_demand_paged)
1279 return "ldscripts/${EMULATION_NAME}.xn";
1280 else
1281 return "ldscripts/${EMULATION_NAME}.x";
1282 }
1283 EOF
1284
1285 fi
1286
1287 cat >>e${EMULATION_NAME}.c <<EOF
1288
1289 static void
1290 gld${EMULATION_NAME}_create_output_section_statements()
1291 {
1292 /* __rtinit */
1293 if ((bfd_get_flavour (output_bfd) == bfd_target_xcoff_flavour)
1294 && (link_info.init_function != NULL || link_info.fini_function != NULL))
1295 {
1296
1297 initfini_file = lang_add_input_file ("initfini",
1298 lang_input_file_is_file_enum,
1299 NULL);
1300
1301 initfini_file->the_bfd = bfd_create ("initfini", output_bfd);
1302 if (initfini_file->the_bfd == NULL
1303 || ! bfd_set_arch_mach (initfini_file->the_bfd,
1304 bfd_get_arch (output_bfd),
1305 bfd_get_mach (output_bfd)))
1306 {
1307 einfo ("%X%P: can not create BFD %E\n");
1308 return;
1309 }
1310
1311 /* Call backend to fill in the rest */
1312 if (false == bfd_xcoff_link_generate_rtinit (initfini_file->the_bfd,
1313 link_info.init_function,
1314 link_info.fini_function))
1315 {
1316 einfo ("%X%P: can not create BFD %E\n");
1317 return;
1318 }
1319 }
1320 }
1321
1322 struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation = {
1323 gld${EMULATION_NAME}_before_parse,
1324 syslib_default,
1325 hll_default,
1326 after_parse_default,
1327 gld${EMULATION_NAME}_after_open,
1328 after_allocation_default,
1329 set_output_arch_default,
1330 choose_target,
1331 gld${EMULATION_NAME}_before_allocation,
1332 gld${EMULATION_NAME}_get_script,
1333 "${EMULATION_NAME}",
1334 "${OUTPUT_FORMAT}",
1335 0, /* finish */
1336 gld${EMULATION_NAME}_create_output_section_statements,
1337 0, /* open_dynamic_archive */
1338 0, /* place_orphan */
1339 0, /* set_symbols */
1340 gld${EMULATION_NAME}_parse_args,
1341 gld${EMULATION_NAME}_unrecognized_file,
1342 NULL, /* list_options */
1343 NULL, /* recognized_file */
1344 NULL, /* find potential_libraries */
1345 };
1346 EOF
This page took 0.064509 seconds and 4 git commands to generate.