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