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