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