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