* lexsup.c (ld_options): New static array.
[external/binutils.git] / ld / lexsup.c
1 /* Parse options for the GNU linker.
2    Copyright (C) 1991, 92, 93, 94, 95, 1996 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 static void help PARAMS ((void));
50
51 /* Non-zero if we are processing a --defsym from the command line.  */
52 int parsing_defsym = 0;
53
54 /* Codes used for the long options with no short synonyms.  150 isn't
55    special; it's just an arbitrary non-ASCII char value.  */
56
57 #define OPTION_ASSERT                   150
58 #define OPTION_CALL_SHARED              (OPTION_ASSERT + 1)
59 #define OPTION_DEFSYM                   (OPTION_CALL_SHARED + 1)
60 #define OPTION_DYNAMIC_LINKER           (OPTION_DEFSYM + 1)
61 #define OPTION_EB                       (OPTION_DYNAMIC_LINKER + 1)
62 #define OPTION_EL                       (OPTION_EB + 1)
63 #define OPTION_EMBEDDED_RELOCS          (OPTION_EL + 1)
64 #define OPTION_EXPORT_DYNAMIC           (OPTION_EMBEDDED_RELOCS + 1)
65 #define OPTION_HELP                     (OPTION_EXPORT_DYNAMIC + 1)
66 #define OPTION_IGNORE                   (OPTION_HELP + 1)
67 #define OPTION_MAP                      (OPTION_IGNORE + 1)
68 #define OPTION_NO_KEEP_MEMORY           (OPTION_MAP + 1)
69 #define OPTION_NOINHIBIT_EXEC           (OPTION_NO_KEEP_MEMORY + 1)
70 #define OPTION_NON_SHARED               (OPTION_NOINHIBIT_EXEC + 1)
71 #define OPTION_NO_WHOLE_ARCHIVE         (OPTION_NON_SHARED + 1)
72 #define OPTION_OFORMAT                  (OPTION_NO_WHOLE_ARCHIVE + 1)
73 #define OPTION_RELAX                    (OPTION_OFORMAT + 1)
74 #define OPTION_RETAIN_SYMBOLS_FILE      (OPTION_RELAX + 1)
75 #define OPTION_RPATH                    (OPTION_RETAIN_SYMBOLS_FILE + 1)
76 #define OPTION_RPATH_LINK               (OPTION_RPATH + 1)
77 #define OPTION_SHARED                   (OPTION_RPATH_LINK + 1)
78 #define OPTION_SONAME                   (OPTION_SHARED + 1)
79 #define OPTION_SORT_COMMON              (OPTION_SONAME + 1)
80 #define OPTION_STATS                    (OPTION_SORT_COMMON + 1)
81 #define OPTION_SYMBOLIC                 (OPTION_STATS + 1)
82 #define OPTION_TBSS                     (OPTION_SYMBOLIC + 1)
83 #define OPTION_TDATA                    (OPTION_TBSS + 1)
84 #define OPTION_TTEXT                    (OPTION_TDATA + 1)
85 #define OPTION_TRADITIONAL_FORMAT       (OPTION_TTEXT + 1)
86 #define OPTION_UR                       (OPTION_TRADITIONAL_FORMAT + 1)
87 #define OPTION_VERBOSE                  (OPTION_UR + 1)
88 #define OPTION_VERSION                  (OPTION_VERBOSE + 1)
89 #define OPTION_WARN_COMMON              (OPTION_VERSION + 1)
90 #define OPTION_WARN_CONSTRUCTORS        (OPTION_WARN_COMMON + 1)
91 #define OPTION_WARN_MULTIPLE_GP         (OPTION_WARN_CONSTRUCTORS + 1)
92 #define OPTION_WARN_ONCE                (OPTION_WARN_MULTIPLE_GP + 1)
93 #define OPTION_SPLIT_BY_RELOC           (OPTION_WARN_ONCE + 1)
94 #define OPTION_SPLIT_BY_FILE            (OPTION_SPLIT_BY_RELOC + 1)
95 #define OPTION_WHOLE_ARCHIVE            (OPTION_SPLIT_BY_FILE + 1)
96 #define OPTION_WRAP                     (OPTION_WHOLE_ARCHIVE + 1)
97
98 /* The long options.  This structure is used for both the option
99    parsing and the help text.  */
100
101 struct ld_option
102 {
103   /* The long option information.  */
104   struct option opt;
105   /* The short option with the same meaning ('\0' if none).  */
106   char shortopt;
107   /* The name of the argument (NULL if none).  */
108   const char *arg;
109   /* The documentation string.  If this is NULL, this is a synonym for
110      the previous option.  */
111   const char *doc;
112   enum
113     {
114       /* Use one dash before long option name.  */
115       ONE_DASH,
116       /* Use two dashes before long option name.  */
117       TWO_DASHES,
118       /* Don't mention this option in --help output.  */
119       NO_HELP
120     } control;
121 };
122
123 static const struct ld_option ld_options[] =
124 {
125   { {NULL, required_argument, NULL, '\0'},
126       'a', "KEYWORD", "Shared library control for HP/UX compatibility",
127       ONE_DASH },
128   { {"architecture", required_argument, NULL, 'A'},
129       'A', "ARCH", "Set architecture" , TWO_DASHES },
130   { {"format", required_argument, NULL, 'b'},
131       'b', "TARGET", "Specify target for following input files", TWO_DASHES },
132   { {"mri-script", required_argument, NULL, 'c'},
133       'c', "FILE", "Read MRI format linker script", TWO_DASHES },
134   { {"dc", no_argument, NULL, 'd'},
135       'd', NULL, "Force common symbols to be defined", ONE_DASH },
136   { {"dp", no_argument, NULL, 'd'},
137       '\0', NULL, NULL, ONE_DASH },
138   { {"entry", required_argument, NULL, 'e'},
139       'e', "ADDRESS", "Set start address", TWO_DASHES },
140   { {"export-dynamic", no_argument, NULL, OPTION_EXPORT_DYNAMIC},
141       'E', NULL, "Export all dynamic symbols", TWO_DASHES },
142   { {NULL, no_argument, NULL, '\0'},
143       'F', NULL, "Ignored", ONE_DASH },
144   { {"gpsize", required_argument, NULL, 'G'},
145       'G', "SIZE", "Small data size (if no size, same as --shared)",
146       TWO_DASHES },
147   { {NULL, no_argument, NULL, '\0'},
148       'g', NULL, "Ignored", ONE_DASH },
149   { {"soname", required_argument, NULL, OPTION_SONAME},
150       'h', "FILENAME", "Set internal name of shared library", ONE_DASH },
151   { {"library", required_argument, NULL, 'l'},
152       'l', "LIBNAME", "Search for library LIBNAME", TWO_DASHES },
153   { {"library-path", required_argument, NULL, 'L'},
154       'L', "DIRECTORY", "Add DIRECTORY to library search path", TWO_DASHES },
155   { {NULL, required_argument, NULL, '\0'},
156       'm', "EMULATION", "Set emulation", ONE_DASH },
157   { {"print-map", no_argument, NULL, 'M'},
158       'M', NULL, "Print map file on standard output", TWO_DASHES },
159   { {"nmagic", no_argument, NULL, 'n'},
160       'n', NULL, "Do not page align data", TWO_DASHES },
161   { {"omagic", no_argument, NULL, 'N'},
162       'N', NULL, "Do not page align data, do not make text readonly",
163       TWO_DASHES },
164   { {"output", required_argument, NULL, 'o'},
165       'o', "FILE", "Set output file name", TWO_DASHES },
166   { {NULL, required_argument, NULL, '\0'},
167       'O', NULL, "Ignored", ONE_DASH },
168   { {"relocateable", no_argument, NULL, 'r'},
169       'r', NULL, "Generate relocateable output", TWO_DASHES },
170   { {NULL, no_argument, NULL, '\0'},
171       'i', NULL, NULL, ONE_DASH },
172   { {"just-symbols", required_argument, NULL, 'R'},
173       'R', "FILE", "Just link symbols (if directory, same as --rpath)",
174       TWO_DASHES },
175   { {"strip-all", no_argument, NULL, 's'},
176       's', NULL, "Strip all symbols", TWO_DASHES },
177   { {"strip-debug", no_argument, NULL, 'S'},
178       'S', NULL, "Strip debugging symbols", TWO_DASHES },
179   { {"trace", no_argument, NULL, 't'},
180       't', NULL, "Trace file opens", TWO_DASHES },
181   { {"script", required_argument, NULL, 'T'},
182       'T', "FILE", "Read linker script", TWO_DASHES },
183   { {"undefined", required_argument, NULL, 'u'},
184       'u', "SYMBOL", "Start with undefined reference to SYMBOL", TWO_DASHES },
185   { {"version", no_argument, NULL, OPTION_VERSION},
186       'v', NULL, "Print version information", TWO_DASHES },
187   { {NULL, no_argument, NULL, '\0'},
188       'V', NULL, "Print version and emulation information", ONE_DASH },
189   { {"discard-all", no_argument, NULL, 'x'},
190       'x', NULL, "Discard all local symbols", TWO_DASHES },
191   { {"discard-locals", no_argument, NULL, 'X'},
192       'X', NULL, "Discard temporary local symbols", TWO_DASHES },
193   { {"trace-symbol", required_argument, NULL, 'y'},
194       'y', "SYMBOL", "Trace mentions of SYMBOL", TWO_DASHES },
195   { {NULL, required_argument, NULL, '\0'},
196       'Y', "PATH", "Default search path for Solaris compatibility", ONE_DASH },
197   { {NULL, required_argument, NULL, '\0'},
198       'z', "KEYWORD", "Ignored for Solaris compatibility", ONE_DASH },
199   { {"start-group", no_argument, NULL, '('},
200       '(', NULL, "Start a group", TWO_DASHES },
201   { {"end-group", no_argument, NULL, ')'},
202       ')', NULL, "End a group", TWO_DASHES },
203   { {"assert", required_argument, NULL, OPTION_ASSERT},
204       '\0', "KEYWORD", "Ignored for SunOS compatibility", ONE_DASH },
205   { {"Bdynamic", no_argument, NULL, OPTION_CALL_SHARED},
206       '\0', NULL, "Link against shared libraries", ONE_DASH },
207   { {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
208       '\0', NULL, NULL, ONE_DASH },
209   { {"dy", no_argument, NULL, OPTION_CALL_SHARED},
210       '\0', NULL, NULL, ONE_DASH },
211   { {"Bstatic", no_argument, NULL, OPTION_NON_SHARED},
212       '\0', NULL, "Do not link against shared libraries", ONE_DASH },
213   { {"dn", no_argument, NULL, OPTION_NON_SHARED},
214       '\0', NULL, NULL, ONE_DASH },
215   { {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
216       '\0', NULL, NULL, ONE_DASH },
217   { {"static", no_argument, NULL, OPTION_NON_SHARED},
218       '\0', NULL, NULL, ONE_DASH },
219   { {"Bsymbolic", no_argument, NULL, OPTION_SYMBOLIC},
220       '\0', NULL, "Bind global references locally", ONE_DASH },
221   { {"defsym", required_argument, NULL, OPTION_DEFSYM},
222       '\0', "SYMBOL=EXPRESSION", "Define a symbol", TWO_DASHES },
223   { {"dynamic-linker", required_argument, NULL, OPTION_DYNAMIC_LINKER},
224       '\0', "PROGRAM", "Set the dynamic linker to use", TWO_DASHES },
225   { {"EB", no_argument, NULL, OPTION_EB},
226       '\0', NULL, "Link big-endian objects", ONE_DASH },
227   { {"EL", no_argument, NULL, OPTION_EL},
228       '\0', NULL, "Link little-endian objects", ONE_DASH },
229   { {"embedded-relocs", no_argument, NULL, OPTION_EMBEDDED_RELOCS},
230       '\0', NULL, "Generate embedded relocs", TWO_DASHES},
231   { {"help", no_argument, NULL, OPTION_HELP},
232       '\0', NULL, "Print option help", TWO_DASHES },
233   { {"Map", required_argument, NULL, OPTION_MAP},
234       '\0', "FILE", "Write a map file", ONE_DASH },
235   { {"no-keep-memory", no_argument, NULL, OPTION_NO_KEEP_MEMORY},
236       '\0', NULL, "Use less memory and more disk I/O", TWO_DASHES },
237   { {"no-whole-archive", no_argument, NULL, OPTION_NO_WHOLE_ARCHIVE},
238       '\0', NULL, "Turn off --whole-archive", TWO_DASHES },
239   { {"noinhibit-exec", no_argument, NULL, OPTION_NOINHIBIT_EXEC},
240       '\0', NULL, "Create an output file even if errors occur", TWO_DASHES },
241   { {"noinhibit_exec", no_argument, NULL, OPTION_NOINHIBIT_EXEC},
242       '\0', NULL, NULL, NO_HELP },
243   { {"oformat", required_argument, NULL, OPTION_OFORMAT},
244       '\0', "TARGET", "Specify target of output file", TWO_DASHES },
245   { {"qmagic", no_argument, NULL, OPTION_IGNORE},
246       '\0', NULL, "Ignored for Linux compatibility", ONE_DASH },
247   { {"Qy", no_argument, NULL, OPTION_IGNORE},
248       '\0', NULL, "Ignored for SVR4 compatibility", ONE_DASH },
249   { {"relax", no_argument, NULL, OPTION_RELAX},
250       '\0', NULL, "Relax branches on certain targets", TWO_DASHES },
251   { {"retain-symbols-file", required_argument, NULL,
252        OPTION_RETAIN_SYMBOLS_FILE},
253       '\0', "FILE", "Keep only symbols listed in FILE", TWO_DASHES },
254   { {"rpath", required_argument, NULL, OPTION_RPATH},
255       '\0', "PATH", "Set runtime shared library search path", ONE_DASH },
256   { {"rpath-link", required_argument, NULL, OPTION_RPATH_LINK},
257       '\0', "PATH", "Set line time shared library search path", ONE_DASH },
258   { {"shared", no_argument, NULL, OPTION_SHARED},
259       '\0', NULL, "Create a shared library", ONE_DASH },
260   { {"Bshareable", no_argument, NULL, OPTION_SHARED }, /* FreeBSD.  */
261       '\0', NULL, NULL, ONE_DASH },
262   { {"sort-common", no_argument, NULL, OPTION_SORT_COMMON},
263       '\0', NULL, "Sort common symbols by size", TWO_DASHES },
264   { {"sort_common", no_argument, NULL, OPTION_SORT_COMMON},
265       '\0', NULL, NULL, NO_HELP },
266   { {"split-by-file", no_argument, NULL, OPTION_SPLIT_BY_FILE},
267       '\0', NULL, "Split output sections for each file", TWO_DASHES },
268   { {"split-by-reloc", required_argument, NULL, OPTION_SPLIT_BY_RELOC},
269       '\0', "COUNT", "Split output sections every COUNT relocs", TWO_DASHES },
270   { {"stats", no_argument, NULL, OPTION_STATS},
271       '\0', NULL, "Print memory usage statistics", TWO_DASHES },
272   { {"traditional-format", no_argument, NULL, OPTION_TRADITIONAL_FORMAT},
273       '\0', NULL, "Use same format as native linker", TWO_DASHES },
274   { {"Tbss", required_argument, NULL, OPTION_TBSS},
275       '\0', "ADDRESS", "Set address of .bss section", ONE_DASH },
276   { {"Tdata", required_argument, NULL, OPTION_TDATA},
277       '\0', "ADDRESS", "Set address of .data section", ONE_DASH },
278   { {"Ttext", required_argument, NULL, OPTION_TTEXT},
279       '\0', "ADDRESS", "Set address of .text section", ONE_DASH },
280   { {"Ur", no_argument, NULL, OPTION_UR},
281       '\0', NULL, "Build global constructor/destructor tables", ONE_DASH },
282   { {"verbose", no_argument, NULL, OPTION_VERBOSE},
283       '\0', NULL, "Output lots of information during link", TWO_DASHES },
284   { {"dll-verbose", no_argument, NULL, OPTION_VERSION}, /* Linux.  */
285       '\0', NULL, NULL, NO_HELP },
286   { {"warn-common", no_argument, NULL, OPTION_WARN_COMMON},
287       '\0', NULL, "Warn about duplicate common symbols", TWO_DASHES },
288   { {"warn-constructors", no_argument, NULL, OPTION_WARN_CONSTRUCTORS},
289       '\0', NULL, "Warn if global constructors/destructors are seen",
290       TWO_DASHES },
291   { {"warn-multiple-gp", no_argument, NULL, OPTION_WARN_MULTIPLE_GP},
292       '\0', NULL, "Warn if the multiple GP values are used", TWO_DASHES },
293   { {"warn-once", no_argument, NULL, OPTION_WARN_ONCE},
294       '\0', NULL, "Warn only once per undefined symbol", TWO_DASHES },
295   { {"whole-archive", no_argument, NULL, OPTION_WHOLE_ARCHIVE},
296       '\0', NULL, "Include all objects from following archives", TWO_DASHES },
297   { {"wrap", required_argument, NULL, OPTION_WRAP},
298       '\0', "SYMBOL", "Use wrapper functions for SYMBOL", TWO_DASHES }
299 };
300
301 #define OPTION_COUNT (sizeof ld_options / sizeof ld_options[0])
302
303 void
304 parse_args (argc, argv)
305      int argc;
306      char **argv;
307 {
308   int i, is, il;
309   int ingroup = 0;
310   char *default_dirlist = NULL;
311   char shortopts[OPTION_COUNT * 2 + 2];
312   struct option longopts[OPTION_COUNT + 1];
313
314   /* Starting the short option string with '-' is for programs that
315      expect options and other ARGV-elements in any order and that care about
316      the ordering of the two.  We describe each non-option ARGV-element
317      as if it were the argument of an option with character code 1.  */
318   shortopts[0] = '-';
319   is = 1;
320   il = 0;
321   for (i = 0; i < OPTION_COUNT; i++)
322     {
323       if (ld_options[i].shortopt != '\0')
324         {
325           shortopts[is] = ld_options[i].shortopt;
326           ++is;
327           if (ld_options[i].opt.has_arg == required_argument)
328             {
329               shortopts[is] = ':';
330               ++is;
331             }
332         }
333       if (ld_options[i].opt.name != NULL)
334         {
335           longopts[il] = ld_options[i].opt;
336           ++il;
337         }
338     }
339   shortopts[is] = '\0';
340   longopts[il].name = NULL;
341
342   /* The -G option is ambiguous on different platforms.  Sometimes it
343      specifies the largest data size to put into the small data
344      section.  Sometimes it is equivalent to --shared.  Unfortunately,
345      the first form takes an argument, while the second does not.
346
347      We need to permit the --shared form because on some platforms,
348      such as Solaris, gcc -shared will pass -G to the linker.
349
350      To permit either usage, we look through the argument list.  If we
351      find -G not followed by a number, we change it into --shared.
352      This will work for most normal cases.  */
353   for (i = 1; i < argc; i++)
354     if (strcmp (argv[i], "-G") == 0
355         && (i + 1 >= argc
356             || ! isdigit (argv[i + 1][0])))
357       argv[i] = (char *) "--shared";
358
359   while (1)
360     {
361       /* getopt_long_only is like getopt_long, but '-' as well as '--' can
362          indicate a long option.  */
363       int longind;
364       int optc;
365
366       if (ldemul_parse_args (argc, argv))
367         continue;
368
369       optc = getopt_long_only (argc, argv, shortopts, longopts, &longind);
370
371       if (optc == -1)
372         break;
373       switch (optc)
374         {
375         default:
376           xexit (1);
377         case 1:                 /* File name.  */
378           lang_add_input_file (optarg, lang_input_file_is_file_enum,
379                                (char *) NULL);
380           break;
381
382         case OPTION_IGNORE:
383           break;
384         case 'a':
385           /* For HP/UX compatibility.  Actually -a shared should mean
386              ``use only shared libraries'' but, then, we don't
387              currently support shared libraries on HP/UX anyhow.  */
388           if (strcmp (optarg, "archive") == 0)
389             config.dynamic_link = false;
390           else if (strcmp (optarg, "shared") == 0
391                    || strcmp (optarg, "default") == 0)
392             config.dynamic_link = true;
393           else
394             einfo ("%P%F: unrecognized -a option `%s'\n", optarg);
395           break;
396         case OPTION_ASSERT:
397           /* FIXME: We just ignore these, but we should handle them.  */
398           if (strcmp (optarg, "definitions") == 0)
399             ;
400           else if (strcmp (optarg, "nodefinitions") == 0)
401             ;
402           else if (strcmp (optarg, "nosymbolic") == 0)
403             ;
404           else if (strcmp (optarg, "pure-text") == 0)
405             ;
406           else
407             einfo ("%P%F: unrecognized -assert option `%s'\n", optarg);
408           break;
409         case 'A':
410           ldfile_add_arch (optarg);
411           break;
412         case 'b':
413           lang_add_target (optarg);
414           break;
415         case 'c':
416           ldfile_open_command_file (optarg);
417           parser_input = input_mri_script;
418           yyparse ();
419           break;
420         case OPTION_CALL_SHARED:
421           config.dynamic_link = true;
422           break;
423         case OPTION_NON_SHARED:
424           config.dynamic_link = false;
425           break;
426         case 'd':
427           command_line.force_common_definition = true;
428           break;
429         case OPTION_DEFSYM:
430           lex_string = optarg;
431           lex_redirect (optarg);
432           parser_input = input_defsym;
433           parsing_defsym = 1;
434           yyparse ();
435           parsing_defsym = 0;
436           lex_string = NULL;
437           break;
438         case OPTION_DYNAMIC_LINKER:
439           command_line.interpreter = optarg;
440           break;
441         case OPTION_EB:
442           command_line.endian = ENDIAN_BIG;
443           break;
444         case OPTION_EL:
445           command_line.endian = ENDIAN_LITTLE;
446           break;
447         case OPTION_EMBEDDED_RELOCS:
448           command_line.embedded_relocs = true;
449           break;
450         case OPTION_EXPORT_DYNAMIC:
451         case 'E': /* HP/UX compatibility.  */
452           command_line.export_dynamic = true;
453           break;
454         case 'e':
455           lang_add_entry (optarg, true);
456           break;
457         case 'F':
458           /* Ignore.  */
459           break;
460         case 'G':
461           {
462             char *end;
463             g_switch_value = strtoul (optarg, &end, 0);
464             if (*end)
465               einfo ("%P%F: invalid number `%s'\n", optarg);
466           }
467           break;
468         case 'g':
469           /* Ignore.  */
470           break;
471         case OPTION_HELP:
472           help ();
473           xexit (0);
474           break;
475         case 'L':
476           ldfile_add_library_path (optarg, true);
477           break;
478         case 'l':
479           lang_add_input_file (optarg, lang_input_file_is_l_enum,
480                                (char *) NULL);
481           break;
482         case 'M':
483           config.map_filename = "-";
484           break;
485         case 'm':
486           /* Ignore.  Was handled in a pre-parse.   */
487           break;
488         case OPTION_MAP:
489           config.map_filename = optarg;
490           break;
491         case 'N':
492           config.text_read_only = false;
493           config.magic_demand_paged = false;
494           config.dynamic_link = false;
495           break;
496         case 'n':
497           config.magic_demand_paged = false;
498           config.dynamic_link = false;
499           break;
500         case OPTION_NO_KEEP_MEMORY:
501           link_info.keep_memory = false;
502           break;
503         case OPTION_NOINHIBIT_EXEC:
504           force_make_executable = true;
505           break;
506         case OPTION_NO_WHOLE_ARCHIVE:
507           whole_archive = false;
508           break;
509         case 'O':
510           /* FIXME "-O<non-digits> <value>" used to set the address of
511              section <non-digits>.  Was this for compatibility with
512              something, or can we create a new option to do that
513              (with a syntax similar to -defsym)?
514              getopt can't handle two args to an option without kludges.  */
515           break;
516         case 'o':
517           lang_add_output (optarg, 0); 
518           break;
519         case OPTION_OFORMAT:
520           lang_add_output_format (optarg, (char *) NULL, (char *) NULL, 0);
521           break;
522         case 'i':
523         case 'r':
524           link_info.relocateable = true;
525           config.build_constructors = false;
526           config.magic_demand_paged = false;
527           config.text_read_only = false;
528           config.dynamic_link = false;
529           break;
530         case 'R':
531           /* The GNU linker traditionally uses -R to mean to include
532              only the symbols from a file.  The Solaris linker uses -R
533              to set the path used by the runtime linker to find
534              libraries.  This is the GNU linker -rpath argument.  We
535              try to support both simultaneously by checking the file
536              named.  If it is a directory, rather than a regular file,
537              we assume -rpath was meant.  */
538           {
539             struct stat s;
540
541             if (stat (optarg, &s) >= 0
542                 && ! S_ISDIR (s.st_mode))
543               {
544                 lang_add_input_file (optarg,
545                                      lang_input_file_is_symbols_only_enum,
546                                      (char *) NULL);
547                 break;
548               }
549           }
550           /* Fall through.  */
551         case OPTION_RPATH:
552           if (command_line.rpath == NULL)
553             command_line.rpath = buystring (optarg);
554           else
555             {
556               char *buf;
557
558               buf = xmalloc (strlen (command_line.rpath)
559                              + strlen (optarg)
560                              + 2);
561               sprintf (buf, "%s:%s", command_line.rpath, optarg);
562               free (command_line.rpath);
563               command_line.rpath = buf;
564             }
565           break;
566         case OPTION_RPATH_LINK:
567           if (command_line.rpath_link == NULL)
568             command_line.rpath_link = buystring (optarg);
569           else
570             {
571               char *buf;
572
573               buf = xmalloc (strlen (command_line.rpath_link)
574                              + strlen (optarg)
575                              + 2);
576               sprintf (buf, "%s:%s", command_line.rpath_link, optarg);
577               free (command_line.rpath_link);
578               command_line.rpath_link = buf;
579             }
580           break;
581         case OPTION_RELAX:
582           command_line.relax = true;
583           break;
584         case OPTION_RETAIN_SYMBOLS_FILE:
585           add_keepsyms_file (optarg);
586           break;
587         case 'S':
588           link_info.strip = strip_debugger;
589           break;
590         case 's':
591           link_info.strip = strip_all;
592           break;
593         case OPTION_SHARED:
594           link_info.shared = true;
595           break;
596         case 'h':               /* Used on Solaris.  */
597         case OPTION_SONAME:
598           command_line.soname = optarg;
599           break;
600         case OPTION_SORT_COMMON:
601           config.sort_common = true;
602           break;
603         case OPTION_STATS:
604           config.stats = true;
605           break;
606         case OPTION_SYMBOLIC:
607           link_info.symbolic = true;
608           break;
609         case 't':
610           trace_files = true;
611           break;
612         case 'T':
613           ldfile_open_command_file (optarg);
614           parser_input = input_script;
615           yyparse ();
616           break;
617         case OPTION_TBSS:
618           set_section_start (".bss", optarg);
619           break;
620         case OPTION_TDATA:
621           set_section_start (".data", optarg);
622           break;
623         case OPTION_TTEXT:
624           set_section_start (".text", optarg);
625           break;
626         case OPTION_TRADITIONAL_FORMAT:
627           config.traditional_format = true;
628           break;
629         case OPTION_UR:
630           link_info.relocateable = true;
631           config.build_constructors = true;
632           config.magic_demand_paged = false;
633           config.text_read_only = false;
634           config.dynamic_link = false;
635           break;
636         case 'u':
637           ldlang_add_undef (optarg);
638           break;
639         case OPTION_VERBOSE:
640           ldversion (1);
641           version_printed = true;
642           trace_file_tries = true;
643           break;
644         case 'v':
645           ldversion (0);
646           version_printed = true;
647           break;
648         case 'V':
649           ldversion (1);
650           version_printed = true;
651           break;
652         case OPTION_VERSION:
653           ldversion (0);
654           version_printed = true;
655           break;
656         case OPTION_WARN_COMMON:
657           config.warn_common = true;
658           break;
659         case OPTION_WARN_CONSTRUCTORS:
660           config.warn_constructors = true;
661           break;
662         case OPTION_WARN_MULTIPLE_GP:
663           config.warn_multiple_gp = true;
664           break;
665         case OPTION_WARN_ONCE:
666           config.warn_once = true;
667           break;
668         case OPTION_WHOLE_ARCHIVE:
669           whole_archive = true;
670           break;
671         case OPTION_WRAP:
672           add_wrap (optarg);
673           break;
674         case 'X':
675           link_info.discard = discard_l;
676           break;
677         case 'x':
678           link_info.discard = discard_all;
679           break;
680         case 'Y':
681           if (strncmp (optarg, "P,", 2) == 0)
682             optarg += 2;
683           default_dirlist = optarg;
684           break;
685         case 'y':
686           add_ysym (optarg);
687           break;
688         case 'z':
689           /* We accept and ignore this option for Solaris
690              compatibility.  Actually, on Solaris, optarg is not
691              ignored.  Someday we should handle it correctly.  FIXME.  */
692           break;
693         case OPTION_SPLIT_BY_RELOC:
694           config.split_by_reloc = atoi (optarg);
695           break; 
696         case OPTION_SPLIT_BY_FILE:
697           config.split_by_file = true;
698           break; 
699         case '(':
700           if (ingroup)
701             {
702               fprintf (stderr,
703                        "%s: may not nest groups (--help for usage)\n",
704                        program_name);
705               xexit (1);
706             }
707           lang_enter_group ();
708           ingroup = 1;
709           break;
710         case ')':
711           if (! ingroup)
712             {
713               fprintf (stderr,
714                        "%s: group ended before it began (--help for usage)\n",
715                        program_name);
716               xexit (1);
717             }
718           lang_leave_group ();
719           ingroup = 0;
720           break;
721
722         }
723     }
724
725   if (ingroup)
726     lang_leave_group ();
727
728   if (default_dirlist != NULL)
729     set_default_dirlist (default_dirlist);
730
731 }
732
733 /* Add the (colon-separated) elements of DIRLIST_PTR to the
734    library search path.  */
735
736 static void
737 set_default_dirlist (dirlist_ptr)
738      char *dirlist_ptr;
739 {
740   char *p;
741
742   while (1)
743     {
744       p = strchr (dirlist_ptr, ':');
745       if (p != NULL)
746         *p = 0;
747       if (*dirlist_ptr)
748         ldfile_add_library_path (dirlist_ptr, true);
749       if (p == NULL)
750         break;
751       *p = ':';
752       dirlist_ptr = p + 1;
753     }
754 }
755
756 static void
757 set_section_start (sect, valstr)
758      char *sect, *valstr;
759 {
760   char *end;
761   unsigned long val = strtoul (valstr, &end, 16);
762   if (*end)
763     einfo ("%P%F: invalid hex number `%s'\n", valstr);
764   lang_section_start (sect, exp_intop (val));
765 }
766 \f
767 /* Print help messages for the options.  */
768
769 static void
770 help ()
771 {
772   int i;
773   const char **targets, **pp;
774
775   printf ("Usage: %s [options] file...\n", program_name);
776
777   printf ("Options:\n");
778   for (i = 0; i < OPTION_COUNT; i++)
779     {
780       if (ld_options[i].doc != NULL)
781         {
782           boolean comma;
783           int len;
784           int j;
785
786           printf ("  ");
787
788           comma = false;
789           len = 2;
790
791           j = i;
792           do
793             {
794               if (ld_options[j].shortopt != '\0'
795                   && ld_options[j].control != NO_HELP)
796                 {
797                   printf ("%s-%c", comma ? ", " : "", ld_options[j].shortopt);
798                   len += (comma ? 2 : 0) + 2;
799                   if (ld_options[j].arg != NULL)
800                     {
801                       printf (" %s", ld_options[j].arg);
802                       len += 1 + strlen (ld_options[j].arg);
803                     }
804                   comma = true;
805                 }
806               ++j;
807             }
808           while (j < OPTION_COUNT && ld_options[j].doc == NULL);
809
810           j = i;
811           do
812             {
813               if (ld_options[j].opt.name != NULL
814                   && ld_options[j].control != NO_HELP)
815                 {
816                   printf ("%s-%s%s",
817                           comma ? ", " : "",
818                           ld_options[j].control == TWO_DASHES ? "-" : "",
819                           ld_options[j].opt.name);
820                   len += ((comma ? 2 : 0)
821                           + 1
822                           + (ld_options[j].control == TWO_DASHES ? 1 : 0)
823                           + strlen (ld_options[j].opt.name));
824                   if (ld_options[j].arg != NULL)
825                     {
826                       printf (" %s", ld_options[j].arg);
827                       len += 1 + strlen (ld_options[j].arg);
828                     }
829                   comma = true;
830                 }
831               ++j;
832             }
833           while (j < OPTION_COUNT && ld_options[j].doc == NULL);
834
835           if (len >= 30)
836             {
837               printf ("\n");
838               len = 0;
839             }
840
841           for (; len < 30; len++)
842             putchar (' ');
843
844           printf ("%s\n", ld_options[i].doc);
845         }
846     }
847
848   printf ("%s: supported targets:", program_name);
849   targets = bfd_target_list ();
850   for (pp = targets; *pp != NULL; pp++)
851     printf (" %s", *pp);
852   free (targets);
853   printf ("\n");
854
855   printf ("%s: supported emulations: ", program_name);
856   ldemul_list_emulations (stdout);
857   printf ("\n");
858 }