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