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