* lexsup.c (parse_args): Recognize --no-whole-archive.
[deliverable/binutils-gdb.git] / ld / lexsup.c
1 /* Parse options for the GNU linker.
2 Copyright (C) 1991, 92, 93, 94, 1995 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, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19
20 #include "bfd.h"
21 #include "sysdep.h"
22 #include <stdio.h>
23 #include <string.h>
24 #include <ctype.h>
25 #include "getopt.h"
26 #include "bfdlink.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 #include "ldemul.h"
37
38 /* Somewhere above, sys/stat.h got included . . . . */
39 #if !defined(S_ISDIR) && defined(S_IFDIR)
40 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
41 #endif
42
43 /* Omit args to avoid the possibility of clashing with a system header
44 that might disagree about consts. */
45 unsigned long strtoul ();
46
47 static void set_default_dirlist PARAMS ((char *dirlist_ptr));
48 static void set_section_start PARAMS ((char *sect, char *valstr));
49
50 /* Non-zero if we are processing a --defsym from the command line. */
51 int parsing_defsym = 0;
52
53 void
54 parse_args (argc, argv)
55 int argc;
56 char **argv;
57 {
58 int i;
59 int ingroup = 0;
60 char *default_dirlist = NULL;
61
62 /* Starting the short option string with '-' is for programs that
63 expect options and other ARGV-elements in any order and that care about
64 the ordering of the two. We describe each non-option ARGV-element
65 as if it were the argument of an option with character code 1. */
66
67 const char *shortopts =
68 "-a:A:b:c:de:F::G:gh:iL:l:Mm:NnO:o:R:rSsT:tu:VvXxY:y:z:()";
69
70 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
71
72 #define OPTION_ASSERT 150
73 #define OPTION_CALL_SHARED (OPTION_ASSERT + 1)
74 #define OPTION_DEFSYM (OPTION_CALL_SHARED + 1)
75 #define OPTION_DYNAMIC_LINKER (OPTION_DEFSYM + 1)
76 #define OPTION_EB (OPTION_DYNAMIC_LINKER + 1)
77 #define OPTION_EL (OPTION_EB + 1)
78 #define OPTION_EMBEDDED_RELOCS (OPTION_EL + 1)
79 #define OPTION_EXPORT_DYNAMIC (OPTION_EMBEDDED_RELOCS + 1)
80 #define OPTION_HELP (OPTION_EXPORT_DYNAMIC + 1)
81 #define OPTION_IGNORE (OPTION_HELP + 1)
82 #define OPTION_MAP (OPTION_IGNORE + 1)
83 #define OPTION_NO_KEEP_MEMORY (OPTION_MAP + 1)
84 #define OPTION_NOINHIBIT_EXEC (OPTION_NO_KEEP_MEMORY + 1)
85 #define OPTION_NON_SHARED (OPTION_NOINHIBIT_EXEC + 1)
86 #define OPTION_NO_WHOLE_ARCHIVE (OPTION_NON_SHARED + 1)
87 #define OPTION_OFORMAT (OPTION_NO_WHOLE_ARCHIVE + 1)
88 #define OPTION_RELAX (OPTION_OFORMAT + 1)
89 #define OPTION_RETAIN_SYMBOLS_FILE (OPTION_RELAX + 1)
90 #define OPTION_RPATH (OPTION_RETAIN_SYMBOLS_FILE + 1)
91 #define OPTION_RPATH_LINK (OPTION_RPATH + 1)
92 #define OPTION_SHARED (OPTION_RPATH_LINK + 1)
93 #define OPTION_SONAME (OPTION_SHARED + 1)
94 #define OPTION_SORT_COMMON (OPTION_SONAME + 1)
95 #define OPTION_STATS (OPTION_SORT_COMMON + 1)
96 #define OPTION_SYMBOLIC (OPTION_STATS + 1)
97 #define OPTION_TBSS (OPTION_SYMBOLIC + 1)
98 #define OPTION_TDATA (OPTION_TBSS + 1)
99 #define OPTION_TTEXT (OPTION_TDATA + 1)
100 #define OPTION_TRADITIONAL_FORMAT (OPTION_TTEXT + 1)
101 #define OPTION_UR (OPTION_TRADITIONAL_FORMAT + 1)
102 #define OPTION_VERBOSE (OPTION_UR + 1)
103 #define OPTION_VERSION (OPTION_VERBOSE + 1)
104 #define OPTION_WARN_COMMON (OPTION_VERSION + 1)
105 #define OPTION_WARN_CONSTRUCTORS (OPTION_WARN_COMMON + 1)
106 #define OPTION_WARN_ONCE (OPTION_WARN_CONSTRUCTORS + 1)
107 #define OPTION_SPLIT_BY_RELOC (OPTION_WARN_ONCE + 1)
108 #define OPTION_SPLIT_BY_FILE (OPTION_SPLIT_BY_RELOC + 1)
109 #define OPTION_WHOLE_ARCHIVE (OPTION_SPLIT_BY_FILE + 1)
110
111
112 static struct option longopts[] = {
113 /* Sorted alphabeticaly, except for the PE options grouped at the end. */
114 {"assert", required_argument, NULL, OPTION_ASSERT},
115 {"Bdynamic", no_argument, NULL, OPTION_CALL_SHARED},
116 {"Bstatic", no_argument, NULL, OPTION_NON_SHARED},
117 {"Bsymbolic", no_argument, NULL, OPTION_SYMBOLIC},
118 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
119 {"dc", no_argument, NULL, 'd'},
120 {"defsym", required_argument, NULL, OPTION_DEFSYM},
121 {"dll-verbose", no_argument, NULL, OPTION_VERSION}, /* Linux. */
122 {"dn", no_argument, NULL, OPTION_NON_SHARED},
123 {"dp", no_argument, NULL, 'd'},
124 {"dy", no_argument, NULL, OPTION_CALL_SHARED},
125 {"dynamic-linker", required_argument, NULL, OPTION_DYNAMIC_LINKER},
126 {"EB", no_argument, NULL, OPTION_EB},
127 {"EL", no_argument, NULL, OPTION_EL},
128 {"embedded-relocs", no_argument, NULL, OPTION_EMBEDDED_RELOCS},
129 {"end-group", no_argument, NULL, ')'},
130 {"export-dynamic", no_argument, NULL, OPTION_EXPORT_DYNAMIC},
131 {"format", required_argument, NULL, 'b'},
132 {"help", no_argument, NULL, OPTION_HELP},
133 {"Map", required_argument, NULL, OPTION_MAP},
134 {"no-keep-memory", no_argument, NULL, OPTION_NO_KEEP_MEMORY},
135 {"no-whole-archive", no_argument, NULL, OPTION_NO_WHOLE_ARCHIVE},
136 {"noinhibit-exec", no_argument, NULL, OPTION_NOINHIBIT_EXEC},
137 {"noinhibit_exec", no_argument, NULL, OPTION_NOINHIBIT_EXEC},
138 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
139 {"oformat", required_argument, NULL, OPTION_OFORMAT},
140 {"Qy", no_argument, NULL, OPTION_IGNORE},
141 {"qmagic", no_argument, NULL, OPTION_IGNORE}, /* Linux compatibility. */
142 {"relax", no_argument, NULL, OPTION_RELAX},
143 {"retain-symbols-file", required_argument, NULL, OPTION_RETAIN_SYMBOLS_FILE},
144 {"rpath", required_argument, NULL, OPTION_RPATH},
145 {"rpath-link", required_argument, NULL, OPTION_RPATH_LINK},
146 {"shared", no_argument, NULL, OPTION_SHARED},
147 {"soname", required_argument, NULL, OPTION_SONAME},
148 {"sort-common", no_argument, NULL, OPTION_SORT_COMMON},
149 {"sort_common", no_argument, NULL, OPTION_SORT_COMMON},
150 {"start-group", no_argument, NULL, '('},
151 {"stats", no_argument, NULL, OPTION_STATS},
152 {"static", no_argument, NULL, OPTION_NON_SHARED},
153 {"Tbss", required_argument, NULL, OPTION_TBSS},
154 {"Tdata", required_argument, NULL, OPTION_TDATA},
155 {"Ttext", required_argument, NULL, OPTION_TTEXT},
156 {"traditional-format", no_argument, NULL, OPTION_TRADITIONAL_FORMAT},
157 {"Ur", no_argument, NULL, OPTION_UR},
158 {"verbose", no_argument, NULL, OPTION_VERBOSE},
159 {"version", no_argument, NULL, OPTION_VERSION},
160 {"warn-common", no_argument, NULL, OPTION_WARN_COMMON},
161 {"warn-constructors", no_argument, NULL, OPTION_WARN_CONSTRUCTORS},
162 {"warn-once", no_argument, NULL, OPTION_WARN_ONCE},
163 {"split-by-reloc", required_argument, NULL, OPTION_SPLIT_BY_RELOC},
164 {"split-by-file", no_argument, NULL, OPTION_SPLIT_BY_FILE},
165 {"whole-archive", no_argument, NULL, OPTION_WHOLE_ARCHIVE},
166
167
168 {NULL, no_argument, NULL, 0}
169 };
170
171 /* The -G option is ambiguous on different platforms. Sometimes it
172 specifies the largest data size to put into the small data
173 section. Sometimes it is equivalent to --shared. Unfortunately,
174 the first form takes an argument, while the second does not.
175
176 We need to permit the --shared form because on some platforms,
177 such as Solaris, gcc -shared will pass -G to the linker.
178
179 To permit either usage, we look through the argument list. If we
180 find -G not followed by a number, we change it into --shared.
181 This will work for most normal cases. */
182 for (i = 1; i < argc; i++)
183 if (strcmp (argv[i], "-G") == 0
184 && (i + 1 >= argc
185 || ! isdigit (argv[i + 1][0])))
186 argv[i] = (char *) "--shared";
187
188 while (1)
189 {
190 /* getopt_long_only is like getopt_long, but '-' as well as '--' can
191 indicate a long option. */
192 int longind;
193 int optc;
194
195 if (ldemul_parse_args (argc, argv))
196 continue;
197
198 optc = getopt_long_only (argc, argv, shortopts, longopts, &longind);
199
200 if (optc == -1)
201 break;
202 switch (optc)
203 {
204 default:
205 xexit (1);
206 case 1: /* File name. */
207 lang_add_input_file (optarg, lang_input_file_is_file_enum,
208 (char *) NULL);
209 break;
210
211 case OPTION_IGNORE:
212 break;
213 case 'a':
214 /* For HP/UX compatibility. Actually -a shared should mean
215 ``use only shared libraries'' but, then, we don't
216 currently support shared libraries on HP/UX anyhow. */
217 if (strcmp (optarg, "archive") == 0)
218 config.dynamic_link = false;
219 else if (strcmp (optarg, "shared") == 0
220 || strcmp (optarg, "default") == 0)
221 config.dynamic_link = true;
222 else
223 einfo ("%P%F: unrecognized -a option `%s'\n", optarg);
224 break;
225 case OPTION_ASSERT:
226 /* FIXME: We just ignore these, but we should handle them. */
227 if (strcmp (optarg, "definitions") == 0)
228 ;
229 else if (strcmp (optarg, "nodefinitions") == 0)
230 ;
231 else if (strcmp (optarg, "nosymbolic") == 0)
232 ;
233 else if (strcmp (optarg, "pure-text") == 0)
234 ;
235 else
236 einfo ("%P%F: unrecognized -assert option `%s'\n", optarg);
237 break;
238 case 'A':
239 ldfile_add_arch (optarg);
240 break;
241 case 'b':
242 lang_add_target (optarg);
243 break;
244 case 'c':
245 ldfile_open_command_file (optarg);
246 parser_input = input_mri_script;
247 yyparse ();
248 break;
249 case OPTION_CALL_SHARED:
250 config.dynamic_link = true;
251 break;
252 case OPTION_NON_SHARED:
253 config.dynamic_link = false;
254 break;
255 case 'd':
256 command_line.force_common_definition = true;
257 break;
258 case OPTION_DEFSYM:
259 lex_string = optarg;
260 lex_redirect (optarg);
261 parser_input = input_defsym;
262 parsing_defsym = 1;
263 yyparse ();
264 parsing_defsym = 0;
265 lex_string = NULL;
266 break;
267 case OPTION_DYNAMIC_LINKER:
268 command_line.interpreter = optarg;
269 break;
270 case OPTION_EB:
271 command_line.endian = ENDIAN_BIG;
272 break;
273 case OPTION_EL:
274 command_line.endian = ENDIAN_LITTLE;
275 break;
276 case OPTION_EMBEDDED_RELOCS:
277 command_line.embedded_relocs = true;
278 break;
279 case OPTION_EXPORT_DYNAMIC:
280 command_line.export_dynamic = true;
281 break;
282 case 'e':
283 lang_add_entry (optarg, true);
284 break;
285 case 'F':
286 /* Ignore. */
287 break;
288 case 'G':
289 {
290 char *end;
291 g_switch_value = strtoul (optarg, &end, 0);
292 if (*end)
293 einfo ("%P%F: invalid number `%s'\n", optarg);
294 }
295 break;
296 case 'g':
297 /* Ignore. */
298 break;
299 case OPTION_HELP:
300 help ();
301 xexit (0);
302 break;
303 case 'L':
304 ldfile_add_library_path (optarg, true);
305 break;
306 case 'l':
307 lang_add_input_file (optarg, lang_input_file_is_l_enum,
308 (char *) NULL);
309 break;
310 case 'M':
311 config.map_filename = "-";
312 break;
313 case 'm':
314 /* Ignore. Was handled in a pre-parse. */
315 break;
316 case OPTION_MAP:
317 config.map_filename = optarg;
318 break;
319 case 'N':
320 config.text_read_only = false;
321 config.magic_demand_paged = false;
322 config.dynamic_link = false;
323 break;
324 case 'n':
325 config.magic_demand_paged = false;
326 config.dynamic_link = false;
327 break;
328 case OPTION_NO_KEEP_MEMORY:
329 link_info.keep_memory = false;
330 break;
331 case OPTION_NOINHIBIT_EXEC:
332 force_make_executable = true;
333 break;
334 case OPTION_NO_WHOLE_ARCHIVE:
335 whole_archive = false;
336 break;
337 case 'O':
338 /* FIXME "-O<non-digits> <value>" used to set the address of
339 section <non-digits>. Was this for compatibility with
340 something, or can we create a new option to do that
341 (with a syntax similar to -defsym)?
342 getopt can't handle two args to an option without kludges. */
343 break;
344 case 'o':
345 lang_add_output (optarg, 0);
346 break;
347 case OPTION_OFORMAT:
348 lang_add_output_format (optarg, (char *) NULL, (char *) NULL, 0);
349 break;
350 case 'i':
351 case 'r':
352 link_info.relocateable = true;
353 config.build_constructors = false;
354 config.magic_demand_paged = false;
355 config.text_read_only = false;
356 config.dynamic_link = false;
357 break;
358 case 'R':
359 /* The GNU linker traditionally uses -R to mean to include
360 only the symbols from a file. The Solaris linker uses -R
361 to set the path used by the runtime linker to find
362 libraries. This is the GNU linker -rpath argument. We
363 try to support both simultaneously by checking the file
364 named. If it is a directory, rather than a regular file,
365 we assume -rpath was meant. */
366 {
367 struct stat s;
368
369 if (stat (optarg, &s) >= 0
370 && ! S_ISDIR (s.st_mode))
371 {
372 lang_add_input_file (optarg,
373 lang_input_file_is_symbols_only_enum,
374 (char *) NULL);
375 break;
376 }
377 }
378 /* Fall through. */
379 case OPTION_RPATH:
380 if (command_line.rpath == NULL)
381 command_line.rpath = buystring (optarg);
382 else
383 {
384 char *buf;
385
386 buf = xmalloc (strlen (command_line.rpath)
387 + strlen (optarg)
388 + 2);
389 sprintf (buf, "%s:%s", command_line.rpath, optarg);
390 free (command_line.rpath);
391 command_line.rpath = buf;
392 }
393 break;
394 case OPTION_RPATH_LINK:
395 if (command_line.rpath_link == NULL)
396 command_line.rpath_link = buystring (optarg);
397 else
398 {
399 char *buf;
400
401 buf = xmalloc (strlen (command_line.rpath_link)
402 + strlen (optarg)
403 + 2);
404 sprintf (buf, "%s:%s", command_line.rpath_link, optarg);
405 free (command_line.rpath_link);
406 command_line.rpath_link = buf;
407 }
408 break;
409 case OPTION_RELAX:
410 command_line.relax = true;
411 break;
412 case OPTION_RETAIN_SYMBOLS_FILE:
413 add_keepsyms_file (optarg);
414 break;
415 case 'S':
416 link_info.strip = strip_debugger;
417 break;
418 case 's':
419 link_info.strip = strip_all;
420 break;
421 case OPTION_SHARED:
422 link_info.shared = true;
423 break;
424 case 'h': /* Used on Solaris. */
425 case OPTION_SONAME:
426 command_line.soname = optarg;
427 break;
428 case OPTION_SORT_COMMON:
429 config.sort_common = true;
430 break;
431 case OPTION_STATS:
432 config.stats = true;
433 break;
434 case OPTION_SYMBOLIC:
435 link_info.symbolic = true;
436 break;
437 case 't':
438 trace_files = true;
439 break;
440 case 'T':
441 ldfile_open_command_file (optarg);
442 parser_input = input_script;
443 yyparse ();
444 break;
445 case OPTION_TBSS:
446 set_section_start (".bss", optarg);
447 break;
448 case OPTION_TDATA:
449 set_section_start (".data", optarg);
450 break;
451 case OPTION_TTEXT:
452 set_section_start (".text", optarg);
453 break;
454 case OPTION_TRADITIONAL_FORMAT:
455 config.traditional_format = true;
456 break;
457 case OPTION_UR:
458 link_info.relocateable = true;
459 config.build_constructors = true;
460 config.magic_demand_paged = false;
461 config.text_read_only = false;
462 config.dynamic_link = false;
463 break;
464 case 'u':
465 ldlang_add_undef (optarg);
466 break;
467 case OPTION_VERBOSE:
468 ldversion (1);
469 version_printed = true;
470 trace_file_tries = true;
471 break;
472 case 'v':
473 ldversion (0);
474 version_printed = true;
475 break;
476 case 'V':
477 ldversion (1);
478 version_printed = true;
479 break;
480 case OPTION_VERSION:
481 ldversion (0);
482 version_printed = true;
483 break;
484 case OPTION_WARN_COMMON:
485 config.warn_common = true;
486 break;
487 case OPTION_WARN_CONSTRUCTORS:
488 config.warn_constructors = true;
489 break;
490 case OPTION_WARN_ONCE:
491 config.warn_once = true;
492 break;
493 case OPTION_WHOLE_ARCHIVE:
494 whole_archive = true;
495 break;
496 case 'X':
497 link_info.discard = discard_l;
498 break;
499 case 'x':
500 link_info.discard = discard_all;
501 break;
502 case 'Y':
503 if (strncmp (optarg, "P,", 2) == 0)
504 optarg += 2;
505 default_dirlist = optarg;
506 break;
507 case 'y':
508 add_ysym (optarg);
509 break;
510 case 'z':
511 /* We accept and ignore this option for Solaris
512 compatibility. Actually, on Solaris, optarg is not
513 ignored. Someday we should handle it correctly. FIXME. */
514 break;
515 case OPTION_SPLIT_BY_RELOC:
516 config.split_by_reloc = atoi (optarg);
517 break;
518 case OPTION_SPLIT_BY_FILE:
519 config.split_by_file = true;
520 break;
521 case '(':
522 if (ingroup)
523 {
524 fprintf (stderr,
525 "%s: may not nest groups (--help for usage)\n",
526 program_name);
527 xexit (1);
528 }
529 lang_enter_group ();
530 ingroup = 1;
531 break;
532 case ')':
533 if (! ingroup)
534 {
535 fprintf (stderr,
536 "%s: group ended before it began (--help for usage)\n",
537 program_name);
538 xexit (1);
539 }
540 lang_leave_group ();
541 ingroup = 0;
542 break;
543
544 }
545 }
546
547 if (ingroup)
548 lang_leave_group ();
549
550 if (default_dirlist != NULL)
551 set_default_dirlist (default_dirlist);
552
553 }
554
555 /* Add the (colon-separated) elements of DIRLIST_PTR to the
556 library search path. */
557
558 static void
559 set_default_dirlist (dirlist_ptr)
560 char *dirlist_ptr;
561 {
562 char *p;
563
564 while (1)
565 {
566 p = strchr (dirlist_ptr, ':');
567 if (p != NULL)
568 *p = 0;
569 if (*dirlist_ptr)
570 ldfile_add_library_path (dirlist_ptr, true);
571 if (p == NULL)
572 break;
573 *p = ':';
574 dirlist_ptr = p + 1;
575 }
576 }
577
578 static void
579 set_section_start (sect, valstr)
580 char *sect, *valstr;
581 {
582 char *end;
583 unsigned long val = strtoul (valstr, &end, 16);
584 if (*end)
585 einfo ("%P%F: invalid hex number `%s'\n", valstr);
586 lang_section_start (sect, exp_intop (val));
587 }
588 \f
This page took 0.050524 seconds and 5 git commands to generate.