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