]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - ld/emultempl/aix.em
Update year range in copyright notice of binutils files
[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
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}
fd67aa11 12 Copyright (C) 1991-2024 Free Software Foundation, Inc.
252b5132
RH
13 Written by Steve Chamberlain <sac@cygnus.com>
14 AIX support by Ian Lance Taylor <ian@cygnus.com>
3b1b01cf 15 AIX 64 bit support by Tom Rix <trix@redhat.com>
252b5132 16
f96b4a7b 17 This file is part of the GNU Binutils.
252b5132 18
f96b4a7b
NC
19 This program is free software; you can redistribute it and/or modify
20 it under the terms of the GNU General Public License as published by
21 the Free Software Foundation; either version 3 of the License, or
22 (at your option) any later version.
252b5132 23
f96b4a7b
NC
24 This program is distributed in the hope that it will be useful,
25 but WITHOUT ANY WARRANTY; without even the implied warranty of
26 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 GNU General Public License for more details.
252b5132 28
f96b4a7b
NC
29 You should have received a copy of the GNU General Public License
30 along with this program; if not, write to the Free Software
31 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
32 MA 02110-1301, USA. */
252b5132
RH
33
34#define TARGET_IS_${EMULATION_NAME}
35
252b5132 36#include "sysdep.h"
3db64b00 37#include "bfd.h"
252b5132 38#include "libiberty.h"
3882b010 39#include "safe-ctype.h"
252b5132
RH
40#include "getopt.h"
41#include "obstack.h"
42#include "bfdlink.h"
1ff6de03 43#include "ctf-api.h"
252b5132 44
252b5132
RH
45#include "ld.h"
46#include "ldmain.h"
252b5132
RH
47#include "ldmisc.h"
48#include "ldexp.h"
49#include "ldlang.h"
20713691
AM
50#include "ldfile.h"
51#include "ldemul.h"
252b5132 52#include "ldctor.h"
df2a7313 53#include <ldgram.h>
252b5132 54
3b1b01cf
TR
55#include "coff/internal.h"
56#include "coff/xcoff.h"
f1f0d9ab
TR
57#include "libcoff.h"
58#include "libxcoff.h"
6ea7de32 59#include "xcofflink.h"
3b1b01cf 60
f38a2680 61static void gld${EMULATION_NAME}_read_file (const char *, bool);
0c7a8e5a
AM
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
2d23f965
CC
66static asection *xcoff_add_stub_section (const char *, asection *);
67static void xcoff_layout_sections_again (void);
68
69static struct bfd_xcoff_link_params params = {
70 NULL,
71 &xcoff_add_stub_section,
72 &xcoff_layout_sections_again
73};
74
252b5132
RH
75
76/* The file alignment required for each section. */
77static unsigned long file_align;
78
79/* The maximum size the stack is permitted to grow. This is stored in
80 the a.out header. */
81static unsigned long maxstack;
82
83/* The maximum data size. This is stored in the a.out header. */
84static unsigned long maxdata;
85
86/* Whether to perform garbage collection. */
87static int gc = 1;
88
89/* The module type to use. */
90static unsigned short modtype = ('1' << 8) | 'L';
91
92/* Whether the .text section must be read-only (i.e., no relocs
93 permitted). */
94static int textro;
95
b64232cc
RS
96/* A mask of XCOFF_EXPALL and XCOFF_EXPFULL flags, as set by their
97 associated -b and -bno options. */
98static unsigned int auto_export_flags;
99
100/* A mask of auto_export_flags bits that were explicitly set on the
101 command line. */
102static unsigned int explicit_auto_export_flags;
103
252b5132
RH
104/* Whether to implement Unix like linker semantics. */
105static int unix_ld;
106
107/* Structure used to hold import file list. */
108
109struct filelist
110{
111 struct filelist *next;
112 const char *name;
113};
114
115/* List of import files. */
116static struct filelist *import_files;
117
118/* List of export symbols read from the export files. */
119
120struct export_symbol_list
121{
122 struct export_symbol_list *next;
123 const char *name;
252b5132
RH
124};
125
126static struct export_symbol_list *export_symbols;
127
3b1b01cf
TR
128/* Maintains the 32 or 64 bit mode state of import file */
129static unsigned int symbol_mode = 0x04;
130
742aeb63
TR
131/* Which symbol modes are valid */
132static unsigned int symbol_mode_mask = 0x0d;
133
134/* Whether this is a 64 bit link */
135static int is_64bit = 0;
136
137/* Which syscalls from import file are valid */
138static unsigned int syscall_mask = 0x77;
139
9a4c7f16
TR
140/* fake file for -binitfini support */
141static lang_input_statement_type *initfini_file;
69f284c7 142
7fc44b9c 143/* Whether to do run time linking
ca10c62d 144 -brtl enables, -bnortl and -bnortllib disable. */
f1f0d9ab 145static int rtld;
69f284c7 146
aa67bccf
TR
147/* Explicit command line library path, -blibpath */
148static char *command_line_blibpath = NULL;
149
2d23f965
CC
150/* Fake input file for stubs. */
151static lang_input_statement_type *stub_file;
152
252b5132
RH
153/* This routine is called before anything else is done. */
154
155static void
0c7a8e5a 156gld${EMULATION_NAME}_before_parse (void)
252b5132 157{
5e2f1575 158 ldfile_set_output_arch ("${OUTPUT_ARCH}", bfd_arch_`echo ${ARCH} | sed -e 's/:.*//'`);
f1f0d9ab 159
f38a2680
AM
160 input_flags.dynamic = true;
161 config.has_shared = true;
3b1b01cf 162
9a4c7f16
TR
163 /* The link_info.[init|fini]_functions are initialized in ld/lexsup.c.
164 Override them here so we can use the link_info.init_function as a
7fc44b9c 165 state flag that lets the backend know that -binitfini has been done. */
9a4c7f16 166
3b1b01cf
TR
167 link_info.init_function = NULL;
168 link_info.fini_function = NULL;
2d23f965 169
252b5132
RH
170}
171
172/* Handle AIX specific options. */
173
3bcf5557 174enum
249172c3 175 {
3b1b01cf 176 OPTION_IGNORE = 300,
fab80407
AM
177 OPTION_AUTOIMP,
178 OPTION_ERNOTOK,
179 OPTION_EROK,
b64232cc
RS
180 OPTION_EXPALL,
181 OPTION_EXPFULL,
fab80407
AM
182 OPTION_EXPORT,
183 OPTION_IMPORT,
184 OPTION_INITFINI,
185 OPTION_LOADMAP,
186 OPTION_MAXDATA,
187 OPTION_MAXSTACK,
188 OPTION_MODTYPE,
189 OPTION_NOAUTOIMP,
b64232cc
RS
190 OPTION_NOEXPALL,
191 OPTION_NOEXPFULL,
fab80407
AM
192 OPTION_NOSTRCMPCT,
193 OPTION_PD,
194 OPTION_PT,
195 OPTION_STRCMPCT,
742aeb63
TR
196 OPTION_UNIX,
197 OPTION_32,
198 OPTION_64,
aa67bccf
TR
199 OPTION_LIBPATH,
200 OPTION_NOLIBPATH,
3b1b01cf
TR
201 };
202
3bcf5557 203static void
0c7a8e5a
AM
204gld${EMULATION_NAME}_add_options
205 (int ns, char **shortopts, int nl, struct option **longopts,
206 int nrl ATTRIBUTE_UNUSED, struct option **really_longopts ATTRIBUTE_UNUSED)
3bcf5557
AM
207{
208 static const char xtra_short[] = "D:H:KT:z";
209 static const struct option xtra_long[] = {
9a4c7f16
TR
210 /* -binitfini has special handling in the linker backend. The native linker
211 uses the arguemnts to generate a table of init and fini functions for
212 the executable. The important use for this option is to support aix 4.2+
213 c++ constructors and destructors. This is tied into gcc via collect2.c.
7fc44b9c 214
9a4c7f16
TR
215 The function table is accessed by the runtime linker/loader by checking if
216 the first symbol in the loader symbol table is __rtinit. The gnu linker
217 generates this symbol and makes it the first loader symbol. */
3b1b01cf 218
252b5132
RH
219 {"basis", no_argument, NULL, OPTION_IGNORE},
220 {"bautoimp", no_argument, NULL, OPTION_AUTOIMP},
221 {"bcomprld", no_argument, NULL, OPTION_IGNORE},
222 {"bcrld", no_argument, NULL, OPTION_IGNORE},
223 {"bcror31", no_argument, NULL, OPTION_IGNORE},
224 {"bD", required_argument, NULL, OPTION_MAXDATA},
225 {"bE", required_argument, NULL, OPTION_EXPORT},
226 {"bernotok", no_argument, NULL, OPTION_ERNOTOK},
227 {"berok", no_argument, NULL, OPTION_EROK},
228 {"berrmsg", no_argument, NULL, OPTION_IGNORE},
b64232cc
RS
229 {"bexpall", no_argument, NULL, OPTION_EXPALL},
230 {"bexpfull", no_argument, NULL, OPTION_EXPFULL},
252b5132 231 {"bexport", required_argument, NULL, OPTION_EXPORT},
ced323bd 232 {"bbigtoc", no_argument, NULL, OPTION_IGNORE},
252b5132
RH
233 {"bf", no_argument, NULL, OPTION_ERNOTOK},
234 {"bgc", no_argument, &gc, 1},
235 {"bh", required_argument, NULL, OPTION_IGNORE},
236 {"bhalt", required_argument, NULL, OPTION_IGNORE},
237 {"bI", required_argument, NULL, OPTION_IMPORT},
238 {"bimport", required_argument, NULL, OPTION_IMPORT},
3b1b01cf 239 {"binitfini", required_argument, NULL, OPTION_INITFINI},
252b5132
RH
240 {"bl", required_argument, NULL, OPTION_LOADMAP},
241 {"bloadmap", required_argument, NULL, OPTION_LOADMAP},
242 {"bmaxdata", required_argument, NULL, OPTION_MAXDATA},
243 {"bmaxstack", required_argument, NULL, OPTION_MAXSTACK},
244 {"bM", required_argument, NULL, OPTION_MODTYPE},
245 {"bmodtype", required_argument, NULL, OPTION_MODTYPE},
246 {"bnoautoimp", no_argument, NULL, OPTION_NOAUTOIMP},
b64232cc
RS
247 {"bnoexpall", no_argument, NULL, OPTION_NOEXPALL},
248 {"bnoexpfull", no_argument, NULL, OPTION_NOEXPFULL},
252b5132
RH
249 {"bnodelcsect", no_argument, NULL, OPTION_IGNORE},
250 {"bnoentry", no_argument, NULL, OPTION_IGNORE},
251 {"bnogc", no_argument, &gc, 0},
252 {"bnso", no_argument, NULL, OPTION_NOAUTOIMP},
253 {"bnostrcmpct", no_argument, NULL, OPTION_NOSTRCMPCT},
254 {"bnotextro", no_argument, &textro, 0},
255 {"bnro", no_argument, &textro, 0},
256 {"bpD", required_argument, NULL, OPTION_PD},
257 {"bpT", required_argument, NULL, OPTION_PT},
258 {"bro", no_argument, &textro, 1},
69f284c7 259 {"brtl", no_argument, &rtld, 1},
ca10c62d
TR
260 {"bnortl", no_argument, &rtld, 0},
261 {"bnortllib", no_argument, &rtld, 0},
252b5132
RH
262 {"bS", required_argument, NULL, OPTION_MAXSTACK},
263 {"bso", no_argument, NULL, OPTION_AUTOIMP},
264 {"bstrcmpct", no_argument, NULL, OPTION_STRCMPCT},
265 {"btextro", no_argument, &textro, 1},
742aeb63
TR
266 {"b32", no_argument, NULL, OPTION_32},
267 {"b64", no_argument, NULL, OPTION_64},
252b5132
RH
268 {"static", no_argument, NULL, OPTION_NOAUTOIMP},
269 {"unix", no_argument, NULL, OPTION_UNIX},
aa67bccf
TR
270 {"blibpath", required_argument, NULL, OPTION_LIBPATH},
271 {"bnolibpath", required_argument, NULL, OPTION_NOLIBPATH},
252b5132
RH
272 {NULL, no_argument, NULL, 0}
273 };
274
a72173de 275 /* Options supported by the AIX linker which we do not support:
252b5132
RH
276 -S, -v, -Z, -bbindcmds, -bbinder, -bbindopts, -bcalls, -bcaps,
277 -bcror15, -bdebugopt, -bdbg, -bdelcsect, -bex?, -bfilelist, -bfl,
278 -bgcbypass, -bglink, -binsert, -bi, -bloadmap, -bl, -bmap, -bnl,
279 -bnobind, -bnocomprld, -bnocrld, -bnoerrmsg, -bnoglink,
280 -bnoloadmap, -bnl, -bnoobjreorder, -bnoquiet, -bnoreorder,
281 -bnotypchk, -bnox, -bquiet, -bR, -brename, -breorder, -btypchk,
282 -bx, -bX, -bxref. */
283
3bcf5557
AM
284 *shortopts = (char *) xrealloc (*shortopts, ns + sizeof (xtra_short));
285 memcpy (*shortopts + ns, &xtra_short, sizeof (xtra_short));
0c7a8e5a
AM
286 *longopts = xrealloc (*longopts,
287 nl * sizeof (struct option) + sizeof (xtra_long));
3bcf5557
AM
288 memcpy (*longopts + nl, &xtra_long, sizeof (xtra_long));
289}
290
f38a2680 291static bool
0c7a8e5a 292gld${EMULATION_NAME}_parse_args (int argc, char **argv)
3bcf5557
AM
293{
294 int indx;
295
252b5132
RH
296 /* If the current option starts with -b, change the first : to an =.
297 The AIX linker uses : to separate the option from the argument;
298 changing it to = lets us treat it as a getopt option. */
299 indx = optind;
fab80407 300 if (indx == 0)
249172c3 301 indx = 1;
fab80407 302
08dedd66 303 if (indx < argc && startswith (argv[indx], "-b"))
fab80407
AM
304 {
305 char *s;
306
307 for (s = argv[indx]; *s != '\0'; s++)
308 {
309 if (*s == ':')
310 {
311 *s = '=';
312 break;
313 }
314 }
252b5132 315 }
f38a2680 316 return false;
3bcf5557 317}
3b1b01cf 318
a72173de
TG
319/* Helper for option '-f', which specify a list of input files.
320 Contrary to the native linker, we don't support shell patterns
321 (simply because glob isn't always available). */
322
323static void
324read_file_list (const char *filename)
325{
326 FILE *f;
327 /* An upper bound on the number of characters in the file. */
328 long pos;
329 /* File in memory. */
330 char *buffer;
331 size_t len;
332 char *b;
333 char *e;
334
335 f = fopen (filename, FOPEN_RT);
336 if (f == NULL)
337 {
d003af55 338 einfo (_("%F%P: cannot open %s\n"), filename);
a72173de
TG
339 return;
340 }
341 if (fseek (f, 0L, SEEK_END) == -1)
342 goto error;
343 pos = ftell (f);
344 if (pos == -1)
345 goto error;
346 if (fseek (f, 0L, SEEK_SET) == -1)
347 goto error;
348
349 buffer = (char *) xmalloc (pos + 1);
350 len = fread (buffer, sizeof (char), pos, f);
351 if (len != (size_t) pos && ferror (f))
352 goto error;
353 /* Add a NUL terminator. */
354 buffer[len] = '\0';
355 fclose (f);
356
357 /* Parse files. */
358 b = buffer;
359 while (1)
360 {
361 /* Skip empty lines. */
362 while (*b == '\n' || *b == '\r')
6c19b93b 363 b++;
a72173de
TG
364
365 /* Stop if end of buffer. */
366 if (b == buffer + len)
6c19b93b 367 break;
a72173de
TG
368
369 /* Eat any byte until end of line. */
370 for (e = b; *e != '\0'; e++)
6c19b93b
AM
371 if (*e == '\n' || *e == '\r')
372 break;
a72173de
TG
373
374 /* Replace end of line by nul. */
375 if (*e != '\0')
6c19b93b 376 *e++ = '\0';
e4492aa0 377
a72173de 378 if (b != e)
6c19b93b 379 lang_add_input_file (b, lang_input_file_is_search_file_enum, NULL);
a72173de
TG
380 b = e;
381 }
382 return;
383
384 error:
d003af55 385 einfo (_("%F%P: cannot read %s\n"), optarg);
a72173de
TG
386 fclose (f);
387}
388
f38a2680 389static bool
0c7a8e5a 390gld${EMULATION_NAME}_handle_option (int optc)
3bcf5557
AM
391{
392 bfd_signed_vma val;
393 const char *end;
252b5132
RH
394
395 switch (optc)
396 {
252b5132 397 default:
f38a2680 398 return false;
252b5132
RH
399
400 case 0:
401 /* Long option which just sets a flag. */
402 break;
403
a72173de
TG
404 case 'f':
405 /* This overrides --auxiliary. This option specifies a file containing
6c19b93b 406 a list of input files. */
a72173de
TG
407 read_file_list (optarg);
408 break;
409
252b5132 410 case 'D':
c89e8944 411 val = bfd_scan_vma (optarg, &end, 0);
252b5132 412 if (*end != '\0')
d003af55 413 einfo (_("%P: warning: ignoring invalid -D number %s\n"), optarg);
252b5132 414 else if (val != -1)
7a5df8d0 415 lang_section_start (".data", exp_intop (val), NULL);
252b5132
RH
416 break;
417
418 case 'H':
c89e8944 419 val = bfd_scan_vma (optarg, &end, 0);
249172c3 420 if (*end != '\0' || (val & (val - 1)) != 0)
d003af55 421 einfo (_("%P: warning: ignoring invalid -H number %s\n"), optarg);
252b5132
RH
422 else
423 file_align = val;
424 break;
425
426 case 'K':
427 case 'z':
428 /* FIXME: This should use the page size for the target system. */
429 file_align = 4096;
430 break;
431
432 case 'T':
433 /* On AIX this is the same as GNU ld -Ttext. When we see -T
7fc44b9c
AM
434 number, we assume the AIX option is intended. Otherwise, we
435 assume the usual GNU ld -T option is intended. We can't just
436 ignore the AIX option, because gcc passes it to the linker. */
c89e8944 437 val = bfd_scan_vma (optarg, &end, 0);
252b5132 438 if (*end != '\0')
f38a2680 439 return false;
7a5df8d0 440 lang_section_start (".text", exp_intop (val), NULL);
252b5132
RH
441 break;
442
443 case OPTION_IGNORE:
444 break;
445
fab80407
AM
446 case OPTION_INITFINI:
447 {
448 /*
449 * The aix linker init fini has the format :
450 *
451 * -binitfini:[ Initial][:Termination][:Priority]
452 *
453 * it allows the Termination and Priority to be optional.
454 *
455 * Since we support only one init/fini pair, we ignore the Priority.
456 *
457 * Define the special symbol __rtinit.
458 *
459 * strtok does not correctly handle the case of -binitfini::fini: so
460 * do it by hand
461 */
462 char *t, *i, *f;
463
464 i = t = optarg;
465 while (*t && ':' != *t)
466 t++;
467 if (*t)
468 *t++ = 0;
469
470 if (0 != strlen (i))
249172c3 471 link_info.init_function = i;
3b1b01cf 472
fab80407
AM
473 f = t;
474 while (*t && ':' != *t)
475 t++;
476 *t = 0;
3b1b01cf 477
fab80407 478 if (0 != strlen (f))
249172c3 479 link_info.fini_function = f;
3b1b01cf 480 }
249172c3 481 break;
7fc44b9c 482
252b5132 483 case OPTION_AUTOIMP:
f38a2680 484 link_info.static_link = false;
252b5132
RH
485 break;
486
487 case OPTION_ERNOTOK:
95a51568
FS
488 link_info.unresolved_syms_in_objects = RM_DIAGNOSE;
489 link_info.unresolved_syms_in_shared_libs = RM_DIAGNOSE;
252b5132
RH
490 break;
491
492 case OPTION_EROK:
858ef0ce
RS
493 link_info.unresolved_syms_in_objects = RM_IGNORE;
494 link_info.unresolved_syms_in_shared_libs = RM_IGNORE;
252b5132
RH
495 break;
496
b64232cc
RS
497 case OPTION_EXPALL:
498 auto_export_flags |= XCOFF_EXPALL;
499 explicit_auto_export_flags |= XCOFF_EXPALL;
500 break;
501
502 case OPTION_EXPFULL:
503 auto_export_flags |= XCOFF_EXPFULL;
504 explicit_auto_export_flags |= XCOFF_EXPFULL;
505 break;
506
252b5132 507 case OPTION_EXPORT:
f38a2680 508 gld${EMULATION_NAME}_read_file (optarg, false);
252b5132
RH
509 break;
510
511 case OPTION_IMPORT:
512 {
513 struct filelist *n;
514 struct filelist **flpp;
515
516 n = (struct filelist *) xmalloc (sizeof (struct filelist));
517 n->next = NULL;
518 n->name = optarg;
519 flpp = &import_files;
520 while (*flpp != NULL)
521 flpp = &(*flpp)->next;
522 *flpp = n;
523 }
524 break;
525
526 case OPTION_LOADMAP:
527 config.map_filename = optarg;
528 break;
529
530 case OPTION_MAXDATA:
c89e8944 531 val = bfd_scan_vma (optarg, &end, 0);
252b5132 532 if (*end != '\0')
d003af55
AM
533 einfo (_("%P: warning: ignoring invalid -bmaxdata number %s\n"),
534 optarg);
252b5132
RH
535 else
536 maxdata = val;
537 break;
538
539 case OPTION_MAXSTACK:
c89e8944 540 val = bfd_scan_vma (optarg, &end, 0);
252b5132 541 if (*end != '\0')
d003af55 542 einfo (_("%P: warning: ignoring invalid -bmaxstack number %s\n"),
249172c3 543 optarg);
252b5132
RH
544 else
545 maxstack = val;
546 break;
547
548 case OPTION_MODTYPE:
549 if (*optarg == 'S')
550 {
0e1862bb 551 link_info.type = type_dll;
252b5132
RH
552 ++optarg;
553 }
554 if (*optarg == '\0' || optarg[1] == '\0')
d003af55 555 einfo (_("%P: warning: ignoring invalid module type %s\n"), optarg);
252b5132
RH
556 else
557 modtype = (*optarg << 8) | optarg[1];
558 break;
559
560 case OPTION_NOAUTOIMP:
f38a2680 561 link_info.static_link = true;
252b5132
RH
562 break;
563
b64232cc
RS
564 case OPTION_NOEXPALL:
565 auto_export_flags &= ~XCOFF_EXPALL;
566 explicit_auto_export_flags |= XCOFF_EXPALL;
567 break;
568
569 case OPTION_NOEXPFULL:
570 auto_export_flags &= ~XCOFF_EXPFULL;
571 explicit_auto_export_flags |= XCOFF_EXPFULL;
572 break;
573
252b5132 574 case OPTION_NOSTRCMPCT:
f38a2680 575 link_info.traditional_format = true;
252b5132
RH
576 break;
577
578 case OPTION_PD:
579 /* This sets the page that the .data section is supposed to
7fc44b9c
AM
580 start on. The offset within the page should still be the
581 offset within the file, so we need to build an appropriate
582 expression. */
c89e8944 583 val = bfd_scan_vma (optarg, &end, 0);
252b5132 584 if (*end != '\0')
d003af55 585 einfo (_("%P: warning: ignoring invalid -pD number %s\n"), optarg);
252b5132
RH
586 else
587 {
588 etree_type *t;
589
590 t = exp_binop ('+',
591 exp_intop (val),
592 exp_binop ('&',
593 exp_nameop (NAME, "."),
594 exp_intop (0xfff)));
595 t = exp_binop ('&',
596 exp_binop ('+', t, exp_intop (31)),
249172c3 597 exp_intop (~(bfd_vma) 31));
7a5df8d0 598 lang_section_start (".data", t, NULL);
252b5132
RH
599 }
600 break;
601
602 case OPTION_PT:
603 /* This set the page that the .text section is supposed to start
7fc44b9c
AM
604 on. The offset within the page should still be the offset
605 within the file. */
c89e8944 606 val = bfd_scan_vma (optarg, &end, 0);
252b5132 607 if (*end != '\0')
d003af55 608 einfo (_("%P: warning: ignoring invalid -pT number %s\n"), optarg);
252b5132
RH
609 else
610 {
611 etree_type *t;
612
613 t = exp_binop ('+',
614 exp_intop (val),
615 exp_nameop (SIZEOF_HEADERS, NULL));
616 t = exp_binop ('&',
617 exp_binop ('+', t, exp_intop (31)),
249172c3 618 exp_intop (~(bfd_vma) 31));
7a5df8d0 619 lang_section_start (".text", t, NULL);
252b5132
RH
620 }
621 break;
622
623 case OPTION_STRCMPCT:
f38a2680 624 link_info.traditional_format = false;
252b5132
RH
625 break;
626
627 case OPTION_UNIX:
f38a2680 628 unix_ld = true;
252b5132 629 break;
742aeb63
TR
630
631 case OPTION_32:
632 is_64bit = 0;
633 syscall_mask = 0x77;
634 symbol_mode_mask = 0x0d;
635 break;
636
637 case OPTION_64:
638 is_64bit = 1;
639 syscall_mask = 0xcc;
640 symbol_mode_mask = 0x0e;
641 break;
642
aa67bccf 643 case OPTION_LIBPATH:
cc7e59b2 644 command_line_blibpath = optarg;
aa67bccf
TR
645 break;
646
647 case OPTION_NOLIBPATH:
cc7e59b2 648 command_line_blibpath = NULL;
aa67bccf
TR
649 break;
650
252b5132
RH
651 }
652
f38a2680 653 return true;
252b5132
RH
654}
655
656/* This is called when an input file can not be recognized as a BFD
657 object or an archive. If the file starts with #!, we must treat it
658 as an import file. This is for AIX compatibility. */
659
f38a2680 660static bool
0c7a8e5a 661gld${EMULATION_NAME}_unrecognized_file (lang_input_statement_type *entry)
252b5132
RH
662{
663 FILE *e;
f38a2680 664 bool ret;
252b5132
RH
665
666 e = fopen (entry->filename, FOPEN_RT);
667 if (e == NULL)
f38a2680 668 return false;
252b5132 669
f38a2680 670 ret = false;
252b5132
RH
671
672 if (getc (e) == '#' && getc (e) == '!')
673 {
674 struct filelist *n;
675 struct filelist **flpp;
676
677 n = (struct filelist *) xmalloc (sizeof (struct filelist));
678 n->next = NULL;
679 n->name = entry->filename;
680 flpp = &import_files;
681 while (*flpp != NULL)
682 flpp = &(*flpp)->next;
683 *flpp = n;
684
f38a2680
AM
685 ret = true;
686 entry->flags.loaded = true;
252b5132
RH
687 }
688
689 fclose (e);
690
691 return ret;
692}
693
694/* This is called after the input files have been opened. */
695
696static void
0c7a8e5a 697gld${EMULATION_NAME}_after_open (void)
252b5132 698{
0e1862bb 699 enum output_type t;
252b5132
RH
700 struct set_info *p;
701
5c3049d2
AM
702 after_open_default ();
703
252b5132 704 /* Call ldctor_build_sets, after pretending that this is a
1049f94e 705 relocatable link. We do this because AIX requires relocation
252b5132
RH
706 entries for all references to symbols, even in a final
707 executable. Of course, we only want to do this if we are
708 producing an XCOFF output file. */
0e1862bb 709 t = link_info.type;
f13a99db 710 if (strstr (bfd_get_target (link_info.output_bfd), "xcoff") != NULL)
0e1862bb 711 link_info.type = type_relocatable;
252b5132 712 ldctor_build_sets ();
0e1862bb 713 link_info.type = t;
252b5132
RH
714
715 /* For each set, record the size, so that the XCOFF backend can
716 output the correct csect length. */
717 for (p = sets; p != (struct set_info *) NULL; p = p->next)
718 {
719 bfd_size_type size;
720
721 /* If the symbol is defined, we may have been invoked from
7fc44b9c
AM
722 collect, and the sets may already have been built, so we do
723 not do anything. */
252b5132
RH
724 if (p->h->type == bfd_link_hash_defined
725 || p->h->type == bfd_link_hash_defweak)
726 continue;
727
728 if (p->reloc != BFD_RELOC_CTOR)
729 {
730 /* Handle this if we need to. */
731 abort ();
732 }
733
734 size = (p->count + 2) * 4;
f13a99db
AM
735 if (!bfd_xcoff_link_record_set (link_info.output_bfd, &link_info,
736 p->h, size))
d003af55 737 einfo (_("%F%P: bfd_xcoff_link_record_set failed: %E\n"));
252b5132
RH
738 }
739}
740
741/* This is called after the sections have been attached to output
742 sections, but before any sizes or addresses have been set. */
743
744static void
0c7a8e5a 745gld${EMULATION_NAME}_before_allocation (void)
252b5132
RH
746{
747 struct filelist *fl;
748 struct export_symbol_list *el;
749 char *libpath;
3b1b01cf 750 asection *special_sections[XCOFF_NUMBER_OF_SPECIAL_SECTIONS];
9ec59d51
RS
751 static const char *const must_keep_sections[] = {
752 ".text",
753 ".data",
754 ".bss"
755 };
b64232cc 756 unsigned int i, flags;
252b5132
RH
757
758 /* Handle the import and export files, if any. */
759 for (fl = import_files; fl != NULL; fl = fl->next)
f38a2680 760 gld${EMULATION_NAME}_read_file (fl->name, true);
fab80407
AM
761 for (el = export_symbols; el != NULL; el = el->next)
762 {
763 struct bfd_link_hash_entry *h;
764
f38a2680 765 h = bfd_link_hash_lookup (link_info.hash, el->name, false, false, false);
fab80407 766 if (h == NULL)
df5f2391 767 einfo (_("%F%P: bfd_link_hash_lookup of export symbol failed: %E\n"));
f13a99db 768 if (!bfd_xcoff_export_symbol (link_info.output_bfd, &link_info, h))
df5f2391 769 einfo (_("%F%P: bfd_xcoff_export_symbol failed: %E\n"));
fab80407 770 }
252b5132
RH
771
772 /* Track down all relocations called for by the linker script (these
773 are typically constructor/destructor entries created by
774 CONSTRUCTORS) and let the backend know it will need to create
775 .loader relocs for them. */
776 lang_for_each_statement (gld${EMULATION_NAME}_find_relocs);
777
aa67bccf 778 /* Precedence of LIBPATH
7fc44b9c
AM
779 -blibpath: native support always first
780 -rpath: gnu extension
781 -L build from command line -L's */
aa67bccf
TR
782 if (command_line_blibpath != NULL)
783 libpath = command_line_blibpath;
784 else if (command_line.rpath != NULL)
252b5132
RH
785 libpath = command_line.rpath;
786 else if (search_head == NULL)
787 libpath = (char *) "";
788 else
789 {
790 size_t len;
791 search_dirs_type *search;
792
057cac08
NC
793 /* PR ld/4023: Strip sysroot prefix from any paths
794 being inserted into the output binary's DT_RPATH. */
795 if (ld_sysroot != NULL
796 && * ld_sysroot != 0)
252b5132 797 {
057cac08
NC
798 const char * name = search_head->name;
799 size_t ld_sysroot_len = strlen (ld_sysroot);
252b5132 800
057cac08
NC
801 if (strncmp (name, ld_sysroot, ld_sysroot_len) == 0)
802 name += ld_sysroot_len;
803
804 len = strlen (name);
805 libpath = xmalloc (len + 1);
806 strcpy (libpath, name);
807
808 for (search = search_head->next; search != NULL; search = search->next)
809 {
810 size_t nlen;
811
812 name = search->name;
813 if (strncmp (name, ld_sysroot, ld_sysroot_len) == 0)
814 name += ld_sysroot_len;
815
816 nlen = strlen (name);
817 libpath = xrealloc (libpath, len + nlen + 2);
818 libpath[len] = ':';
819 strcpy (libpath + len + 1, name);
820 len += nlen + 1;
821 }
822 }
823 else
824 {
825 len = strlen (search_head->name);
826 libpath = xmalloc (len + 1);
827 strcpy (libpath, search_head->name);
828
829 for (search = search_head->next; search != NULL; search = search->next)
830 {
831 size_t nlen;
832
833 nlen = strlen (search->name);
834 libpath = xrealloc (libpath, len + nlen + 2);
835 libpath[len] = ':';
836 strcpy (libpath + len + 1, search->name);
837 len += nlen + 1;
838 }
252b5132
RH
839 }
840 }
841
b64232cc
RS
842 /* Default to -bexpfull for SVR4-like semantics. */
843 flags = (unix_ld ? XCOFF_EXPFULL : 0);
844 flags &= ~explicit_auto_export_flags;
845 flags |= auto_export_flags;
846
252b5132 847 /* Let the XCOFF backend set up the .loader section. */
7fc44b9c 848 if (!bfd_xcoff_size_dynamic_sections
f38a2680
AM
849 (link_info.output_bfd, &link_info, libpath, entry_symbol.name,
850 file_align, maxstack, maxdata, gc && !unix_ld,
63b4cc53 851 modtype, textro, flags, special_sections, rtld))
df5f2391 852 einfo (_("%F%P: failed to set dynamic section sizes: %E\n"));
252b5132
RH
853
854 /* Look through the special sections, and put them in the right
855 place in the link ordering. This is especially magic. */
fab80407
AM
856 for (i = 0; i < XCOFF_NUMBER_OF_SPECIAL_SECTIONS; i++)
857 {
858 asection *sec;
859 lang_output_section_statement_type *os;
860 lang_statement_union_type **pls;
861 lang_input_section_type *is;
862 const char *oname;
f38a2680 863 bool start;
fab80407
AM
864
865 sec = special_sections[i];
866 if (sec == NULL)
867 continue;
3b1b01cf 868
fab80407 869 /* Remove this section from the list of the output section.
7fc44b9c 870 This assumes we know what the script looks like. */
fab80407 871 is = NULL;
24ef1aa7 872 os = lang_output_section_get (sec->output_section);
7fc44b9c 873 if (os == NULL)
df5f2391 874 einfo (_("%F%P: can't find output section %s\n"),
fab80407 875 sec->output_section->name);
fab80407
AM
876
877 for (pls = &os->children.head; *pls != NULL; pls = &(*pls)->header.next)
878 {
7fc44b9c 879 if ((*pls)->header.type == lang_input_section_enum
249172c3 880 && (*pls)->input_section.section == sec)
fab80407 881 {
249172c3 882 is = (lang_input_section_type *) * pls;
fab80407 883 *pls = (*pls)->header.next;
252b5132 884 break;
fab80407
AM
885 }
886
887 if ((*pls)->header.type == lang_wild_statement_enum)
888 {
889 lang_statement_union_type **pwls;
890
891 for (pwls = &(*pls)->wild_statement.children.head;
249172c3 892 *pwls != NULL; pwls = &(*pwls)->header.next)
fab80407
AM
893 {
894
7fc44b9c 895 if ((*pwls)->header.type == lang_input_section_enum
249172c3 896 && (*pwls)->input_section.section == sec)
fab80407 897 {
249172c3 898 is = (lang_input_section_type *) * pwls;
fab80407
AM
899 *pwls = (*pwls)->header.next;
900 break;
901 }
902 }
903
904 if (is != NULL)
905 break;
906 }
907 }
908
909 if (is == NULL)
910 {
df5f2391 911 einfo (_("%F%P: can't find %s in output section\n"),
fd361982 912 bfd_section_name (sec));
3b1b01cf 913 }
fab80407
AM
914
915 /* Now figure out where the section should go. */
916 switch (i)
917 {
918
249172c3 919 default: /* to avoid warnings */
fab80407
AM
920 case XCOFF_SPECIAL_SECTION_TEXT:
921 /* _text */
922 oname = ".text";
f38a2680 923 start = true;
3b1b01cf 924 break;
252b5132 925
fab80407
AM
926 case XCOFF_SPECIAL_SECTION_ETEXT:
927 /* _etext */
928 oname = ".text";
f38a2680 929 start = false;
fab80407 930 break;
252b5132 931
fab80407
AM
932 case XCOFF_SPECIAL_SECTION_DATA:
933 /* _data */
934 oname = ".data";
f38a2680 935 start = true;
fab80407 936 break;
252b5132 937
fab80407
AM
938 case XCOFF_SPECIAL_SECTION_EDATA:
939 /* _edata */
940 oname = ".data";
f38a2680 941 start = false;
fab80407
AM
942 break;
943
944 case XCOFF_SPECIAL_SECTION_END:
945 case XCOFF_SPECIAL_SECTION_END2:
946 /* _end and end */
947 oname = ".bss";
f38a2680 948 start = false;
fab80407
AM
949 break;
950 }
252b5132 951
fab80407 952 os = lang_output_section_find (oname);
252b5132 953
fab80407
AM
954 if (start)
955 {
956 is->header.next = os->children.head;
957 os->children.head = (lang_statement_union_type *) is;
958 }
959 else
960 {
961 is->header.next = NULL;
36983a93
AM
962 *os->children.tail = (lang_statement_union_type *) is;
963 os->children.tail = &is->header.next;
fab80407 964 }
3b1b01cf 965 }
8423293d 966
9ec59d51
RS
967 /* Executables and shared objects must always have .text, .data
968 and .bss output sections, so that the header can refer to them.
969 The kernel refuses to load objects that have missing sections. */
0e1862bb 970 if (!bfd_link_relocatable (&link_info))
9ec59d51
RS
971 for (i = 0; i < ARRAY_SIZE (must_keep_sections); i++)
972 {
973 asection *sec;
974
975 sec = bfd_get_section_by_name (link_info.output_bfd,
976 must_keep_sections[i]);
977 if (sec == NULL)
d003af55
AM
978 einfo (_("%P: can't find required output section %s\n"),
979 must_keep_sections[i]);
9ec59d51
RS
980 else
981 sec->flags |= SEC_KEEP;
982 }
983
1b2cb8e2
CC
984 /* Make sure .tdata is removed if empty, even with -r flag.
985 .tdata is always being generated because its size is needed
986 to cumpute .data address. */
987 if (bfd_link_relocatable (&link_info))
988 {
989 static const char *const thread_sections[] = {
990 ".tdata",
991 ".tbss"
992 };
993
994 /* Run lang_size_sections (if not already done). */
995 if (expld.phase != lang_mark_phase_enum)
996 {
997 expld.phase = lang_mark_phase_enum;
998 expld.dataseg.phase = exp_seg_none;
f38a2680 999 one_lang_size_sections_pass (NULL, false);
1b2cb8e2
CC
1000 lang_reset_memory_regions ();
1001 }
1002
1003 for (i = 0; i < ARRAY_SIZE (thread_sections); i++)
1004 {
1005 asection *sec;
1006
1007 sec = bfd_get_section_by_name (link_info.output_bfd,
1008 thread_sections[i]);
1009
1010 if (sec != NULL && sec->rawsize == 0
1011 && (sec->flags & SEC_KEEP) == 0
1012 && !bfd_section_removed_from_list (link_info.output_bfd,
1013 sec))
1014 {
1015 sec->flags |= SEC_EXCLUDE;
1016 bfd_section_list_remove (link_info.output_bfd, sec);
1017 link_info.output_bfd->section_count--;
1018 }
1019 }
1020 }
1021
1e035701 1022 before_allocation_default ();
3b1b01cf
TR
1023}
1024
2d23f965
CC
1025struct hook_stub_info
1026{
1027 lang_statement_list_type add;
1028 asection *input_section;
1029};
1030
1031/* Traverse the linker tree to find the spot where the stub goes. */
1032
1033static bool
1034hook_in_stub (struct hook_stub_info *info, lang_statement_union_type **lp)
1035{
1036 lang_statement_union_type *l;
1037 bool ret;
1038
1039 for (; (l = *lp) != NULL; lp = &l->header.next)
1040 {
1041 switch (l->header.type)
1042 {
1043 case lang_constructors_statement_enum:
1044 ret = hook_in_stub (info, &constructor_list.head);
1045 if (ret)
1046 return ret;
1047 break;
1048
1049 case lang_output_section_statement_enum:
1050 ret = hook_in_stub (info,
1051 &l->output_section_statement.children.head);
1052 if (ret)
1053 return ret;
1054 break;
1055
1056 case lang_wild_statement_enum:
1057 ret = hook_in_stub (info, &l->wild_statement.children.head);
1058 if (ret)
1059 return ret;
1060 break;
1061
1062 case lang_group_statement_enum:
1063 ret = hook_in_stub (info, &l->group_statement.children.head);
1064 if (ret)
1065 return ret;
1066 break;
1067
1068 case lang_input_section_enum:
1069 if (l->input_section.section == info->input_section)
1070 {
1071 /* We've found our section. Insert the stub immediately
1072 after its associated input section. */
1073 *(info->add.tail) = l->header.next;
1074 l->header.next = info->add.head;
1075 return true;
1076 }
1077 break;
1078
1079 case lang_data_statement_enum:
1080 case lang_reloc_statement_enum:
1081 case lang_object_symbols_statement_enum:
1082 case lang_output_statement_enum:
1083 case lang_target_statement_enum:
1084 case lang_input_statement_enum:
1085 case lang_assignment_statement_enum:
1086 case lang_padding_statement_enum:
1087 case lang_address_statement_enum:
1088 case lang_fill_statement_enum:
1089 break;
1090
1091 default:
1092 FAIL ();
1093 break;
1094 }
1095 }
1096 return false;
1097}
1098
1099/* Call-back for bfd_xcoff_link_relocations.
1100 Create a new stub section, and arrange for it to be linked
1101 immediately before INPUT_SECTION. */
1102
1103static asection *
1104xcoff_add_stub_section (const char *stub_sec_name, asection *input_section)
1105{
1106 asection *stub_sec;
1107 flagword flags;
1108 asection *output_section;
1109 lang_output_section_statement_type *os;
1110 struct hook_stub_info info;
1111
1112 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE
1113 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_KEEP);
1114 stub_sec = bfd_make_section_anyway_with_flags (stub_file->the_bfd,
1115 stub_sec_name, flags);
1116 if (stub_sec == NULL)
1117 goto err_ret;
1118
1119 output_section = input_section->output_section;
1120 os = lang_output_section_get (output_section);
1121
1122 info.input_section = input_section;
1123 lang_list_init (&info.add);
1124 lang_add_section (&info.add, stub_sec, NULL, NULL, os);
1125
1126 if (info.add.head == NULL)
1127 goto err_ret;
1128
1129 if (hook_in_stub (&info, &os->children.head))
1130 return stub_sec;
1131
1132 err_ret:
1133 einfo (_("%X%P: can not make stub section: %E\n"));
1134 return NULL;
1135}
1136
1137/* Another call-back for bfd_xcoff_link_relocations. */
1138
1139static void
1140xcoff_layout_sections_again (void)
1141{
1142 /* If we have changed sizes of the stub sections, then we need
1143 to recalculate all the section offsets. This may mean we need to
1144 add even more stubs. */
1145 lang_relax_sections (true);
1146}
1147
1148/* Call the back-end to verify relocations. */
1149
1876a542
CC
1150static void
1151gld${EMULATION_NAME}_after_allocation (void)
1152{
2d23f965
CC
1153
1154 /* If generating a relocatable output file, then we don't have any
1155 stubs. */
1156 if (stub_file != NULL && !bfd_link_relocatable (&link_info))
1157 {
1158 /* Call into the BFD backend to do the real work. */
1159 if (!bfd_xcoff_size_stubs (&link_info))
1160 einfo (_("%X%P: can not size stub sections: %E\n"));
1161 }
1162
1876a542
CC
1163 /* Now that everything is in place, finalize the dynamic sections. */
1164 if (!bfd_xcoff_build_dynamic_sections (link_info.output_bfd, &link_info))
1165 einfo (_("%F%P: failed to layout dynamic sections: %E\n"));
2d23f965
CC
1166
1167 if (!bfd_link_relocatable (&link_info))
1168 {
1169 /* Now build the linker stubs. */
1170 if (stub_file != NULL && stub_file->the_bfd->sections != NULL)
1171 {
1172 if (! bfd_xcoff_build_stubs (&link_info))
1173 einfo (_("%X%P: can not build stubs: %E\n"));
1174 }
1175 }
1876a542
CC
1176}
1177
742aeb63 1178static char *
0c7a8e5a 1179gld${EMULATION_NAME}_choose_target (int argc, char **argv)
742aeb63
TR
1180{
1181 int i, j, jmax;
1182 static char *from_outside;
1183 static char *from_inside;
7fc44b9c 1184 static char *argv_to_target[][2] = {
249172c3 1185 {NULL, "${OUTPUT_FORMAT}"},
eb1e0e80
NC
1186 {"-b32", "${OUTPUT_FORMAT_32BIT}"},
1187 {"-b64", "${OUTPUT_FORMAT_64BIT}"},
249172c3 1188 };
742aeb63
TR
1189
1190 jmax = 3;
1191
1192 from_outside = getenv (TARGET_ENVIRON);
249172c3 1193 if (from_outside != (char *) NULL)
742aeb63
TR
1194 return from_outside;
1195
1196 /* Set to default. */
1197 from_inside = argv_to_target[0][1];
1198 for (i = 1; i < argc; i++)
1199 {
7fc44b9c 1200 for (j = 1; j < jmax; j++)
742aeb63
TR
1201 {
1202 if (0 == strcmp (argv[i], argv_to_target[j][0]))
1203 from_inside = argv_to_target[j][1];
1204 }
1205 }
249172c3 1206
742aeb63
TR
1207 return from_inside;
1208}
1209
7fc44b9c 1210/* Returns
249172c3
TR
1211 1 : state changed
1212 0 : no change */
7fc44b9c 1213static int
0c7a8e5a 1214change_symbol_mode (char *input)
3b1b01cf 1215{
3b1b01cf 1216 char *symbol_mode_string[] = {
249172c3
TR
1217 "# 32", /* 0x01 */
1218 "# 64", /* 0x02 */
1219 "# no32", /* 0x04 */
1220 "# no64", /* 0x08 */
3b1b01cf
TR
1221 NULL,
1222 };
249172c3 1223
3b1b01cf
TR
1224 unsigned int bit;
1225 char *string;
1226
249172c3 1227 for (bit = 0;; bit++)
fab80407
AM
1228 {
1229 string = symbol_mode_string[bit];
249172c3 1230 if (string == NULL)
fab80407
AM
1231 return 0;
1232
1233 if (0 == strcmp (input, string))
1234 {
1235 symbol_mode = (1 << bit);
1236 return 1;
1237 }
3b1b01cf 1238 }
3b1b01cf
TR
1239 /* should not be here */
1240 return 0;
1241}
1242
7fc44b9c 1243/* Returns
249172c3
TR
1244 1 : yes
1245 0 : ignore
1246 -1 : error, try something else */
7fc44b9c 1247static int
0c7a8e5a 1248is_syscall (char *input, unsigned int *flag)
3b1b01cf 1249{
3b1b01cf
TR
1250 unsigned int bit;
1251 char *string;
7fc44b9c 1252
1fdf0249
TR
1253 struct sc {
1254 char *syscall_string;
1255 unsigned int flag;
1256 } s [] = {
1257 { "svc" /* 0x01 */, XCOFF_SYSCALL32 },
1258 { "svc32" /* 0x02 */, XCOFF_SYSCALL32 },
1259 { "svc3264" /* 0x04 */, XCOFF_SYSCALL32 | XCOFF_SYSCALL64 },
1260 { "svc64" /* 0x08 */, XCOFF_SYSCALL64 },
1261 { "syscall" /* 0x10 */, XCOFF_SYSCALL32 },
1262 { "syscall32" /* 0x20 */, XCOFF_SYSCALL32 },
1263 { "syscall3264" /* 0x40 */, XCOFF_SYSCALL32 | XCOFF_SYSCALL64 },
1264 { "syscall64" /* 0x80 */, XCOFF_SYSCALL64 },
1265 { NULL, 0 },
3b1b01cf
TR
1266 };
1267
1fdf0249 1268 *flag = 0;
3b1b01cf 1269
7fc44b9c 1270 for (bit = 0;; bit++)
249172c3
TR
1271 {
1272 string = s[bit].syscall_string;
7fc44b9c 1273 if (string == NULL)
249172c3 1274 return -1;
fab80407 1275
7fc44b9c 1276 if (0 == strcmp (input, string))
249172c3 1277 {
7fc44b9c 1278 if (1 << bit & syscall_mask)
249172c3
TR
1279 {
1280 *flag = s[bit].flag;
1281 return 1;
7fc44b9c
AM
1282 }
1283 else
249172c3
TR
1284 {
1285 return 0;
1286 }
1287 }
252b5132 1288 }
3b1b01cf
TR
1289 /* should not be here */
1290 return -1;
252b5132
RH
1291}
1292
1293/* Read an import or export file. For an import file, this is called
1294 by the before_allocation emulation routine. For an export file,
3bcf5557 1295 this is called by the handle_option emulation routine. */
252b5132
RH
1296
1297static void
f38a2680 1298gld${EMULATION_NAME}_read_file (const char *filename, bool import)
252b5132
RH
1299{
1300 struct obstack *o;
1301 FILE *f;
1302 int lineno;
1303 int c;
f38a2680 1304 bool keep;
252b5132
RH
1305 const char *imppath;
1306 const char *impfile;
1307 const char *impmember;
1308
1309 o = (struct obstack *) xmalloc (sizeof (struct obstack));
1310 obstack_specify_allocation (o, 0, 0, xmalloc, gld${EMULATION_NAME}_free);
1311
1312 f = fopen (filename, FOPEN_RT);
1313 if (f == NULL)
1314 {
1315 bfd_set_error (bfd_error_system_call);
df5f2391 1316 einfo ("%F%P: %s: %E\n", filename);
b2e951ec 1317 return;
252b5132
RH
1318 }
1319
f38a2680 1320 keep = false;
252b5132
RH
1321
1322 imppath = NULL;
1323 impfile = NULL;
1324 impmember = NULL;
1325
1326 lineno = 0;
3b1b01cf 1327
69f284c7
TR
1328 /* Default to 32 and 64 bit mode
1329 symbols at top of /lib/syscalls.exp do not have a mode modifier and they
1330 are not repeated, assume 64 bit routines also want to use them.
1331 See the routine change_symbol_mode for more information. */
1332
3b1b01cf
TR
1333 symbol_mode = 0x04;
1334
252b5132
RH
1335 while ((c = getc (f)) != EOF)
1336 {
1337 char *s;
1338 char *symname;
1fdf0249 1339 unsigned int syscall_flag = 0;
252b5132
RH
1340 bfd_vma address;
1341 struct bfd_link_hash_entry *h;
1342
1343 if (c != '\n')
1344 {
1345 obstack_1grow (o, c);
1346 continue;
1347 }
1348
1349 obstack_1grow (o, '\0');
1350 ++lineno;
1351
1352 s = (char *) obstack_base (o);
3882b010 1353 while (ISSPACE (*s))
252b5132 1354 ++s;
fab80407
AM
1355 if (*s == '\0'
1356 || *s == '*'
1357 || change_symbol_mode (s)
1358 || (*s == '#' && s[1] == ' ')
249172c3 1359 || (!import && *s == '#' && s[1] == '!'))
252b5132
RH
1360 {
1361 obstack_free (o, obstack_base (o));
1362 continue;
1363 }
1364
1365 if (*s == '#' && s[1] == '!')
1366 {
1367 s += 2;
3882b010 1368 while (ISSPACE (*s))
252b5132
RH
1369 ++s;
1370 if (*s == '\0')
1371 {
1372 imppath = NULL;
1373 impfile = NULL;
1374 impmember = NULL;
1375 obstack_free (o, obstack_base (o));
1376 }
1377 else if (*s == '(')
df5f2391 1378 einfo (_("%F%P:%s:%d: #! ([member]) is not supported "
d003af55 1379 "in import files\n"),
252b5132
RH
1380 filename, lineno);
1381 else
1382 {
1383 char cs;
24c611d1 1384 char *start;
252b5132
RH
1385
1386 (void) obstack_finish (o);
f38a2680 1387 keep = true;
24c611d1 1388 start = s;
249172c3 1389 while (!ISSPACE (*s) && *s != '(' && *s != '\0')
24c611d1 1390 ++s;
252b5132
RH
1391 cs = *s;
1392 *s = '\0';
24c611d1
RS
1393 if (!bfd_xcoff_split_import_path (link_info.output_bfd,
1394 start, &imppath, &impfile))
df5f2391 1395 einfo (_("%F%P: could not parse import path: %E\n"));
3882b010 1396 while (ISSPACE (cs))
252b5132
RH
1397 {
1398 ++s;
1399 cs = *s;
1400 }
1401 if (cs != '(')
1402 {
1403 impmember = "";
1404 if (cs != '\0')
df5f2391 1405 einfo (_("%P:%s:%d: warning: syntax error in import file\n"),
252b5132
RH
1406 filename, lineno);
1407 }
1408 else
1409 {
1410 ++s;
1411 impmember = s;
1412 while (*s != ')' && *s != '\0')
1413 ++s;
1414 if (*s == ')')
1415 *s = '\0';
1416 else
df5f2391 1417 einfo (_("%P:%s:%d: warning: syntax error in import file\n"),
252b5132
RH
1418 filename, lineno);
1419 }
1420 }
1421
1422 continue;
1423 }
1424
742aeb63 1425 if (symbol_mode & symbol_mode_mask)
fab80407
AM
1426 {
1427 /* This is a symbol to be imported or exported. */
1428 symname = s;
1fdf0249 1429 syscall_flag = 0;
fab80407
AM
1430 address = (bfd_vma) -1;
1431
249172c3 1432 while (!ISSPACE (*s) && *s != '\0')
fab80407
AM
1433 ++s;
1434 if (*s != '\0')
1435 {
1436 char *se;
3b1b01cf 1437
fab80407 1438 *s++ = '\0';
3b1b01cf 1439
3882b010 1440 while (ISSPACE (*s))
fab80407
AM
1441 ++s;
1442
1443 se = s;
249172c3 1444 while (!ISSPACE (*se) && *se != '\0')
fab80407
AM
1445 ++se;
1446 if (*se != '\0')
1447 {
1448 *se++ = '\0';
3882b010 1449 while (ISSPACE (*se))
fab80407
AM
1450 ++se;
1451 if (*se != '\0')
df5f2391 1452 einfo (_("%P:%s%d: warning: syntax error in "
d003af55 1453 "import/export file\n"),
fab80407
AM
1454 filename, lineno);
1455 }
1456
1457 if (s != se)
1458 {
1459 int status;
c89e8944 1460 const char *end;
fab80407 1461
249172c3 1462 status = is_syscall (s, &syscall_flag);
7fc44b9c
AM
1463
1464 if (0 > status)
249172c3
TR
1465 {
1466 /* not a system call, check for address */
c89e8944 1467 address = bfd_scan_vma (s, &end, 0);
249172c3
TR
1468 if (*end != '\0')
1469 {
df5f2391 1470 einfo (_("%P:%s:%d: warning: syntax error in "
d003af55 1471 "import/export file\n"),
249172c3 1472 filename, lineno);
7fc44b9c 1473
249172c3
TR
1474 }
1475 }
3b1b01cf 1476 }
252b5132 1477 }
3b1b01cf 1478
249172c3 1479 if (!import)
fab80407
AM
1480 {
1481 struct export_symbol_list *n;
1482
f38a2680 1483 ldlang_add_undef (symname, true);
fab80407
AM
1484 n = ((struct export_symbol_list *)
1485 xmalloc (sizeof (struct export_symbol_list)));
1486 n->next = export_symbols;
1487 n->name = xstrdup (symname);
fab80407
AM
1488 export_symbols = n;
1489 }
1490 else
1491 {
f38a2680
AM
1492 h = bfd_link_hash_lookup (link_info.hash, symname, false, false,
1493 true);
fab80407
AM
1494 if (h == NULL || h->type == bfd_link_hash_new)
1495 {
1496 /* We can just ignore attempts to import an unreferenced
1497 symbol. */
1498 }
1499 else
1500 {
f13a99db
AM
1501 if (!bfd_xcoff_import_symbol (link_info.output_bfd,
1502 &link_info, h,
249172c3
TR
1503 address, imppath, impfile,
1504 impmember, syscall_flag))
df5f2391 1505 einfo (_("%X%P:%s:%d: failed to import symbol %s: %E\n"),
fab80407
AM
1506 filename, lineno, symname);
1507 }
1508 }
1509 }
252b5132
RH
1510 obstack_free (o, obstack_base (o));
1511 }
fab80407 1512
252b5132
RH
1513 if (obstack_object_size (o) > 0)
1514 {
df5f2391 1515 einfo (_("%P:%s:%d: warning: ignoring unterminated last line\n"),
fab80407
AM
1516 filename, lineno);
1517 obstack_free (o, obstack_base (o));
1518 }
252b5132 1519
249172c3 1520 if (!keep)
fab80407
AM
1521 {
1522 obstack_free (o, NULL);
1523 free (o);
252b5132 1524 }
b2e951ec
NC
1525
1526 fclose (f);
fab80407 1527}
252b5132
RH
1528
1529/* This routine saves us from worrying about declaring free. */
1530
1531static void
0c7a8e5a 1532gld${EMULATION_NAME}_free (void *p)
252b5132
RH
1533{
1534 free (p);
1535}
1536
1537/* This is called by the before_allocation routine via
1538 lang_for_each_statement. It looks for relocations and assignments
1539 to symbols. */
1540
1541static void
0c7a8e5a 1542gld${EMULATION_NAME}_find_relocs (lang_statement_union_type *s)
252b5132
RH
1543{
1544 if (s->header.type == lang_reloc_statement_enum)
1545 {
1546 lang_reloc_statement_type *rs;
1547
1548 rs = &s->reloc_statement;
1549 if (rs->name == NULL)
d003af55 1550 einfo (_("%F%P: only relocations against symbols are permitted\n"));
f13a99db
AM
1551 if (!bfd_xcoff_link_count_reloc (link_info.output_bfd, &link_info,
1552 rs->name))
d003af55 1553 einfo (_("%F%P: bfd_xcoff_link_count_reloc failed: %E\n"));
252b5132
RH
1554 }
1555
1556 if (s->header.type == lang_assignment_statement_enum)
1557 gld${EMULATION_NAME}_find_exp_assignment (s->assignment_statement.exp);
1558}
1559
1560/* Look through an expression for an assignment statement. */
1561
1562static void
0c7a8e5a 1563gld${EMULATION_NAME}_find_exp_assignment (etree_type *exp)
252b5132
RH
1564{
1565 struct bfd_link_hash_entry *h;
1566
1567 switch (exp->type.node_class)
1568 {
1569 case etree_provide:
fb3dc213 1570 case etree_provided:
252b5132 1571 h = bfd_link_hash_lookup (link_info.hash, exp->assign.dst,
f38a2680 1572 false, false, false);
252b5132
RH
1573 if (h == NULL)
1574 break;
1575 /* Fall through. */
1576 case etree_assign:
1577 if (strcmp (exp->assign.dst, ".") != 0)
1578 {
f13a99db
AM
1579 if (!bfd_xcoff_record_link_assignment (link_info.output_bfd,
1580 &link_info,
249172c3 1581 exp->assign.dst))
df5f2391 1582 einfo (_("%F%P: failed to record assignment to %s: %E\n"),
252b5132
RH
1583 exp->assign.dst);
1584 }
1585 gld${EMULATION_NAME}_find_exp_assignment (exp->assign.src);
1586 break;
1587
1588 case etree_binary:
1589 gld${EMULATION_NAME}_find_exp_assignment (exp->binary.lhs);
1590 gld${EMULATION_NAME}_find_exp_assignment (exp->binary.rhs);
1591 break;
1592
1593 case etree_trinary:
1594 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.cond);
1595 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.lhs);
1596 gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.rhs);
1597 break;
1598
1599 case etree_unary:
1600 gld${EMULATION_NAME}_find_exp_assignment (exp->unary.child);
1601 break;
1602
1603 default:
1604 break;
1605 }
1606}
1607
1608static char *
0c7a8e5a 1609gld${EMULATION_NAME}_get_script (int *isfile)
252b5132
RH
1610EOF
1611
7225345d 1612if test x"$COMPILE_IN" = xyes
252b5132
RH
1613then
1614# Scripts compiled in.
1615
1616# sed commands to quote an ld script as a C string.
9cadc5fa 1617sc="-f ${srcdir}/emultempl/stringify.sed"
252b5132 1618
92b93329 1619fragment <<EOF
fab80407 1620{
252b5132
RH
1621 *isfile = 0;
1622
0e1862bb 1623 if (bfd_link_relocatable (&link_info) && config.build_constructors)
252b5132
RH
1624 return
1625EOF
b34976b6 1626sed $sc ldscripts/${EMULATION_NAME}.xu >> e${EMULATION_NAME}.c
0e1862bb 1627echo ' ; else if (bfd_link_relocatable (&link_info)) return' >> e${EMULATION_NAME}.c
b34976b6
AM
1628sed $sc ldscripts/${EMULATION_NAME}.xr >> e${EMULATION_NAME}.c
1629echo ' ; else if (!config.text_read_only) return' >> e${EMULATION_NAME}.c
1630sed $sc ldscripts/${EMULATION_NAME}.xbn >> e${EMULATION_NAME}.c
1631echo ' ; else if (!config.magic_demand_paged) return' >> e${EMULATION_NAME}.c
1632sed $sc ldscripts/${EMULATION_NAME}.xn >> e${EMULATION_NAME}.c
1633echo ' ; else return' >> e${EMULATION_NAME}.c
1634sed $sc ldscripts/${EMULATION_NAME}.x >> e${EMULATION_NAME}.c
1635echo '; }' >> e${EMULATION_NAME}.c
252b5132
RH
1636
1637else
1638# Scripts read from the filesystem.
1639
92b93329 1640fragment <<EOF
fab80407 1641{
252b5132
RH
1642 *isfile = 1;
1643
0e1862bb 1644 if (bfd_link_relocatable (&link_info) && config.build_constructors)
252b5132 1645 return "ldscripts/${EMULATION_NAME}.xu";
0e1862bb 1646 else if (bfd_link_relocatable (&link_info))
252b5132
RH
1647 return "ldscripts/${EMULATION_NAME}.xr";
1648 else if (!config.text_read_only)
1649 return "ldscripts/${EMULATION_NAME}.xbn";
1650 else if (!config.magic_demand_paged)
1651 return "ldscripts/${EMULATION_NAME}.xn";
1652 else
1653 return "ldscripts/${EMULATION_NAME}.x";
1654}
1655EOF
1656
1657fi
1658
92b93329 1659fragment <<EOF
252b5132 1660
7fc44b9c 1661static void
0c7a8e5a 1662gld${EMULATION_NAME}_create_output_section_statements (void)
9a4c7f16 1663{
2d23f965
CC
1664 if ((bfd_get_flavour (link_info.output_bfd) != bfd_target_xcoff_flavour))
1665 return;
1666
1667 /* Stub file */
1668 stub_file = lang_add_input_file ("linker stubs",
1669 lang_input_file_is_fake_enum,
1670 NULL);
1671 stub_file->the_bfd = bfd_create ("linker stubs", link_info.output_bfd);
1672 if (stub_file->the_bfd == NULL
1673 || !bfd_set_arch_mach (stub_file->the_bfd,
1674 bfd_get_arch (link_info.output_bfd),
1675 bfd_get_mach (link_info.output_bfd)))
1676 {
1677 einfo (_("%F%P: can not create stub BFD: %E\n"));
1678 return;
1679 }
1680
1681 stub_file->the_bfd->flags |= BFD_LINKER_CREATED;
1682 ldlang_add_file (stub_file);
1683 params.stub_bfd = stub_file->the_bfd;
1684
1685 /* Pass linker params to the back-end. */
1686 if (!bfd_xcoff_link_init (&link_info, &params))
1687 einfo (_("%F%P: can not init BFD: %E\n"));
1688
9a4c7f16 1689 /* __rtinit */
2d23f965
CC
1690 if (link_info.init_function != NULL
1691 || link_info.fini_function != NULL
1692 || rtld)
9a4c7f16 1693 {
9a4c7f16
TR
1694 initfini_file = lang_add_input_file ("initfini",
1695 lang_input_file_is_file_enum,
1696 NULL);
7fc44b9c 1697
f13a99db 1698 initfini_file->the_bfd = bfd_create ("initfini", link_info.output_bfd);
9a4c7f16
TR
1699 if (initfini_file->the_bfd == NULL
1700 || ! bfd_set_arch_mach (initfini_file->the_bfd,
f13a99db
AM
1701 bfd_get_arch (link_info.output_bfd),
1702 bfd_get_mach (link_info.output_bfd)))
9a4c7f16 1703 {
df5f2391 1704 einfo (_("%F%P: can not create BFD: %E\n"));
9a4c7f16
TR
1705 return;
1706 }
7fc44b9c 1707
9a4c7f16 1708 /* Call backend to fill in the rest */
b34976b6
AM
1709 if (! bfd_xcoff_link_generate_rtinit (initfini_file->the_bfd,
1710 link_info.init_function,
1711 link_info.fini_function,
1712 rtld))
9a4c7f16 1713 {
df5f2391 1714 einfo (_("%F%P: can not create BFD: %E\n"));
9a4c7f16
TR
1715 return;
1716 }
69f284c7
TR
1717
1718 /* __rtld defined in /lib/librtl.a */
b34976b6 1719 if (rtld)
69f284c7 1720 lang_add_input_file ("rtl", lang_input_file_is_l_enum, NULL);
9a4c7f16
TR
1721 }
1722}
1723
f1f0d9ab 1724static void
0c7a8e5a 1725gld${EMULATION_NAME}_set_output_arch (void)
f1f0d9ab 1726{
f13a99db
AM
1727 bfd_set_arch_mach (link_info.output_bfd,
1728 bfd_xcoff_architecture (link_info.output_bfd),
1729 bfd_xcoff_machine (link_info.output_bfd));
f1f0d9ab 1730
f13a99db
AM
1731 ldfile_output_architecture = bfd_get_arch (link_info.output_bfd);
1732 ldfile_output_machine = bfd_get_mach (link_info.output_bfd);
1733 ldfile_output_machine_name = bfd_printable_name (link_info.output_bfd);
f1f0d9ab
TR
1734}
1735
f38a2680 1736static bool
24c611d1
RS
1737gld${EMULATION_NAME}_open_dynamic_archive (const char *arch,
1738 search_dirs_type *search,
1739 lang_input_statement_type *entry)
1740{
24c611d1
RS
1741 char *path;
1742
66be1055 1743 if (!entry->flags.maybe_archive)
f38a2680 1744 return false;
24c611d1 1745
d4ae5fb0
AM
1746 if (entry->flags.full_name_provided)
1747 path = concat (search->name, "/", entry->filename,
1748 (const char *) NULL);
1749 else
1750 path = concat (search->name, "/lib", entry->filename, arch, ".a",
1751 (const char *) NULL);
1752
24c611d1
RS
1753 if (!ldfile_try_open_bfd (path, entry))
1754 {
1755 free (path);
f38a2680 1756 return false;
24c611d1
RS
1757 }
1758 /* Don't include the searched directory in the import path. */
1759 bfd_xcoff_set_archive_import_path (&link_info, entry->the_bfd,
1760 path + strlen (search->name) + 1);
1761 entry->filename = path;
f38a2680 1762 return true;
24c611d1
RS
1763}
1764
f38a2680 1765static bool
3edf7b9f
DR
1766gld${EMULATION_NAME}_print_symbol (struct bfd_link_hash_entry *hash_entry,
1767 void *ptr)
1768{
1769 asection *sec = (asection *) ptr;
1770
1771 if ((hash_entry->type == bfd_link_hash_defined
1772 || hash_entry->type == bfd_link_hash_defweak)
1773 && sec == hash_entry->u.def.section)
1774 {
3edf7b9f
DR
1775 struct xcoff_link_hash_entry *h;
1776
6b9bd54c 1777 print_spaces (SECTION_NAME_MAP_LENGTH);
3edf7b9f
DR
1778 minfo ("0x%V ",
1779 (hash_entry->u.def.value
1780 + hash_entry->u.def.section->output_offset
1781 + hash_entry->u.def.section->output_section->vma));
1782
1783 /* Flag symbol if it has been garbage collected. */
1784 h = (struct xcoff_link_hash_entry *) hash_entry;
1785 if ((h != NULL) && !(h->flags & XCOFF_MARK))
1786 minfo (" -->gc");
1787 minfo (" %pT\n", hash_entry->root.string);
1788 }
1789
f38a2680 1790 return true;
3edf7b9f 1791}
252b5132 1792EOF
a9261391
L
1793
1794LDEMUL_AFTER_OPEN=gld${EMULATION_NAME}_after_open
1795LDEMUL_SET_OUTPUT_ARCH=gld${EMULATION_NAME}_set_output_arch
1796LDEMUL_CHOOSE_TARGET=gld${EMULATION_NAME}_choose_target
1797LDEMUL_BEFORE_ALLOCATION=gld${EMULATION_NAME}_before_allocation
1876a542 1798LDEMUL_AFTER_ALLOCATION=gld${EMULATION_NAME}_after_allocation
a9261391
L
1799LDEMUL_CREATE_OUTPUT_SECTION_STATEMENTS=gld${EMULATION_NAME}_create_output_section_statements
1800LDEMUL_OPEN_DYNAMIC_ARCHIVE=gld${EMULATION_NAME}_open_dynamic_archive
1801LDEMUL_PARSE_ARGS=gld${EMULATION_NAME}_parse_args
1802LDEMUL_ADD_OPTIONS=gld${EMULATION_NAME}_add_options
1803LDEMUL_HANDLE_OPTION=gld${EMULATION_NAME}_handle_option
1804LDEMUL_UNRECOGNIZED_FILE=gld${EMULATION_NAME}_unrecognized_file
1805LDEMUL_PRINT_SYMBOL=gld${EMULATION_NAME}_print_symbol
1806
1807source_em ${srcdir}/emultempl/emulation.em