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