]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - ld/emultempl/aix.em
* emultempl/aix.em: Add support for various AIX linker options,
[thirdparty/binutils-gdb.git] / ld / emultempl / aix.em
1 # This shell script emits a C file. -*- C -*-
2 # It does some substitutions.
3 cat >e${EMULATION_NAME}.c <<EOF
4 /* This file is is generated by a shell script. DO NOT EDIT! */
5
6 /* AIX emulation code for ${EMULATION_NAME}
7 Copyright (C) 1991, 1993, 1995 Free Software Foundation, Inc.
8 Written by Steve Chamberlain <sac@cygnus.com>
9 AIX support by Ian Lance Taylor <ian@cygnus.com>
10
11 This file is part of GLD, the Gnu Linker.
12
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
17
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
26
27 #define TARGET_IS_${EMULATION_NAME}
28
29 #include "bfd.h"
30 #include "sysdep.h"
31 #include "libiberty.h"
32 #include "getopt.h"
33 #include "bfdlink.h"
34
35 #include <ctype.h>
36
37 #include "ld.h"
38 #include "ldmain.h"
39 #include "ldemul.h"
40 #include "ldfile.h"
41 #include "ldmisc.h"
42 #include "ldexp.h"
43 #include "ldlang.h"
44
45 static void gld${EMULATION_NAME}_before_parse PARAMS ((void));
46 static int gld${EMULATION_NAME}_parse_args PARAMS ((int, char **));
47 static void gld${EMULATION_NAME}_before_allocation PARAMS ((void));
48 static void gld${EMULATION_NAME}_read_file PARAMS ((const char *, boolean));
49 static void gld${EMULATION_NAME}_free PARAMS ((PTR));
50 static char *gld${EMULATION_NAME}_get_script PARAMS ((int *isfile));
51
52 /* The file alignment required for each section. */
53 static unsigned long file_align;
54
55 /* The maximum size the stack is permitted to grow. This is stored in
56 the a.out header. */
57 static unsigned long maxstack;
58
59 /* The maximum data size. This is stored in the a.out header. */
60 static unsigned long maxdata;
61
62 /* Whether to perform garbage collection. */
63 static int gc = 1;
64
65 /* The module type to use. */
66 static unsigned short modtype = ('1' << 8) | 'L';
67
68 /* Whether the .text section must be read-only (i.e., no relocs
69 permitted). */
70 static int textro;
71
72 /* Structure used to hold import or export file list. */
73
74 struct filelist
75 {
76 struct filelist *next;
77 const char *name;
78 };
79
80 /* List of import files. */
81 struct filelist *import_files;
82
83 /* List of export files. */
84 struct filelist *export_files;
85
86 static void
87 gld${EMULATION_NAME}_before_parse()
88 {
89 #ifndef TARGET_ /* I.e., if not generic. */
90 ldfile_output_architecture = bfd_arch_${ARCH};
91 #endif /* not TARGET_ */
92 }
93
94 /* Handle AIX specific options. */
95
96 static int
97 gld${EMULATION_NAME}_parse_args (argc, argv)
98 int argc;
99 char **argv;
100 {
101 int prevoptind = optind;
102 int prevopterr = opterr;
103 int longind;
104 int optc;
105 long val;
106 char *end;
107
108 #define OPTION_IGNORE (300)
109 #define OPTION_AUTOIMP (OPTION_IGNORE + 1)
110 #define OPTION_ERNOTOK (OPTION_AUTOIMP + 1)
111 #define OPTION_EROK (OPTION_ERNOTOK + 1)
112 #define OPTION_EXPORT (OPTION_EROK + 1)
113 #define OPTION_IMPORT (OPTION_EXPORT + 1)
114 #define OPTION_MAXDATA (OPTION_IMPORT + 1)
115 #define OPTION_MAXSTACK (OPTION_MAXDATA + 1)
116 #define OPTION_MODTYPE (OPTION_MAXSTACK + 1)
117 #define OPTION_NOAUTOIMP (OPTION_MODTYPE + 1)
118 #define OPTION_NOSTRCMPCT (OPTION_NOAUTOIMP + 1)
119 #define OPTION_STRCMPCT (OPTION_NOSTRCMPCT + 1)
120
121 static struct option longopts[] = {
122 {"basis", no_argument, NULL, OPTION_IGNORE},
123 {"bautoimp", no_argument, NULL, OPTION_AUTOIMP},
124 {"bcomprld", no_argument, NULL, OPTION_IGNORE},
125 {"bcrld", no_argument, NULL, OPTION_IGNORE},
126 {"bcror31", no_argument, NULL, OPTION_IGNORE},
127 {"bD", required_argument, NULL, OPTION_MAXDATA},
128 {"bE", required_argument, NULL, OPTION_EXPORT},
129 {"bernotok", no_argument, NULL, OPTION_ERNOTOK},
130 {"berok", no_argument, NULL, OPTION_EROK},
131 {"berrmsg", no_argument, NULL, OPTION_IGNORE},
132 {"bexport", required_argument, NULL, OPTION_EXPORT},
133 {"bf", no_argument, NULL, OPTION_ERNOTOK},
134 {"bgc", no_argument, &gc, 1},
135 {"bh", required_argument, NULL, OPTION_IGNORE},
136 {"bhalt", required_argument, NULL, OPTION_IGNORE},
137 {"bI", required_argument, NULL, OPTION_IMPORT},
138 {"bimport", required_argument, NULL, OPTION_IMPORT},
139 {"bmaxdata", required_argument, NULL, OPTION_MAXDATA},
140 {"bmaxstack", required_argument, NULL, OPTION_MAXSTACK},
141 {"bM", required_argument, NULL, OPTION_MODTYPE},
142 {"bmodtype", required_argument, NULL, OPTION_MODTYPE},
143 {"bnoautoimp", no_argument, NULL, OPTION_NOAUTOIMP},
144 {"bnodelcsect", no_argument, NULL, OPTION_IGNORE},
145 {"bnogc", no_argument, &gc, 0},
146 {"bnso", no_argument, NULL, OPTION_NOAUTOIMP},
147 {"bnostrcmpct", no_argument, NULL, OPTION_NOSTRCMPCT},
148 {"bnotextro", no_argument, &textro, 0},
149 {"bnro", no_argument, &textro, 0},
150 {"bro", no_argument, &textro, 1},
151 {"bS", required_argument, NULL, OPTION_MAXSTACK},
152 {"bso", no_argument, NULL, OPTION_AUTOIMP},
153 {"bstrcmpct", no_argument, NULL, OPTION_STRCMPCT},
154 {"btextro", no_argument, &textro, 1},
155 {NULL, no_argument, NULL, 0}
156 };
157
158 /* Options supported by the AIX linker which we do not support: -f,
159 -S, -v, -Z, -bbindcmds, -bbinder, -bbindopts, -bcalls, -bcaps,
160 -bcror15, -bdebugopt, -bdbg, -bdelcsect, -bex?, -bfilelist, -bfl,
161 -bgcbypass, -bglink, -binsert, -bi, -bloadmap, -bl, -bmap, -bnl,
162 -bnobind, -bnocomprld, -bnocrld, -bnoerrmsg, -bnoglink,
163 -bnoloadmap, -bnl, -bnoobjreorder, -bnoquiet, -bnoreorder,
164 -bnotypchk, -bnox, -bquiet, -bR, -brename, -breorder, -btypchk,
165 -bx, -bX, -bxref. */
166
167 /* If the first option starts with -b, change the first : to an =.
168 The AIX linker uses : to separate the option from the argument;
169 changing it to = lets us treat it as a getopt option. */
170 if (optind < argc && strncmp (argv[optind], "-b", 2) == 0)
171 {
172 char *s;
173
174 for (s = argv[optind]; *s != '\0'; s++)
175 {
176 if (*s == ':')
177 {
178 *s = '=';
179 break;
180 }
181 }
182 }
183
184 opterr = 0;
185 optc = getopt_long_only (argc, argv, "-D:H:KT:z", longopts, &longind);
186 opterr = prevopterr;
187
188 switch (optc)
189 {
190 default:
191 optind = prevoptind;
192 return 0;
193
194 case 'D':
195 val = strtol (optarg, &end, 0);
196 if (*end != '\0')
197 einfo ("%P: warning: ignoring invalid -D number %s\n", optarg);
198 else if (val != -1)
199 lang_section_start (".data", exp_intop (val));
200 break;
201
202 case 'H':
203 val = strtoul (optarg, &end, 0);
204 if (*end != '\0'
205 || (val & (val - 1)) != 0)
206 einfo ("%P: warning: ignoring invalid -H number %s\n", optarg);
207 else
208 file_align = val;
209 break;
210
211 case 'K':
212 case 'z':
213 /* FIXME: This should use the page size for the target system. */
214 file_align = 4096;
215 break;
216
217 case 'T':
218 /* On AIX this is the same as GNU ld -Ttext. When we see -T
219 number, we assume the AIX option is intended. Otherwise, we
220 assume the usual GNU ld -T option is intended. We can't just
221 ignore the AIX option, because gcc passes it to the linker. */
222 val = strtoul (optarg, &end, 0);
223 if (*end != '\0')
224 {
225 optind = prevoptind;
226 return 0;
227 }
228 lang_section_start (".text", exp_intop (val));
229 break;
230
231 case OPTION_IGNORE:
232 break;
233
234 case OPTION_AUTOIMP:
235 link_info.static_link = false;
236 break;
237
238 case OPTION_ERNOTOK:
239 force_make_executable = false;
240 break;
241
242 case OPTION_EROK:
243 force_make_executable = true;
244 break;
245
246 case OPTION_EXPORT:
247 case OPTION_IMPORT:
248 {
249 struct filelist *n;
250 struct filelist **flpp;
251
252 n = (struct filelist *) xmalloc (sizeof (struct filelist));
253 n->next = NULL;
254 n->name = optarg;
255 if (optc == OPTION_EXPORT)
256 flpp = &export_files;
257 else
258 flpp = &import_files;
259 while (*flpp != NULL)
260 flpp = &(*flpp)->next;
261 *flpp = n;
262 }
263 break;
264
265 case OPTION_MAXDATA:
266 val = strtoul (optarg, &end, 0);
267 if (*end != '\0')
268 einfo ("%P: warning: ignoring invalid -bmaxdata number %s\n",
269 optarg);
270 else
271 maxdata = val;
272 break;
273
274 case OPTION_MAXSTACK:
275 val = strtoul (optarg, &end, 0);
276 if (*end != '\0')
277 einfo ("%P: warning: ignoring invalid -bmaxstack number %s\n",
278 optarg);
279 else
280 maxstack = val;
281 break;
282
283 case OPTION_MODTYPE:
284 if (*optarg == 'S')
285 {
286 link_info.shared = true;
287 ++optarg;
288 }
289 if (*optarg == '\0' || optarg[1] == '\0')
290 einfo ("%P: warning: ignoring invalid module type %s\n", optarg);
291 else
292 modtype = (*optarg << 8) | optarg[1];
293 break;
294
295 case OPTION_NOAUTOIMP:
296 link_info.static_link = true;
297 break;
298
299 case OPTION_NOSTRCMPCT:
300 config.traditional_format = true;
301 break;
302
303 case OPTION_STRCMPCT:
304 config.traditional_format = false;
305 break;
306 }
307
308 return 1;
309 }
310
311 /* This is called after the sections have been attached to output
312 sections, but before any sizes or addresses have been set. */
313
314 static void
315 gld${EMULATION_NAME}_before_allocation ()
316 {
317 struct filelist *fl;
318 char *libpath;
319
320 /* Handle the import and export files, if any. */
321 for (fl = import_files; fl != NULL; fl = fl->next)
322 gld${EMULATION_NAME}_read_file (fl->name, true);
323 for (fl = export_files; fl != NULL; fl = fl->next)
324 gld${EMULATION_NAME}_read_file (fl->name, false);
325
326 /* We need to build LIBPATH from the -L arguments. If any -rpath
327 arguments were used, though, we use -rpath instead, as a GNU
328 extension. */
329 if (command_line.rpath != NULL)
330 libpath = command_line.rpath;
331 else if (search_head == NULL)
332 libpath = (char *) "";
333 else
334 {
335 size_t len;
336 search_dirs_type *search;
337
338 len = strlen (search_head->name);
339 libpath = xmalloc (len + 1);
340 strcpy (libpath, search_head->name);
341 for (search = search_head->next; search != NULL; search = search->next)
342 {
343 size_t nlen;
344
345 nlen = strlen (search->name);
346 libpath = xrealloc (libpath, len + nlen + 2);
347 libpath[len] = ':';
348 strcpy (libpath + len + 1, search->name);
349 len += nlen + 1;
350 }
351 }
352
353 /* Let the XCOFF backend set up the .loader section. */
354 if (! bfd_xcoff_size_dynamic_sections (output_bfd, &link_info, libpath,
355 entry_symbol, file_align,
356 maxstack, maxdata,
357 gc ? true : false,
358 modtype,
359 textro ? true : false))
360 einfo ("%P%F: failed to set dynamic section sizes: %E\n");
361 }
362
363 /* Read an import or export file. */
364
365 static void
366 gld${EMULATION_NAME}_read_file (filename, import)
367 const char *filename;
368 boolean import;
369 {
370 struct obstack *o;
371 FILE *f;
372 int lineno;
373 int c;
374 boolean keep;
375 const char *imppath;
376 const char *impfile;
377 const char *impmember;
378
379 o = (struct obstack *) xmalloc (sizeof (struct obstack));
380 obstack_specify_allocation (o, 0, 0, xmalloc, gld${EMULATION_NAME}_free);
381
382 f = fopen (filename, "r");
383 if (f == NULL)
384 {
385 bfd_set_error (bfd_error_system_call);
386 einfo ("%F%s: %E\n", filename);
387 }
388
389 keep = false;
390
391 imppath = NULL;
392 impfile = NULL;
393 impmember = NULL;
394
395 lineno = 0;
396 while ((c = getc (f)) != EOF)
397 {
398 char *s;
399 char *symname;
400 boolean syscall;
401 bfd_vma address;
402 struct bfd_link_hash_entry *h;
403
404 if (c != '\n')
405 {
406 obstack_1grow (o, c);
407 continue;
408 }
409
410 obstack_1grow (o, '\0');
411 ++lineno;
412
413 s = (char *) obstack_base (o);
414 while (isspace ((unsigned char) *s))
415 ++s;
416 if (*s == '\0'
417 || *s == '*'
418 || (*s == '#' && s[1] == ' ')
419 || (! import && *s == '#' && s[1] == '!'))
420 {
421 obstack_free (o, obstack_base (o));
422 continue;
423 }
424
425 if (*s == '#' && s[1] == '!')
426 {
427 s += 2;
428 while (isspace ((unsigned char) *s))
429 ++s;
430 if (*s == '\0')
431 {
432 imppath = NULL;
433 impfile = NULL;
434 impmember = NULL;
435 obstack_free (o, obstack_base (o));
436 }
437 else if (*s == '(')
438 einfo ("%F%s%d: #! ([member]) is not supported in import files",
439 filename, lineno);
440 else
441 {
442 char cs;
443 char *file;
444
445 (void) obstack_finish (o);
446 keep = true;
447 imppath = s;
448 impfile = NULL;
449 while (! isspace ((unsigned char) *s) && *s != '(' && *s != '\0')
450 {
451 if (*s == '/')
452 file = s + 1;
453 ++s;
454 }
455 if (file != NULL)
456 {
457 file[-1] = '\0';
458 impfile = file;
459 if (imppath == file - 1)
460 imppath = "/";
461 }
462 else
463 {
464 impfile = imppath;
465 imppath = "";
466 }
467 cs = *s;
468 *s = '\0';
469 while (isspace ((unsigned char) cs))
470 {
471 ++s;
472 cs = *s;
473 }
474 if (cs != '(')
475 {
476 impmember = "";
477 if (cs != '\0')
478 einfo ("%s:%d: warning: syntax error in import file\n",
479 filename, lineno);
480 }
481 else
482 {
483 ++s;
484 impmember = s;
485 while (*s != ')' && *s != '\0')
486 ++s;
487 if (*s == ')')
488 *s = '\0';
489 else
490 einfo ("%s:%d: warning: syntax error in import file\n",
491 filename, lineno);
492 }
493 }
494
495 continue;
496 }
497
498 /* This is a symbol to be imported or exported. */
499 symname = s;
500 syscall = false;
501 address = (bfd_vma) -1;
502
503 while (! isspace ((unsigned char) *s) && *s != '\0')
504 ++s;
505 if (*s != '\0')
506 {
507 char *se;
508
509 *s++ = '\0';
510
511 while (isspace ((unsigned char) *s))
512 ++s;
513
514 se = s;
515 while (! isspace ((unsigned char) *se) && *se != '\0')
516 ++se;
517 if (*se != '\0')
518 {
519 *se++ = '\0';
520 while (isspace ((unsigned char) *se))
521 ++se;
522 if (*se != '\0')
523 einfo ("%s%d: warning: syntax error in import/export file\n",
524 filename, lineno);
525 }
526
527 if (strcasecmp (s, "svc") == 0
528 || strcasecmp (s, "syscall") == 0)
529 syscall = true;
530 else
531 {
532 char *end;
533
534 address = strtoul (s, &end, 0);
535 if (*end != '\0')
536 einfo ("%s:%d: warning: syntax error in import/export file\n",
537 filename, lineno);
538 }
539 }
540
541 h = bfd_link_hash_lookup (link_info.hash, symname, false, false, true);
542 if (h == NULL || h->type == bfd_link_hash_new)
543 {
544 /* We can just ignore attempts to import an unreferenced
545 symbol. */
546 if (! import)
547 einfo ("%X%s:%d: attempt to export undefined symbol %s\n",
548 filename, lineno, symname);
549 }
550 else if (import)
551 {
552 if (! bfd_xcoff_import_symbol (output_bfd, &link_info, h, address,
553 imppath, impfile, impmember))
554 einfo ("%X%s:%d: failed to import symbol %s: %E\n",
555 filename, lineno, symname);
556 }
557 else
558 {
559 if (! bfd_xcoff_export_symbol (output_bfd, &link_info, h, syscall))
560 einfo ("%X%s:%d: failed to export symbol %s: %E\n",
561 filename, lineno, symname);
562 }
563
564 obstack_free (o, obstack_base (o));
565 }
566
567 if (obstack_object_size (o) > 0)
568 {
569 einfo ("%s:%d: warning: ignoring unterminated last line\n",
570 filename, lineno);
571 obstack_free (o, obstack_base (o));
572 }
573
574 if (! keep)
575 {
576 obstack_free (o, NULL);
577 free (o);
578 }
579 }
580
581 /* This routine saves us from worrying about declaring free. */
582
583 static void
584 gld${EMULATION_NAME}_free (p)
585 PTR p;
586 {
587 free (p);
588 }
589
590 static char *
591 gld${EMULATION_NAME}_get_script(isfile)
592 int *isfile;
593 EOF
594
595 if test -n "$COMPILE_IN"
596 then
597 # Scripts compiled in.
598
599 # sed commands to quote an ld script as a C string.
600 sc="-f ${srcdir}/emultempl/stringify.sed"
601
602 cat >>e${EMULATION_NAME}.c <<EOF
603 {
604 *isfile = 0;
605
606 if (link_info.relocateable == true && config.build_constructors == true)
607 return
608 EOF
609 sed $sc ldscripts/${EMULATION_NAME}.xu >> e${EMULATION_NAME}.c
610 echo ' ; else if (link_info.relocateable == true) return' >> e${EMULATION_NAME}.c
611 sed $sc ldscripts/${EMULATION_NAME}.xr >> e${EMULATION_NAME}.c
612 echo ' ; else if (!config.text_read_only) return' >> e${EMULATION_NAME}.c
613 sed $sc ldscripts/${EMULATION_NAME}.xbn >> e${EMULATION_NAME}.c
614 echo ' ; else if (!config.magic_demand_paged) return' >> e${EMULATION_NAME}.c
615 sed $sc ldscripts/${EMULATION_NAME}.xn >> e${EMULATION_NAME}.c
616 echo ' ; else return' >> e${EMULATION_NAME}.c
617 sed $sc ldscripts/${EMULATION_NAME}.x >> e${EMULATION_NAME}.c
618 echo '; }' >> e${EMULATION_NAME}.c
619
620 else
621 # Scripts read from the filesystem.
622
623 cat >>e${EMULATION_NAME}.c <<EOF
624 {
625 *isfile = 1;
626
627 if (link_info.relocateable == true && config.build_constructors == true)
628 return "ldscripts/${EMULATION_NAME}.xu";
629 else if (link_info.relocateable == true)
630 return "ldscripts/${EMULATION_NAME}.xr";
631 else if (!config.text_read_only)
632 return "ldscripts/${EMULATION_NAME}.xbn";
633 else if (!config.magic_demand_paged)
634 return "ldscripts/${EMULATION_NAME}.xn";
635 else
636 return "ldscripts/${EMULATION_NAME}.x";
637 }
638 EOF
639
640 fi
641
642 cat >>e${EMULATION_NAME}.c <<EOF
643
644 struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
645 {
646 gld${EMULATION_NAME}_before_parse,
647 syslib_default,
648 hll_default,
649 after_parse_default,
650 after_open_default,
651 after_allocation_default,
652 set_output_arch_default,
653 ldemul_default_target,
654 gld${EMULATION_NAME}_before_allocation,
655 gld${EMULATION_NAME}_get_script,
656 "${EMULATION_NAME}",
657 "${OUTPUT_FORMAT}",
658 0, /* finish */
659 0, /* create_output_section_statements */
660 0, /* open_dynamic_archive */
661 0, /* place_orphan */
662 0, /* set_symbols */
663 gld${EMULATION_NAME}_parse_args,
664 };
665 EOF