tweak option handling
[external/binutils.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 0:
195       /* Long option which just sets a flag.  */
196       break;
197
198     case 'D':
199       val = strtol (optarg, &end, 0);
200       if (*end != '\0')
201         einfo ("%P: warning: ignoring invalid -D number %s\n", optarg);
202       else if (val != -1)
203         lang_section_start (".data", exp_intop (val));
204       break;
205
206     case 'H':
207       val = strtoul (optarg, &end, 0);
208       if (*end != '\0'
209           || (val & (val - 1)) != 0)
210         einfo ("%P: warning: ignoring invalid -H number %s\n", optarg);
211       else
212         file_align = val;
213       break;
214
215     case 'K':
216     case 'z':
217       /* FIXME: This should use the page size for the target system.  */
218       file_align = 4096;
219       break;
220
221     case 'T':
222       /* On AIX this is the same as GNU ld -Ttext.  When we see -T
223          number, we assume the AIX option is intended.  Otherwise, we
224          assume the usual GNU ld -T option is intended.  We can't just
225          ignore the AIX option, because gcc passes it to the linker.  */
226       val = strtoul (optarg, &end, 0);
227       if (*end != '\0')
228         {
229           optind = prevoptind;
230           return 0;
231         }
232       lang_section_start (".text", exp_intop (val));
233       break;
234
235     case OPTION_IGNORE:
236       break;
237
238     case OPTION_AUTOIMP:
239       link_info.static_link = false;
240       break;
241
242     case OPTION_ERNOTOK:
243       force_make_executable = false;
244       break;
245
246     case OPTION_EROK:
247       force_make_executable = true;
248       break;
249
250     case OPTION_EXPORT:
251     case OPTION_IMPORT:
252       {
253         struct filelist *n;
254         struct filelist **flpp;
255
256         n = (struct filelist *) xmalloc (sizeof (struct filelist));
257         n->next = NULL;
258         n->name = optarg;
259         if (optc == OPTION_EXPORT)
260           flpp = &export_files;
261         else
262           flpp = &import_files;
263         while (*flpp != NULL)
264           flpp = &(*flpp)->next;
265         *flpp = n;
266       }
267       break;
268
269     case OPTION_MAXDATA:
270       val = strtoul (optarg, &end, 0);
271       if (*end != '\0')
272         einfo ("%P: warning: ignoring invalid -bmaxdata number %s\n",
273                optarg);
274       else
275         maxdata = val;
276       break;
277
278     case OPTION_MAXSTACK:
279       val = strtoul (optarg, &end, 0);
280       if (*end != '\0')
281         einfo ("%P: warning: ignoring invalid -bmaxstack number %s\n",
282                optarg);
283       else
284         maxstack = val;
285       break;
286
287     case OPTION_MODTYPE:
288       if (*optarg == 'S')
289         {
290           link_info.shared = true;
291           ++optarg;
292         }
293       if (*optarg == '\0' || optarg[1] == '\0')
294         einfo ("%P: warning: ignoring invalid module type %s\n", optarg);
295       else
296         modtype = (*optarg << 8) | optarg[1];
297       break;
298
299     case OPTION_NOAUTOIMP:
300       link_info.static_link = true;
301       break;
302
303     case OPTION_NOSTRCMPCT:
304       config.traditional_format = true;
305       break;
306
307     case OPTION_STRCMPCT:
308       config.traditional_format = false;
309       break;
310     }
311
312   return 1;
313 }
314
315 /* This is called after the sections have been attached to output
316    sections, but before any sizes or addresses have been set.  */
317
318 static void
319 gld${EMULATION_NAME}_before_allocation ()
320 {
321   struct filelist *fl;
322   char *libpath;
323
324   /* Handle the import and export files, if any.  */
325   for (fl = import_files; fl != NULL; fl = fl->next)
326     gld${EMULATION_NAME}_read_file (fl->name, true);
327   for (fl = export_files; fl != NULL; fl = fl->next)
328     gld${EMULATION_NAME}_read_file (fl->name, false);
329
330   /* We need to build LIBPATH from the -L arguments.  If any -rpath
331      arguments were used, though, we use -rpath instead, as a GNU
332      extension.  */
333   if (command_line.rpath != NULL)
334     libpath = command_line.rpath;
335   else if (search_head == NULL)
336     libpath = (char *) "";
337   else
338     {
339       size_t len;
340       search_dirs_type *search;
341
342       len = strlen (search_head->name);
343       libpath = xmalloc (len + 1);
344       strcpy (libpath, search_head->name);
345       for (search = search_head->next; search != NULL; search = search->next)
346         {
347           size_t nlen;
348
349           nlen = strlen (search->name);
350           libpath = xrealloc (libpath, len + nlen + 2);
351           libpath[len] = ':';
352           strcpy (libpath + len + 1, search->name);
353           len += nlen + 1;
354         }
355     }
356
357   /* Let the XCOFF backend set up the .loader section.  */
358   if (! bfd_xcoff_size_dynamic_sections (output_bfd, &link_info, libpath,
359                                          entry_symbol, file_align,
360                                          maxstack, maxdata,
361                                          gc ? true : false,
362                                          modtype,
363                                          textro ? true : false))
364     einfo ("%P%F: failed to set dynamic section sizes: %E\n");
365 }
366
367 /* Read an import or export file.  */
368
369 static void
370 gld${EMULATION_NAME}_read_file (filename, import)
371      const char *filename;
372      boolean import;
373 {
374   struct obstack *o;
375   FILE *f;
376   int lineno;
377   int c;
378   boolean keep;
379   const char *imppath;
380   const char *impfile;
381   const char *impmember;
382
383   o = (struct obstack *) xmalloc (sizeof (struct obstack));
384   obstack_specify_allocation (o, 0, 0, xmalloc, gld${EMULATION_NAME}_free);
385
386   f = fopen (filename, "r");
387   if (f == NULL)
388     {
389       bfd_set_error (bfd_error_system_call);
390       einfo ("%F%s: %E\n", filename);
391     }
392
393   keep = false;
394
395   imppath = NULL;
396   impfile = NULL;
397   impmember = NULL;
398
399   lineno = 0;
400   while ((c = getc (f)) != EOF)
401     {
402       char *s;
403       char *symname;
404       boolean syscall;
405       bfd_vma address;
406       struct bfd_link_hash_entry *h;
407
408       if (c != '\n')
409         {
410           obstack_1grow (o, c);
411           continue;
412         }
413
414       obstack_1grow (o, '\0');
415       ++lineno;
416
417       s = (char *) obstack_base (o);
418       while (isspace ((unsigned char) *s))
419         ++s;
420       if (*s == '\0'
421           || *s == '*'
422           || (*s == '#' && s[1] == ' ')
423           || (! import && *s == '#' && s[1] == '!'))
424         {
425           obstack_free (o, obstack_base (o));
426           continue;
427         }
428
429       if (*s == '#' && s[1] == '!')
430         {
431           s += 2;
432           while (isspace ((unsigned char) *s))
433             ++s;
434           if (*s == '\0')
435             {
436               imppath = NULL;
437               impfile = NULL;
438               impmember = NULL;
439               obstack_free (o, obstack_base (o));
440             }
441           else if (*s == '(')
442             einfo ("%F%s%d: #! ([member]) is not supported in import files",
443                    filename, lineno);
444           else
445             {
446               char cs;
447               char *file;
448
449               (void) obstack_finish (o);
450               keep = true;
451               imppath = s;
452               impfile = NULL;
453               while (! isspace ((unsigned char) *s) && *s != '(' && *s != '\0')
454                 {
455                   if (*s == '/')
456                     file = s + 1;
457                   ++s;
458                 }
459               if (file != NULL)
460                 {
461                   file[-1] = '\0';
462                   impfile = file;
463                   if (imppath == file - 1)
464                     imppath = "/";
465                 }
466               else
467                 {
468                   impfile = imppath;
469                   imppath = "";
470                 }
471               cs = *s;
472               *s = '\0';
473               while (isspace ((unsigned char) cs))
474                 {
475                   ++s;
476                   cs = *s;
477                 }
478               if (cs != '(')
479                 {
480                   impmember = "";
481                   if (cs != '\0')
482                     einfo ("%s:%d: warning: syntax error in import file\n",
483                            filename, lineno);
484                 }
485               else
486                 {
487                   ++s;
488                   impmember = s;
489                   while (*s != ')' && *s != '\0')
490                     ++s;
491                   if (*s == ')')
492                     *s = '\0';
493                   else
494                     einfo ("%s:%d: warning: syntax error in import file\n",
495                            filename, lineno);
496                 }
497             }
498
499           continue;
500         }
501
502       /* This is a symbol to be imported or exported.  */
503       symname = s;
504       syscall = false;
505       address = (bfd_vma) -1;
506
507       while (! isspace ((unsigned char) *s) && *s != '\0')
508         ++s;
509       if (*s != '\0')
510         {
511           char *se;
512
513           *s++ = '\0';
514
515           while (isspace ((unsigned char) *s))
516             ++s;
517
518           se = s;
519           while (! isspace ((unsigned char) *se) && *se != '\0')
520             ++se;
521           if (*se != '\0')
522             {
523               *se++ = '\0';
524               while (isspace ((unsigned char) *se))
525                 ++se;
526               if (*se != '\0')
527                 einfo ("%s%d: warning: syntax error in import/export file\n",
528                        filename, lineno);
529             }
530
531           if (strcasecmp (s, "svc") == 0
532               || strcasecmp (s, "syscall") == 0)
533             syscall = true;
534           else
535             {
536               char *end;
537
538               address = strtoul (s, &end, 0);
539               if (*end != '\0')
540                 einfo ("%s:%d: warning: syntax error in import/export file\n",
541                        filename, lineno);
542             }
543         }
544
545       h = bfd_link_hash_lookup (link_info.hash, symname, false, false, true);
546       if (h == NULL || h->type == bfd_link_hash_new)
547         {
548           /* We can just ignore attempts to import an unreferenced
549              symbol.  */
550           if (! import)
551             einfo ("%X%s:%d: attempt to export undefined symbol %s\n",
552                    filename, lineno, symname);
553         }
554       else if (import)
555         {
556           if (! bfd_xcoff_import_symbol (output_bfd, &link_info, h, address,
557                                          imppath, impfile, impmember))
558             einfo ("%X%s:%d: failed to import symbol %s: %E\n",
559                    filename, lineno, symname);
560         }
561       else
562         {
563           if (! bfd_xcoff_export_symbol (output_bfd, &link_info, h, syscall))
564             einfo ("%X%s:%d: failed to export symbol %s: %E\n",
565                    filename, lineno, symname);
566         }
567
568       obstack_free (o, obstack_base (o));
569     }
570
571   if (obstack_object_size (o) > 0)
572     {
573       einfo ("%s:%d: warning: ignoring unterminated last line\n",
574              filename, lineno);
575       obstack_free (o, obstack_base (o));
576     }
577
578   if (! keep)
579     {
580       obstack_free (o, NULL);
581       free (o);
582     }
583 }
584
585 /* This routine saves us from worrying about declaring free.  */
586
587 static void
588 gld${EMULATION_NAME}_free (p)
589      PTR p;
590 {
591   free (p);
592 }
593
594 static char *
595 gld${EMULATION_NAME}_get_script(isfile)
596      int *isfile;
597 EOF
598
599 if test -n "$COMPILE_IN"
600 then
601 # Scripts compiled in.
602
603 # sed commands to quote an ld script as a C string.
604 sc="-f ${srcdir}/emultempl/stringify.sed"
605
606 cat >>e${EMULATION_NAME}.c <<EOF
607 {                            
608   *isfile = 0;
609
610   if (link_info.relocateable == true && config.build_constructors == true)
611     return
612 EOF
613 sed $sc ldscripts/${EMULATION_NAME}.xu                     >> e${EMULATION_NAME}.c
614 echo '  ; else if (link_info.relocateable == true) return' >> e${EMULATION_NAME}.c
615 sed $sc ldscripts/${EMULATION_NAME}.xr                     >> e${EMULATION_NAME}.c
616 echo '  ; else if (!config.text_read_only) return'         >> e${EMULATION_NAME}.c
617 sed $sc ldscripts/${EMULATION_NAME}.xbn                    >> e${EMULATION_NAME}.c
618 echo '  ; else if (!config.magic_demand_paged) return'     >> e${EMULATION_NAME}.c
619 sed $sc ldscripts/${EMULATION_NAME}.xn                     >> e${EMULATION_NAME}.c
620 echo '  ; else return'                                     >> e${EMULATION_NAME}.c
621 sed $sc ldscripts/${EMULATION_NAME}.x                      >> e${EMULATION_NAME}.c
622 echo '; }'                                                 >> e${EMULATION_NAME}.c
623
624 else
625 # Scripts read from the filesystem.
626
627 cat >>e${EMULATION_NAME}.c <<EOF
628 {                            
629   *isfile = 1;
630
631   if (link_info.relocateable == true && config.build_constructors == true)
632     return "ldscripts/${EMULATION_NAME}.xu";
633   else if (link_info.relocateable == true)
634     return "ldscripts/${EMULATION_NAME}.xr";
635   else if (!config.text_read_only)
636     return "ldscripts/${EMULATION_NAME}.xbn";
637   else if (!config.magic_demand_paged)
638     return "ldscripts/${EMULATION_NAME}.xn";
639   else
640     return "ldscripts/${EMULATION_NAME}.x";
641 }
642 EOF
643
644 fi
645
646 cat >>e${EMULATION_NAME}.c <<EOF
647
648 struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation = 
649 {
650   gld${EMULATION_NAME}_before_parse,
651   syslib_default,
652   hll_default,
653   after_parse_default,
654   after_open_default,
655   after_allocation_default,
656   set_output_arch_default,
657   ldemul_default_target,
658   gld${EMULATION_NAME}_before_allocation,
659   gld${EMULATION_NAME}_get_script,
660   "${EMULATION_NAME}",
661   "${OUTPUT_FORMAT}",
662   0,    /* finish */
663   0,    /* create_output_section_statements */
664   0,    /* open_dynamic_archive */
665   0,    /* place_orphan */
666   0,    /* set_symbols */
667   gld${EMULATION_NAME}_parse_args,
668 };
669 EOF