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