Tue Mar 12 12:43:59 1996 David Mosberger-Tang <davidm@koala.azstarnet.com>
[external/binutils.git] / ld / lexsup.c
1 /* Parse options for the GNU linker.
2    Copyright (C) 1991, 92, 93, 94, 95, 1996 Free Software Foundation, Inc.
3
4 This file is part of GLD, the Gnu Linker.
5
6 GLD is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GLD is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GLD; see the file COPYING.  If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
19
20 #include "bfd.h"
21 #include "sysdep.h"
22 #include <stdio.h>
23 #include <string.h>
24 #include <ctype.h>
25 #include "getopt.h"
26 #include "bfdlink.h"
27 #include "ld.h"
28 #include "ldmain.h"
29 #include "ldmisc.h"
30 #include "ldexp.h"
31 #include "ldlang.h"
32 #include "ldgram.h"
33 #include "ldlex.h"
34 #include "ldfile.h"
35 #include "ldver.h"
36 #include "ldemul.h"
37
38 /* Somewhere above, sys/stat.h got included . . . . */
39 #if !defined(S_ISDIR) && defined(S_IFDIR)
40 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
41 #endif
42
43 /* Omit args to avoid the possibility of clashing with a system header
44    that might disagree about consts.  */
45 unsigned long strtoul ();
46
47 static void set_default_dirlist PARAMS ((char *dirlist_ptr));
48 static void set_section_start PARAMS ((char *sect, char *valstr));
49
50 /* Non-zero if we are processing a --defsym from the command line.  */
51 int parsing_defsym = 0;
52
53 void
54 parse_args (argc, argv)
55      int argc;
56      char **argv;
57 {
58   int i;
59   int ingroup = 0;
60   char *default_dirlist = NULL;
61
62   /* Starting the short option string with '-' is for programs that
63      expect options and other ARGV-elements in any order and that care about
64      the ordering of the two.  We describe each non-option ARGV-element
65      as if it were the argument of an option with character code 1.  */
66
67   const char *shortopts =
68     "-a:A:b:c:de:F::G:gh:iL:l:Mm:NnO:o:R:rSsT:tu:VvXxY:y:z:()";
69
70   /* 150 isn't special; it's just an arbitrary non-ASCII char value.  */
71
72 #define OPTION_ASSERT                   150
73 #define OPTION_CALL_SHARED              (OPTION_ASSERT + 1)
74 #define OPTION_DEFSYM                   (OPTION_CALL_SHARED + 1)
75 #define OPTION_DYNAMIC_LINKER           (OPTION_DEFSYM + 1)
76 #define OPTION_EB                       (OPTION_DYNAMIC_LINKER + 1)
77 #define OPTION_EL                       (OPTION_EB + 1)
78 #define OPTION_EMBEDDED_RELOCS          (OPTION_EL + 1)
79 #define OPTION_EXPORT_DYNAMIC           (OPTION_EMBEDDED_RELOCS + 1)
80 #define OPTION_HELP                     (OPTION_EXPORT_DYNAMIC + 1)
81 #define OPTION_IGNORE                   (OPTION_HELP + 1)
82 #define OPTION_MAP                      (OPTION_IGNORE + 1)
83 #define OPTION_NO_KEEP_MEMORY           (OPTION_MAP + 1)
84 #define OPTION_NOINHIBIT_EXEC           (OPTION_NO_KEEP_MEMORY + 1)
85 #define OPTION_NON_SHARED               (OPTION_NOINHIBIT_EXEC + 1)
86 #define OPTION_NO_WHOLE_ARCHIVE         (OPTION_NON_SHARED + 1)
87 #define OPTION_OFORMAT                  (OPTION_NO_WHOLE_ARCHIVE + 1)
88 #define OPTION_RELAX                    (OPTION_OFORMAT + 1)
89 #define OPTION_RETAIN_SYMBOLS_FILE      (OPTION_RELAX + 1)
90 #define OPTION_RPATH                    (OPTION_RETAIN_SYMBOLS_FILE + 1)
91 #define OPTION_RPATH_LINK               (OPTION_RPATH + 1)
92 #define OPTION_SHARED                   (OPTION_RPATH_LINK + 1)
93 #define OPTION_SONAME                   (OPTION_SHARED + 1)
94 #define OPTION_SORT_COMMON              (OPTION_SONAME + 1)
95 #define OPTION_STATS                    (OPTION_SORT_COMMON + 1)
96 #define OPTION_SYMBOLIC                 (OPTION_STATS + 1)
97 #define OPTION_TBSS                     (OPTION_SYMBOLIC + 1)
98 #define OPTION_TDATA                    (OPTION_TBSS + 1)
99 #define OPTION_TTEXT                    (OPTION_TDATA + 1)
100 #define OPTION_TRADITIONAL_FORMAT       (OPTION_TTEXT + 1)
101 #define OPTION_UR                       (OPTION_TRADITIONAL_FORMAT + 1)
102 #define OPTION_VERBOSE                  (OPTION_UR + 1)
103 #define OPTION_VERSION                  (OPTION_VERBOSE + 1)
104 #define OPTION_WARN_COMMON              (OPTION_VERSION + 1)
105 #define OPTION_WARN_CONSTRUCTORS        (OPTION_WARN_COMMON + 1)
106 #define OPTION_WARN_MULTIPLE_GP         (OPTION_WARN_CONSTRUCTORS + 1)
107 #define OPTION_WARN_ONCE                (OPTION_WARN_MULTIPLE_GP + 1)
108 #define OPTION_SPLIT_BY_RELOC           (OPTION_WARN_ONCE + 1)
109 #define OPTION_SPLIT_BY_FILE            (OPTION_SPLIT_BY_RELOC + 1)
110 #define OPTION_WHOLE_ARCHIVE            (OPTION_SPLIT_BY_FILE + 1)
111
112
113   static struct option longopts[] = {
114   /* Sorted alphabeticaly, except for the PE options grouped at the end. */
115     {"assert", required_argument, NULL, OPTION_ASSERT},
116     {"Bdynamic", no_argument, NULL, OPTION_CALL_SHARED},
117     {"Bstatic", no_argument, NULL, OPTION_NON_SHARED},
118     {"Bsymbolic", no_argument, NULL, OPTION_SYMBOLIC},
119     {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
120     {"dc", no_argument, NULL, 'd'},
121     {"defsym", required_argument, NULL, OPTION_DEFSYM},
122     {"dll-verbose", no_argument, NULL, OPTION_VERSION}, /* Linux.  */
123     {"dn", no_argument, NULL, OPTION_NON_SHARED},
124     {"dp", no_argument, NULL, 'd'},
125     {"dy", no_argument, NULL, OPTION_CALL_SHARED},
126     {"dynamic-linker", required_argument, NULL, OPTION_DYNAMIC_LINKER},
127     {"EB", no_argument, NULL, OPTION_EB},
128     {"EL", no_argument, NULL, OPTION_EL},
129     {"embedded-relocs", no_argument, NULL, OPTION_EMBEDDED_RELOCS},
130     {"end-group", no_argument, NULL, ')'},
131     {"export-dynamic", no_argument, NULL, OPTION_EXPORT_DYNAMIC},
132     {"format", required_argument, NULL, 'b'},
133     {"help", no_argument, NULL, OPTION_HELP},
134     {"Map", required_argument, NULL, OPTION_MAP},
135     {"no-keep-memory", no_argument, NULL, OPTION_NO_KEEP_MEMORY},
136     {"no-whole-archive", no_argument, NULL, OPTION_NO_WHOLE_ARCHIVE},
137     {"noinhibit-exec", no_argument, NULL, OPTION_NOINHIBIT_EXEC},
138     {"noinhibit_exec", no_argument, NULL, OPTION_NOINHIBIT_EXEC},
139     {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
140     {"oformat", required_argument, NULL, OPTION_OFORMAT},
141     {"Qy", no_argument, NULL, OPTION_IGNORE},
142     {"qmagic", no_argument, NULL, OPTION_IGNORE}, /* Linux compatibility.  */
143     {"relax", no_argument, NULL, OPTION_RELAX},
144     {"retain-symbols-file", required_argument, NULL, OPTION_RETAIN_SYMBOLS_FILE},
145     {"rpath", required_argument, NULL, OPTION_RPATH},
146     {"rpath-link", required_argument, NULL, OPTION_RPATH_LINK},
147     {"shared", no_argument, NULL, OPTION_SHARED},
148     {"soname", required_argument, NULL, OPTION_SONAME},
149     {"sort-common", no_argument, NULL, OPTION_SORT_COMMON},
150     {"sort_common", no_argument, NULL, OPTION_SORT_COMMON},
151     {"start-group", no_argument, NULL, '('},
152     {"stats", no_argument, NULL, OPTION_STATS},
153     {"static", no_argument, NULL, OPTION_NON_SHARED},
154     {"Tbss", required_argument, NULL, OPTION_TBSS},
155     {"Tdata", required_argument, NULL, OPTION_TDATA},
156     {"Ttext", required_argument, NULL, OPTION_TTEXT},
157     {"traditional-format", no_argument, NULL, OPTION_TRADITIONAL_FORMAT},
158     {"Ur", no_argument, NULL, OPTION_UR},
159     {"verbose", no_argument, NULL, OPTION_VERBOSE},
160     {"version", no_argument, NULL, OPTION_VERSION},
161     {"warn-common", no_argument, NULL, OPTION_WARN_COMMON},
162     {"warn-constructors", no_argument, NULL, OPTION_WARN_CONSTRUCTORS},
163     {"warn-multiple-gp", no_argument, NULL, OPTION_WARN_MULTIPLE_GP},
164     {"warn-once", no_argument, NULL, OPTION_WARN_ONCE},
165     {"split-by-reloc", required_argument, NULL, OPTION_SPLIT_BY_RELOC},
166     {"split-by-file", no_argument, NULL, OPTION_SPLIT_BY_FILE},
167     {"whole-archive", no_argument, NULL, OPTION_WHOLE_ARCHIVE},
168
169       
170     {NULL, no_argument, NULL, 0}
171   };
172
173   /* The -G option is ambiguous on different platforms.  Sometimes it
174      specifies the largest data size to put into the small data
175      section.  Sometimes it is equivalent to --shared.  Unfortunately,
176      the first form takes an argument, while the second does not.
177
178      We need to permit the --shared form because on some platforms,
179      such as Solaris, gcc -shared will pass -G to the linker.
180
181      To permit either usage, we look through the argument list.  If we
182      find -G not followed by a number, we change it into --shared.
183      This will work for most normal cases.  */
184   for (i = 1; i < argc; i++)
185     if (strcmp (argv[i], "-G") == 0
186         && (i + 1 >= argc
187             || ! isdigit (argv[i + 1][0])))
188       argv[i] = (char *) "--shared";
189
190   while (1)
191     {
192       /* getopt_long_only is like getopt_long, but '-' as well as '--' can
193          indicate a long option.  */
194       int longind;
195       int optc;
196
197       if (ldemul_parse_args (argc, argv))
198         continue;
199
200       optc = getopt_long_only (argc, argv, shortopts, longopts, &longind);
201
202       if (optc == -1)
203         break;
204       switch (optc)
205         {
206         default:
207           xexit (1);
208         case 1:                 /* File name.  */
209           lang_add_input_file (optarg, lang_input_file_is_file_enum,
210                                (char *) NULL);
211           break;
212
213         case OPTION_IGNORE:
214           break;
215         case 'a':
216           /* For HP/UX compatibility.  Actually -a shared should mean
217              ``use only shared libraries'' but, then, we don't
218              currently support shared libraries on HP/UX anyhow.  */
219           if (strcmp (optarg, "archive") == 0)
220             config.dynamic_link = false;
221           else if (strcmp (optarg, "shared") == 0
222                    || strcmp (optarg, "default") == 0)
223             config.dynamic_link = true;
224           else
225             einfo ("%P%F: unrecognized -a option `%s'\n", optarg);
226           break;
227         case OPTION_ASSERT:
228           /* FIXME: We just ignore these, but we should handle them.  */
229           if (strcmp (optarg, "definitions") == 0)
230             ;
231           else if (strcmp (optarg, "nodefinitions") == 0)
232             ;
233           else if (strcmp (optarg, "nosymbolic") == 0)
234             ;
235           else if (strcmp (optarg, "pure-text") == 0)
236             ;
237           else
238             einfo ("%P%F: unrecognized -assert option `%s'\n", optarg);
239           break;
240         case 'A':
241           ldfile_add_arch (optarg);
242           break;
243         case 'b':
244           lang_add_target (optarg);
245           break;
246         case 'c':
247           ldfile_open_command_file (optarg);
248           parser_input = input_mri_script;
249           yyparse ();
250           break;
251         case OPTION_CALL_SHARED:
252           config.dynamic_link = true;
253           break;
254         case OPTION_NON_SHARED:
255           config.dynamic_link = false;
256           break;
257         case 'd':
258           command_line.force_common_definition = true;
259           break;
260         case OPTION_DEFSYM:
261           lex_string = optarg;
262           lex_redirect (optarg);
263           parser_input = input_defsym;
264           parsing_defsym = 1;
265           yyparse ();
266           parsing_defsym = 0;
267           lex_string = NULL;
268           break;
269         case OPTION_DYNAMIC_LINKER:
270           command_line.interpreter = optarg;
271           break;
272         case OPTION_EB:
273           command_line.endian = ENDIAN_BIG;
274           break;
275         case OPTION_EL:
276           command_line.endian = ENDIAN_LITTLE;
277           break;
278         case OPTION_EMBEDDED_RELOCS:
279           command_line.embedded_relocs = true;
280           break;
281         case OPTION_EXPORT_DYNAMIC:
282           command_line.export_dynamic = true;
283           break;
284         case 'e':
285           lang_add_entry (optarg, true);
286           break;
287         case 'F':
288           /* Ignore.  */
289           break;
290         case 'G':
291           {
292             char *end;
293             g_switch_value = strtoul (optarg, &end, 0);
294             if (*end)
295               einfo ("%P%F: invalid number `%s'\n", optarg);
296           }
297           break;
298         case 'g':
299           /* Ignore.  */
300           break;
301         case OPTION_HELP:
302           help ();
303           xexit (0);
304           break;
305         case 'L':
306           ldfile_add_library_path (optarg, true);
307           break;
308         case 'l':
309           lang_add_input_file (optarg, lang_input_file_is_l_enum,
310                                (char *) NULL);
311           break;
312         case 'M':
313           config.map_filename = "-";
314           break;
315         case 'm':
316           /* Ignore.  Was handled in a pre-parse.   */
317           break;
318         case OPTION_MAP:
319           config.map_filename = optarg;
320           break;
321         case 'N':
322           config.text_read_only = false;
323           config.magic_demand_paged = false;
324           config.dynamic_link = false;
325           break;
326         case 'n':
327           config.magic_demand_paged = false;
328           config.dynamic_link = false;
329           break;
330         case OPTION_NO_KEEP_MEMORY:
331           link_info.keep_memory = false;
332           break;
333         case OPTION_NOINHIBIT_EXEC:
334           force_make_executable = true;
335           break;
336         case OPTION_NO_WHOLE_ARCHIVE:
337           whole_archive = false;
338           break;
339         case 'O':
340           /* FIXME "-O<non-digits> <value>" used to set the address of
341              section <non-digits>.  Was this for compatibility with
342              something, or can we create a new option to do that
343              (with a syntax similar to -defsym)?
344              getopt can't handle two args to an option without kludges.  */
345           break;
346         case 'o':
347           lang_add_output (optarg, 0); 
348           break;
349         case OPTION_OFORMAT:
350           lang_add_output_format (optarg, (char *) NULL, (char *) NULL, 0);
351           break;
352         case 'i':
353         case 'r':
354           link_info.relocateable = true;
355           config.build_constructors = false;
356           config.magic_demand_paged = false;
357           config.text_read_only = false;
358           config.dynamic_link = false;
359           break;
360         case 'R':
361           /* The GNU linker traditionally uses -R to mean to include
362              only the symbols from a file.  The Solaris linker uses -R
363              to set the path used by the runtime linker to find
364              libraries.  This is the GNU linker -rpath argument.  We
365              try to support both simultaneously by checking the file
366              named.  If it is a directory, rather than a regular file,
367              we assume -rpath was meant.  */
368           {
369             struct stat s;
370
371             if (stat (optarg, &s) >= 0
372                 && ! S_ISDIR (s.st_mode))
373               {
374                 lang_add_input_file (optarg,
375                                      lang_input_file_is_symbols_only_enum,
376                                      (char *) NULL);
377                 break;
378               }
379           }
380           /* Fall through.  */
381         case OPTION_RPATH:
382           if (command_line.rpath == NULL)
383             command_line.rpath = buystring (optarg);
384           else
385             {
386               char *buf;
387
388               buf = xmalloc (strlen (command_line.rpath)
389                              + strlen (optarg)
390                              + 2);
391               sprintf (buf, "%s:%s", command_line.rpath, optarg);
392               free (command_line.rpath);
393               command_line.rpath = buf;
394             }
395           break;
396         case OPTION_RPATH_LINK:
397           if (command_line.rpath_link == NULL)
398             command_line.rpath_link = buystring (optarg);
399           else
400             {
401               char *buf;
402
403               buf = xmalloc (strlen (command_line.rpath_link)
404                              + strlen (optarg)
405                              + 2);
406               sprintf (buf, "%s:%s", command_line.rpath_link, optarg);
407               free (command_line.rpath_link);
408               command_line.rpath_link = buf;
409             }
410           break;
411         case OPTION_RELAX:
412           command_line.relax = true;
413           break;
414         case OPTION_RETAIN_SYMBOLS_FILE:
415           add_keepsyms_file (optarg);
416           break;
417         case 'S':
418           link_info.strip = strip_debugger;
419           break;
420         case 's':
421           link_info.strip = strip_all;
422           break;
423         case OPTION_SHARED:
424           link_info.shared = true;
425           break;
426         case 'h':               /* Used on Solaris.  */
427         case OPTION_SONAME:
428           command_line.soname = optarg;
429           break;
430         case OPTION_SORT_COMMON:
431           config.sort_common = true;
432           break;
433         case OPTION_STATS:
434           config.stats = true;
435           break;
436         case OPTION_SYMBOLIC:
437           link_info.symbolic = true;
438           break;
439         case 't':
440           trace_files = true;
441           break;
442         case 'T':
443           ldfile_open_command_file (optarg);
444           parser_input = input_script;
445           yyparse ();
446           break;
447         case OPTION_TBSS:
448           set_section_start (".bss", optarg);
449           break;
450         case OPTION_TDATA:
451           set_section_start (".data", optarg);
452           break;
453         case OPTION_TTEXT:
454           set_section_start (".text", optarg);
455           break;
456         case OPTION_TRADITIONAL_FORMAT:
457           config.traditional_format = true;
458           break;
459         case OPTION_UR:
460           link_info.relocateable = true;
461           config.build_constructors = true;
462           config.magic_demand_paged = false;
463           config.text_read_only = false;
464           config.dynamic_link = false;
465           break;
466         case 'u':
467           ldlang_add_undef (optarg);
468           break;
469         case OPTION_VERBOSE:
470           ldversion (1);
471           version_printed = true;
472           trace_file_tries = true;
473           break;
474         case 'v':
475           ldversion (0);
476           version_printed = true;
477           break;
478         case 'V':
479           ldversion (1);
480           version_printed = true;
481           break;
482         case OPTION_VERSION:
483           ldversion (0);
484           version_printed = true;
485           break;
486         case OPTION_WARN_COMMON:
487           config.warn_common = true;
488           break;
489         case OPTION_WARN_CONSTRUCTORS:
490           config.warn_constructors = true;
491           break;
492         case OPTION_WARN_MULTIPLE_GP:
493           config.warn_multiple_gp = true;
494           break;
495         case OPTION_WARN_ONCE:
496           config.warn_once = true;
497           break;
498         case OPTION_WHOLE_ARCHIVE:
499           whole_archive = true;
500           break;
501         case 'X':
502           link_info.discard = discard_l;
503           break;
504         case 'x':
505           link_info.discard = discard_all;
506           break;
507         case 'Y':
508           if (strncmp (optarg, "P,", 2) == 0)
509             optarg += 2;
510           default_dirlist = optarg;
511           break;
512         case 'y':
513           add_ysym (optarg);
514           break;
515         case 'z':
516           /* We accept and ignore this option for Solaris
517              compatibility.  Actually, on Solaris, optarg is not
518              ignored.  Someday we should handle it correctly.  FIXME.  */
519           break;
520         case OPTION_SPLIT_BY_RELOC:
521           config.split_by_reloc = atoi (optarg);
522           break; 
523         case OPTION_SPLIT_BY_FILE:
524           config.split_by_file = true;
525           break; 
526         case '(':
527           if (ingroup)
528             {
529               fprintf (stderr,
530                        "%s: may not nest groups (--help for usage)\n",
531                        program_name);
532               xexit (1);
533             }
534           lang_enter_group ();
535           ingroup = 1;
536           break;
537         case ')':
538           if (! ingroup)
539             {
540               fprintf (stderr,
541                        "%s: group ended before it began (--help for usage)\n",
542                        program_name);
543               xexit (1);
544             }
545           lang_leave_group ();
546           ingroup = 0;
547           break;
548
549         }
550     }
551
552   if (ingroup)
553     lang_leave_group ();
554
555   if (default_dirlist != NULL)
556     set_default_dirlist (default_dirlist);
557
558 }
559
560 /* Add the (colon-separated) elements of DIRLIST_PTR to the
561    library search path.  */
562
563 static void
564 set_default_dirlist (dirlist_ptr)
565      char *dirlist_ptr;
566 {
567   char *p;
568
569   while (1)
570     {
571       p = strchr (dirlist_ptr, ':');
572       if (p != NULL)
573         *p = 0;
574       if (*dirlist_ptr)
575         ldfile_add_library_path (dirlist_ptr, true);
576       if (p == NULL)
577         break;
578       *p = ':';
579       dirlist_ptr = p + 1;
580     }
581 }
582
583 static void
584 set_section_start (sect, valstr)
585      char *sect, *valstr;
586 {
587   char *end;
588   unsigned long val = strtoul (valstr, &end, 16);
589   if (*end)
590     einfo ("%P%F: invalid hex number `%s'\n", valstr);
591   lang_section_start (sect, exp_intop (val));
592 }
593 \f