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