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