ld/
[platform/upstream/binutils.git] / ld / lexsup.c
1 /* Parse options for the GNU linker.
2    Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3    2001, 2002, 2003, 2004, 2005
4    Free Software Foundation, Inc.
5
6    This file is part of GLD, the Gnu Linker.
7
8    GLD is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2, or (at your option)
11    any later version.
12
13    GLD is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with GLD; see the file COPYING.  If not, write to the Free
20    Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
21    02110-1301, USA.  */
22
23 #include "config.h"
24 #include "bfd.h"
25 #include "sysdep.h"
26 #include "libiberty.h"
27 #include <stdio.h>
28 #include <string.h>
29 #include "safe-ctype.h"
30 #include "getopt.h"
31 #include "bfdlink.h"
32 #include "ld.h"
33 #include "ldmain.h"
34 #include "ldmisc.h"
35 #include "ldexp.h"
36 #include "ldlang.h"
37 #include <ldgram.h>
38 #include "ldlex.h"
39 #include "ldfile.h"
40 #include "ldver.h"
41 #include "ldemul.h"
42 #include "demangle.h"
43
44 #ifndef PATH_SEPARATOR
45 #if defined (__MSDOS__) || (defined (_WIN32) && ! defined (__CYGWIN32__))
46 #define PATH_SEPARATOR ';'
47 #else
48 #define PATH_SEPARATOR ':'
49 #endif
50 #endif
51
52 /* Somewhere above, sys/stat.h got included . . . .  */
53 #if !defined(S_ISDIR) && defined(S_IFDIR)
54 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
55 #endif
56
57 static void set_default_dirlist (char *);
58 static void set_section_start (char *, char *);
59 static void set_segment_start (const char *, char *);
60 static void help (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 enum option_values
68 {
69   OPTION_ASSERT = 150,
70   OPTION_CALL_SHARED,
71   OPTION_CREF,
72   OPTION_DEFSYM,
73   OPTION_DEMANGLE,
74   OPTION_DYNAMIC_LINKER,
75   OPTION_SYSROOT,
76   OPTION_EB,
77   OPTION_EL,
78   OPTION_EMBEDDED_RELOCS,
79   OPTION_EXPORT_DYNAMIC,
80   OPTION_HELP,
81   OPTION_IGNORE,
82   OPTION_MAP,
83   OPTION_NO_DEMANGLE,
84   OPTION_NO_KEEP_MEMORY,
85   OPTION_NO_WARN_MISMATCH,
86   OPTION_NOINHIBIT_EXEC,
87   OPTION_NON_SHARED,
88   OPTION_NO_WHOLE_ARCHIVE,
89   OPTION_OFORMAT,
90   OPTION_RELAX,
91   OPTION_RETAIN_SYMBOLS_FILE,
92   OPTION_RPATH,
93   OPTION_RPATH_LINK,
94   OPTION_SHARED,
95   OPTION_SONAME,
96   OPTION_SORT_COMMON,
97   OPTION_SORT_SECTION,
98   OPTION_STATS,
99   OPTION_SYMBOLIC,
100   OPTION_TASK_LINK,
101   OPTION_TBSS,
102   OPTION_TDATA,
103   OPTION_TTEXT,
104   OPTION_TRADITIONAL_FORMAT,
105   OPTION_UR,
106   OPTION_VERBOSE,
107   OPTION_VERSION,
108   OPTION_VERSION_SCRIPT,
109   OPTION_VERSION_EXPORTS_SECTION,
110   OPTION_DYNAMIC_LIST,
111   OPTION_DYNAMIC_LIST_CPP_NEW,
112   OPTION_DYNAMIC_LIST_CPP_TYPEINFO,
113   OPTION_DYNAMIC_LIST_DATA,
114   OPTION_WARN_COMMON,
115   OPTION_WARN_CONSTRUCTORS,
116   OPTION_WARN_FATAL,
117   OPTION_WARN_MULTIPLE_GP,
118   OPTION_WARN_ONCE,
119   OPTION_WARN_SECTION_ALIGN,
120   OPTION_SPLIT_BY_RELOC,
121   OPTION_SPLIT_BY_FILE ,
122   OPTION_WHOLE_ARCHIVE,
123   OPTION_ADD_NEEDED,
124   OPTION_NO_ADD_NEEDED,
125   OPTION_AS_NEEDED,
126   OPTION_NO_AS_NEEDED,
127   OPTION_WRAP,
128   OPTION_FORCE_EXE_SUFFIX,
129   OPTION_GC_SECTIONS,
130   OPTION_NO_GC_SECTIONS,
131   OPTION_PRINT_GC_SECTIONS,
132   OPTION_NO_PRINT_GC_SECTIONS,
133   OPTION_HASH_SIZE,
134   OPTION_CHECK_SECTIONS,
135   OPTION_NO_CHECK_SECTIONS,
136   OPTION_NO_UNDEFINED,
137   OPTION_INIT,
138   OPTION_FINI,
139   OPTION_SECTION_START,
140   OPTION_UNIQUE,
141   OPTION_TARGET_HELP,
142   OPTION_ALLOW_SHLIB_UNDEFINED,
143   OPTION_NO_ALLOW_SHLIB_UNDEFINED,
144   OPTION_ALLOW_MULTIPLE_DEFINITION,
145   OPTION_NO_UNDEFINED_VERSION,
146   OPTION_DEFAULT_SYMVER,
147   OPTION_DEFAULT_IMPORTED_SYMVER,
148   OPTION_DISCARD_NONE,
149   OPTION_SPARE_DYNAMIC_TAGS,
150   OPTION_NO_DEFINE_COMMON,
151   OPTION_NOSTDLIB,
152   OPTION_NO_OMAGIC,
153   OPTION_STRIP_DISCARDED,
154   OPTION_NO_STRIP_DISCARDED,
155   OPTION_ACCEPT_UNKNOWN_INPUT_ARCH,
156   OPTION_NO_ACCEPT_UNKNOWN_INPUT_ARCH,
157   OPTION_PIE,
158   OPTION_UNRESOLVED_SYMBOLS,
159   OPTION_WARN_UNRESOLVED_SYMBOLS,
160   OPTION_ERROR_UNRESOLVED_SYMBOLS,
161   OPTION_WARN_SHARED_TEXTREL,
162   OPTION_REDUCE_MEMORY_OVERHEADS,
163   OPTION_DEFAULT_SCRIPT
164 };
165
166 /* The long options.  This structure is used for both the option
167    parsing and the help text.  */
168
169 struct ld_option
170 {
171   /* The long option information.  */
172   struct option opt;
173   /* The short option with the same meaning ('\0' if none).  */
174   char shortopt;
175   /* The name of the argument (NULL if none).  */
176   const char *arg;
177   /* The documentation string.  If this is NULL, this is a synonym for
178      the previous option.  */
179   const char *doc;
180   enum {
181     /* Use one dash before long option name.  */
182     ONE_DASH,
183     /* Use two dashes before long option name.  */
184     TWO_DASHES,
185     /* Only accept two dashes before the long option name.
186        This is an overloading of the use of this enum, since originally it
187        was only intended to tell the --help display function how to display
188        the long option name.  This feature was added in order to resolve
189        the confusion about the -omagic command line switch.  Is it setting
190        the output file name to "magic" or is it setting the NMAGIC flag on
191        the output ?  It has been decided that it is setting the output file
192        name, and that if you want to set the NMAGIC flag you should use -N
193        or --omagic.  */
194     EXACTLY_TWO_DASHES,
195     /* Don't mention this option in --help output.  */
196     NO_HELP
197   } control;
198 };
199
200 static const struct ld_option ld_options[] =
201 {
202   { {NULL, required_argument, NULL, '\0'},
203     'a', N_("KEYWORD"), N_("Shared library control for HP/UX compatibility"),
204     ONE_DASH },
205   { {"architecture", required_argument, NULL, 'A'},
206     'A', N_("ARCH"), N_("Set architecture") , TWO_DASHES },
207   { {"format", required_argument, NULL, 'b'},
208     'b', N_("TARGET"), N_("Specify target for following input files"),
209     TWO_DASHES },
210   { {"mri-script", required_argument, NULL, 'c'},
211     'c', N_("FILE"), N_("Read MRI format linker script"), TWO_DASHES },
212   { {"dc", no_argument, NULL, 'd'},
213     'd', NULL, N_("Force common symbols to be defined"), ONE_DASH },
214   { {"dp", no_argument, NULL, 'd'},
215     '\0', NULL, NULL, ONE_DASH },
216   { {"entry", required_argument, NULL, 'e'},
217     'e', N_("ADDRESS"), N_("Set start address"), TWO_DASHES },
218   { {"export-dynamic", no_argument, NULL, OPTION_EXPORT_DYNAMIC},
219     'E', NULL, N_("Export all dynamic symbols"), TWO_DASHES },
220   { {"EB", no_argument, NULL, OPTION_EB},
221     '\0', NULL, N_("Link big-endian objects"), ONE_DASH },
222   { {"EL", no_argument, NULL, OPTION_EL},
223     '\0', NULL, N_("Link little-endian objects"), ONE_DASH },
224   { {"auxiliary", required_argument, NULL, 'f'},
225     'f', N_("SHLIB"), N_("Auxiliary filter for shared object symbol table"),
226     TWO_DASHES },
227   { {"filter", required_argument, NULL, 'F'},
228     'F', N_("SHLIB"), N_("Filter for shared object symbol table"),
229     TWO_DASHES },
230   { {NULL, no_argument, NULL, '\0'},
231     'g', NULL, N_("Ignored"), ONE_DASH },
232   { {"gpsize", required_argument, NULL, 'G'},
233     'G', N_("SIZE"), N_("Small data size (if no size, same as --shared)"),
234     TWO_DASHES },
235   { {"soname", required_argument, NULL, OPTION_SONAME},
236     'h', N_("FILENAME"), N_("Set internal name of shared library"), ONE_DASH },
237   { {"dynamic-linker", required_argument, NULL, OPTION_DYNAMIC_LINKER},
238     'I', N_("PROGRAM"), N_("Set PROGRAM as the dynamic linker to use"),
239     TWO_DASHES },
240   { {"library", required_argument, NULL, 'l'},
241     'l', N_("LIBNAME"), N_("Search for library LIBNAME"), TWO_DASHES },
242   { {"library-path", required_argument, NULL, 'L'},
243     'L', N_("DIRECTORY"), N_("Add DIRECTORY to library search path"),
244     TWO_DASHES },
245   { {"sysroot=<DIRECTORY>", required_argument, NULL, OPTION_SYSROOT},
246     '\0', NULL, N_("Override the default sysroot location"), TWO_DASHES },
247   { {NULL, required_argument, NULL, '\0'},
248     'm', N_("EMULATION"), N_("Set emulation"), ONE_DASH },
249   { {"print-map", no_argument, NULL, 'M'},
250     'M', NULL, N_("Print map file on standard output"), TWO_DASHES },
251   { {"nmagic", no_argument, NULL, 'n'},
252     'n', NULL, N_("Do not page align data"), TWO_DASHES },
253   { {"omagic", no_argument, NULL, 'N'},
254     'N', NULL, N_("Do not page align data, do not make text readonly"),
255     EXACTLY_TWO_DASHES },
256   { {"no-omagic", no_argument, NULL, OPTION_NO_OMAGIC},
257     '\0', NULL, N_("Page align data, make text readonly"),
258     EXACTLY_TWO_DASHES },
259   { {"output", required_argument, NULL, 'o'},
260     'o', N_("FILE"), N_("Set output file name"), EXACTLY_TWO_DASHES },
261   { {NULL, required_argument, NULL, '\0'},
262     'O', NULL, N_("Optimize output file"), ONE_DASH },
263   { {"Qy", no_argument, NULL, OPTION_IGNORE},
264     '\0', NULL, N_("Ignored for SVR4 compatibility"), ONE_DASH },
265   { {"emit-relocs", no_argument, NULL, 'q'},
266     'q', NULL, "Generate relocations in final output", TWO_DASHES },
267   { {"relocatable", no_argument, NULL, 'r'},
268     'r', NULL, N_("Generate relocatable output"), TWO_DASHES },
269   { {NULL, no_argument, NULL, '\0'},
270     'i', NULL, NULL, ONE_DASH },
271   { {"just-symbols", required_argument, NULL, 'R'},
272     'R', N_("FILE"), N_("Just link symbols (if directory, same as --rpath)"),
273     TWO_DASHES },
274   { {"strip-all", no_argument, NULL, 's'},
275     's', NULL, N_("Strip all symbols"), TWO_DASHES },
276   { {"strip-debug", no_argument, NULL, 'S'},
277     'S', NULL, N_("Strip debugging symbols"), TWO_DASHES },
278   { {"strip-discarded", no_argument, NULL, OPTION_STRIP_DISCARDED},
279     '\0', NULL, N_("Strip symbols in discarded sections"), TWO_DASHES },
280   { {"no-strip-discarded", no_argument, NULL, OPTION_NO_STRIP_DISCARDED},
281     '\0', NULL, N_("Do not strip symbols in discarded sections"), TWO_DASHES },
282   { {"trace", no_argument, NULL, 't'},
283     't', NULL, N_("Trace file opens"), TWO_DASHES },
284   { {"script", required_argument, NULL, 'T'},
285     'T', N_("FILE"), N_("Read linker script"), TWO_DASHES },
286   { {"default-script", required_argument, NULL, OPTION_DEFAULT_SCRIPT},
287     '\0', N_("FILE"), N_("Read default linker script"), TWO_DASHES },
288   { {"dT", required_argument, NULL, OPTION_DEFAULT_SCRIPT},
289     '\0', NULL, NULL, ONE_DASH },
290   { {"undefined", required_argument, NULL, 'u'},
291     'u', N_("SYMBOL"), N_("Start with undefined reference to SYMBOL"),
292     TWO_DASHES },
293   { {"unique", optional_argument, NULL, OPTION_UNIQUE},
294     '\0', N_("[=SECTION]"),
295     N_("Don't merge input [SECTION | orphan] sections"), TWO_DASHES },
296   { {"Ur", no_argument, NULL, OPTION_UR},
297     '\0', NULL, N_("Build global constructor/destructor tables"), ONE_DASH },
298   { {"version", no_argument, NULL, OPTION_VERSION},
299     'v', NULL, N_("Print version information"), TWO_DASHES },
300   { {NULL, no_argument, NULL, '\0'},
301     'V', NULL, N_("Print version and emulation information"), ONE_DASH },
302   { {"discard-all", no_argument, NULL, 'x'},
303     'x', NULL, N_("Discard all local symbols"), TWO_DASHES },
304   { {"discard-locals", no_argument, NULL, 'X'},
305     'X', NULL, N_("Discard temporary local symbols (default)"), TWO_DASHES },
306   { {"discard-none", no_argument, NULL, OPTION_DISCARD_NONE},
307     '\0', NULL, N_("Don't discard any local symbols"), TWO_DASHES },
308   { {"trace-symbol", required_argument, NULL, 'y'},
309     'y', N_("SYMBOL"), N_("Trace mentions of SYMBOL"), TWO_DASHES },
310   { {NULL, required_argument, NULL, '\0'},
311     'Y', N_("PATH"), N_("Default search path for Solaris compatibility"),
312     ONE_DASH },
313   { {"start-group", no_argument, NULL, '('},
314     '(', NULL, N_("Start a group"), TWO_DASHES },
315   { {"end-group", no_argument, NULL, ')'},
316     ')', NULL, N_("End a group"), TWO_DASHES },
317   { {"accept-unknown-input-arch", no_argument, NULL,
318      OPTION_ACCEPT_UNKNOWN_INPUT_ARCH},
319     '\0', NULL,
320     N_("Accept input files whose architecture cannot be determined"),
321     TWO_DASHES },
322   { {"no-accept-unknown-input-arch", no_argument, NULL,
323      OPTION_NO_ACCEPT_UNKNOWN_INPUT_ARCH},
324     '\0', NULL, N_("Reject input files whose architecture is unknown"),
325     TWO_DASHES },
326   { {"add-needed", no_argument, NULL, OPTION_ADD_NEEDED},
327     '\0', NULL, N_("Set DT_NEEDED tags for DT_NEEDED entries in\n"
328                    "\t\t\t\tfollowing dynamic libs"), TWO_DASHES },
329   { {"no-add-needed", no_argument, NULL, OPTION_NO_ADD_NEEDED},
330     '\0', NULL, N_("Do not set DT_NEEDED tags for DT_NEEDED entries\n"
331                    "\t\t\t\tin following dynamic libs"), TWO_DASHES },
332   { {"as-needed", no_argument, NULL, OPTION_AS_NEEDED},
333     '\0', NULL, N_("Only set DT_NEEDED for following dynamic libs if used"),
334     TWO_DASHES },
335   { {"no-as-needed", no_argument, NULL, OPTION_NO_AS_NEEDED},
336     '\0', NULL, N_("Always set DT_NEEDED for following dynamic libs"),
337     TWO_DASHES },
338   { {"assert", required_argument, NULL, OPTION_ASSERT},
339     '\0', N_("KEYWORD"), N_("Ignored for SunOS compatibility"), ONE_DASH },
340   { {"Bdynamic", no_argument, NULL, OPTION_CALL_SHARED},
341     '\0', NULL, N_("Link against shared libraries"), ONE_DASH },
342   { {"dy", no_argument, NULL, OPTION_CALL_SHARED},
343     '\0', NULL, NULL, ONE_DASH },
344   { {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
345     '\0', NULL, NULL, ONE_DASH },
346   { {"Bstatic", no_argument, NULL, OPTION_NON_SHARED},
347     '\0', NULL, N_("Do not link against shared libraries"), ONE_DASH },
348   { {"dn", no_argument, NULL, OPTION_NON_SHARED},
349     '\0', NULL, NULL, ONE_DASH },
350   { {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
351     '\0', NULL, NULL, ONE_DASH },
352   { {"static", no_argument, NULL, OPTION_NON_SHARED},
353     '\0', NULL, NULL, ONE_DASH },
354   { {"Bsymbolic", no_argument, NULL, OPTION_SYMBOLIC},
355     '\0', NULL, N_("Bind global references locally"), ONE_DASH },
356   { {"Bsymbolic-functions", no_argument, NULL, OPTION_DYNAMIC_LIST_DATA},
357     '\0', NULL, N_("Bind global function references locally"), ONE_DASH },
358   { {"check-sections", no_argument, NULL, OPTION_CHECK_SECTIONS},
359     '\0', NULL, N_("Check section addresses for overlaps (default)"),
360     TWO_DASHES },
361   { {"no-check-sections", no_argument, NULL, OPTION_NO_CHECK_SECTIONS},
362     '\0', NULL, N_("Do not check section addresses for overlaps"),
363     TWO_DASHES },
364   { {"cref", no_argument, NULL, OPTION_CREF},
365     '\0', NULL, N_("Output cross reference table"), TWO_DASHES },
366   { {"defsym", required_argument, NULL, OPTION_DEFSYM},
367     '\0', N_("SYMBOL=EXPRESSION"), N_("Define a symbol"), TWO_DASHES },
368   { {"demangle", optional_argument, NULL, OPTION_DEMANGLE},
369     '\0', N_("[=STYLE]"), N_("Demangle symbol names [using STYLE]"),
370     TWO_DASHES },
371   { {"embedded-relocs", no_argument, NULL, OPTION_EMBEDDED_RELOCS},
372     '\0', NULL, N_("Generate embedded relocs"), TWO_DASHES},
373   { {"fatal-warnings", no_argument, NULL, OPTION_WARN_FATAL},
374     '\0', NULL, N_("Treat warnings as errors"),
375     TWO_DASHES },
376   { {"fini", required_argument, NULL, OPTION_FINI},
377     '\0', N_("SYMBOL"), N_("Call SYMBOL at unload-time"), ONE_DASH },
378   { {"force-exe-suffix", no_argument, NULL, OPTION_FORCE_EXE_SUFFIX},
379     '\0', NULL, N_("Force generation of file with .exe suffix"), TWO_DASHES},
380   { {"gc-sections", no_argument, NULL, OPTION_GC_SECTIONS},
381     '\0', NULL, N_("Remove unused sections (on some targets)"),
382     TWO_DASHES },
383   { {"no-gc-sections", no_argument, NULL, OPTION_NO_GC_SECTIONS},
384     '\0', NULL, N_("Don't remove unused sections (default)"),
385     TWO_DASHES },
386   { {"print-gc-sections", no_argument, NULL, OPTION_PRINT_GC_SECTIONS},
387     '\0', NULL, N_("List removed unused sections on stderr"),
388     TWO_DASHES },
389   { {"no-print-gc-sections", no_argument, NULL, OPTION_NO_PRINT_GC_SECTIONS},
390     '\0', NULL, N_("Do not list removed unused sections"),
391     TWO_DASHES },
392   { {"hash-size=<NUMBER>", required_argument, NULL, OPTION_HASH_SIZE},
393     '\0', NULL, N_("Set default hash table size close to <NUMBER>"),
394     TWO_DASHES },
395   { {"help", no_argument, NULL, OPTION_HELP},
396     '\0', NULL, N_("Print option help"), TWO_DASHES },
397   { {"init", required_argument, NULL, OPTION_INIT},
398     '\0', N_("SYMBOL"), N_("Call SYMBOL at load-time"), ONE_DASH },
399   { {"Map", required_argument, NULL, OPTION_MAP},
400     '\0', N_("FILE"), N_("Write a map file"), ONE_DASH },
401   { {"no-define-common", no_argument, NULL, OPTION_NO_DEFINE_COMMON},
402     '\0', NULL, N_("Do not define Common storage"), TWO_DASHES },
403   { {"no-demangle", no_argument, NULL, OPTION_NO_DEMANGLE },
404     '\0', NULL, N_("Do not demangle symbol names"), TWO_DASHES },
405   { {"no-keep-memory", no_argument, NULL, OPTION_NO_KEEP_MEMORY},
406     '\0', NULL, N_("Use less memory and more disk I/O"), TWO_DASHES },
407   { {"no-undefined", no_argument, NULL, OPTION_NO_UNDEFINED},
408     '\0', NULL, N_("Do not allow unresolved references in object files"),
409     TWO_DASHES },
410   { {"allow-shlib-undefined", no_argument, NULL, OPTION_ALLOW_SHLIB_UNDEFINED},
411     '\0', NULL, N_("Allow unresolved references in shared libaries"),
412     TWO_DASHES },
413   { {"no-allow-shlib-undefined", no_argument, NULL,
414      OPTION_NO_ALLOW_SHLIB_UNDEFINED},
415     '\0', NULL, N_("Do not allow unresolved references in shared libs"),
416     TWO_DASHES },
417   { {"allow-multiple-definition", no_argument, NULL,
418      OPTION_ALLOW_MULTIPLE_DEFINITION},
419     '\0', NULL, N_("Allow multiple definitions"), TWO_DASHES },
420   { {"no-undefined-version", no_argument, NULL, OPTION_NO_UNDEFINED_VERSION},
421     '\0', NULL, N_("Disallow undefined version"), TWO_DASHES },
422   { {"default-symver", no_argument, NULL, OPTION_DEFAULT_SYMVER},
423     '\0', NULL, N_("Create default symbol version"), TWO_DASHES },
424   { {"default-imported-symver", no_argument, NULL,
425       OPTION_DEFAULT_IMPORTED_SYMVER},
426     '\0', NULL, N_("Create default symbol version for imported symbols"),
427     TWO_DASHES },
428   { {"no-warn-mismatch", no_argument, NULL, OPTION_NO_WARN_MISMATCH},
429     '\0', NULL, N_("Don't warn about mismatched input files"), TWO_DASHES},
430   { {"no-whole-archive", no_argument, NULL, OPTION_NO_WHOLE_ARCHIVE},
431     '\0', NULL, N_("Turn off --whole-archive"), TWO_DASHES },
432   { {"noinhibit-exec", no_argument, NULL, OPTION_NOINHIBIT_EXEC},
433     '\0', NULL, N_("Create an output file even if errors occur"),
434     TWO_DASHES },
435   { {"noinhibit_exec", no_argument, NULL, OPTION_NOINHIBIT_EXEC},
436     '\0', NULL, NULL, NO_HELP },
437   { {"nostdlib", no_argument, NULL, OPTION_NOSTDLIB},
438     '\0', NULL, N_("Only use library directories specified on\n"
439                    "\t\t\t\tthe command line"), ONE_DASH },
440   { {"oformat", required_argument, NULL, OPTION_OFORMAT},
441     '\0', N_("TARGET"), N_("Specify target of output file"),
442     EXACTLY_TWO_DASHES },
443   { {"qmagic", no_argument, NULL, OPTION_IGNORE},
444     '\0', NULL, N_("Ignored for Linux compatibility"), ONE_DASH },
445   { {"reduce-memory-overheads", no_argument, NULL,
446      OPTION_REDUCE_MEMORY_OVERHEADS},
447     '\0', NULL, N_("Reduce memory overheads, possibly taking much longer"),
448     TWO_DASHES },
449   { {"relax", no_argument, NULL, OPTION_RELAX},
450     '\0', NULL, N_("Relax branches on certain targets"), TWO_DASHES },
451   { {"retain-symbols-file", required_argument, NULL,
452      OPTION_RETAIN_SYMBOLS_FILE},
453     '\0', N_("FILE"), N_("Keep only symbols listed in FILE"), TWO_DASHES },
454   { {"rpath", required_argument, NULL, OPTION_RPATH},
455     '\0', N_("PATH"), N_("Set runtime shared library search path"), ONE_DASH },
456   { {"rpath-link", required_argument, NULL, OPTION_RPATH_LINK},
457     '\0', N_("PATH"), N_("Set link time shared library search path"),
458     ONE_DASH },
459   { {"shared", no_argument, NULL, OPTION_SHARED},
460     '\0', NULL, N_("Create a shared library"), ONE_DASH },
461   { {"Bshareable", no_argument, NULL, OPTION_SHARED }, /* FreeBSD.  */
462     '\0', NULL, NULL, ONE_DASH },
463   { {"pie", no_argument, NULL, OPTION_PIE},
464     '\0', NULL, N_("Create a position independent executable"), ONE_DASH },
465   { {"pic-executable", no_argument, NULL, OPTION_PIE},
466     '\0', NULL, NULL, TWO_DASHES },
467   { {"sort-common", no_argument, NULL, OPTION_SORT_COMMON},
468     '\0', NULL, N_("Sort common symbols by size"), TWO_DASHES },
469   { {"sort_common", no_argument, NULL, OPTION_SORT_COMMON},
470     '\0', NULL, NULL, NO_HELP },
471   { {"sort-section", required_argument, NULL, OPTION_SORT_SECTION},
472     '\0', N_("name|alignment"), 
473     N_("Sort sections by name or maximum alignment"), TWO_DASHES },
474   { {"spare-dynamic-tags", required_argument, NULL, OPTION_SPARE_DYNAMIC_TAGS},
475     '\0', N_("COUNT"), N_("How many tags to reserve in .dynamic section"),
476     TWO_DASHES },
477   { {"split-by-file", optional_argument, NULL, OPTION_SPLIT_BY_FILE},
478     '\0', N_("[=SIZE]"), N_("Split output sections every SIZE octets"),
479     TWO_DASHES },
480   { {"split-by-reloc", optional_argument, NULL, OPTION_SPLIT_BY_RELOC},
481     '\0', N_("[=COUNT]"), N_("Split output sections every COUNT relocs"),
482     TWO_DASHES },
483   { {"stats", no_argument, NULL, OPTION_STATS},
484     '\0', NULL, N_("Print memory usage statistics"), TWO_DASHES },
485   { {"target-help", no_argument, NULL, OPTION_TARGET_HELP},
486     '\0', NULL, N_("Display target specific options"), TWO_DASHES },
487   { {"task-link", required_argument, NULL, OPTION_TASK_LINK},
488     '\0', N_("SYMBOL"), N_("Do task level linking"), TWO_DASHES },
489   { {"traditional-format", no_argument, NULL, OPTION_TRADITIONAL_FORMAT},
490     '\0', NULL, N_("Use same format as native linker"), TWO_DASHES },
491   { {"section-start", required_argument, NULL, OPTION_SECTION_START},
492     '\0', N_("SECTION=ADDRESS"), N_("Set address of named section"),
493     TWO_DASHES },
494   { {"Tbss", required_argument, NULL, OPTION_TBSS},
495     '\0', N_("ADDRESS"), N_("Set address of .bss section"), ONE_DASH },
496   { {"Tdata", required_argument, NULL, OPTION_TDATA},
497     '\0', N_("ADDRESS"), N_("Set address of .data section"), ONE_DASH },
498   { {"Ttext", required_argument, NULL, OPTION_TTEXT},
499     '\0', N_("ADDRESS"), N_("Set address of .text section"), ONE_DASH },
500   { {"unresolved-symbols=<method>", required_argument, NULL,
501      OPTION_UNRESOLVED_SYMBOLS},
502     '\0', NULL, N_("How to handle unresolved symbols.  <method> is:\n"
503                    "\t\t\t\tignore-all, report-all, ignore-in-object-files,\n"
504                    "\t\t\t\tignore-in-shared-libs"), TWO_DASHES },
505   { {"verbose", no_argument, NULL, OPTION_VERBOSE},
506     '\0', NULL, N_("Output lots of information during link"), TWO_DASHES },
507   { {"dll-verbose", no_argument, NULL, OPTION_VERBOSE}, /* Linux.  */
508     '\0', NULL, NULL, NO_HELP },
509   { {"version-script", required_argument, NULL, OPTION_VERSION_SCRIPT },
510     '\0', N_("FILE"), N_("Read version information script"), TWO_DASHES },
511   { {"version-exports-section", required_argument, NULL,
512      OPTION_VERSION_EXPORTS_SECTION },
513     '\0', N_("SYMBOL"), N_("Take export symbols list from .exports, using\n"
514                            "\t\t\t\tSYMBOL as the version."), TWO_DASHES },
515   { {"dynamic-list-data", no_argument, NULL, OPTION_DYNAMIC_LIST_DATA},
516     '\0', NULL, N_("Add data symbols to dynamic list"), TWO_DASHES },
517   { {"dynamic-list-cpp-new", no_argument, NULL, OPTION_DYNAMIC_LIST_CPP_NEW},
518     '\0', NULL, N_("Use C++ operator new/delete dynamic list"), TWO_DASHES },
519   { {"dynamic-list-cpp-typeinfo", no_argument, NULL, OPTION_DYNAMIC_LIST_CPP_TYPEINFO},
520     '\0', NULL, N_("Use C++ typeinfo dynamic list"), TWO_DASHES },
521   { {"dynamic-list", required_argument, NULL, OPTION_DYNAMIC_LIST},
522     '\0', N_("FILE"), N_("Read dynamic list"), TWO_DASHES },
523   { {"warn-common", no_argument, NULL, OPTION_WARN_COMMON},
524     '\0', NULL, N_("Warn about duplicate common symbols"), TWO_DASHES },
525   { {"warn-constructors", no_argument, NULL, OPTION_WARN_CONSTRUCTORS},
526     '\0', NULL, N_("Warn if global constructors/destructors are seen"),
527     TWO_DASHES },
528   { {"warn-multiple-gp", no_argument, NULL, OPTION_WARN_MULTIPLE_GP},
529     '\0', NULL, N_("Warn if the multiple GP values are used"), TWO_DASHES },
530   { {"warn-once", no_argument, NULL, OPTION_WARN_ONCE},
531     '\0', NULL, N_("Warn only once per undefined symbol"), TWO_DASHES },
532   { {"warn-section-align", no_argument, NULL, OPTION_WARN_SECTION_ALIGN},
533     '\0', NULL, N_("Warn if start of section changes due to alignment"),
534     TWO_DASHES },
535   { {"warn-shared-textrel", no_argument, NULL, OPTION_WARN_SHARED_TEXTREL},
536     '\0', NULL, N_("Warn if shared object has DT_TEXTREL"),
537     TWO_DASHES },
538   { {"warn-unresolved-symbols", no_argument, NULL,
539      OPTION_WARN_UNRESOLVED_SYMBOLS},
540     '\0', NULL, N_("Report unresolved symbols as warnings"), TWO_DASHES },
541   { {"error-unresolved-symbols", no_argument, NULL,
542      OPTION_ERROR_UNRESOLVED_SYMBOLS},
543     '\0', NULL, N_("Report unresolved symbols as errors"), TWO_DASHES },
544   { {"whole-archive", no_argument, NULL, OPTION_WHOLE_ARCHIVE},
545     '\0', NULL, N_("Include all objects from following archives"),
546     TWO_DASHES },
547   { {"wrap", required_argument, NULL, OPTION_WRAP},
548     '\0', N_("SYMBOL"), N_("Use wrapper functions for SYMBOL"), TWO_DASHES },
549 };
550
551 #define OPTION_COUNT ARRAY_SIZE (ld_options)
552
553 void
554 parse_args (unsigned argc, char **argv)
555 {
556   unsigned i;
557   int is, il, irl;
558   int ingroup = 0;
559   char *default_dirlist = NULL;
560   char *shortopts;
561   struct option *longopts;
562   struct option *really_longopts;
563   int last_optind;
564   enum report_method how_to_report_unresolved_symbols = RM_GENERATE_ERROR;
565
566   shortopts = xmalloc (OPTION_COUNT * 3 + 2);
567   longopts = xmalloc (sizeof (*longopts) * (OPTION_COUNT + 1));
568   really_longopts = xmalloc (sizeof (*really_longopts) * (OPTION_COUNT + 1));
569
570   /* Starting the short option string with '-' is for programs that
571      expect options and other ARGV-elements in any order and that care about
572      the ordering of the two.  We describe each non-option ARGV-element
573      as if it were the argument of an option with character code 1.  */
574   shortopts[0] = '-';
575   is = 1;
576   il = 0;
577   irl = 0;
578   for (i = 0; i < OPTION_COUNT; i++)
579     {
580       if (ld_options[i].shortopt != '\0')
581         {
582           shortopts[is] = ld_options[i].shortopt;
583           ++is;
584           if (ld_options[i].opt.has_arg == required_argument
585               || ld_options[i].opt.has_arg == optional_argument)
586             {
587               shortopts[is] = ':';
588               ++is;
589               if (ld_options[i].opt.has_arg == optional_argument)
590                 {
591                   shortopts[is] = ':';
592                   ++is;
593                 }
594             }
595         }
596       if (ld_options[i].opt.name != NULL)
597         {
598           if (ld_options[i].control == EXACTLY_TWO_DASHES)
599             {
600               really_longopts[irl] = ld_options[i].opt;
601               ++irl;
602             }
603           else
604             {
605               longopts[il] = ld_options[i].opt;
606               ++il;
607             }
608         }
609     }
610   shortopts[is] = '\0';
611   longopts[il].name = NULL;
612   really_longopts[irl].name = NULL;
613
614   ldemul_add_options (is, &shortopts, il, &longopts, irl, &really_longopts);
615
616   /* The -G option is ambiguous on different platforms.  Sometimes it
617      specifies the largest data size to put into the small data
618      section.  Sometimes it is equivalent to --shared.  Unfortunately,
619      the first form takes an argument, while the second does not.
620
621      We need to permit the --shared form because on some platforms,
622      such as Solaris, gcc -shared will pass -G to the linker.
623
624      To permit either usage, we look through the argument list.  If we
625      find -G not followed by a number, we change it into --shared.
626      This will work for most normal cases.  */
627   for (i = 1; i < argc; i++)
628     if (strcmp (argv[i], "-G") == 0
629         && (i + 1 >= argc
630             || ! ISDIGIT (argv[i + 1][0])))
631       argv[i] = (char *) "--shared";
632
633   /* Because we permit long options to start with a single dash, and
634      we have a --library option, and the -l option is conventionally
635      used with an immediately following argument, we can have bad
636      results if somebody tries to use -l with a library whose name
637      happens to start with "ibrary", as in -li.  We avoid problems by
638      simply turning -l into --library.  This means that users will
639      have to use two dashes in order to use --library, which is OK
640      since that's how it is documented.
641
642      FIXME: It's possible that this problem can arise for other short
643      options as well, although the user does always have the recourse
644      of adding a space between the option and the argument.  */
645   for (i = 1; i < argc; i++)
646     {
647       if (argv[i][0] == '-'
648           && argv[i][1] == 'l'
649           && argv[i][2] != '\0')
650         {
651           char *n;
652
653           n = xmalloc (strlen (argv[i]) + 20);
654           sprintf (n, "--library=%s", argv[i] + 2);
655           argv[i] = n;
656         }
657     }
658
659   last_optind = -1;
660   while (1)
661     {
662       int longind;
663       int optc;
664
665       /* Using last_optind lets us avoid calling ldemul_parse_args
666          multiple times on a single option, which would lead to
667          confusion in the internal static variables maintained by
668          getopt.  This could otherwise happen for an argument like
669          -nx, in which the -n is parsed as a single option, and we
670          loop around to pick up the -x.  */
671       if (optind != last_optind)
672         if (ldemul_parse_args (argc, argv))
673           continue;
674
675       /* getopt_long_only is like getopt_long, but '-' as well as '--'
676          can indicate a long option.  */
677       opterr = 0;
678       last_optind = optind;
679       optc = getopt_long_only (argc, argv, shortopts, longopts, &longind);
680       if (optc == '?')
681         {
682           optind = last_optind;
683           optc = getopt_long (argc, argv, "-", really_longopts, &longind);
684         }
685
686       if (ldemul_handle_option (optc))
687         continue;
688
689       if (optc == -1)
690         break;
691
692       switch (optc)
693         {
694         case '?':
695           einfo (_("%P: unrecognized option '%s'\n"), argv[last_optind]);
696           /* Fall through.  */
697
698         default:
699           einfo (_("%P%F: use the --help option for usage information\n"));
700
701         case 1:                 /* File name.  */
702           lang_add_input_file (optarg, lang_input_file_is_file_enum, NULL);
703           break;
704
705         case OPTION_IGNORE:
706           break;
707         case 'a':
708           /* For HP/UX compatibility.  Actually -a shared should mean
709              ``use only shared libraries'' but, then, we don't
710              currently support shared libraries on HP/UX anyhow.  */
711           if (strcmp (optarg, "archive") == 0)
712             config.dynamic_link = FALSE;
713           else if (strcmp (optarg, "shared") == 0
714                    || strcmp (optarg, "default") == 0)
715             config.dynamic_link = TRUE;
716           else
717             einfo (_("%P%F: unrecognized -a option `%s'\n"), optarg);
718           break;
719         case OPTION_ASSERT:
720           /* FIXME: We just ignore these, but we should handle them.  */
721           if (strcmp (optarg, "definitions") == 0)
722             ;
723           else if (strcmp (optarg, "nodefinitions") == 0)
724             ;
725           else if (strcmp (optarg, "nosymbolic") == 0)
726             ;
727           else if (strcmp (optarg, "pure-text") == 0)
728             ;
729           else
730             einfo (_("%P%F: unrecognized -assert option `%s'\n"), optarg);
731           break;
732         case 'A':
733           ldfile_add_arch (optarg);
734           break;
735         case 'b':
736           lang_add_target (optarg);
737           break;
738         case 'c':
739           ldfile_open_command_file (optarg);
740           parser_input = input_mri_script;
741           yyparse ();
742           break;
743         case OPTION_CALL_SHARED:
744           config.dynamic_link = TRUE;
745           break;
746         case OPTION_NON_SHARED:
747           config.dynamic_link = FALSE;
748           break;
749         case OPTION_CREF:
750           command_line.cref = TRUE;
751           link_info.notice_all = TRUE;
752           break;
753         case 'd':
754           command_line.force_common_definition = TRUE;
755           break;
756         case OPTION_DEFSYM:
757           lex_string = optarg;
758           lex_redirect (optarg);
759           parser_input = input_defsym;
760           parsing_defsym = 1;
761           yyparse ();
762           parsing_defsym = 0;
763           lex_string = NULL;
764           break;
765         case OPTION_DEMANGLE:
766           demangling = TRUE;
767           if (optarg != NULL)
768             {
769               enum demangling_styles style;
770
771               style = cplus_demangle_name_to_style (optarg);
772               if (style == unknown_demangling)
773                 einfo (_("%F%P: unknown demangling style `%s'"),
774                        optarg);
775
776               cplus_demangle_set_style (style);
777             }
778           break;
779         case 'I':               /* Used on Solaris.  */
780         case OPTION_DYNAMIC_LINKER:
781           command_line.interpreter = optarg;
782           break;
783         case OPTION_SYSROOT:
784           /* Already handled in ldmain.c.  */
785           break;
786         case OPTION_EB:
787           command_line.endian = ENDIAN_BIG;
788           break;
789         case OPTION_EL:
790           command_line.endian = ENDIAN_LITTLE;
791           break;
792         case OPTION_EMBEDDED_RELOCS:
793           command_line.embedded_relocs = TRUE;
794           break;
795         case OPTION_EXPORT_DYNAMIC:
796         case 'E': /* HP/UX compatibility.  */
797           link_info.export_dynamic = TRUE;
798           break;
799         case 'e':
800           lang_add_entry (optarg, TRUE);
801           break;
802         case 'f':
803           if (command_line.auxiliary_filters == NULL)
804             {
805               command_line.auxiliary_filters = xmalloc (2 * sizeof (char *));
806               command_line.auxiliary_filters[0] = optarg;
807               command_line.auxiliary_filters[1] = NULL;
808             }
809           else
810             {
811               int c;
812               char **p;
813
814               c = 0;
815               for (p = command_line.auxiliary_filters; *p != NULL; p++)
816                 ++c;
817               command_line.auxiliary_filters
818                 = xrealloc (command_line.auxiliary_filters,
819                             (c + 2) * sizeof (char *));
820               command_line.auxiliary_filters[c] = optarg;
821               command_line.auxiliary_filters[c + 1] = NULL;
822             }
823           break;
824         case 'F':
825           command_line.filter_shlib = optarg;
826           break;
827         case OPTION_FORCE_EXE_SUFFIX:
828           command_line.force_exe_suffix = TRUE;
829           break;
830         case 'G':
831           {
832             char *end;
833             g_switch_value = strtoul (optarg, &end, 0);
834             if (*end)
835               einfo (_("%P%F: invalid number `%s'\n"), optarg);
836           }
837           break;
838         case 'g':
839           /* Ignore.  */
840           break;
841         case OPTION_GC_SECTIONS:
842           link_info.gc_sections = TRUE;
843           break;
844         case OPTION_PRINT_GC_SECTIONS:
845           link_info.print_gc_sections = TRUE;
846           break;
847         case OPTION_HELP:
848           help ();
849           xexit (0);
850           break;
851         case 'L':
852           ldfile_add_library_path (optarg, TRUE);
853           break;
854         case 'l':
855           lang_add_input_file (optarg, lang_input_file_is_l_enum, NULL);
856           break;
857         case 'M':
858           config.map_filename = "-";
859           break;
860         case 'm':
861           /* Ignore.  Was handled in a pre-parse.   */
862           break;
863         case OPTION_MAP:
864           config.map_filename = optarg;
865           break;
866         case 'N':
867           config.text_read_only = FALSE;
868           config.magic_demand_paged = FALSE;
869           config.dynamic_link = FALSE;
870           break;
871         case OPTION_NO_OMAGIC:
872           config.text_read_only = TRUE;
873           config.magic_demand_paged = TRUE;
874           /* NB/ Does not set dynamic_link to TRUE.
875              Use --call-shared or -Bdynamic for this.  */
876           break;
877         case 'n':
878           config.magic_demand_paged = FALSE;
879           config.dynamic_link = FALSE;
880           break;
881         case OPTION_NO_DEFINE_COMMON:
882           command_line.inhibit_common_definition = TRUE;
883           break;
884         case OPTION_NO_DEMANGLE:
885           demangling = FALSE;
886           break;
887         case OPTION_NO_GC_SECTIONS:
888           link_info.gc_sections = FALSE;
889           break;
890         case OPTION_NO_PRINT_GC_SECTIONS:
891           link_info.print_gc_sections = FALSE;
892           break;
893         case OPTION_NO_KEEP_MEMORY:
894           link_info.keep_memory = FALSE;
895           break;
896         case OPTION_NO_UNDEFINED:
897           link_info.unresolved_syms_in_objects
898             = how_to_report_unresolved_symbols;
899           break;
900         case OPTION_ALLOW_SHLIB_UNDEFINED:
901           link_info.unresolved_syms_in_shared_libs = RM_IGNORE;
902           break;
903         case OPTION_NO_ALLOW_SHLIB_UNDEFINED:
904           link_info.unresolved_syms_in_shared_libs
905             = how_to_report_unresolved_symbols;
906           break;
907         case OPTION_UNRESOLVED_SYMBOLS:
908           if (strcmp (optarg, "ignore-all") == 0)
909             {
910               link_info.unresolved_syms_in_objects = RM_IGNORE;
911               link_info.unresolved_syms_in_shared_libs = RM_IGNORE;
912             }
913           else if (strcmp (optarg, "report-all") == 0)
914             {
915               link_info.unresolved_syms_in_objects
916                 = how_to_report_unresolved_symbols;
917               link_info.unresolved_syms_in_shared_libs
918                 = how_to_report_unresolved_symbols;
919             }
920           else if (strcmp (optarg, "ignore-in-object-files") == 0)
921             {
922               link_info.unresolved_syms_in_objects = RM_IGNORE;
923               link_info.unresolved_syms_in_shared_libs
924                 = how_to_report_unresolved_symbols;
925             }
926           else if (strcmp (optarg, "ignore-in-shared-libs") == 0)
927             {
928               link_info.unresolved_syms_in_objects
929                 = how_to_report_unresolved_symbols;
930               link_info.unresolved_syms_in_shared_libs = RM_IGNORE;
931             }
932           else
933             einfo (_("%P%F: bad --unresolved-symbols option: %s\n"), optarg);
934           break;
935         case OPTION_WARN_UNRESOLVED_SYMBOLS:
936           how_to_report_unresolved_symbols = RM_GENERATE_WARNING;
937           if (link_info.unresolved_syms_in_objects == RM_GENERATE_ERROR)
938             link_info.unresolved_syms_in_objects = RM_GENERATE_WARNING;
939           if (link_info.unresolved_syms_in_shared_libs == RM_GENERATE_ERROR)
940             link_info.unresolved_syms_in_shared_libs = RM_GENERATE_WARNING;
941           break;
942
943         case OPTION_ERROR_UNRESOLVED_SYMBOLS:
944           how_to_report_unresolved_symbols = RM_GENERATE_ERROR;
945           if (link_info.unresolved_syms_in_objects == RM_GENERATE_WARNING)
946             link_info.unresolved_syms_in_objects = RM_GENERATE_ERROR;
947           if (link_info.unresolved_syms_in_shared_libs == RM_GENERATE_WARNING)
948             link_info.unresolved_syms_in_shared_libs = RM_GENERATE_ERROR;
949           break;
950         case OPTION_ALLOW_MULTIPLE_DEFINITION:
951           link_info.allow_multiple_definition = TRUE;
952           break;
953         case OPTION_NO_UNDEFINED_VERSION:
954           link_info.allow_undefined_version = FALSE;
955           break;
956         case OPTION_DEFAULT_SYMVER:
957           link_info.create_default_symver = TRUE;
958           break;
959         case OPTION_DEFAULT_IMPORTED_SYMVER:
960           link_info.default_imported_symver = TRUE;
961           break;
962         case OPTION_NO_WARN_MISMATCH:
963           command_line.warn_mismatch = FALSE;
964           break;
965         case OPTION_NOINHIBIT_EXEC:
966           force_make_executable = TRUE;
967           break;
968         case OPTION_NOSTDLIB:
969           config.only_cmd_line_lib_dirs = TRUE;
970           break;
971         case OPTION_NO_WHOLE_ARCHIVE:
972           whole_archive = FALSE;
973           break;
974         case 'O':
975           /* FIXME "-O<non-digits> <value>" used to set the address of
976              section <non-digits>.  Was this for compatibility with
977              something, or can we create a new option to do that
978              (with a syntax similar to -defsym)?
979              getopt can't handle two args to an option without kludges.  */
980
981           /* Enable optimizations of output files.  */
982           link_info.optimize = strtoul (optarg, NULL, 0) ? TRUE : FALSE;
983           break;
984         case 'o':
985           lang_add_output (optarg, 0);
986           break;
987         case OPTION_OFORMAT:
988           lang_add_output_format (optarg, NULL, NULL, 0);
989           break;
990         case 'q':
991           link_info.emitrelocations = TRUE;
992           break;
993         case 'i':
994         case 'r':
995           if (optind == last_optind)
996             /* This can happen if the user put "-rpath,a" on the command
997                line.  (Or something similar.  The comma is important).
998                Getopt becomes confused and thinks that this is a -r option
999                but it cannot parse the text after the -r so it refuses to
1000                increment the optind counter.  Detect this case and issue
1001                an error message here.  We cannot just make this a warning,
1002                increment optind, and continue because getopt is too confused
1003                and will seg-fault the next time around.  */
1004             einfo(_("%P%F: bad -rpath option\n"));
1005
1006           link_info.relocatable = TRUE;
1007           config.build_constructors = FALSE;
1008           config.magic_demand_paged = FALSE;
1009           config.text_read_only = FALSE;
1010           config.dynamic_link = FALSE;
1011           break;
1012         case 'R':
1013           /* The GNU linker traditionally uses -R to mean to include
1014              only the symbols from a file.  The Solaris linker uses -R
1015              to set the path used by the runtime linker to find
1016              libraries.  This is the GNU linker -rpath argument.  We
1017              try to support both simultaneously by checking the file
1018              named.  If it is a directory, rather than a regular file,
1019              we assume -rpath was meant.  */
1020           {
1021             struct stat s;
1022
1023             if (stat (optarg, &s) >= 0
1024                 && ! S_ISDIR (s.st_mode))
1025               {
1026                 lang_add_input_file (optarg,
1027                                      lang_input_file_is_symbols_only_enum,
1028                                      NULL);
1029                 break;
1030               }
1031           }
1032           /* Fall through.  */
1033         case OPTION_RPATH:
1034           if (command_line.rpath == NULL)
1035             command_line.rpath = xstrdup (optarg);
1036           else
1037             {
1038               size_t rpath_len = strlen (command_line.rpath);
1039               size_t optarg_len = strlen (optarg);
1040               char *buf;
1041               char *cp = command_line.rpath;
1042
1043               /* First see whether OPTARG is already in the path.  */
1044               do
1045                 {
1046                   size_t idx = 0;
1047
1048                   while (optarg[idx] != '\0' && optarg[idx] == cp[idx])
1049                     ++idx;
1050                   if (optarg[idx] == '\0'
1051                       && (cp[idx] == '\0' || cp[idx] == ':'))
1052                     /* We found it.  */
1053                     break;
1054
1055                   /* Not yet found.  */
1056                   cp = strchr (cp, ':');
1057                   if (cp != NULL)
1058                     ++cp;
1059                 }
1060               while (cp != NULL);
1061
1062               if (cp == NULL)
1063                 {
1064                   buf = xmalloc (rpath_len + optarg_len + 2);
1065                   sprintf (buf, "%s:%s", command_line.rpath, optarg);
1066                   free (command_line.rpath);
1067                   command_line.rpath = buf;
1068                 }
1069             }
1070           break;
1071         case OPTION_RPATH_LINK:
1072           if (command_line.rpath_link == NULL)
1073             command_line.rpath_link = xstrdup (optarg);
1074           else
1075             {
1076               char *buf;
1077
1078               buf = xmalloc (strlen (command_line.rpath_link)
1079                              + strlen (optarg)
1080                              + 2);
1081               sprintf (buf, "%s:%s", command_line.rpath_link, optarg);
1082               free (command_line.rpath_link);
1083               command_line.rpath_link = buf;
1084             }
1085           break;
1086         case OPTION_RELAX:
1087           command_line.relax = TRUE;
1088           break;
1089         case OPTION_RETAIN_SYMBOLS_FILE:
1090           add_keepsyms_file (optarg);
1091           break;
1092         case 'S':
1093           link_info.strip = strip_debugger;
1094           break;
1095         case 's':
1096           link_info.strip = strip_all;
1097           break;
1098         case OPTION_STRIP_DISCARDED:
1099           link_info.strip_discarded = TRUE;
1100           break;
1101         case OPTION_NO_STRIP_DISCARDED:
1102           link_info.strip_discarded = FALSE;
1103           break;
1104         case OPTION_SHARED:
1105           if (config.has_shared)
1106             {
1107               link_info.shared = TRUE;
1108               /* When creating a shared library, the default
1109                  behaviour is to ignore any unresolved references.  */
1110               if (link_info.unresolved_syms_in_objects == RM_NOT_YET_SET)
1111                 link_info.unresolved_syms_in_objects = RM_IGNORE;
1112               if (link_info.unresolved_syms_in_shared_libs == RM_NOT_YET_SET)
1113                 link_info.unresolved_syms_in_shared_libs = RM_IGNORE;
1114             }
1115           else
1116             einfo (_("%P%F: -shared not supported\n"));
1117           break;
1118         case OPTION_PIE:
1119           if (config.has_shared)
1120             {
1121               link_info.shared = TRUE;
1122               link_info.pie = TRUE;
1123             }
1124           else
1125             einfo (_("%P%F: -pie not supported\n"));
1126           break;
1127         case 'h':               /* Used on Solaris.  */
1128         case OPTION_SONAME:
1129           command_line.soname = optarg;
1130           break;
1131         case OPTION_SORT_COMMON:
1132           config.sort_common = TRUE;
1133           break;
1134         case OPTION_SORT_SECTION:
1135           if (strcmp (optarg, N_("name")) == 0)
1136             sort_section = by_name;
1137           else if (strcmp (optarg, N_("alignment")) == 0)
1138             sort_section = by_alignment;
1139           else
1140             einfo (_("%P%F: invalid section sorting option: %s\n"),
1141                    optarg);
1142           break;
1143         case OPTION_STATS:
1144           config.stats = TRUE;
1145           break;
1146         case OPTION_SYMBOLIC:
1147           link_info.symbolic = TRUE;
1148           break;
1149         case 't':
1150           trace_files = TRUE;
1151           break;
1152         case 'T':
1153           ldfile_open_command_file (optarg);
1154           parser_input = input_script;
1155           yyparse ();
1156           break;
1157         case OPTION_DEFAULT_SCRIPT:
1158           command_line.default_script = optarg;
1159           break;
1160         case OPTION_SECTION_START:
1161           {
1162             char *optarg2;
1163             char *sec_name;
1164             int len;
1165
1166             /* Check for <something>=<somthing>...  */
1167             optarg2 = strchr (optarg, '=');
1168             if (optarg2 == NULL)
1169               einfo (_("%P%F: invalid argument to option"
1170                        " \"--section-start\"\n"));
1171
1172             optarg2++;
1173
1174             /* So far so good.  Are all the args present?  */
1175             if ((*optarg == '\0') || (*optarg2 == '\0'))
1176               einfo (_("%P%F: missing argument(s) to option"
1177                        " \"--section-start\"\n"));
1178
1179             /* We must copy the section name as set_section_start
1180                doesn't do it for us.  */
1181             len = optarg2 - optarg;
1182             sec_name = xmalloc (len);
1183             memcpy (sec_name, optarg, len - 1);
1184             sec_name[len - 1] = 0;
1185
1186             /* Then set it...  */
1187             set_section_start (sec_name, optarg2);
1188           }
1189           break;
1190         case OPTION_TARGET_HELP:
1191           /* Mention any target specific options.  */
1192           ldemul_list_emulation_options (stdout);
1193           exit (0);
1194         case OPTION_TBSS:
1195           set_segment_start (".bss", optarg);
1196           break;
1197         case OPTION_TDATA:
1198           set_segment_start (".data", optarg);
1199           break;
1200         case OPTION_TTEXT:
1201           set_segment_start (".text", optarg);
1202           break;
1203         case OPTION_TRADITIONAL_FORMAT:
1204           link_info.traditional_format = TRUE;
1205           break;
1206         case OPTION_TASK_LINK:
1207           link_info.task_link = TRUE;
1208           /* Fall through - do an implied -r option.  */
1209         case OPTION_UR:
1210           link_info.relocatable = TRUE;
1211           config.build_constructors = TRUE;
1212           config.magic_demand_paged = FALSE;
1213           config.text_read_only = FALSE;
1214           config.dynamic_link = FALSE;
1215           break;
1216         case 'u':
1217           ldlang_add_undef (optarg);
1218           break;
1219         case OPTION_UNIQUE:
1220           if (optarg != NULL)
1221             lang_add_unique (optarg);
1222           else
1223             config.unique_orphan_sections = TRUE;
1224           break;
1225         case OPTION_VERBOSE:
1226           ldversion (1);
1227           version_printed = TRUE;
1228           trace_file_tries = TRUE;
1229           overflow_cutoff_limit = -2;
1230           break;
1231         case 'v':
1232           ldversion (0);
1233           version_printed = TRUE;
1234           break;
1235         case 'V':
1236           ldversion (1);
1237           version_printed = TRUE;
1238           break;
1239         case OPTION_VERSION:
1240           ldversion (2);
1241           xexit (0);
1242           break;
1243         case OPTION_VERSION_SCRIPT:
1244           /* This option indicates a small script that only specifies
1245              version information.  Read it, but don't assume that
1246              we've seen a linker script.  */
1247           {
1248             FILE *hold_script_handle;
1249
1250             hold_script_handle = saved_script_handle;
1251             ldfile_open_command_file (optarg);
1252             saved_script_handle = hold_script_handle;
1253             parser_input = input_version_script;
1254             yyparse ();
1255           }
1256           break;
1257         case OPTION_VERSION_EXPORTS_SECTION:
1258           /* This option records a version symbol to be applied to the
1259              symbols listed for export to be found in the object files
1260              .exports sections.  */
1261           command_line.version_exports_section = optarg;
1262           break;
1263         case OPTION_DYNAMIC_LIST_DATA:
1264           link_info.dynamic_data = TRUE;
1265           link_info.dynamic = TRUE;
1266           break;
1267         case OPTION_DYNAMIC_LIST_CPP_TYPEINFO:
1268           lang_append_dynamic_list_cpp_typeinfo ();
1269           link_info.dynamic = TRUE;
1270           break;
1271         case OPTION_DYNAMIC_LIST_CPP_NEW:
1272           lang_append_dynamic_list_cpp_new ();
1273           link_info.dynamic = TRUE;
1274           break;
1275         case OPTION_DYNAMIC_LIST:
1276           /* This option indicates a small script that only specifies
1277              a dynamic list.  Read it, but don't assume that we've
1278              seen a linker script.  */
1279           {
1280             FILE *hold_script_handle;
1281
1282             hold_script_handle = saved_script_handle;
1283             ldfile_open_command_file (optarg);
1284             saved_script_handle = hold_script_handle;
1285             parser_input = input_dynamic_list;
1286             yyparse ();
1287           }
1288           link_info.dynamic = TRUE;
1289           break;
1290         case OPTION_WARN_COMMON:
1291           config.warn_common = TRUE;
1292           break;
1293         case OPTION_WARN_CONSTRUCTORS:
1294           config.warn_constructors = TRUE;
1295           break;
1296         case OPTION_WARN_FATAL:
1297           config.fatal_warnings = TRUE;
1298           break;
1299         case OPTION_WARN_MULTIPLE_GP:
1300           config.warn_multiple_gp = TRUE;
1301           break;
1302         case OPTION_WARN_ONCE:
1303           config.warn_once = TRUE;
1304           break;
1305         case OPTION_WARN_SECTION_ALIGN:
1306           config.warn_section_align = TRUE;
1307           break;
1308         case OPTION_WARN_SHARED_TEXTREL:
1309           link_info.warn_shared_textrel = TRUE;
1310           break;
1311         case OPTION_WHOLE_ARCHIVE:
1312           whole_archive = TRUE;
1313           break;
1314         case OPTION_ADD_NEEDED:
1315           add_needed = TRUE;
1316           break;
1317         case OPTION_NO_ADD_NEEDED:
1318           add_needed = FALSE;
1319           break;
1320         case OPTION_AS_NEEDED:
1321           as_needed = TRUE;
1322           break;
1323         case OPTION_NO_AS_NEEDED:
1324           as_needed = FALSE;
1325           break;
1326         case OPTION_WRAP:
1327           add_wrap (optarg);
1328           break;
1329         case OPTION_DISCARD_NONE:
1330           link_info.discard = discard_none;
1331           break;
1332         case 'X':
1333           link_info.discard = discard_l;
1334           break;
1335         case 'x':
1336           link_info.discard = discard_all;
1337           break;
1338         case 'Y':
1339           if (CONST_STRNEQ (optarg, "P,"))
1340             optarg += 2;
1341           if (default_dirlist != NULL)
1342             free (default_dirlist);
1343           default_dirlist = xstrdup (optarg);
1344           break;
1345         case 'y':
1346           add_ysym (optarg);
1347           break;
1348         case OPTION_SPARE_DYNAMIC_TAGS:
1349           link_info.spare_dynamic_tags = strtoul (optarg, NULL, 0);
1350           break;
1351         case OPTION_SPLIT_BY_RELOC:
1352           if (optarg != NULL)
1353             config.split_by_reloc = strtoul (optarg, NULL, 0);
1354           else
1355             config.split_by_reloc = 32768;
1356           break;
1357         case OPTION_SPLIT_BY_FILE:
1358           if (optarg != NULL)
1359             config.split_by_file = bfd_scan_vma (optarg, NULL, 0);
1360           else
1361             config.split_by_file = 1;
1362           break;
1363         case OPTION_CHECK_SECTIONS:
1364           command_line.check_section_addresses = TRUE;
1365           break;
1366         case OPTION_NO_CHECK_SECTIONS:
1367           command_line.check_section_addresses = FALSE;
1368           break;
1369         case OPTION_ACCEPT_UNKNOWN_INPUT_ARCH:
1370           command_line.accept_unknown_input_arch = TRUE;
1371           break;
1372         case OPTION_NO_ACCEPT_UNKNOWN_INPUT_ARCH:
1373           command_line.accept_unknown_input_arch = FALSE;
1374           break;
1375         case '(':
1376           if (ingroup)
1377             einfo (_("%P%F: may not nest groups (--help for usage)\n"));
1378
1379           lang_enter_group ();
1380           ingroup = 1;
1381           break;
1382         case ')':
1383           if (! ingroup)
1384             einfo (_("%P%F: group ended before it began (--help for usage)\n"));
1385
1386           lang_leave_group ();
1387           ingroup = 0;
1388           break;
1389
1390         case OPTION_INIT:
1391           link_info.init_function = optarg;
1392           break;
1393
1394         case OPTION_FINI:
1395           link_info.fini_function = optarg;
1396           break;
1397
1398         case OPTION_REDUCE_MEMORY_OVERHEADS:
1399           link_info.reduce_memory_overheads = TRUE;
1400           if (config.hash_table_size == 0)
1401             config.hash_table_size = 1021;
1402           break;
1403
1404         case OPTION_HASH_SIZE:
1405           {
1406             bfd_size_type new_size;
1407
1408             new_size = strtoul (optarg, NULL, 0);
1409             if (new_size)
1410               config.hash_table_size = new_size;
1411             else
1412               einfo (_("%P%X: --hash-size needs a numeric argument\n"));
1413           }
1414           break;
1415         }
1416     }
1417
1418   if (ingroup)
1419     lang_leave_group ();
1420
1421   if (default_dirlist != NULL)
1422     {
1423       set_default_dirlist (default_dirlist);
1424       free (default_dirlist);
1425     }
1426
1427   if (link_info.unresolved_syms_in_objects == RM_NOT_YET_SET)
1428     /* FIXME: Should we allow emulations a chance to set this ?  */
1429     link_info.unresolved_syms_in_objects = how_to_report_unresolved_symbols;
1430
1431   if (link_info.unresolved_syms_in_shared_libs == RM_NOT_YET_SET)
1432     /* FIXME: Should we allow emulations a chance to set this ?  */
1433     link_info.unresolved_syms_in_shared_libs = how_to_report_unresolved_symbols;
1434 }
1435
1436 /* Add the (colon-separated) elements of DIRLIST_PTR to the
1437    library search path.  */
1438
1439 static void
1440 set_default_dirlist (char *dirlist_ptr)
1441 {
1442   char *p;
1443
1444   while (1)
1445     {
1446       p = strchr (dirlist_ptr, PATH_SEPARATOR);
1447       if (p != NULL)
1448         *p = '\0';
1449       if (*dirlist_ptr != '\0')
1450         ldfile_add_library_path (dirlist_ptr, TRUE);
1451       if (p == NULL)
1452         break;
1453       dirlist_ptr = p + 1;
1454     }
1455 }
1456
1457 static void
1458 set_section_start (char *sect, char *valstr)
1459 {
1460   const char *end;
1461   bfd_vma val = bfd_scan_vma (valstr, &end, 16);
1462   if (*end)
1463     einfo (_("%P%F: invalid hex number `%s'\n"), valstr);
1464   lang_section_start (sect, exp_intop (val), NULL);
1465 }
1466
1467 static void
1468 set_segment_start (const char *section, char *valstr)
1469 {
1470   const char *name;
1471   const char *end;
1472   segment_type *seg;
1473
1474   bfd_vma val = bfd_scan_vma (valstr, &end, 16);
1475   if (*end)
1476     einfo (_("%P%F: invalid hex number `%s'\n"), valstr);
1477   /* If we already have an entry for this segment, update the existing
1478      value.  */
1479   name = section + 1;
1480   for (seg = segments; seg; seg = seg->next)
1481     if (strcmp (seg->name, name) == 0)
1482       {
1483         seg->value = val;
1484         return;
1485       }
1486   /* There was no existing value so we must create a new segment
1487      entry.  */
1488   seg = stat_alloc (sizeof (*seg));
1489   seg->name = name;
1490   seg->value = val;
1491   seg->used = FALSE;
1492   /* Add it to the linked list of segments.  */
1493   seg->next = segments;
1494   segments = seg;
1495   /* Historically, -Ttext and friends set the base address of a
1496      particular section.  For backwards compatibility, we still do
1497      that.  If a SEGMENT_START directive is seen, the section address
1498      assignment will be disabled.  */
1499   lang_section_start (section, exp_intop (val), seg);
1500 }
1501
1502 \f
1503 /* Print help messages for the options.  */
1504
1505 static void
1506 help (void)
1507 {
1508   unsigned i;
1509   const char **targets, **pp;
1510   int len;
1511
1512   printf (_("Usage: %s [options] file...\n"), program_name);
1513
1514   printf (_("Options:\n"));
1515   for (i = 0; i < OPTION_COUNT; i++)
1516     {
1517       if (ld_options[i].doc != NULL)
1518         {
1519           bfd_boolean comma;
1520           unsigned j;
1521
1522           printf ("  ");
1523
1524           comma = FALSE;
1525           len = 2;
1526
1527           j = i;
1528           do
1529             {
1530               if (ld_options[j].shortopt != '\0'
1531                   && ld_options[j].control != NO_HELP)
1532                 {
1533                   printf ("%s-%c", comma ? ", " : "", ld_options[j].shortopt);
1534                   len += (comma ? 2 : 0) + 2;
1535                   if (ld_options[j].arg != NULL)
1536                     {
1537                       if (ld_options[j].opt.has_arg != optional_argument)
1538                         {
1539                           printf (" ");
1540                           ++len;
1541                         }
1542                       printf ("%s", _(ld_options[j].arg));
1543                       len += strlen (_(ld_options[j].arg));
1544                     }
1545                   comma = TRUE;
1546                 }
1547               ++j;
1548             }
1549           while (j < OPTION_COUNT && ld_options[j].doc == NULL);
1550
1551           j = i;
1552           do
1553             {
1554               if (ld_options[j].opt.name != NULL
1555                   && ld_options[j].control != NO_HELP)
1556                 {
1557                   int two_dashes =
1558                     (ld_options[j].control == TWO_DASHES
1559                      || ld_options[j].control == EXACTLY_TWO_DASHES);
1560
1561                   printf ("%s-%s%s",
1562                           comma ? ", " : "",
1563                           two_dashes ? "-" : "",
1564                           ld_options[j].opt.name);
1565                   len += ((comma ? 2 : 0)
1566                           + 1
1567                           + (two_dashes ? 1 : 0)
1568                           + strlen (ld_options[j].opt.name));
1569                   if (ld_options[j].arg != NULL)
1570                     {
1571                       printf (" %s", _(ld_options[j].arg));
1572                       len += 1 + strlen (_(ld_options[j].arg));
1573                     }
1574                   comma = TRUE;
1575                 }
1576               ++j;
1577             }
1578           while (j < OPTION_COUNT && ld_options[j].doc == NULL);
1579
1580           if (len >= 30)
1581             {
1582               printf ("\n");
1583               len = 0;
1584             }
1585
1586           for (; len < 30; len++)
1587             putchar (' ');
1588
1589           printf ("%s\n", _(ld_options[i].doc));
1590         }
1591     }
1592   printf (_("  @FILE"));
1593   for (len = strlen ("  @FILE"); len < 30; len++)
1594     putchar (' ');
1595   printf (_("Read options from FILE\n"));
1596
1597   /* Note: Various tools (such as libtool) depend upon the
1598      format of the listings below - do not change them.  */
1599   /* xgettext:c-format */
1600   printf (_("%s: supported targets:"), program_name);
1601   targets = bfd_target_list ();
1602   for (pp = targets; *pp != NULL; pp++)
1603     printf (" %s", *pp);
1604   free (targets);
1605   printf ("\n");
1606
1607   /* xgettext:c-format */
1608   printf (_("%s: supported emulations: "), program_name);
1609   ldemul_list_emulations (stdout);
1610   printf ("\n");
1611
1612   /* xgettext:c-format */
1613   printf (_("%s: emulation specific options:\n"), program_name);
1614   ldemul_list_emulation_options (stdout);
1615   printf ("\n");
1616
1617   printf (_("Report bugs to %s\n"), REPORT_BUGS_TO);
1618 }