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