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