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