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