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