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