* lexsup.c (parse_args): Use symbolic numbers for long options.
[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:y:";
54
55   /* 150 isn't special; it's just an arbitrary non-ASCII char value.  */
56
57   static struct option longopts[] = {
58 #define OPTION_CALL_SHARED 150
59     {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
60     {"dc", no_argument, NULL, 'd'},
61 #define OPTION_DEFSYM 151
62     {"defsym", required_argument, NULL, OPTION_DEFSYM},
63     {"dn", no_argument, NULL, OPTION_CALL_SHARED},
64     {"dp", no_argument, NULL, 'd'},
65 #define OPTION_EB 152
66     {"EB", no_argument, NULL, OPTION_EB},
67 #define OPTION_EL 153
68     {"EL", no_argument, NULL, OPTION_EL},
69     {"format", required_argument, NULL, 'b'},
70 #define OPTION_HELP 154
71     {"help", no_argument, NULL, OPTION_HELP},
72 #define OPTION_MAP 155
73     {"Map", required_argument, NULL, OPTION_MAP},
74 #define OPTION_NO_KEEP_MEMORY 156
75     {"no-keep-memory", no_argument, NULL, OPTION_NO_KEEP_MEMORY},
76 #define OPTION_NOINHIBIT_EXEC 157
77     {"noinhibit-exec", no_argument, NULL, OPTION_NOINHIBIT_EXEC},
78     {"noinhibit_exec", no_argument, NULL, OPTION_NOINHIBIT_EXEC},
79     {"non_shared", no_argument, NULL, OPTION_CALL_SHARED},
80 #define OPTION_OFORMAT 158
81     {"oformat", required_argument, NULL, OPTION_OFORMAT},
82     {"Qy", no_argument, NULL, OPTION_CALL_SHARED},
83 #define OPTION_RELAX 159
84     {"relax", no_argument, NULL, OPTION_RELAX},
85 #define OPTION_RETAIN_SYMBOLS_FILE 160
86     {"retain-symbols-file", no_argument, NULL, OPTION_RETAIN_SYMBOLS_FILE},
87 #define OPTION_SORT_COMMON 161
88     {"sort-common", no_argument, NULL, OPTION_SORT_COMMON},
89     {"sort_common", no_argument, NULL, OPTION_SORT_COMMON},
90 #define OPTION_STATS 162
91     {"stats", no_argument, NULL, OPTION_STATS},
92 #define OPTION_TBSS 163
93     {"Tbss", required_argument, NULL, OPTION_TBSS},
94 #define OPTION_TDATA 164
95     {"Tdata", required_argument, NULL, OPTION_TDATA},
96 #define OPTION_TTEXT 165
97     {"Ttext", required_argument, NULL, OPTION_TTEXT},
98 #define OPTION_UR 166
99     {"Ur", no_argument, NULL, OPTION_UR},
100 #define OPTION_VERSION 167
101     {"version", no_argument, NULL, OPTION_VERSION},
102 #define OPTION_WARN_COMMON 168
103     {"warn-common", no_argument, NULL, OPTION_WARN_COMMON},
104     {NULL, no_argument, NULL, 0}
105   };
106
107   while (1)
108     {
109       /* getopt_long_only is like getopt_long, but '-' as well as '--' can
110          indicate a long option.  */
111       int longind;
112       int optc = getopt_long_only (argc, argv, shortopts, longopts, &longind);
113
114       if (optc == -1)
115         break;
116
117       switch (optc)
118         {
119         default:
120           xexit (1);
121         case 1:                 /* File name.  */
122           lang_add_input_file (optarg, lang_input_file_is_file_enum,
123                                (char *) NULL);
124           break;
125
126         case 'A':
127           ldfile_add_arch (optarg);
128           break;
129         case 'B':
130           /* Ignore.  */
131           break;
132         case 'b':
133           lang_add_target (optarg);
134           break;
135         case 'c':
136           ldfile_open_command_file (optarg);
137           parser_input = input_mri_script;
138           yyparse ();
139           break;
140         case OPTION_CALL_SHARED:
141           set_default_dirlist ((char *) longopts[longind].name);
142           break;
143         case 'd':
144           command_line.force_common_definition = true;
145           break;
146         case OPTION_DEFSYM:
147           lex_redirect (optarg);
148           parser_input = input_defsym;
149           yyparse ();
150           break;
151         case OPTION_EB:
152           /* FIXME: This is currently ignored.  It means
153              ``produce a big-endian object file''.  It could
154              be used to select an output format.  */
155           break;
156         case OPTION_EL:
157           /* FIXME: This is currently ignored.  It means
158              ``produce a little-endian object file''.  It could
159              be used to select an output format.  */
160           break;
161         case 'e':
162           lang_add_entry (optarg);
163           break;
164         case 'F':
165           /* Ignore.  */
166           break;
167         case 'G':
168           {
169             char *end;
170             g_switch_value = strtoul (optarg, &end, 0);
171             if (*end)
172               einfo ("%P%F: invalid number `%s'", optarg);
173           }
174           break;
175         case 'g':
176           /* Ignore.  */
177           break;
178         case OPTION_HELP:
179           help ();
180           xexit (0);
181           break;
182         case 'L':
183           ldfile_add_library_path (optarg);
184           break;
185         case 'l':
186           lang_add_input_file (optarg, lang_input_file_is_l_enum,
187                                (char *) NULL);
188           break;
189         case 'M':
190           config.map_filename = "-";
191           break;
192         case 'm':
193           /* Ignore.  Was handled in a pre-parse.   */
194           break;
195         case OPTION_MAP:
196           write_map = true;
197           config.map_filename = optarg;
198           break;
199         case 'N':
200           config.text_read_only = false;
201           config.magic_demand_paged = false;
202           break;
203         case 'n':
204           config.magic_demand_paged = false;
205           break;
206         case OPTION_NO_KEEP_MEMORY:
207           link_info.keep_memory = false;
208           break;
209         case OPTION_NOINHIBIT_EXEC:
210           force_make_executable = true;
211           break;
212         case 'O':
213           /* FIXME "-O<non-digits> <value>" used to set the address of
214              section <non-digits>.  Was this for compatibility with
215              something, or can we create a new option to do that
216              (with a syntax similar to -defsym)?
217              getopt can't handle two args to an option without kludges.  */
218           set_default_dirlist (optarg);
219           break;
220         case 'o':
221           lang_add_output (optarg, 0); 
222           break;
223         case OPTION_OFORMAT:
224           lang_add_output_format (optarg, 0);
225           break;
226         case 'r':
227           link_info.relocateable = true;
228           config.build_constructors = false;
229           config.magic_demand_paged = false;
230           config.text_read_only = false;
231           break;
232         case 'R':
233           lang_add_input_file (optarg,
234                                lang_input_file_is_symbols_only_enum,
235                                (char *) NULL);
236           break;
237         case OPTION_RELAX:
238           command_line.relax = true;
239           break;
240         case OPTION_RETAIN_SYMBOLS_FILE:
241           add_keepsyms_file (optarg);
242           break;
243         case 'S':
244           link_info.strip = strip_debugger;
245           break;
246         case 's':
247           link_info.strip = strip_all;
248           break;
249         case OPTION_SORT_COMMON:
250           config.sort_common = true;
251           break;
252         case OPTION_STATS:
253           config.stats = true;
254           break;
255         case 't':
256           trace_files = true;
257           break;
258         case 'T':
259           ldfile_open_command_file (optarg);
260           parser_input = input_script;
261           yyparse ();
262           break;
263         case OPTION_TBSS:
264           set_section_start (".bss", optarg);
265           break;
266         case OPTION_TDATA:
267           set_section_start (".data", optarg);
268           break;
269         case OPTION_TTEXT:
270           set_section_start (".text", optarg);
271           break;
272         case OPTION_UR:
273           link_info.relocateable = true;
274           config.build_constructors = true;
275           config.magic_demand_paged = false;
276           config.text_read_only = false;
277           break;
278         case 'u':
279           ldlang_add_undef (optarg);
280           break;
281         case 'V':
282           ldversion (1);
283           version_printed = true;
284           trace_file_tries = true;
285           break;
286         case 'v':
287           ldversion (0);
288           version_printed = true;
289           break;
290         case OPTION_VERSION:
291           ldversion (0);
292           version_printed = true;
293           break;
294         case OPTION_WARN_COMMON:
295           config.warn_common = true;
296           break;
297         case 'X':
298           link_info.discard = discard_l;
299           break;
300         case 'x':
301           link_info.discard = discard_all;
302           break;
303         case 'Y':
304           set_default_dirlist (optarg);
305           break;
306         case 'y':
307           add_ysym (optarg);
308           break;
309         }
310     }
311 }
312
313 /* Add the (colon-separated) elements of DIRLIST_PTR to the
314    library search path.  */
315
316 static void
317 set_default_dirlist (dirlist_ptr)
318      char *dirlist_ptr;
319 {
320   char *p;
321
322   while (1)
323     {
324       p = strchr (dirlist_ptr, ':');
325       if (p != NULL)
326         *p = 0;
327       if (*dirlist_ptr)
328         ldfile_add_library_path (dirlist_ptr);
329       if (p == NULL)
330         break;
331       *p = ':';
332       dirlist_ptr = p + 1;
333     }
334 }
335
336 static void
337 set_section_start (sect, valstr)
338      char *sect, *valstr;
339 {
340   char *end;
341   unsigned long val = strtoul (valstr, &end, 16);
342   if (*end)
343     einfo ("%P%F: invalid hex number `%s'\n", valstr);
344   lang_section_start (sect, exp_intop (val));
345 }