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