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