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