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