oops - omitted from previous delta
[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 void gld${EMULATION_NAME}_before_allocation PARAMS ((void));
61 static void gld${EMULATION_NAME}_read_file PARAMS ((const char *, boolean));
62 static void gld${EMULATION_NAME}_free PARAMS ((PTR));
63 static void gld${EMULATION_NAME}_find_relocs
64 PARAMS ((lang_statement_union_type *));
65 static void gld${EMULATION_NAME}_find_exp_assignment PARAMS ((etree_type *));
66 static char *gld${EMULATION_NAME}_get_script PARAMS ((int *isfile));
67 static boolean gld${EMULATION_NAME}_unrecognized_file
68 PARAMS ((lang_input_statement_type *));
69
70 /* The file alignment required for each section. */
71 static unsigned long file_align;
72
73 /* The maximum size the stack is permitted to grow. This is stored in
74 the a.out header. */
75 static unsigned long maxstack;
76
77 /* The maximum data size. This is stored in the a.out header. */
78 static unsigned long maxdata;
79
80 /* Whether to perform garbage collection. */
81 static int gc = 1;
82
83 /* The module type to use. */
84 static unsigned short modtype = ('1' << 8) | 'L';
85
86 /* Whether the .text section must be read-only (i.e., no relocs
87 permitted). */
88 static int textro;
89
90 /* Whether to implement Unix like linker semantics. */
91 static int unix_ld;
92
93 /* Structure used to hold import file list. */
94
95 struct filelist
96 {
97 struct filelist *next;
98 const char *name;
99 };
100
101 /* List of import files. */
102 static struct filelist *import_files;
103
104 /* List of export symbols read from the export files. */
105
106 struct export_symbol_list
107 {
108 struct export_symbol_list *next;
109 const char *name;
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))
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 static int is_syscall(char *input, unsigned int *flag)
844 {
845 /*
846 * 1 : yes
847 * 0 : ignore
848 * -1 : error, try something else
849 */
850 unsigned int bit;
851 char *string;
852
853 struct sc {
854 char *syscall_string;
855 unsigned int flag;
856 } s [] = {
857 { "svc" /* 0x01 */, XCOFF_SYSCALL32 },
858 { "svc32" /* 0x02 */, XCOFF_SYSCALL32 },
859 { "svc3264" /* 0x04 */, XCOFF_SYSCALL32 | XCOFF_SYSCALL64 },
860 { "svc64" /* 0x08 */, XCOFF_SYSCALL64 },
861 { "syscall" /* 0x10 */, XCOFF_SYSCALL32 },
862 { "syscall32" /* 0x20 */, XCOFF_SYSCALL32 },
863 { "syscall3264" /* 0x40 */, XCOFF_SYSCALL32 | XCOFF_SYSCALL64 },
864 { "syscall64" /* 0x80 */, XCOFF_SYSCALL64 },
865 { NULL, 0 },
866 };
867
868 *flag = 0;
869
870 for (bit = 0; ;bit++) {
871
872 string = s[bit].syscall_string;
873 if (NULL == string) {
874 return -1;
875 }
876
877 if (0 == strcmp(input, string)) {
878 if (1 << bit & ${SYSCALL_MASK}) {
879 *flag = s[bit].flag;
880 return 1;
881 } else {
882 return 0;
883 }
884 }
885 }
886 /* should not be here */
887 return -1;
888 }
889
890 /* Read an import or export file. For an import file, this is called
891 by the before_allocation emulation routine. For an export file,
892 this is called by the parse_args emulation routine. */
893
894 static void
895 gld${EMULATION_NAME}_read_file (filename, import)
896 const char *filename;
897 boolean import;
898 {
899 struct obstack *o;
900 FILE *f;
901 int lineno;
902 int c;
903 boolean keep;
904 const char *imppath;
905 const char *impfile;
906 const char *impmember;
907
908 o = (struct obstack *) xmalloc (sizeof (struct obstack));
909 obstack_specify_allocation (o, 0, 0, xmalloc, gld${EMULATION_NAME}_free);
910
911 f = fopen (filename, FOPEN_RT);
912 if (f == NULL)
913 {
914 bfd_set_error (bfd_error_system_call);
915 einfo ("%F%s: %E\n", filename);
916 }
917
918 keep = false;
919
920 imppath = NULL;
921 impfile = NULL;
922 impmember = NULL;
923
924 lineno = 0;
925
926 /*
927 * default to 32 and 64 bit mode
928 * symbols at top of /lib/syscalls.exp do not have a mode modifier and they
929 * are not repeated, assume 64 bit routines also want to use them.
930 * See the routine change_symbol_mode for more information.
931 */
932 symbol_mode = 0x04;
933
934 while ((c = getc (f)) != EOF)
935 {
936 char *s;
937 char *symname;
938 unsigned int syscall_flag = 0;
939 bfd_vma address;
940 struct bfd_link_hash_entry *h;
941
942 if (c != '\n')
943 {
944 obstack_1grow (o, c);
945 continue;
946 }
947
948 obstack_1grow (o, '\0');
949 ++lineno;
950
951 s = (char *) obstack_base (o);
952 while (ISSPACE (*s))
953 ++s;
954 if (*s == '\0'
955 || *s == '*'
956 || change_symbol_mode (s)
957 || (*s == '#' && s[1] == ' ')
958 || (! import && *s == '#' && s[1] == '!'))
959 {
960 obstack_free (o, obstack_base (o));
961 continue;
962 }
963
964 if (*s == '#' && s[1] == '!')
965 {
966 s += 2;
967 while (ISSPACE (*s))
968 ++s;
969 if (*s == '\0')
970 {
971 imppath = NULL;
972 impfile = NULL;
973 impmember = NULL;
974 obstack_free (o, obstack_base (o));
975 }
976 else if (*s == '(')
977 einfo ("%F%s%d: #! ([member]) is not supported in import files\n",
978 filename, lineno);
979 else
980 {
981 char cs;
982 char *file;
983
984 (void) obstack_finish (o);
985 keep = true;
986 imppath = s;
987 file = NULL;
988 while (! ISSPACE (*s)
989 && *s != '(' && *s != '\0')
990 {
991 if (*s == '/')
992 file = s + 1;
993 ++s;
994 }
995 if (file != NULL)
996 {
997 file[-1] = '\0';
998 impfile = file;
999 if (imppath == file - 1)
1000 imppath = "/";
1001 }
1002 else
1003 {
1004 impfile = imppath;
1005 imppath = "";
1006 }
1007 cs = *s;
1008 *s = '\0';
1009 while (ISSPACE (cs))
1010 {
1011 ++s;
1012 cs = *s;
1013 }
1014 if (cs != '(')
1015 {
1016 impmember = "";
1017 if (cs != '\0')
1018 einfo ("%s:%d: warning: syntax error in import file\n",
1019 filename, lineno);
1020 }
1021 else
1022 {
1023 ++s;
1024 impmember = s;
1025 while (*s != ')' && *s != '\0')
1026 ++s;
1027 if (*s == ')')
1028 *s = '\0';
1029 else
1030 einfo ("%s:%d: warning: syntax error in import file\n",
1031 filename, lineno);
1032 }
1033 }
1034
1035 continue;
1036 }
1037
1038 if (symbol_mode & ${SYMBOL_MODE_MASK})
1039 {
1040 /* This is a symbol to be imported or exported. */
1041 symname = s;
1042 syscall_flag = 0;
1043 address = (bfd_vma) -1;
1044
1045 while (! ISSPACE (*s) && *s != '\0')
1046 ++s;
1047 if (*s != '\0')
1048 {
1049 char *se;
1050
1051 *s++ = '\0';
1052
1053 while (ISSPACE (*s))
1054 ++s;
1055
1056 se = s;
1057 while (! ISSPACE (*se) && *se != '\0')
1058 ++se;
1059 if (*se != '\0')
1060 {
1061 *se++ = '\0';
1062 while (ISSPACE (*se))
1063 ++se;
1064 if (*se != '\0')
1065 einfo ("%s%d: warning: syntax error in import/export file\n",
1066 filename, lineno);
1067 }
1068
1069 if (s != se)
1070 {
1071 int status;
1072 char *end;
1073
1074 status = is_syscall(s, &syscall_flag);
1075
1076 if (0 > status) {
1077 /* not a system call, check for address */
1078 address = strtoul (s, &end, 0);
1079
1080 /* not a system call, check for address */
1081 address = strtoul (s, &end, 0);
1082 if (*end != '\0')
1083 {
1084 einfo ("%s:%d: warning: syntax error in import/export file\n",
1085 filename, lineno);
1086
1087 }
1088 }
1089 }
1090 }
1091
1092 if (! import)
1093 {
1094 struct export_symbol_list *n;
1095
1096 ldlang_add_undef (symname);
1097 n = ((struct export_symbol_list *)
1098 xmalloc (sizeof (struct export_symbol_list)));
1099 n->next = export_symbols;
1100 n->name = xstrdup (symname);
1101 export_symbols = n;
1102 }
1103 else
1104 {
1105 h = bfd_link_hash_lookup (link_info.hash, symname, false, false,
1106 true);
1107 if (h == NULL || h->type == bfd_link_hash_new)
1108 {
1109 /* We can just ignore attempts to import an unreferenced
1110 symbol. */
1111 }
1112 else
1113 {
1114 if (! bfd_xcoff_import_symbol (output_bfd, &link_info, h,
1115 address, imppath, impfile,
1116 impmember, syscall_flag))
1117 einfo ("%X%s:%d: failed to import symbol %s: %E\n",
1118 filename, lineno, symname);
1119 }
1120 }
1121 }
1122 obstack_free (o, obstack_base (o));
1123 }
1124
1125 if (obstack_object_size (o) > 0)
1126 {
1127 einfo ("%s:%d: warning: ignoring unterminated last line\n",
1128 filename, lineno);
1129 obstack_free (o, obstack_base (o));
1130 }
1131
1132 if (! keep)
1133 {
1134 obstack_free (o, NULL);
1135 free (o);
1136 }
1137 }
1138
1139 /* This routine saves us from worrying about declaring free. */
1140
1141 static void
1142 gld${EMULATION_NAME}_free (p)
1143 PTR p;
1144 {
1145 free (p);
1146 }
1147
1148 /* This is called by the before_allocation routine via
1149 lang_for_each_statement. It looks for relocations and assignments
1150 to symbols. */
1151
1152 static void
1153 gld${EMULATION_NAME}_find_relocs (s)
1154 lang_statement_union_type *s;
1155 {
1156 if (s->header.type == lang_reloc_statement_enum)
1157 {
1158 lang_reloc_statement_type *rs;
1159
1160 rs = &s->reloc_statement;
1161 if (rs->name == NULL)
1162 einfo ("%F%P: only relocations against symbols are permitted\n");
1163 if (! bfd_xcoff_link_count_reloc (output_bfd, &link_info, rs->name))
1164 einfo ("%F%P: bfd_xcoff_link_count_reloc failed: %E\n");
1165 }
1166
1167 if (s->header.type == lang_assignment_statement_enum)
1168 gld${EMULATION_NAME}_find_exp_assignment (s->assignment_statement.exp);
1169 }
1170
1171 /* Look through an expression for an assignment statement. */
1172
1173 static void
1174 gld${EMULATION_NAME}_find_exp_assignment (exp)
1175 etree_type *exp;
1176 {
1177 struct bfd_link_hash_entry *h;
1178
1179 switch (exp->type.node_class)
1180 {
1181 case etree_provide:
1182 h = bfd_link_hash_lookup (link_info.hash, exp->assign.dst,
1183 false, false, false);
1184 if (h == NULL)
1185 break;
1186 /* Fall through. */
1187 case etree_assign:
1188 if (strcmp (exp->assign.dst, ".") != 0)
1189 {
1190 if (! bfd_xcoff_record_link_assignment (output_bfd, &link_info,
1191 exp->assign.dst))
1192 einfo ("%P%F: failed to record assignment to %s: %E\n",
1193 exp->assign.dst);
1194 }
1195 gld${EMULATION_NAME}_find_exp_assignment (exp->assign.src);
1196 break;
1197
1198 case etree_binary:
1199 gld${EMULATION_NAME}_find_exp_assignment (exp->binary.lhs);
1200 gld${EMULATION_NAME}_find_exp_assignment (exp->binary.rhs);
1201 break;
1202
1203 case etree_trinary:
1204 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.cond);
1205 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.lhs);
1206 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.rhs);
1207 break;
1208
1209 case etree_unary:
1210 gld${EMULATION_NAME}_find_exp_assignment (exp->unary.child);
1211 break;
1212
1213 default:
1214 break;
1215 }
1216 }
1217
1218 static char *
1219 gld${EMULATION_NAME}_get_script (isfile)
1220 int *isfile;
1221 EOF
1222
1223 if test -n "$COMPILE_IN"
1224 then
1225 # Scripts compiled in.
1226
1227 # sed commands to quote an ld script as a C string.
1228 sc="-f ${srcdir}/emultempl/ostring.sed"
1229
1230 cat >>e${EMULATION_NAME}.c <<EOF
1231 {
1232 *isfile = 0;
1233
1234 if (link_info.relocateable == true && config.build_constructors == true)
1235 return
1236 EOF
1237 sed $sc ldscripts/${EMULATION_NAME}.xu >> e${EMULATION_NAME}.c
1238 echo ' ; else if (link_info.relocateable == true) return' >> e${EMULATION_NAME}.c
1239 sed $sc ldscripts/${EMULATION_NAME}.xr >> e${EMULATION_NAME}.c
1240 echo ' ; else if (!config.text_read_only) return' >> e${EMULATION_NAME}.c
1241 sed $sc ldscripts/${EMULATION_NAME}.xbn >> e${EMULATION_NAME}.c
1242 echo ' ; else if (!config.magic_demand_paged) return' >> e${EMULATION_NAME}.c
1243 sed $sc ldscripts/${EMULATION_NAME}.xn >> e${EMULATION_NAME}.c
1244 echo ' ; else return' >> e${EMULATION_NAME}.c
1245 sed $sc ldscripts/${EMULATION_NAME}.x >> e${EMULATION_NAME}.c
1246 echo '; }' >> e${EMULATION_NAME}.c
1247
1248 else
1249 # Scripts read from the filesystem.
1250
1251 cat >>e${EMULATION_NAME}.c <<EOF
1252 {
1253 *isfile = 1;
1254
1255 if (link_info.relocateable == true && config.build_constructors == true)
1256 return "ldscripts/${EMULATION_NAME}.xu";
1257 else if (link_info.relocateable == true)
1258 return "ldscripts/${EMULATION_NAME}.xr";
1259 else if (!config.text_read_only)
1260 return "ldscripts/${EMULATION_NAME}.xbn";
1261 else if (!config.magic_demand_paged)
1262 return "ldscripts/${EMULATION_NAME}.xn";
1263 else
1264 return "ldscripts/${EMULATION_NAME}.x";
1265 }
1266 EOF
1267
1268 fi
1269
1270 cat >>e${EMULATION_NAME}.c <<EOF
1271
1272 struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
1273 {
1274 gld${EMULATION_NAME}_before_parse,
1275 syslib_default,
1276 hll_default,
1277 after_parse_default,
1278 gld${EMULATION_NAME}_after_open,
1279 after_allocation_default,
1280 set_output_arch_default,
1281 ldemul_default_target,
1282 gld${EMULATION_NAME}_before_allocation,
1283 gld${EMULATION_NAME}_get_script,
1284 "${EMULATION_NAME}",
1285 "${OUTPUT_FORMAT}",
1286 0, /* finish */
1287 0, /* create_output_section_statements */
1288 0, /* open_dynamic_archive */
1289 0, /* place_orphan */
1290 0, /* set_symbols */
1291 gld${EMULATION_NAME}_parse_args,
1292 gld${EMULATION_NAME}_unrecognized_file,
1293 NULL, /* list_options */
1294 NULL, /* recognized_file */
1295 NULL, /* find potential_libraries */
1296 };
1297 EOF
This page took 0.055031 seconds and 4 git commands to generate.