2d26587fa452238586f5e964a6c8eb83141c8d06
[binutils-gdb.git] / ld / emultempl / aix.em
1 # This shell script emits a C file. -*- C -*-
2 # It does some substitutions.
3 cat >e${EMULATION_NAME}.c <<EOF
4 /* This file is is generated by a shell script. DO NOT EDIT! */
5
6 /* AIX emulation code for ${EMULATION_NAME}
7 Copyright 1991, 1993, 1995, 1996, 1997, 1998, 2000
8 Free Software Foundation, Inc.
9 Written by Steve Chamberlain <sac@cygnus.com>
10 AIX support by Ian Lance Taylor <ian@cygnus.com>
11
12 This file is part of GLD, the Gnu Linker.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 2 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
27
28 #define TARGET_IS_${EMULATION_NAME}
29
30 #include "bfd.h"
31 #include "sysdep.h"
32 #include "libiberty.h"
33 #include "getopt.h"
34 #include "obstack.h"
35 #include "bfdlink.h"
36
37 #include <ctype.h>
38
39 #include "ld.h"
40 #include "ldmain.h"
41 #include "ldmisc.h"
42 #include "ldexp.h"
43 #include "ldlang.h"
44 #include "ldfile.h"
45 #include "ldemul.h"
46 #include "ldctor.h"
47 #include "ldgram.h"
48
49 static void gld${EMULATION_NAME}_before_parse PARAMS ((void));
50 static int gld${EMULATION_NAME}_parse_args PARAMS ((int, char **));
51 static void gld${EMULATION_NAME}_after_open PARAMS ((void));
52 static void gld${EMULATION_NAME}_before_allocation PARAMS ((void));
53 static void gld${EMULATION_NAME}_read_file PARAMS ((const char *, boolean));
54 static void gld${EMULATION_NAME}_free PARAMS ((PTR));
55 static void gld${EMULATION_NAME}_find_relocs
56 PARAMS ((lang_statement_union_type *));
57 static void gld${EMULATION_NAME}_find_exp_assignment PARAMS ((etree_type *));
58 static char *gld${EMULATION_NAME}_get_script PARAMS ((int *isfile));
59
60 /* The file alignment required for each section. */
61 static unsigned long file_align;
62
63 /* The maximum size the stack is permitted to grow. This is stored in
64 the a.out header. */
65 static unsigned long maxstack;
66
67 /* The maximum data size. This is stored in the a.out header. */
68 static unsigned long maxdata;
69
70 /* Whether to perform garbage collection. */
71 static int gc = 1;
72
73 /* The module type to use. */
74 static unsigned short modtype = ('1' << 8) | 'L';
75
76 /* Whether the .text section must be read-only (i.e., no relocs
77 permitted). */
78 static int textro;
79
80 /* Whether to implement Unix like linker semantics. */
81 static int unix_ld;
82
83 /* Structure used to hold import file list. */
84
85 struct filelist
86 {
87 struct filelist *next;
88 const char *name;
89 };
90
91 /* List of import files. */
92 static struct filelist *import_files;
93
94 /* List of export symbols read from the export files. */
95
96 struct export_symbol_list
97 {
98 struct export_symbol_list *next;
99 const char *name;
100 boolean syscall;
101 };
102
103 static struct export_symbol_list *export_symbols;
104
105 /* This routine is called before anything else is done. */
106
107 static void
108 gld${EMULATION_NAME}_before_parse()
109 {
110 #ifndef TARGET_ /* I.e., if not generic. */
111 ldfile_output_architecture = bfd_arch_${ARCH};
112 #endif /* not TARGET_ */
113 config.has_shared = true;
114 }
115
116 /* Handle AIX specific options. */
117
118 static int
119 gld${EMULATION_NAME}_parse_args (argc, argv)
120 int argc;
121 char **argv;
122 {
123 int prevoptind = optind;
124 int prevopterr = opterr;
125 int indx;
126 int longind;
127 int optc;
128 long val;
129 char *end;
130
131 #define OPTION_IGNORE (300)
132 #define OPTION_AUTOIMP (OPTION_IGNORE + 1)
133 #define OPTION_ERNOTOK (OPTION_AUTOIMP + 1)
134 #define OPTION_EROK (OPTION_ERNOTOK + 1)
135 #define OPTION_EXPORT (OPTION_EROK + 1)
136 #define OPTION_IMPORT (OPTION_EXPORT + 1)
137 #define OPTION_LOADMAP (OPTION_IMPORT + 1)
138 #define OPTION_MAXDATA (OPTION_LOADMAP + 1)
139 #define OPTION_MAXSTACK (OPTION_MAXDATA + 1)
140 #define OPTION_MODTYPE (OPTION_MAXSTACK + 1)
141 #define OPTION_NOAUTOIMP (OPTION_MODTYPE + 1)
142 #define OPTION_NOSTRCMPCT (OPTION_NOAUTOIMP + 1)
143 #define OPTION_PD (OPTION_NOSTRCMPCT + 1)
144 #define OPTION_PT (OPTION_PD + 1)
145 #define OPTION_STRCMPCT (OPTION_PT + 1)
146 #define OPTION_UNIX (OPTION_STRCMPCT + 1)
147
148 static struct option longopts[] = {
149 {"basis", no_argument, NULL, OPTION_IGNORE},
150 {"bautoimp", no_argument, NULL, OPTION_AUTOIMP},
151 {"bcomprld", no_argument, NULL, OPTION_IGNORE},
152 {"bcrld", no_argument, NULL, OPTION_IGNORE},
153 {"bcror31", no_argument, NULL, OPTION_IGNORE},
154 {"bD", required_argument, NULL, OPTION_MAXDATA},
155 {"bE", required_argument, NULL, OPTION_EXPORT},
156 {"bernotok", no_argument, NULL, OPTION_ERNOTOK},
157 {"berok", no_argument, NULL, OPTION_EROK},
158 {"berrmsg", no_argument, NULL, OPTION_IGNORE},
159 {"bexport", required_argument, NULL, OPTION_EXPORT},
160 {"bf", no_argument, NULL, OPTION_ERNOTOK},
161 {"bgc", no_argument, &gc, 1},
162 {"bh", required_argument, NULL, OPTION_IGNORE},
163 {"bhalt", required_argument, NULL, OPTION_IGNORE},
164 {"bI", required_argument, NULL, OPTION_IMPORT},
165 {"bimport", required_argument, NULL, OPTION_IMPORT},
166 {"bl", required_argument, NULL, OPTION_LOADMAP},
167 {"bloadmap", required_argument, NULL, OPTION_LOADMAP},
168 {"bmaxdata", required_argument, NULL, OPTION_MAXDATA},
169 {"bmaxstack", required_argument, NULL, OPTION_MAXSTACK},
170 {"bM", required_argument, NULL, OPTION_MODTYPE},
171 {"bmodtype", required_argument, NULL, OPTION_MODTYPE},
172 {"bnoautoimp", no_argument, NULL, OPTION_NOAUTOIMP},
173 {"bnodelcsect", no_argument, NULL, OPTION_IGNORE},
174 {"bnoentry", no_argument, NULL, OPTION_IGNORE},
175 {"bnogc", no_argument, &gc, 0},
176 {"bnso", no_argument, NULL, OPTION_NOAUTOIMP},
177 {"bnostrcmpct", no_argument, NULL, OPTION_NOSTRCMPCT},
178 {"bnotextro", no_argument, &textro, 0},
179 {"bnro", no_argument, &textro, 0},
180 {"bpD", required_argument, NULL, OPTION_PD},
181 {"bpT", required_argument, NULL, OPTION_PT},
182 {"bro", no_argument, &textro, 1},
183 {"bS", required_argument, NULL, OPTION_MAXSTACK},
184 {"bso", no_argument, NULL, OPTION_AUTOIMP},
185 {"bstrcmpct", no_argument, NULL, OPTION_STRCMPCT},
186 {"btextro", no_argument, &textro, 1},
187 {"static", no_argument, NULL, OPTION_NOAUTOIMP},
188 {"unix", no_argument, NULL, OPTION_UNIX},
189 {NULL, no_argument, NULL, 0}
190 };
191
192 /* Options supported by the AIX linker which we do not support: -f,
193 -S, -v, -Z, -bbindcmds, -bbinder, -bbindopts, -bcalls, -bcaps,
194 -bcror15, -bdebugopt, -bdbg, -bdelcsect, -bex?, -bfilelist, -bfl,
195 -bgcbypass, -bglink, -binsert, -bi, -bloadmap, -bl, -bmap, -bnl,
196 -bnobind, -bnocomprld, -bnocrld, -bnoerrmsg, -bnoglink,
197 -bnoloadmap, -bnl, -bnoobjreorder, -bnoquiet, -bnoreorder,
198 -bnotypchk, -bnox, -bquiet, -bR, -brename, -breorder, -btypchk,
199 -bx, -bX, -bxref. */
200
201 /* If the current option starts with -b, change the first : to an =.
202 The AIX linker uses : to separate the option from the argument;
203 changing it to = lets us treat it as a getopt option. */
204 indx = optind;
205 if (indx == 0)
206 indx = 1;
207 if (indx < argc && strncmp (argv[indx], "-b", 2) == 0)
208 {
209 char *s;
210
211 for (s = argv[indx]; *s != '\0'; s++)
212 {
213 if (*s == ':')
214 {
215 *s = '=';
216 break;
217 }
218 }
219 }
220
221 /* We add s and u so to the short options list so that -s and -u on
222 the command line do not match -static and -unix. */
223
224 opterr = 0;
225 optc = getopt_long_only (argc, argv, "-D:H:KT:zsu", longopts, &longind);
226 opterr = prevopterr;
227
228 switch (optc)
229 {
230 case 's':
231 case 'u':
232 default:
233 optind = prevoptind;
234 return 0;
235
236 case 0:
237 /* Long option which just sets a flag. */
238 break;
239
240 case 'D':
241 val = strtol (optarg, &end, 0);
242 if (*end != '\0')
243 einfo ("%P: warning: ignoring invalid -D number %s\n", optarg);
244 else if (val != -1)
245 lang_section_start (".data", exp_intop (val));
246 break;
247
248 case 'H':
249 val = strtoul (optarg, &end, 0);
250 if (*end != '\0'
251 || (val & (val - 1)) != 0)
252 einfo ("%P: warning: ignoring invalid -H number %s\n", optarg);
253 else
254 file_align = val;
255 break;
256
257 case 'K':
258 case 'z':
259 /* FIXME: This should use the page size for the target system. */
260 file_align = 4096;
261 break;
262
263 case 'T':
264 /* On AIX this is the same as GNU ld -Ttext. When we see -T
265 number, we assume the AIX option is intended. Otherwise, we
266 assume the usual GNU ld -T option is intended. We can't just
267 ignore the AIX option, because gcc passes it to the linker. */
268 val = strtoul (optarg, &end, 0);
269 if (*end != '\0')
270 {
271 optind = prevoptind;
272 return 0;
273 }
274 lang_section_start (".text", exp_intop (val));
275 break;
276
277 case OPTION_IGNORE:
278 break;
279
280 case OPTION_AUTOIMP:
281 link_info.static_link = false;
282 break;
283
284 case OPTION_ERNOTOK:
285 force_make_executable = false;
286 break;
287
288 case OPTION_EROK:
289 force_make_executable = true;
290 break;
291
292 case OPTION_EXPORT:
293 gld${EMULATION_NAME}_read_file (optarg, false);
294 break;
295
296 case OPTION_IMPORT:
297 {
298 struct filelist *n;
299 struct filelist **flpp;
300
301 n = (struct filelist *) xmalloc (sizeof (struct filelist));
302 n->next = NULL;
303 n->name = optarg;
304 flpp = &import_files;
305 while (*flpp != NULL)
306 flpp = &(*flpp)->next;
307 *flpp = n;
308 }
309 break;
310
311 case OPTION_LOADMAP:
312 config.map_filename = optarg;
313 break;
314
315 case OPTION_MAXDATA:
316 val = strtoul (optarg, &end, 0);
317 if (*end != '\0')
318 einfo ("%P: warning: ignoring invalid -bmaxdata number %s\n",
319 optarg);
320 else
321 maxdata = val;
322 break;
323
324 case OPTION_MAXSTACK:
325 val = strtoul (optarg, &end, 0);
326 if (*end != '\0')
327 einfo ("%P: warning: ignoring invalid -bmaxstack number %s\n",
328 optarg);
329 else
330 maxstack = val;
331 break;
332
333 case OPTION_MODTYPE:
334 if (*optarg == 'S')
335 {
336 link_info.shared = true;
337 ++optarg;
338 }
339 if (*optarg == '\0' || optarg[1] == '\0')
340 einfo ("%P: warning: ignoring invalid module type %s\n", optarg);
341 else
342 modtype = (*optarg << 8) | optarg[1];
343 break;
344
345 case OPTION_NOAUTOIMP:
346 link_info.static_link = true;
347 break;
348
349 case OPTION_NOSTRCMPCT:
350 link_info.traditional_format = true;
351 break;
352
353 case OPTION_PD:
354 /* This sets the page that the .data section is supposed to
355 start on. The offset within the page should still be the
356 offset within the file, so we need to build an appropriate
357 expression. */
358 val = strtoul (optarg, &end, 0);
359 if (*end != '\0')
360 einfo ("%P: warning: ignoring invalid -pD number %s\n", optarg);
361 else
362 {
363 etree_type *t;
364
365 t = exp_binop ('+',
366 exp_intop (val),
367 exp_binop ('&',
368 exp_nameop (NAME, "."),
369 exp_intop (0xfff)));
370 t = exp_binop ('&',
371 exp_binop ('+', t, exp_intop (31)),
372 exp_intop (~ (bfd_vma) 31));
373 lang_section_start (".data", t);
374 }
375 break;
376
377 case OPTION_PT:
378 /* This set the page that the .text section is supposed to start
379 on. The offset within the page should still be the offset
380 within the file. */
381 val = strtoul (optarg, &end, 0);
382 if (*end != '\0')
383 einfo ("%P: warning: ignoring invalid -pT number %s\n", optarg);
384 else
385 {
386 etree_type *t;
387
388 t = exp_binop ('+',
389 exp_intop (val),
390 exp_nameop (SIZEOF_HEADERS, NULL));
391 t = exp_binop ('&',
392 exp_binop ('+', t, exp_intop (31)),
393 exp_intop (~ (bfd_vma) 31));
394 lang_section_start (".text", t);
395 }
396 break;
397
398 case OPTION_STRCMPCT:
399 link_info.traditional_format = false;
400 break;
401
402 case OPTION_UNIX:
403 unix_ld = true;
404 break;
405 }
406
407 return 1;
408 }
409
410 /* This is called when an input file can not be recognized as a BFD
411 object or an archive. If the file starts with #!, we must treat it
412 as an import file. This is for AIX compatibility. */
413
414 static boolean
415 gld${EMULATION_NAME}_unrecognized_file (entry)
416 lang_input_statement_type *entry;
417 {
418 FILE *e;
419 boolean ret;
420
421 e = fopen (entry->filename, FOPEN_RT);
422 if (e == NULL)
423 return false;
424
425 ret = false;
426
427 if (getc (e) == '#' && getc (e) == '!')
428 {
429 struct filelist *n;
430 struct filelist **flpp;
431
432 n = (struct filelist *) xmalloc (sizeof (struct filelist));
433 n->next = NULL;
434 n->name = entry->filename;
435 flpp = &import_files;
436 while (*flpp != NULL)
437 flpp = &(*flpp)->next;
438 *flpp = n;
439
440 ret = true;
441 entry->loaded = true;
442 }
443
444 fclose (e);
445
446 return ret;
447 }
448
449 /* This is called after the input files have been opened. */
450
451 static void
452 gld${EMULATION_NAME}_after_open ()
453 {
454 boolean r;
455 struct set_info *p;
456
457 /* Call ldctor_build_sets, after pretending that this is a
458 relocateable link. We do this because AIX requires relocation
459 entries for all references to symbols, even in a final
460 executable. Of course, we only want to do this if we are
461 producing an XCOFF output file. */
462 r = link_info.relocateable;
463 if (strstr (bfd_get_target (output_bfd), "xcoff") != NULL)
464 link_info.relocateable = true;
465 ldctor_build_sets ();
466 link_info.relocateable = r;
467
468 /* For each set, record the size, so that the XCOFF backend can
469 output the correct csect length. */
470 for (p = sets; p != (struct set_info *) NULL; p = p->next)
471 {
472 bfd_size_type size;
473
474 /* If the symbol is defined, we may have been invoked from
475 collect, and the sets may already have been built, so we do
476 not do anything. */
477 if (p->h->type == bfd_link_hash_defined
478 || p->h->type == bfd_link_hash_defweak)
479 continue;
480
481 if (p->reloc != BFD_RELOC_CTOR)
482 {
483 /* Handle this if we need to. */
484 abort ();
485 }
486
487 size = (p->count + 2) * 4;
488 if (! bfd_xcoff_link_record_set (output_bfd, &link_info, p->h, size))
489 einfo ("%F%P: bfd_xcoff_link_record_set failed: %E\n");
490 }
491 }
492
493 /* This is called after the sections have been attached to output
494 sections, but before any sizes or addresses have been set. */
495
496 static void
497 gld${EMULATION_NAME}_before_allocation ()
498 {
499 struct filelist *fl;
500 struct export_symbol_list *el;
501 char *libpath;
502 asection *special_sections[6];
503 int i;
504
505 /* Handle the import and export files, if any. */
506 for (fl = import_files; fl != NULL; fl = fl->next)
507 gld${EMULATION_NAME}_read_file (fl->name, true);
508 for (el = export_symbols; el != NULL; el = el->next)
509 {
510 struct bfd_link_hash_entry *h;
511
512 h = bfd_link_hash_lookup (link_info.hash, el->name, false, false, false);
513 if (h == NULL)
514 einfo ("%P%F: bfd_link_hash_lookup of export symbol failed: %E\n");
515 if (! bfd_xcoff_export_symbol (output_bfd, &link_info, h, el->syscall))
516 einfo ("%P%F: bfd_xcoff_export_symbol failed: %E\n");
517 }
518
519 /* Track down all relocations called for by the linker script (these
520 are typically constructor/destructor entries created by
521 CONSTRUCTORS) and let the backend know it will need to create
522 .loader relocs for them. */
523 lang_for_each_statement (gld${EMULATION_NAME}_find_relocs);
524
525 /* We need to build LIBPATH from the -L arguments. If any -rpath
526 arguments were used, though, we use -rpath instead, as a GNU
527 extension. */
528 if (command_line.rpath != NULL)
529 libpath = command_line.rpath;
530 else if (search_head == NULL)
531 libpath = (char *) "";
532 else
533 {
534 size_t len;
535 search_dirs_type *search;
536
537 len = strlen (search_head->name);
538 libpath = xmalloc (len + 1);
539 strcpy (libpath, search_head->name);
540 for (search = search_head->next; search != NULL; search = search->next)
541 {
542 size_t nlen;
543
544 nlen = strlen (search->name);
545 libpath = xrealloc (libpath, len + nlen + 2);
546 libpath[len] = ':';
547 strcpy (libpath + len + 1, search->name);
548 len += nlen + 1;
549 }
550 }
551
552 /* Let the XCOFF backend set up the .loader section. */
553 if (! bfd_xcoff_size_dynamic_sections (output_bfd, &link_info, libpath,
554 entry_symbol, file_align,
555 maxstack, maxdata,
556 gc && ! unix_ld ? true : false,
557 modtype,
558 textro ? true : false,
559 unix_ld,
560 special_sections))
561 einfo ("%P%F: failed to set dynamic section sizes: %E\n");
562
563 /* Look through the special sections, and put them in the right
564 place in the link ordering. This is especially magic. */
565 for (i = 0; i < 6; i++)
566 {
567 asection *sec;
568 lang_output_section_statement_type *os;
569 lang_statement_union_type **pls;
570 lang_input_section_type *is;
571 const char *oname;
572 boolean start;
573
574 sec = special_sections[i];
575 if (sec == NULL)
576 continue;
577
578 /* Remove this section from the list of the output section.
579 This assumes we know what the script looks like. */
580 is = NULL;
581 os = lang_output_section_find (sec->output_section->name);
582 if (os == NULL)
583 einfo ("%P%F: can't find output section %s\n",
584 sec->output_section->name);
585 for (pls = &os->children.head; *pls != NULL; pls = &(*pls)->next)
586 {
587 if ((*pls)->header.type == lang_input_section_enum
588 && (*pls)->input_section.section == sec)
589 {
590 is = (lang_input_section_type *) *pls;
591 *pls = (*pls)->next;
592 break;
593 }
594 if ((*pls)->header.type == lang_wild_statement_enum)
595 {
596 lang_statement_union_type **pwls;
597
598 for (pwls = &(*pls)->wild_statement.children.head;
599 *pwls != NULL;
600 pwls = &(*pwls)->next)
601 {
602 if ((*pwls)->header.type == lang_input_section_enum
603 && (*pwls)->input_section.section == sec)
604 {
605 is = (lang_input_section_type *) *pwls;
606 *pwls = (*pwls)->next;
607 break;
608 }
609 }
610 if (is != NULL)
611 break;
612 }
613 }
614
615 if (is == NULL)
616 einfo ("%P%F: can't find %s in output section\n",
617 bfd_get_section_name (sec->owner, sec));
618
619 /* Now figure out where the section should go. */
620 switch (i)
621 {
622 default: /* to avoid warnings */
623 case 0:
624 /* _text */
625 oname = ".text";
626 start = true;
627 break;
628 case 1:
629 /* _etext */
630 oname = ".text";
631 start = false;
632 break;
633 case 2:
634 /* _data */
635 oname = ".data";
636 start = true;
637 break;
638 case 3:
639 /* _edata */
640 oname = ".data";
641 start = false;
642 break;
643 case 4:
644 case 5:
645 /* _end and end */
646 oname = ".bss";
647 start = false;
648 break;
649 }
650
651 os = lang_output_section_find (oname);
652
653 if (start)
654 {
655 is->header.next = os->children.head;
656 os->children.head = (lang_statement_union_type *) is;
657 }
658 else
659 {
660 is->header.next = NULL;
661 lang_statement_append (&os->children,
662 (lang_statement_union_type *) is,
663 &is->header.next);
664 }
665 }
666 }
667
668 /* Read an import or export file. For an import file, this is called
669 by the before_allocation emulation routine. For an export file,
670 this is called by the parse_args emulation routine. */
671
672 static void
673 gld${EMULATION_NAME}_read_file (filename, import)
674 const char *filename;
675 boolean import;
676 {
677 struct obstack *o;
678 FILE *f;
679 int lineno;
680 int c;
681 boolean keep;
682 const char *imppath;
683 const char *impfile;
684 const char *impmember;
685
686 o = (struct obstack *) xmalloc (sizeof (struct obstack));
687 obstack_specify_allocation (o, 0, 0, xmalloc, gld${EMULATION_NAME}_free);
688
689 f = fopen (filename, FOPEN_RT);
690 if (f == NULL)
691 {
692 bfd_set_error (bfd_error_system_call);
693 einfo ("%F%s: %E\n", filename);
694 }
695
696 keep = false;
697
698 imppath = NULL;
699 impfile = NULL;
700 impmember = NULL;
701
702 lineno = 0;
703 while ((c = getc (f)) != EOF)
704 {
705 char *s;
706 char *symname;
707 boolean syscall;
708 bfd_vma address;
709 struct bfd_link_hash_entry *h;
710
711 if (c != '\n')
712 {
713 obstack_1grow (o, c);
714 continue;
715 }
716
717 obstack_1grow (o, '\0');
718 ++lineno;
719
720 s = (char *) obstack_base (o);
721 while (isspace ((unsigned char) *s))
722 ++s;
723 if (*s == '\0'
724 || *s == '*'
725 || (*s == '#' && s[1] == ' ')
726 || (! import && *s == '#' && s[1] == '!'))
727 {
728 obstack_free (o, obstack_base (o));
729 continue;
730 }
731
732 if (*s == '#' && s[1] == '!')
733 {
734 s += 2;
735 while (isspace ((unsigned char) *s))
736 ++s;
737 if (*s == '\0')
738 {
739 imppath = NULL;
740 impfile = NULL;
741 impmember = NULL;
742 obstack_free (o, obstack_base (o));
743 }
744 else if (*s == '(')
745 einfo ("%F%s%d: #! ([member]) is not supported in import files\n",
746 filename, lineno);
747 else
748 {
749 char cs;
750 char *file;
751
752 (void) obstack_finish (o);
753 keep = true;
754 imppath = s;
755 file = NULL;
756 while (! isspace ((unsigned char) *s) && *s != '(' && *s != '\0')
757 {
758 if (*s == '/')
759 file = s + 1;
760 ++s;
761 }
762 if (file != NULL)
763 {
764 file[-1] = '\0';
765 impfile = file;
766 if (imppath == file - 1)
767 imppath = "/";
768 }
769 else
770 {
771 impfile = imppath;
772 imppath = "";
773 }
774 cs = *s;
775 *s = '\0';
776 while (isspace ((unsigned char) cs))
777 {
778 ++s;
779 cs = *s;
780 }
781 if (cs != '(')
782 {
783 impmember = "";
784 if (cs != '\0')
785 einfo ("%s:%d: warning: syntax error in import file\n",
786 filename, lineno);
787 }
788 else
789 {
790 ++s;
791 impmember = s;
792 while (*s != ')' && *s != '\0')
793 ++s;
794 if (*s == ')')
795 *s = '\0';
796 else
797 einfo ("%s:%d: warning: syntax error in import file\n",
798 filename, lineno);
799 }
800 }
801
802 continue;
803 }
804
805 /* This is a symbol to be imported or exported. */
806 symname = s;
807 syscall = false;
808 address = (bfd_vma) -1;
809
810 while (! isspace ((unsigned char) *s) && *s != '\0')
811 ++s;
812 if (*s != '\0')
813 {
814 char *se;
815
816 *s++ = '\0';
817
818 while (isspace ((unsigned char) *s))
819 ++s;
820
821 se = s;
822 while (! isspace ((unsigned char) *se) && *se != '\0')
823 ++se;
824 if (*se != '\0')
825 {
826 *se++ = '\0';
827 while (isspace ((unsigned char) *se))
828 ++se;
829 if (*se != '\0')
830 einfo ("%s%d: warning: syntax error in import/export file\n",
831 filename, lineno);
832 }
833
834 if (s == se)
835 {
836 /* There was no address after all. */
837 }
838 else if (strcasecmp (s, "svc") == 0
839 || strcasecmp (s, "syscall") == 0)
840 syscall = true;
841 else
842 {
843 char *end;
844
845 address = strtoul (s, &end, 0);
846 if (*end != '\0')
847 einfo ("%s:%d: warning: syntax error in import/export file\n",
848 filename, lineno);
849 }
850 }
851
852 if (! import)
853 {
854 struct export_symbol_list *n;
855
856 ldlang_add_undef (symname);
857 n = ((struct export_symbol_list *)
858 xmalloc (sizeof (struct export_symbol_list)));
859 n->next = export_symbols;
860 n->name = buystring (symname);
861 n->syscall = syscall;
862 export_symbols = n;
863 }
864 else
865 {
866 h = bfd_link_hash_lookup (link_info.hash, symname, false, false,
867 true);
868 if (h == NULL || h->type == bfd_link_hash_new)
869 {
870 /* We can just ignore attempts to import an unreferenced
871 symbol. */
872 }
873 else
874 {
875 if (! bfd_xcoff_import_symbol (output_bfd, &link_info, h,
876 address, imppath, impfile,
877 impmember))
878 einfo ("%X%s:%d: failed to import symbol %s: %E\n",
879 filename, lineno, symname);
880 }
881 }
882
883 obstack_free (o, obstack_base (o));
884 }
885
886 if (obstack_object_size (o) > 0)
887 {
888 einfo ("%s:%d: warning: ignoring unterminated last line\n",
889 filename, lineno);
890 obstack_free (o, obstack_base (o));
891 }
892
893 if (! keep)
894 {
895 obstack_free (o, NULL);
896 free (o);
897 }
898 }
899
900 /* This routine saves us from worrying about declaring free. */
901
902 static void
903 gld${EMULATION_NAME}_free (p)
904 PTR p;
905 {
906 free (p);
907 }
908
909 /* This is called by the before_allocation routine via
910 lang_for_each_statement. It looks for relocations and assignments
911 to symbols. */
912
913 static void
914 gld${EMULATION_NAME}_find_relocs (s)
915 lang_statement_union_type *s;
916 {
917 if (s->header.type == lang_reloc_statement_enum)
918 {
919 lang_reloc_statement_type *rs;
920
921 rs = &s->reloc_statement;
922 if (rs->name == NULL)
923 einfo ("%F%P: only relocations against symbols are permitted\n");
924 if (! bfd_xcoff_link_count_reloc (output_bfd, &link_info, rs->name))
925 einfo ("%F%P: bfd_xcoff_link_count_reloc failed: %E\n");
926 }
927
928 if (s->header.type == lang_assignment_statement_enum)
929 gld${EMULATION_NAME}_find_exp_assignment (s->assignment_statement.exp);
930 }
931
932 /* Look through an expression for an assignment statement. */
933
934 static void
935 gld${EMULATION_NAME}_find_exp_assignment (exp)
936 etree_type *exp;
937 {
938 struct bfd_link_hash_entry *h;
939
940 switch (exp->type.node_class)
941 {
942 case etree_provide:
943 h = bfd_link_hash_lookup (link_info.hash, exp->assign.dst,
944 false, false, false);
945 if (h == NULL)
946 break;
947 /* Fall through. */
948 case etree_assign:
949 if (strcmp (exp->assign.dst, ".") != 0)
950 {
951 if (! bfd_xcoff_record_link_assignment (output_bfd, &link_info,
952 exp->assign.dst))
953 einfo ("%P%F: failed to record assignment to %s: %E\n",
954 exp->assign.dst);
955 }
956 gld${EMULATION_NAME}_find_exp_assignment (exp->assign.src);
957 break;
958
959 case etree_binary:
960 gld${EMULATION_NAME}_find_exp_assignment (exp->binary.lhs);
961 gld${EMULATION_NAME}_find_exp_assignment (exp->binary.rhs);
962 break;
963
964 case etree_trinary:
965 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.cond);
966 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.lhs);
967 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.rhs);
968 break;
969
970 case etree_unary:
971 gld${EMULATION_NAME}_find_exp_assignment (exp->unary.child);
972 break;
973
974 default:
975 break;
976 }
977 }
978
979 static char *
980 gld${EMULATION_NAME}_get_script(isfile)
981 int *isfile;
982 EOF
983
984 if test -n "$COMPILE_IN"
985 then
986 # Scripts compiled in.
987
988 # sed commands to quote an ld script as a C string.
989 sc="-f stringify.sed"
990
991 cat >>e${EMULATION_NAME}.c <<EOF
992 {
993 *isfile = 0;
994
995 if (link_info.relocateable == true && config.build_constructors == true)
996 return
997 EOF
998 sed $sc ldscripts/${EMULATION_NAME}.xu >> e${EMULATION_NAME}.c
999 echo ' ; else if (link_info.relocateable == true) return' >> e${EMULATION_NAME}.c
1000 sed $sc ldscripts/${EMULATION_NAME}.xr >> e${EMULATION_NAME}.c
1001 echo ' ; else if (!config.text_read_only) return' >> e${EMULATION_NAME}.c
1002 sed $sc ldscripts/${EMULATION_NAME}.xbn >> e${EMULATION_NAME}.c
1003 echo ' ; else if (!config.magic_demand_paged) return' >> e${EMULATION_NAME}.c
1004 sed $sc ldscripts/${EMULATION_NAME}.xn >> e${EMULATION_NAME}.c
1005 echo ' ; else return' >> e${EMULATION_NAME}.c
1006 sed $sc ldscripts/${EMULATION_NAME}.x >> e${EMULATION_NAME}.c
1007 echo '; }' >> e${EMULATION_NAME}.c
1008
1009 else
1010 # Scripts read from the filesystem.
1011
1012 cat >>e${EMULATION_NAME}.c <<EOF
1013 {
1014 *isfile = 1;
1015
1016 if (link_info.relocateable == true && config.build_constructors == true)
1017 return "ldscripts/${EMULATION_NAME}.xu";
1018 else if (link_info.relocateable == true)
1019 return "ldscripts/${EMULATION_NAME}.xr";
1020 else if (!config.text_read_only)
1021 return "ldscripts/${EMULATION_NAME}.xbn";
1022 else if (!config.magic_demand_paged)
1023 return "ldscripts/${EMULATION_NAME}.xn";
1024 else
1025 return "ldscripts/${EMULATION_NAME}.x";
1026 }
1027 EOF
1028
1029 fi
1030
1031 cat >>e${EMULATION_NAME}.c <<EOF
1032
1033 struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
1034 {
1035 gld${EMULATION_NAME}_before_parse,
1036 syslib_default,
1037 hll_default,
1038 after_parse_default,
1039 gld${EMULATION_NAME}_after_open,
1040 after_allocation_default,
1041 set_output_arch_default,
1042 ldemul_default_target,
1043 gld${EMULATION_NAME}_before_allocation,
1044 gld${EMULATION_NAME}_get_script,
1045 "${EMULATION_NAME}",
1046 "${OUTPUT_FORMAT}",
1047 0, /* finish */
1048 0, /* create_output_section_statements */
1049 0, /* open_dynamic_archive */
1050 0, /* place_orphan */
1051 0, /* set_symbols */
1052 gld${EMULATION_NAME}_parse_args,
1053 gld${EMULATION_NAME}_unrecognized_file,
1054 NULL /* find_potential_libraries */
1055 };
1056 EOF