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