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