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