Use getopt instead of lex and yacc to parse the command line.
[external/binutils.git] / ld / lexsup.c
1 /* Parse options for the GNU linker.
2    Copyright (C) 1991, 92, 93, 94 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, 675 Mass Ave, Cambridge, MA 02139, USA.  */
19
20 #include "bfd.h"
21 #include "sysdep.h"
22 #include <stdio.h>
23 #include <string.h>
24 #include "getopt.h"
25 #include "bfdlink.h"
26 #include "config.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
36 /* Omit args to avoid the possibility of clashing with a system header
37    that might disagree about consts.  */
38 unsigned long strtoul ();
39
40 static void set_default_dirlist PARAMS ((char *dirlist_ptr));
41 static void set_section_start PARAMS ((char *sect, char *valstr));
42
43 void
44 parse_args (argc, argv)
45      int argc;
46      char **argv;
47 {
48   /* Starting the short option string with '-' is for programs that
49      expect options and other ARGV-elements in any order and that care about
50      the ordering of the two.  We describe each non-option ARGV-element
51      as if it were the argument of an option with character code 1.  */
52
53   const char *shortopts = "-A:B::b:cde:F::G:giL:l:Mm:NnO:o:R:rSsT:tu:VvXxy:";
54
55   static struct option longopts[] =
56   {
57     {"call_shared", no_argument, NULL, 158},
58     {"dc", no_argument, NULL, 'd'},
59     {"defsym", required_argument, NULL, 160},
60     {"dn", no_argument, NULL, 158},
61     {"dp", no_argument, NULL, 'd'},
62     {"EB", no_argument, NULL, 150},
63     {"EL", no_argument, NULL, 151},
64     {"format", required_argument, NULL, 'b'},
65     {"help", no_argument, NULL, 164},
66     {"Map", required_argument, NULL, 152},
67     {"no-keep-memory", no_argument, NULL, 168},
68     {"noinhibit-exec", no_argument, NULL, 169},
69     {"noinhibit_exec", no_argument, NULL, 169},
70     {"non_shared", no_argument, NULL, 158},
71     {"oformat", required_argument, NULL, 172},
72     {"Qy", no_argument, NULL, 158},
73     {"relax", no_argument, NULL, 173},
74     {"retain-symbols-file", no_argument, NULL, 174},
75     {"sort-common", no_argument, NULL, 175},
76     {"sort_common", no_argument, NULL, 175},
77     {"stats", no_argument, NULL, 176},
78     {"Tbss", required_argument, NULL, 154},
79     {"Tdata", required_argument, NULL, 155},
80     {"Ttext", required_argument, NULL, 156},
81     {"Ur", no_argument, NULL, 157},
82     {"version", no_argument, NULL, 178},
83     {"warn-common", no_argument, NULL, 177},
84     {"YP,", required_argument, NULL, 158},
85     {NULL, no_argument, NULL, 0}
86   };
87
88   while (1)
89     {
90       /* getopt_long_only is like getopt_long, but '-' as well as '--' can
91          indicate a long option.  */
92       int optc = getopt_long_only (argc, argv, shortopts, longopts,
93                                    (int *) NULL);
94       if (optc == -1)
95         break;
96
97       switch (optc)
98         {
99         default:
100           xexit (1);
101         case 1:                 /* File name.  */
102           lang_add_input_file (optarg, lang_input_file_is_file_enum,
103                                (char *) NULL);
104           break;
105
106         case 'A':
107           ldfile_add_arch (optarg);
108           break;
109         case 'B':
110           /* Ignore.  */
111           break;
112         case 'b':
113           lang_add_target (optarg);
114           break;
115         case 'c':
116           ldfile_open_command_file (optarg);
117           parser_input = input_mri_script;
118           yyparse ();
119           break;
120         case 158:               /* call_shared, dn, non_shared, Qy, YP */
121           set_default_dirlist (optarg);
122           break;
123         case 'd':
124           command_line.force_common_definition = true;
125           break;
126         case 160:               /* defsym */
127           lex_redirect (optarg);
128           parser_input = input_defsym;
129           yyparse ();
130           break;
131         case 150:               /* EB */
132           /* FIXME: This is currently ignored.  It means
133              ``produce a big-endian object file''.  It could
134              be used to select an output format.  */
135           break;
136         case 151:               /* EL */
137           /* FIXME: This is currently ignored.  It means
138              ``produce a little-endian object file''.  It could
139              be used to select an output format.  */
140           break;
141         case 'e':
142           lang_add_entry (optarg);
143           break;
144         case 'F':
145           /* Ignore.  */
146           break;
147         case 'G':
148           {
149             char *end;
150             g_switch_value = strtoul (optarg, &end, 0);
151             if (*end)
152               einfo ("%P%F: invalid number `%s'", optarg);
153           }
154           break;
155         case 'g':
156           /* Ignore.  */
157           break;
158         case 164:               /* help */
159           help ();
160           xexit (0);
161           break;
162         case 'L':
163           ldfile_add_library_path (optarg);
164           break;
165         case 'l':
166           lang_add_input_file (optarg, lang_input_file_is_l_enum,
167                                (char *) NULL);
168           break;
169         case 'M':
170           config.map_filename = "-";
171           break;
172         case 'm':
173           /* Ignore.  Was handled in a pre-parse.   */
174           break;
175         case 152:               /* Map */
176           write_map = true;
177           config.map_filename = optarg;
178           break;
179         case 'N':
180           config.text_read_only = false;
181           config.magic_demand_paged = false;
182           break;
183         case 'n':
184           config.magic_demand_paged = false;
185           break;
186         case 168:               /* no-keep-memory */
187           link_info.keep_memory = false;
188           break;
189         case 169:               /* noinhibit-exec */
190           force_make_executable = true;
191           break;
192         case 'O':
193           /* FIXME "-O<non-digits> <value>" used to set the address of
194              section <non-digits>.  Was this for compatibility with
195              something, or can we create a new option to do that
196              (with a syntax similar to -defsym)?
197              getopt can't handle two args to an option without kludges.  */
198           set_default_dirlist (optarg);
199           break;
200         case 'o':
201           lang_add_output (optarg, 0); 
202           break;
203         case 172:               /* oformat */
204           lang_add_output_format (optarg, 0);
205           break;
206         case 'r':
207           link_info.relocateable = true;
208           config.build_constructors = false;
209           config.magic_demand_paged = false;
210           config.text_read_only = false;
211           break;
212         case 'R':
213           lang_add_input_file (optarg,
214                                lang_input_file_is_symbols_only_enum,
215                                (char *) NULL);
216           break;
217         case 173:               /* relax */
218           command_line.relax = true;
219           break;
220         case 174:               /* retain-symbols-file */
221           add_keepsyms_file (optarg);
222           break;
223         case 'S':
224           link_info.strip = strip_debugger;
225           break;
226         case 's':
227           link_info.strip = strip_all;
228           break;
229         case 175:               /* sort-common */
230           config.sort_common = true;
231           break;
232         case 176:               /* stats */
233           config.stats = true;
234           break;
235         case 't':
236           trace_files = true;
237           break;
238         case 'T':
239           ldfile_open_command_file (optarg);
240           parser_input = input_script;
241           yyparse ();
242           break;
243         case 154:               /* Tbss */
244           set_section_start (".bss", optarg);
245           break;
246         case 155:               /* Tdata */
247           set_section_start (".data", optarg);
248           break;
249         case 156:               /* Ttext */
250           set_section_start (".text", optarg);
251           break;
252         case 157:               /* Ur */
253           link_info.relocateable = true;
254           config.build_constructors = true;
255           config.magic_demand_paged = false;
256           config.text_read_only = false;
257           break;
258         case 'u':
259           ldlang_add_undef (optarg);
260           break;
261         case 'V':
262           ldversion (1);
263           version_printed = true;
264           trace_file_tries = true;
265           break;
266         case 'v':
267           ldversion (0);
268           version_printed = true;
269           break;
270         case 178:               /* version */
271           ldversion (0);
272           version_printed = true;
273           break;
274         case 177:               /* warn-common */
275           config.warn_common = true;
276           break;
277         case 'X':
278           link_info.discard = discard_l;
279           break;
280         case 'x':
281           link_info.discard = discard_all;
282           break;
283         case 'y':
284           add_ysym (optarg);
285           break;
286         }
287     }
288 }
289
290 /* Add the (colon-separated) elements of DIRLIST_PTR to the
291    library search path.  */
292
293 static void
294 set_default_dirlist (dirlist_ptr)
295      char *dirlist_ptr;
296 {
297   char *p;
298
299   while (1)
300     {
301       p = strchr (dirlist_ptr, ':');
302       if (p != NULL)
303         *p = 0;
304       if (*dirlist_ptr)
305         ldfile_add_library_path (dirlist_ptr);
306       if (p == NULL)
307         break;
308       *p = ':';
309       dirlist_ptr = p + 1;
310     }
311 }
312
313 static void
314 set_section_start (sect, valstr)
315      char *sect, *valstr;
316 {
317   char *end;
318   unsigned long val = strtoul (valstr, &end, 16);
319   if (*end)
320     einfo ("%P%F: invalid hex number `%s'", valstr);
321   lang_section_start (sect, exp_intop (val));
322 }