* lexsup.c (parse_args): Change -l options into --library options
[external/binutils.git] / ld / lexsup.c
1 /* Parse options for the GNU linker.
2    Copyright (C) 1991, 92, 93, 94, 95, 96, 97, 1998
3    Free Software Foundation, Inc.
4
5 This file is part of GLD, the Gnu Linker.
6
7 GLD is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GLD is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GLD; see the file COPYING.  If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.  */
21
22 #include "bfd.h"
23 #include "sysdep.h"
24 #include "libiberty.h"
25 #include <stdio.h>
26 #include <string.h>
27 #include <ctype.h>
28 #include "getopt.h"
29 #include "bfdlink.h"
30 #include "ld.h"
31 #include "ldmain.h"
32 #include "ldmisc.h"
33 #include "ldexp.h"
34 #include "ldlang.h"
35 #include "ldgram.h"
36 #include "ldlex.h"
37 #include "ldfile.h"
38 #include "ldver.h"
39 #include "ldemul.h"
40
41 #ifndef PATH_SEPARATOR
42 #if defined (__MSDOS__) || (defined (_WIN32) && ! defined (__CYGWIN32__))
43 #define PATH_SEPARATOR ';'
44 #else
45 #define PATH_SEPARATOR ':'
46 #endif
47 #endif
48
49 /* Somewhere above, sys/stat.h got included . . . . */
50 #if !defined(S_ISDIR) && defined(S_IFDIR)
51 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
52 #endif
53
54 /* Omit args to avoid the possibility of clashing with a system header
55    that might disagree about consts.  */
56 unsigned long strtoul ();
57
58 static void set_default_dirlist PARAMS ((char *dirlist_ptr));
59 static void set_section_start PARAMS ((char *sect, char *valstr));
60 static void help PARAMS ((void));
61
62 /* Non-zero if we are processing a --defsym from the command line.  */
63 int parsing_defsym = 0;
64
65 /* Codes used for the long options with no short synonyms.  150 isn't
66    special; it's just an arbitrary non-ASCII char value.  */
67
68 #define OPTION_ASSERT                   150
69 #define OPTION_CALL_SHARED              (OPTION_ASSERT + 1)
70 #define OPTION_CREF                     (OPTION_CALL_SHARED + 1)
71 #define OPTION_DEFSYM                   (OPTION_CREF + 1)
72 #define OPTION_DYNAMIC_LINKER           (OPTION_DEFSYM + 1)
73 #define OPTION_EB                       (OPTION_DYNAMIC_LINKER + 1)
74 #define OPTION_EL                       (OPTION_EB + 1)
75 #define OPTION_EMBEDDED_RELOCS          (OPTION_EL + 1)
76 #define OPTION_EXPORT_DYNAMIC           (OPTION_EMBEDDED_RELOCS + 1)
77 #define OPTION_HELP                     (OPTION_EXPORT_DYNAMIC + 1)
78 #define OPTION_IGNORE                   (OPTION_HELP + 1)
79 #define OPTION_MAP                      (OPTION_IGNORE + 1)
80 #define OPTION_NO_KEEP_MEMORY           (OPTION_MAP + 1)
81 #define OPTION_NO_WARN_MISMATCH         (OPTION_NO_KEEP_MEMORY + 1)
82 #define OPTION_NOINHIBIT_EXEC           (OPTION_NO_WARN_MISMATCH + 1)
83 #define OPTION_NON_SHARED               (OPTION_NOINHIBIT_EXEC + 1)
84 #define OPTION_NO_WHOLE_ARCHIVE         (OPTION_NON_SHARED + 1)
85 #define OPTION_OFORMAT                  (OPTION_NO_WHOLE_ARCHIVE + 1)
86 #define OPTION_RELAX                    (OPTION_OFORMAT + 1)
87 #define OPTION_RETAIN_SYMBOLS_FILE      (OPTION_RELAX + 1)
88 #define OPTION_RPATH                    (OPTION_RETAIN_SYMBOLS_FILE + 1)
89 #define OPTION_RPATH_LINK               (OPTION_RPATH + 1)
90 #define OPTION_SHARED                   (OPTION_RPATH_LINK + 1)
91 #define OPTION_SONAME                   (OPTION_SHARED + 1)
92 #define OPTION_SORT_COMMON              (OPTION_SONAME + 1)
93 #define OPTION_STATS                    (OPTION_SORT_COMMON + 1)
94 #define OPTION_SYMBOLIC                 (OPTION_STATS + 1)
95 #define OPTION_TASK_LINK                (OPTION_SYMBOLIC + 1)
96 #define OPTION_TBSS                     (OPTION_TASK_LINK + 1)
97 #define OPTION_TDATA                    (OPTION_TBSS + 1)
98 #define OPTION_TTEXT                    (OPTION_TDATA + 1)
99 #define OPTION_TRADITIONAL_FORMAT       (OPTION_TTEXT + 1)
100 #define OPTION_UR                       (OPTION_TRADITIONAL_FORMAT + 1)
101 #define OPTION_VERBOSE                  (OPTION_UR + 1)
102 #define OPTION_VERSION                  (OPTION_VERBOSE + 1)
103 #define OPTION_VERSION_SCRIPT           (OPTION_VERSION + 1)
104 #define OPTION_WARN_COMMON              (OPTION_VERSION_SCRIPT + 1)
105 #define OPTION_WARN_CONSTRUCTORS        (OPTION_WARN_COMMON + 1)
106 #define OPTION_WARN_MULTIPLE_GP         (OPTION_WARN_CONSTRUCTORS + 1)
107 #define OPTION_WARN_ONCE                (OPTION_WARN_MULTIPLE_GP + 1)
108 #define OPTION_WARN_SECTION_ALIGN       (OPTION_WARN_ONCE + 1)
109 #define OPTION_SPLIT_BY_RELOC           (OPTION_WARN_SECTION_ALIGN + 1)
110 #define OPTION_SPLIT_BY_FILE            (OPTION_SPLIT_BY_RELOC + 1)
111 #define OPTION_WHOLE_ARCHIVE            (OPTION_SPLIT_BY_FILE + 1)
112 #define OPTION_WRAP                     (OPTION_WHOLE_ARCHIVE + 1)
113 #define OPTION_FORCE_EXE_SUFFIX         (OPTION_WRAP + 1)
114
115 /* The long options.  This structure is used for both the option
116    parsing and the help text.  */
117
118 struct ld_option
119 {
120   /* The long option information.  */
121   struct option opt;
122   /* The short option with the same meaning ('\0' if none).  */
123   char shortopt;
124   /* The name of the argument (NULL if none).  */
125   const char *arg;
126   /* The documentation string.  If this is NULL, this is a synonym for
127      the previous option.  */
128   const char *doc;
129   enum
130     {
131       /* Use one dash before long option name.  */
132       ONE_DASH,
133       /* Use two dashes before long option name.  */
134       TWO_DASHES,
135       /* Don't mention this option in --help output.  */
136       NO_HELP
137     } control;
138 };
139
140 static const struct ld_option ld_options[] =
141 {
142   { {NULL, required_argument, NULL, '\0'},
143       'a', N_("KEYWORD"), N_("Shared library control for HP/UX compatibility"),
144       ONE_DASH },
145   { {"architecture", required_argument, NULL, 'A'},
146       'A', N_("ARCH"), N_("Set architecture") , TWO_DASHES },
147   { {"format", required_argument, NULL, 'b'},
148       'b', N_("TARGET"), N_("Specify target for following input files"), TWO_DASHES },
149   { {"mri-script", required_argument, NULL, 'c'},
150       'c', N_("FILE"), N_("Read MRI format linker script"), TWO_DASHES },
151   { {"dc", no_argument, NULL, 'd'},
152       'd', NULL, N_("Force common symbols to be defined"), ONE_DASH },
153   { {"dp", no_argument, NULL, 'd'},
154       '\0', NULL, NULL, ONE_DASH },
155   { {"entry", required_argument, NULL, 'e'},
156       'e', N_("ADDRESS"), N_("Set start address"), TWO_DASHES },
157   { {"export-dynamic", no_argument, NULL, OPTION_EXPORT_DYNAMIC},
158       'E', NULL, N_("Export all dynamic symbols"), TWO_DASHES },
159   { {"auxiliary", required_argument, NULL, 'f'},
160       'f', N_("SHLIB"), N_("Auxiliary filter for shared object symbol table"),
161       TWO_DASHES },
162   { {"filter", required_argument, NULL, 'F'},
163       'F', N_("SHLIB"), N_("Filter for shared object symbol table"), TWO_DASHES },
164   { {NULL, no_argument, NULL, '\0'},
165       'g', NULL, N_("Ignored"), ONE_DASH },
166   { {"gpsize", required_argument, NULL, 'G'},
167       'G', N_("SIZE"), N_("Small data size (if no size, same as --shared)"),
168       TWO_DASHES },
169   { {"soname", required_argument, NULL, OPTION_SONAME},
170       'h', N_("FILENAME"), N_("Set internal name of shared library"), ONE_DASH },
171   { {"library", required_argument, NULL, 'l'},
172       'l', N_("LIBNAME"), N_("Search for library LIBNAME"), TWO_DASHES },
173   { {"library-path", required_argument, NULL, 'L'},
174       'L', N_("DIRECTORY"), N_("Add DIRECTORY to library search path"), TWO_DASHES },
175   { {NULL, required_argument, NULL, '\0'},
176       'm', N_("EMULATION"), N_("Set emulation"), ONE_DASH },
177   { {"print-map", no_argument, NULL, 'M'},
178       'M', NULL, N_("Print map file on standard output"), TWO_DASHES },
179   { {"nmagic", no_argument, NULL, 'n'},
180       'n', NULL, N_("Do not page align data"), TWO_DASHES },
181   { {"omagic", no_argument, NULL, 'N'},
182       'N', NULL, N_("Do not page align data, do not make text readonly"),
183       TWO_DASHES },
184   { {"output", required_argument, NULL, 'o'},
185       'o', N_("FILE"), N_("Set output file name"), TWO_DASHES },
186   { {NULL, required_argument, NULL, '\0'},
187       'O', NULL, N_("Ignored"), ONE_DASH },
188   { {"relocateable", no_argument, NULL, 'r'},
189       'r', NULL, N_("Generate relocateable output"), TWO_DASHES },
190   { {NULL, no_argument, NULL, '\0'},
191       'i', NULL, NULL, ONE_DASH },
192   { {"just-symbols", required_argument, NULL, 'R'},
193       'R', N_("FILE"), N_("Just link symbols (if directory, same as --rpath)"),
194       TWO_DASHES },
195   { {"strip-all", no_argument, NULL, 's'},
196       's', NULL, N_("Strip all symbols"), TWO_DASHES },
197   { {"strip-debug", no_argument, NULL, 'S'},
198       'S', NULL, N_("Strip debugging symbols"), TWO_DASHES },
199   { {"trace", no_argument, NULL, 't'},
200       't', NULL, N_("Trace file opens"), TWO_DASHES },
201   { {"script", required_argument, NULL, 'T'},
202       'T', N_("FILE"), N_("Read linker script"), TWO_DASHES },
203   { {"undefined", required_argument, NULL, 'u'},
204       'u', N_("SYMBOL"), N_("Start with undefined reference to SYMBOL"), TWO_DASHES },
205   { {"version", no_argument, NULL, OPTION_VERSION},
206       'v', NULL, N_("Print version information"), TWO_DASHES },
207   { {NULL, no_argument, NULL, '\0'},
208       'V', NULL, N_("Print version and emulation information"), ONE_DASH },
209   { {"discard-all", no_argument, NULL, 'x'},
210       'x', NULL, N_("Discard all local symbols"), TWO_DASHES },
211   { {"discard-locals", no_argument, NULL, 'X'},
212       'X', NULL, N_("Discard temporary local symbols"), TWO_DASHES },
213   { {"trace-symbol", required_argument, NULL, 'y'},
214       'y', N_("SYMBOL"), N_("Trace mentions of SYMBOL"), TWO_DASHES },
215   { {NULL, required_argument, NULL, '\0'},
216       'Y', N_("PATH"), N_("Default search path for Solaris compatibility"), ONE_DASH },
217   { {NULL, required_argument, NULL, '\0'},
218       'z', N_("KEYWORD"), N_("Ignored for Solaris compatibility"), ONE_DASH },
219   { {"start-group", no_argument, NULL, '('},
220       '(', NULL, N_("Start a group"), TWO_DASHES },
221   { {"end-group", no_argument, NULL, ')'},
222       ')', NULL, N_("End a group"), TWO_DASHES },
223   { {"assert", required_argument, NULL, OPTION_ASSERT},
224       '\0', N_("KEYWORD"), N_("Ignored for SunOS compatibility"), ONE_DASH },
225   { {"Bdynamic", no_argument, NULL, OPTION_CALL_SHARED},
226       '\0', NULL, N_("Link against shared libraries"), ONE_DASH },
227   { {"dy", no_argument, NULL, OPTION_CALL_SHARED},
228       '\0', NULL, NULL, ONE_DASH },
229   { {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
230       '\0', NULL, NULL, ONE_DASH },
231   { {"Bstatic", no_argument, NULL, OPTION_NON_SHARED},
232       '\0', NULL, N_("Do not link against shared libraries"), ONE_DASH },
233   { {"dn", no_argument, NULL, OPTION_NON_SHARED},
234       '\0', NULL, NULL, ONE_DASH },
235   { {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
236       '\0', NULL, NULL, ONE_DASH },
237   { {"static", no_argument, NULL, OPTION_NON_SHARED},
238       '\0', NULL, NULL, ONE_DASH },
239   { {"Bsymbolic", no_argument, NULL, OPTION_SYMBOLIC},
240       '\0', NULL, N_("Bind global references locally"), ONE_DASH },
241   { {"cref", no_argument, NULL, OPTION_CREF},
242       '\0', NULL, N_("Output cross reference table"), TWO_DASHES },
243   { {"defsym", required_argument, NULL, OPTION_DEFSYM},
244       '\0', N_("SYMBOL=EXPRESSION"), N_("Define a symbol"), TWO_DASHES },
245   { {"dynamic-linker", required_argument, NULL, OPTION_DYNAMIC_LINKER},
246       '\0', N_("PROGRAM"), N_("Set the dynamic linker to use"), TWO_DASHES },
247   { {"EB", no_argument, NULL, OPTION_EB},
248       '\0', NULL, N_("Link big-endian objects"), ONE_DASH },
249   { {"EL", no_argument, NULL, OPTION_EL},
250       '\0', NULL, N_("Link little-endian objects"), ONE_DASH },
251   { {"embedded-relocs", no_argument, NULL, OPTION_EMBEDDED_RELOCS},
252       '\0', NULL, N_("Generate embedded relocs"), TWO_DASHES},
253   { {"force-exe-suffix", no_argument, NULL, OPTION_FORCE_EXE_SUFFIX},
254       '\0', NULL, N_("Force generation of file with .exe suffix"), TWO_DASHES},
255   { {"help", no_argument, NULL, OPTION_HELP},
256       '\0', NULL, N_("Print option help"), TWO_DASHES },
257   { {"Map", required_argument, NULL, OPTION_MAP},
258       '\0', N_("FILE"), N_("Write a map file"), ONE_DASH },
259   { {"no-keep-memory", no_argument, NULL, OPTION_NO_KEEP_MEMORY},
260       '\0', NULL, N_("Use less memory and more disk I/O"), TWO_DASHES },
261   { {"no-warn-mismatch", no_argument, NULL, OPTION_NO_WARN_MISMATCH},
262       '\0', NULL, N_("Don't warn about mismatched input files"), TWO_DASHES},
263   { {"no-whole-archive", no_argument, NULL, OPTION_NO_WHOLE_ARCHIVE},
264       '\0', NULL, N_("Turn off --whole-archive"), TWO_DASHES },
265   { {"noinhibit-exec", no_argument, NULL, OPTION_NOINHIBIT_EXEC},
266       '\0', NULL, N_("Create an output file even if errors occur"), TWO_DASHES },
267   { {"noinhibit_exec", no_argument, NULL, OPTION_NOINHIBIT_EXEC},
268       '\0', NULL, NULL, NO_HELP },
269   { {"oformat", required_argument, NULL, OPTION_OFORMAT},
270       '\0', N_("TARGET"), N_("Specify target of output file"), TWO_DASHES },
271   { {"qmagic", no_argument, NULL, OPTION_IGNORE},
272       '\0', NULL, N_("Ignored for Linux compatibility"), ONE_DASH },
273   { {"Qy", no_argument, NULL, OPTION_IGNORE},
274       '\0', NULL, N_("Ignored for SVR4 compatibility"), ONE_DASH },
275   { {"relax", no_argument, NULL, OPTION_RELAX},
276       '\0', NULL, N_("Relax branches on certain targets"), TWO_DASHES },
277   { {"retain-symbols-file", required_argument, NULL,
278        OPTION_RETAIN_SYMBOLS_FILE},
279       '\0', N_("FILE"), N_("Keep only symbols listed in FILE"), TWO_DASHES },
280   { {"rpath", required_argument, NULL, OPTION_RPATH},
281       '\0', N_("PATH"), N_("Set runtime shared library search path"), ONE_DASH },
282   { {"rpath-link", required_argument, NULL, OPTION_RPATH_LINK},
283       '\0', N_("PATH"), N_("Set link time shared library search path"), ONE_DASH },
284   { {"shared", no_argument, NULL, OPTION_SHARED},
285       '\0', NULL, N_("Create a shared library"), ONE_DASH },
286   { {"Bshareable", no_argument, NULL, OPTION_SHARED }, /* FreeBSD.  */
287       '\0', NULL, NULL, ONE_DASH },
288   { {"sort-common", no_argument, NULL, OPTION_SORT_COMMON},
289       '\0', NULL, N_("Sort common symbols by size"), TWO_DASHES },
290   { {"sort_common", no_argument, NULL, OPTION_SORT_COMMON},
291       '\0', NULL, NULL, NO_HELP },
292   { {"split-by-file", no_argument, NULL, OPTION_SPLIT_BY_FILE},
293       '\0', NULL, N_("Split output sections for each file"), TWO_DASHES },
294   { {"split-by-reloc", required_argument, NULL, OPTION_SPLIT_BY_RELOC},
295       '\0', N_("COUNT"), N_("Split output sections every COUNT relocs"), TWO_DASHES },
296   { {"stats", no_argument, NULL, OPTION_STATS},
297       '\0', NULL, N_("Print memory usage statistics"), TWO_DASHES },
298   { {"task-link", required_argument, NULL, OPTION_TASK_LINK},
299       '\0', N_("SYMBOL"), N_("Do task level linking"), TWO_DASHES },
300   { {"traditional-format", no_argument, NULL, OPTION_TRADITIONAL_FORMAT},
301       '\0', NULL, N_("Use same format as native linker"), TWO_DASHES },
302   { {"Tbss", required_argument, NULL, OPTION_TBSS},
303       '\0', N_("ADDRESS"), N_("Set address of .bss section"), ONE_DASH },
304   { {"Tdata", required_argument, NULL, OPTION_TDATA},
305       '\0', N_("ADDRESS"), N_("Set address of .data section"), ONE_DASH },
306   { {"Ttext", required_argument, NULL, OPTION_TTEXT},
307       '\0', N_("ADDRESS"), N_("Set address of .text section"), ONE_DASH },
308   { {"Ur", no_argument, NULL, OPTION_UR},
309       '\0', NULL, N_("Build global constructor/destructor tables"), ONE_DASH },
310   { {"verbose", no_argument, NULL, OPTION_VERBOSE},
311       '\0', NULL, N_("Output lots of information during link"), TWO_DASHES },
312   { {"dll-verbose", no_argument, NULL, OPTION_VERBOSE}, /* Linux.  */
313       '\0', NULL, NULL, NO_HELP },
314   { {"version-script", required_argument, NULL, OPTION_VERSION_SCRIPT },
315       '\0', N_("FILE"), N_("Read version information script"), TWO_DASHES },
316   { {"warn-common", no_argument, NULL, OPTION_WARN_COMMON},
317       '\0', NULL, N_("Warn about duplicate common symbols"), TWO_DASHES },
318   { {"warn-constructors", no_argument, NULL, OPTION_WARN_CONSTRUCTORS},
319       '\0', NULL, N_("Warn if global constructors/destructors are seen"),
320       TWO_DASHES },
321   { {"warn-multiple-gp", no_argument, NULL, OPTION_WARN_MULTIPLE_GP},
322       '\0', NULL, N_("Warn if the multiple GP values are used"), TWO_DASHES },
323   { {"warn-once", no_argument, NULL, OPTION_WARN_ONCE},
324       '\0', NULL, N_("Warn only once per undefined symbol"), TWO_DASHES },
325   { {"warn-section-align", no_argument, NULL, OPTION_WARN_SECTION_ALIGN},
326       '\0', NULL, N_("Warn if start of section changes due to alignment"),
327       TWO_DASHES },
328   { {"whole-archive", no_argument, NULL, OPTION_WHOLE_ARCHIVE},
329       '\0', NULL, N_("Include all objects from following archives"), TWO_DASHES },
330   { {"wrap", required_argument, NULL, OPTION_WRAP},
331       '\0', N_("SYMBOL"), N_("Use wrapper functions for SYMBOL"), TWO_DASHES }
332 };
333
334 #define OPTION_COUNT ((int) (sizeof ld_options / sizeof ld_options[0]))
335
336 void
337 parse_args (argc, argv)
338      int argc;
339      char **argv;
340 {
341   int i, is, il;
342   int ingroup = 0;
343   char *default_dirlist = NULL;
344   char shortopts[OPTION_COUNT * 3 + 2];
345   struct option longopts[OPTION_COUNT + 1];
346   int last_optind;
347
348   /* Starting the short option string with '-' is for programs that
349      expect options and other ARGV-elements in any order and that care about
350      the ordering of the two.  We describe each non-option ARGV-element
351      as if it were the argument of an option with character code 1.  */
352   shortopts[0] = '-';
353   is = 1;
354   il = 0;
355   for (i = 0; i < OPTION_COUNT; i++)
356     {
357       if (ld_options[i].shortopt != '\0')
358         {
359           shortopts[is] = ld_options[i].shortopt;
360           ++is;
361           if (ld_options[i].opt.has_arg == required_argument
362               || ld_options[i].opt.has_arg == optional_argument)
363             {
364               shortopts[is] = ':';
365               ++is;
366               if (ld_options[i].opt.has_arg == optional_argument)
367                 {
368                   shortopts[is] = ':';
369                   ++is;
370                 }
371             }
372         }
373       if (ld_options[i].opt.name != NULL)
374         {
375           longopts[il] = ld_options[i].opt;
376           ++il;
377         }
378     }
379   shortopts[is] = '\0';
380   longopts[il].name = NULL;
381
382   /* The -G option is ambiguous on different platforms.  Sometimes it
383      specifies the largest data size to put into the small data
384      section.  Sometimes it is equivalent to --shared.  Unfortunately,
385      the first form takes an argument, while the second does not.
386
387      We need to permit the --shared form because on some platforms,
388      such as Solaris, gcc -shared will pass -G to the linker.
389
390      To permit either usage, we look through the argument list.  If we
391      find -G not followed by a number, we change it into --shared.
392      This will work for most normal cases.  */
393   for (i = 1; i < argc; i++)
394     if (strcmp (argv[i], "-G") == 0
395         && (i + 1 >= argc
396             || ! isdigit ((unsigned char) argv[i + 1][0])))
397       argv[i] = (char *) "--shared";
398
399   /* Because we permit long options to start with a single dash, and
400      we have a --library option, and the -l option is conventionally
401      used with an immediately following argument, we can have bad
402      results of somebody tries to use -l with a library whose name
403      happens to start with "ibrary", as in -li.  We avoid problems by
404      simply turning -l into --library.  This means that users will
405      have to use two dashes in order to use --library, which is OK
406      since that's how it is documented.
407
408      FIXME: It's possible that this problem can arise for other short
409      options as well, although the user does always have the recourse
410      of adding a space between the option and the argument.  */
411   for (i = 1; i < argc; i++)
412     {
413       if (argv[i][0] == '-'
414           && argv[i][1] == 'l'
415           && argv[i][2] != '\0')
416         {
417           char *n;
418
419           n = (char *) xmalloc (strlen (argv[i]) + 20);
420           sprintf (n, "--library=%s", argv[i] + 2);
421           argv[i] = n;
422         }
423     }
424
425   last_optind = -1;
426   while (1)
427     {
428       int longind;
429       int optc;
430
431       /* Using last_optind lets us avoid calling ldemul_parse_args
432          multiple times on a single option, which would lead to
433          confusion in the internal static variables maintained by
434          getopt.  This could otherwise happen for an argument like
435          -nx, in which the -n is parsed as a single option, and we
436          loop around to pick up the -x.  */
437       if (optind != last_optind)
438         {
439           if (ldemul_parse_args (argc, argv))
440             continue;
441           last_optind = optind;
442         }
443
444       /* getopt_long_only is like getopt_long, but '-' as well as '--'
445          can indicate a long option.  */
446       optc = getopt_long_only (argc, argv, shortopts, longopts, &longind);
447
448       if (optc == -1)
449         break;
450       switch (optc)
451         {
452         default:
453           xexit (1);
454         case 1:                 /* File name.  */
455           lang_add_input_file (optarg, lang_input_file_is_file_enum,
456                                (char *) NULL);
457           break;
458
459         case OPTION_IGNORE:
460           break;
461         case 'a':
462           /* For HP/UX compatibility.  Actually -a shared should mean
463              ``use only shared libraries'' but, then, we don't
464              currently support shared libraries on HP/UX anyhow.  */
465           if (strcmp (optarg, "archive") == 0)
466             config.dynamic_link = false;
467           else if (strcmp (optarg, "shared") == 0
468                    || strcmp (optarg, "default") == 0)
469             config.dynamic_link = true;
470           else
471             einfo (_("%P%F: unrecognized -a option `%s'\n"), optarg);
472           break;
473         case OPTION_ASSERT:
474           /* FIXME: We just ignore these, but we should handle them.  */
475           if (strcmp (optarg, "definitions") == 0)
476             ;
477           else if (strcmp (optarg, "nodefinitions") == 0)
478             ;
479           else if (strcmp (optarg, "nosymbolic") == 0)
480             ;
481           else if (strcmp (optarg, "pure-text") == 0)
482             ;
483           else
484             einfo (_("%P%F: unrecognized -assert option `%s'\n"), optarg);
485           break;
486         case 'A':
487           ldfile_add_arch (optarg);
488           break;
489         case 'b':
490           lang_add_target (optarg);
491           break;
492         case 'c':
493           ldfile_open_command_file (optarg);
494           parser_input = input_mri_script;
495           yyparse ();
496           break;
497         case OPTION_CALL_SHARED:
498           config.dynamic_link = true;
499           break;
500         case OPTION_NON_SHARED:
501           config.dynamic_link = false;
502           break;
503         case OPTION_CREF:
504           command_line.cref = true;
505           link_info.notice_all = true;
506           break;
507         case 'd':
508           command_line.force_common_definition = true;
509           break;
510         case OPTION_DEFSYM:
511           lex_string = optarg;
512           lex_redirect (optarg);
513           parser_input = input_defsym;
514           parsing_defsym = 1;
515           yyparse ();
516           parsing_defsym = 0;
517           lex_string = NULL;
518           break;
519         case OPTION_DYNAMIC_LINKER:
520           command_line.interpreter = optarg;
521           break;
522         case OPTION_EB:
523           command_line.endian = ENDIAN_BIG;
524           break;
525         case OPTION_EL:
526           command_line.endian = ENDIAN_LITTLE;
527           break;
528         case OPTION_EMBEDDED_RELOCS:
529           command_line.embedded_relocs = true;
530           break;
531         case OPTION_EXPORT_DYNAMIC:
532         case 'E': /* HP/UX compatibility.  */
533           command_line.export_dynamic = true;
534           break;
535         case 'e':
536           lang_add_entry (optarg, true);
537           break;
538         case 'f':
539           if (command_line.auxiliary_filters == NULL)
540             {
541               command_line.auxiliary_filters =
542                 (char **) xmalloc (2 * sizeof (char *));
543               command_line.auxiliary_filters[0] = optarg;
544               command_line.auxiliary_filters[1] = NULL;
545             }
546           else
547             {
548               int c;
549               char **p;
550
551               c = 0;
552               for (p = command_line.auxiliary_filters; *p != NULL; p++)
553                 ++c;
554               command_line.auxiliary_filters =
555                 (char **) xrealloc (command_line.auxiliary_filters,
556                                     (c + 2) * sizeof (char *));
557               command_line.auxiliary_filters[c] = optarg;
558               command_line.auxiliary_filters[c + 1] = NULL;
559             }
560           break;
561         case 'F':
562           command_line.filter_shlib = optarg;
563           break;
564         case OPTION_FORCE_EXE_SUFFIX:
565           command_line.force_exe_suffix = true;
566           break;
567         case 'G':
568           {
569             char *end;
570             g_switch_value = strtoul (optarg, &end, 0);
571             if (*end)
572               einfo (_("%P%F: invalid number `%s'\n"), optarg);
573           }
574           break;
575         case 'g':
576           /* Ignore.  */
577           break;
578         case OPTION_HELP:
579           help ();
580           xexit (0);
581           break;
582         case 'L':
583           ldfile_add_library_path (optarg, true);
584           break;
585         case 'l':
586           lang_add_input_file (optarg, lang_input_file_is_l_enum,
587                                (char *) NULL);
588           break;
589         case 'M':
590           config.map_filename = "-";
591           break;
592         case 'm':
593           /* Ignore.  Was handled in a pre-parse.   */
594           break;
595         case OPTION_MAP:
596           config.map_filename = optarg;
597           break;
598         case 'N':
599           config.text_read_only = false;
600           config.magic_demand_paged = false;
601           config.dynamic_link = false;
602           break;
603         case 'n':
604           config.magic_demand_paged = false;
605           config.dynamic_link = false;
606           break;
607         case OPTION_NO_KEEP_MEMORY:
608           link_info.keep_memory = false;
609           break;
610         case OPTION_NO_WARN_MISMATCH:
611           command_line.warn_mismatch = false;
612           break;
613         case OPTION_NOINHIBIT_EXEC:
614           force_make_executable = true;
615           break;
616         case OPTION_NO_WHOLE_ARCHIVE:
617           whole_archive = false;
618           break;
619         case 'O':
620           /* FIXME "-O<non-digits> <value>" used to set the address of
621              section <non-digits>.  Was this for compatibility with
622              something, or can we create a new option to do that
623              (with a syntax similar to -defsym)?
624              getopt can't handle two args to an option without kludges.  */
625           break;
626         case 'o':
627           lang_add_output (optarg, 0); 
628           break;
629         case OPTION_OFORMAT:
630           lang_add_output_format (optarg, (char *) NULL, (char *) NULL, 0);
631           break;
632         case 'i':
633         case 'r':
634           link_info.relocateable = true;
635           config.build_constructors = false;
636           config.magic_demand_paged = false;
637           config.text_read_only = false;
638           config.dynamic_link = false;
639           break;
640         case 'R':
641           /* The GNU linker traditionally uses -R to mean to include
642              only the symbols from a file.  The Solaris linker uses -R
643              to set the path used by the runtime linker to find
644              libraries.  This is the GNU linker -rpath argument.  We
645              try to support both simultaneously by checking the file
646              named.  If it is a directory, rather than a regular file,
647              we assume -rpath was meant.  */
648           {
649             struct stat s;
650
651             if (stat (optarg, &s) >= 0
652                 && ! S_ISDIR (s.st_mode))
653               {
654                 lang_add_input_file (optarg,
655                                      lang_input_file_is_symbols_only_enum,
656                                      (char *) NULL);
657                 break;
658               }
659           }
660           /* Fall through.  */
661         case OPTION_RPATH:
662           if (command_line.rpath == NULL)
663             command_line.rpath = buystring (optarg);
664           else
665             {
666               char *buf;
667
668               buf = xmalloc (strlen (command_line.rpath)
669                              + strlen (optarg)
670                              + 2);
671               sprintf (buf, "%s:%s", command_line.rpath, optarg);
672               free (command_line.rpath);
673               command_line.rpath = buf;
674             }
675           break;
676         case OPTION_RPATH_LINK:
677           if (command_line.rpath_link == NULL)
678             command_line.rpath_link = buystring (optarg);
679           else
680             {
681               char *buf;
682
683               buf = xmalloc (strlen (command_line.rpath_link)
684                              + strlen (optarg)
685                              + 2);
686               sprintf (buf, "%s:%s", command_line.rpath_link, optarg);
687               free (command_line.rpath_link);
688               command_line.rpath_link = buf;
689             }
690           break;
691         case OPTION_RELAX:
692           command_line.relax = true;
693           break;
694         case OPTION_RETAIN_SYMBOLS_FILE:
695           add_keepsyms_file (optarg);
696           break;
697         case 'S':
698           link_info.strip = strip_debugger;
699           break;
700         case 's':
701           link_info.strip = strip_all;
702           break;
703         case OPTION_SHARED:
704           link_info.shared = true;
705           break;
706         case 'h':               /* Used on Solaris.  */
707         case OPTION_SONAME:
708           command_line.soname = optarg;
709           break;
710         case OPTION_SORT_COMMON:
711           config.sort_common = true;
712           break;
713         case OPTION_STATS:
714           config.stats = true;
715           break;
716         case OPTION_SYMBOLIC:
717           link_info.symbolic = true;
718           break;
719         case 't':
720           trace_files = true;
721           break;
722         case 'T':
723           ldfile_open_command_file (optarg);
724           parser_input = input_script;
725           yyparse ();
726           break;
727         case OPTION_TBSS:
728           set_section_start (".bss", optarg);
729           break;
730         case OPTION_TDATA:
731           set_section_start (".data", optarg);
732           break;
733         case OPTION_TTEXT:
734           set_section_start (".text", optarg);
735           break;
736         case OPTION_TRADITIONAL_FORMAT:
737           link_info.traditional_format = true;
738           break;
739         case OPTION_TASK_LINK:
740           link_info.task_link = true;
741           /* Fall through - do an implied -r option.  */
742         case OPTION_UR:
743           link_info.relocateable = true;
744           config.build_constructors = true;
745           config.magic_demand_paged = false;
746           config.text_read_only = false;
747           config.dynamic_link = false;
748           break;
749         case 'u':
750           ldlang_add_undef (optarg);
751           break;
752         case OPTION_VERBOSE:
753           ldversion (1);
754           version_printed = true;
755           trace_file_tries = true;
756           break;
757         case 'v':
758           ldversion (0);
759           version_printed = true;
760           break;
761         case 'V':
762           ldversion (1);
763           version_printed = true;
764           break;
765         case OPTION_VERSION:
766           /* This output is intended to follow the GNU standards document.  */
767           printf ("GNU ld %s\n", ld_program_version);
768           printf (_("Copyright 1997 Free Software Foundation, Inc.\n"));
769           printf (_("\
770 This program is free software; you may redistribute it under the terms of\n\
771 the GNU General Public License.  This program has absolutely no warranty.\n"));
772           {
773             ld_emulation_xfer_type **ptr = ld_emulations;
774     
775             printf (_("  Supported emulations:\n"));
776             while (*ptr) 
777               {
778                 printf ("   %s\n", (*ptr)->emulation_name);
779                 ptr++;
780               }
781           }
782           xexit (0);
783           break;
784         case OPTION_VERSION_SCRIPT:
785           /* This option indicates a small script that only specifies
786              version information.  Read it, but don't assume that
787              we've seen a linker script.  */
788           {
789             boolean hold_had_script;
790
791             hold_had_script = had_script;
792             ldfile_open_command_file (optarg);
793             had_script = hold_had_script;
794             parser_input = input_version_script;
795             yyparse ();
796           }
797           break;
798         case OPTION_WARN_COMMON:
799           config.warn_common = true;
800           break;
801         case OPTION_WARN_CONSTRUCTORS:
802           config.warn_constructors = true;
803           break;
804         case OPTION_WARN_MULTIPLE_GP:
805           config.warn_multiple_gp = true;
806           break;
807         case OPTION_WARN_ONCE:
808           config.warn_once = true;
809           break;
810         case OPTION_WARN_SECTION_ALIGN:
811           config.warn_section_align = true;
812           break;
813         case OPTION_WHOLE_ARCHIVE:
814           whole_archive = true;
815           break;
816         case OPTION_WRAP:
817           add_wrap (optarg);
818           break;
819         case 'X':
820           link_info.discard = discard_l;
821           break;
822         case 'x':
823           link_info.discard = discard_all;
824           break;
825         case 'Y':
826           if (strncmp (optarg, "P,", 2) == 0)
827             optarg += 2;
828           default_dirlist = xstrdup (optarg);
829           break;
830         case 'y':
831           add_ysym (optarg);
832           break;
833         case 'z':
834           /* We accept and ignore this option for Solaris
835              compatibility.  Actually, on Solaris, optarg is not
836              ignored.  Someday we should handle it correctly.  FIXME.  */
837           break;
838         case OPTION_SPLIT_BY_RELOC:
839           config.split_by_reloc = atoi (optarg);
840           break; 
841         case OPTION_SPLIT_BY_FILE:
842           config.split_by_file = true;
843           break; 
844         case '(':
845           if (ingroup)
846             {
847               fprintf (stderr,
848                        _("%s: may not nest groups (--help for usage)\n"),
849                        program_name);
850               xexit (1);
851             }
852           lang_enter_group ();
853           ingroup = 1;
854           break;
855         case ')':
856           if (! ingroup)
857             {
858               fprintf (stderr,
859                        _("%s: group ended before it began (--help for usage)\n"),
860                        program_name);
861               xexit (1);
862             }
863           lang_leave_group ();
864           ingroup = 0;
865           break;
866
867         }
868     }
869
870   if (ingroup)
871     lang_leave_group ();
872
873   if (default_dirlist != NULL)
874     set_default_dirlist (default_dirlist);
875
876 }
877
878 /* Add the (colon-separated) elements of DIRLIST_PTR to the
879    library search path.  */
880
881 static void
882 set_default_dirlist (dirlist_ptr)
883      char *dirlist_ptr;
884 {
885   char *p;
886
887   while (1)
888     {
889       p = strchr (dirlist_ptr, PATH_SEPARATOR);
890       if (p != NULL)
891         *p = '\0';
892       if (*dirlist_ptr != '\0')
893         ldfile_add_library_path (dirlist_ptr, true);
894       if (p == NULL)
895         break;
896       dirlist_ptr = p + 1;
897     }
898 }
899
900 static void
901 set_section_start (sect, valstr)
902      char *sect, *valstr;
903 {
904   char *end;
905   unsigned long val = strtoul (valstr, &end, 16);
906   if (*end)
907     einfo (_("%P%F: invalid hex number `%s'\n"), valstr);
908   lang_section_start (sect, exp_intop (val));
909 }
910 \f
911 /* Print help messages for the options.  */
912
913 static void
914 help ()
915 {
916   int i;
917   const char **targets, **pp;
918
919   printf (_("Usage: %s [options] file...\n"), program_name);
920
921   printf (_("Options:\n"));
922   for (i = 0; i < OPTION_COUNT; i++)
923     {
924       if (ld_options[i].doc != NULL)
925         {
926           boolean comma;
927           int len;
928           int j;
929
930           printf ("  ");
931
932           comma = false;
933           len = 2;
934
935           j = i;
936           do
937             {
938               if (ld_options[j].shortopt != '\0'
939                   && ld_options[j].control != NO_HELP)
940                 {
941                   printf ("%s-%c", comma ? ", " : "", ld_options[j].shortopt);
942                   len += (comma ? 2 : 0) + 2;
943                   if (ld_options[j].arg != NULL)
944                     {
945                       if (ld_options[j].opt.has_arg != optional_argument)
946                         {
947                           printf (" ");
948                           ++len;
949                         }
950                       printf ("%s", _(ld_options[j].arg));
951                       len += strlen (_(ld_options[j].arg));
952                     }
953                   comma = true;
954                 }
955               ++j;
956             }
957           while (j < OPTION_COUNT && ld_options[j].doc == NULL);
958
959           j = i;
960           do
961             {
962               if (ld_options[j].opt.name != NULL
963                   && ld_options[j].control != NO_HELP)
964                 {
965                   printf ("%s-%s%s",
966                           comma ? ", " : "",
967                           ld_options[j].control == TWO_DASHES ? "-" : "",
968                           ld_options[j].opt.name);
969                   len += ((comma ? 2 : 0)
970                           + 1
971                           + (ld_options[j].control == TWO_DASHES ? 1 : 0)
972                           + strlen (ld_options[j].opt.name));
973                   if (ld_options[j].arg != NULL)
974                     {
975                       printf (" %s", _(ld_options[j].arg));
976                       len += 1 + strlen (_(ld_options[j].arg));
977                     }
978                   comma = true;
979                 }
980               ++j;
981             }
982           while (j < OPTION_COUNT && ld_options[j].doc == NULL);
983
984           if (len >= 30)
985             {
986               printf ("\n");
987               len = 0;
988             }
989
990           for (; len < 30; len++)
991             putchar (' ');
992
993           printf ("%s\n", _(ld_options[i].doc));
994         }
995     }
996
997   printf (_("%s: supported targets:"), program_name);
998   targets = bfd_target_list ();
999   for (pp = targets; *pp != NULL; pp++)
1000     printf (" %s", *pp);
1001   free (targets);
1002   printf ("\n");
1003
1004   printf (_("%s: supported emulations: "), program_name);
1005   ldemul_list_emulations (stdout);
1006   printf ("\n");
1007   printf (_("\nReport bugs to bug-gnu-utils@gnu.org\n"));
1008 }