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