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