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