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