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