xref: /openbsd/gnu/usr.bin/binutils-2.17/ld/lexsup.c (revision 73471bf0)
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 	  break;
726 	case OPTION_CREF:
727 	  command_line.cref = TRUE;
728 	  link_info.notice_all = TRUE;
729 	  break;
730 	case 'd':
731 	  command_line.force_common_definition = TRUE;
732 	  break;
733 	case OPTION_DEFSYM:
734 	  lex_string = optarg;
735 	  lex_redirect (optarg);
736 	  parser_input = input_defsym;
737 	  parsing_defsym = 1;
738 	  yyparse ();
739 	  parsing_defsym = 0;
740 	  lex_string = NULL;
741 	  break;
742 	case OPTION_DEMANGLE:
743 	  demangling = TRUE;
744 	  if (optarg != NULL)
745 	    {
746 	      enum demangling_styles style;
747 
748 	      style = cplus_demangle_name_to_style (optarg);
749 	      if (style == unknown_demangling)
750 		einfo (_("%F%P: unknown demangling style `%s'"),
751 		       optarg);
752 
753 	      cplus_demangle_set_style (style);
754 	    }
755 	  break;
756 	case 'I':		/* Used on Solaris.  */
757 	case OPTION_DYNAMIC_LINKER:
758 	  command_line.interpreter = optarg;
759 	  break;
760 	case OPTION_SYSROOT:
761 	  /* Already handled in ldmain.c.  */
762 	  break;
763 	case OPTION_EB:
764 	  command_line.endian = ENDIAN_BIG;
765 	  break;
766 	case OPTION_EL:
767 	  command_line.endian = ENDIAN_LITTLE;
768 	  break;
769 	case OPTION_EMBEDDED_RELOCS:
770 	  command_line.embedded_relocs = TRUE;
771 	  break;
772 	case OPTION_EXPORT_DYNAMIC:
773 	case 'E': /* HP/UX compatibility.  */
774 	  link_info.export_dynamic = TRUE;
775 	  break;
776 	case 'e':
777 	  lang_add_entry (optarg, TRUE);
778 	  break;
779 	case 'f':
780 	  if (command_line.auxiliary_filters == NULL)
781 	    {
782 	      command_line.auxiliary_filters = xmalloc (2 * sizeof (char *));
783 	      command_line.auxiliary_filters[0] = optarg;
784 	      command_line.auxiliary_filters[1] = NULL;
785 	    }
786 	  else
787 	    {
788 	      int c;
789 	      char **p;
790 
791 	      c = 0;
792 	      for (p = command_line.auxiliary_filters; *p != NULL; p++)
793 		++c;
794 	      command_line.auxiliary_filters
795 		= xrealloc (command_line.auxiliary_filters,
796 			    (c + 2) * sizeof (char *));
797 	      command_line.auxiliary_filters[c] = optarg;
798 	      command_line.auxiliary_filters[c + 1] = NULL;
799 	    }
800 	  break;
801 	case 'F':
802 	  command_line.filter_shlib = optarg;
803 	  break;
804 	case OPTION_FORCE_EXE_SUFFIX:
805 	  command_line.force_exe_suffix = TRUE;
806 	  break;
807 	case 'G':
808 	  {
809 	    char *end;
810 	    g_switch_value = strtoul (optarg, &end, 0);
811 	    if (*end)
812 	      einfo (_("%P%F: invalid number `%s'\n"), optarg);
813 	  }
814 	  break;
815 	case 'g':
816 	  /* Ignore.  */
817 	  break;
818 	case OPTION_GC_SECTIONS:
819 	  link_info.gc_sections = TRUE;
820 	  break;
821 	case OPTION_HELP:
822 	  help ();
823 	  xexit (0);
824 	  break;
825 	case 'L':
826 	  ldfile_add_library_path (optarg, TRUE);
827 	  break;
828 	case 'l':
829 	  lang_add_input_file (optarg, lang_input_file_is_l_enum, NULL);
830 	  break;
831 	case 'M':
832 	  config.map_filename = "-";
833 	  break;
834 	case 'm':
835 	  /* Ignore.  Was handled in a pre-parse.   */
836 	  break;
837 	case OPTION_MAP:
838 	  config.map_filename = optarg;
839 	  break;
840 	case 'N':
841 	  config.text_read_only = FALSE;
842 	  config.magic_demand_paged = FALSE;
843 	  config.dynamic_link = FALSE;
844 	  break;
845 	case OPTION_NO_OMAGIC:
846 	  config.text_read_only = TRUE;
847 	  config.magic_demand_paged = TRUE;
848 	  /* NB/ Does not set dynamic_link to TRUE.
849 	     Use --call-shared or -Bdynamic for this.  */
850 	  break;
851 	case 'n':
852 	  config.magic_demand_paged = FALSE;
853 	  config.dynamic_link = FALSE;
854 	  break;
855 	case OPTION_NO_DEFINE_COMMON:
856 	  command_line.inhibit_common_definition = TRUE;
857 	  break;
858 	case OPTION_NO_DEMANGLE:
859 	  demangling = FALSE;
860 	  break;
861 	case OPTION_NO_GC_SECTIONS:
862 	  link_info.gc_sections = FALSE;
863 	  break;
864 	case OPTION_NO_KEEP_MEMORY:
865 	  link_info.keep_memory = FALSE;
866 	  break;
867 	case OPTION_NO_UNDEFINED:
868 	  link_info.unresolved_syms_in_objects
869 	    = how_to_report_unresolved_symbols;
870 	  break;
871 	case OPTION_ALLOW_SHLIB_UNDEFINED:
872 	  link_info.unresolved_syms_in_shared_libs = RM_IGNORE;
873 	  break;
874 	case OPTION_NO_ALLOW_SHLIB_UNDEFINED:
875 	  link_info.unresolved_syms_in_shared_libs
876 	    = how_to_report_unresolved_symbols;
877 	  break;
878 	case OPTION_UNRESOLVED_SYMBOLS:
879 	  if (strcmp (optarg, "ignore-all") == 0)
880 	    {
881 	      link_info.unresolved_syms_in_objects = RM_IGNORE;
882 	      link_info.unresolved_syms_in_shared_libs = RM_IGNORE;
883 	    }
884 	  else if (strcmp (optarg, "report-all") == 0)
885 	    {
886 	      link_info.unresolved_syms_in_objects
887 		= how_to_report_unresolved_symbols;
888 	      link_info.unresolved_syms_in_shared_libs
889 		= how_to_report_unresolved_symbols;
890 	    }
891 	  else if (strcmp (optarg, "ignore-in-object-files") == 0)
892 	    {
893 	      link_info.unresolved_syms_in_objects = RM_IGNORE;
894 	      link_info.unresolved_syms_in_shared_libs
895 		= how_to_report_unresolved_symbols;
896 	    }
897       	  else if (strcmp (optarg, "ignore-in-shared-libs") == 0)
898 	    {
899 	      link_info.unresolved_syms_in_objects
900 		= how_to_report_unresolved_symbols;
901 	      link_info.unresolved_syms_in_shared_libs = RM_IGNORE;
902 	    }
903 	  else
904 	    einfo (_("%P%F: bad --unresolved-symbols option: %s\n"), optarg);
905 	  break;
906 	case OPTION_WARN_UNRESOLVED_SYMBOLS:
907 	  how_to_report_unresolved_symbols = RM_GENERATE_WARNING;
908 	  if (link_info.unresolved_syms_in_objects == RM_GENERATE_ERROR)
909 	    link_info.unresolved_syms_in_objects = RM_GENERATE_WARNING;
910 	  if (link_info.unresolved_syms_in_shared_libs == RM_GENERATE_ERROR)
911 	    link_info.unresolved_syms_in_shared_libs = RM_GENERATE_WARNING;
912 	  break;
913 
914 	case OPTION_ERROR_UNRESOLVED_SYMBOLS:
915 	  how_to_report_unresolved_symbols = RM_GENERATE_ERROR;
916 	  if (link_info.unresolved_syms_in_objects == RM_GENERATE_WARNING)
917 	    link_info.unresolved_syms_in_objects = RM_GENERATE_ERROR;
918 	  if (link_info.unresolved_syms_in_shared_libs == RM_GENERATE_WARNING)
919 	    link_info.unresolved_syms_in_shared_libs = RM_GENERATE_ERROR;
920 	  break;
921 	case OPTION_ALLOW_MULTIPLE_DEFINITION:
922 	  link_info.allow_multiple_definition = TRUE;
923 	  break;
924 	case OPTION_NO_UNDEFINED_VERSION:
925 	  link_info.allow_undefined_version = FALSE;
926 	  break;
927 	case OPTION_DEFAULT_SYMVER:
928 	  link_info.create_default_symver = TRUE;
929 	  break;
930 	case OPTION_DEFAULT_IMPORTED_SYMVER:
931 	  link_info.default_imported_symver = TRUE;
932 	  break;
933 	case OPTION_NO_WARN_MISMATCH:
934 	  command_line.warn_mismatch = FALSE;
935 	  break;
936 	case OPTION_NOINHIBIT_EXEC:
937 	  force_make_executable = TRUE;
938 	  break;
939 	case OPTION_NOSTDLIB:
940 	  config.only_cmd_line_lib_dirs = TRUE;
941 	  break;
942 	case OPTION_NO_WHOLE_ARCHIVE:
943 	  whole_archive = FALSE;
944 	  break;
945 	case 'O':
946 	  /* FIXME "-O<non-digits> <value>" used to set the address of
947 	     section <non-digits>.  Was this for compatibility with
948 	     something, or can we create a new option to do that
949 	     (with a syntax similar to -defsym)?
950 	     getopt can't handle two args to an option without kludges.  */
951 
952 	  /* Enable optimizations of output files.  */
953 	  link_info.optimize = strtoul (optarg, NULL, 0) ? TRUE : FALSE;
954 	  break;
955 	case 'o':
956 	  lang_add_output (optarg, 0);
957 	  break;
958 	case OPTION_OFORMAT:
959 	  lang_add_output_format (optarg, NULL, NULL, 0);
960 	  break;
961 	case 'q':
962 	  link_info.emitrelocations = TRUE;
963 	  break;
964 	case 'i':
965 	case 'r':
966 	  if (optind == last_optind)
967 	    /* This can happen if the user put "-rpath,a" on the command
968 	       line.  (Or something similar.  The comma is important).
969 	       Getopt becomes confused and thinks that this is a -r option
970 	       but it cannot parse the text after the -r so it refuses to
971 	       increment the optind counter.  Detect this case and issue
972 	       an error message here.  We cannot just make this a warning,
973 	       increment optind, and continue because getopt is too confused
974 	       and will seg-fault the next time around.  */
975 	    einfo(_("%P%F: bad -rpath option\n"));
976 
977 	  link_info.relocatable = TRUE;
978 	  config.build_constructors = FALSE;
979 	  config.magic_demand_paged = FALSE;
980 	  config.text_read_only = FALSE;
981 	  config.dynamic_link = FALSE;
982 	  break;
983 	case 'R':
984 	  /* The GNU linker traditionally uses -R to mean to include
985 	     only the symbols from a file.  The Solaris linker uses -R
986 	     to set the path used by the runtime linker to find
987 	     libraries.  This is the GNU linker -rpath argument.  We
988 	     try to support both simultaneously by checking the file
989 	     named.  If it is a directory, rather than a regular file,
990 	     we assume -rpath was meant.  */
991 	  {
992 	    struct stat s;
993 
994 	    if (stat (optarg, &s) >= 0
995 		&& ! S_ISDIR (s.st_mode))
996 	      {
997 		lang_add_input_file (optarg,
998 				     lang_input_file_is_symbols_only_enum,
999 				     NULL);
1000 		break;
1001 	      }
1002 	  }
1003 	  /* Fall through.  */
1004 	case OPTION_RPATH:
1005 	  if (command_line.rpath == NULL)
1006 	    command_line.rpath = xstrdup (optarg);
1007 	  else
1008 	    {
1009 	      size_t rpath_len = strlen (command_line.rpath);
1010 	      size_t optarg_len = strlen (optarg);
1011 	      char *buf;
1012 	      char *cp = command_line.rpath;
1013 
1014 	      /* First see whether OPTARG is already in the path.  */
1015 	      do
1016 		{
1017 		  size_t idx = 0;
1018 
1019 		  while (optarg[idx] != '\0' && optarg[idx] == cp[idx])
1020 		    ++idx;
1021 		  if (optarg[idx] == '\0'
1022 		      && (cp[idx] == '\0' || cp[idx] == ':'))
1023 		    /* We found it.  */
1024 		    break;
1025 
1026 		  /* Not yet found.  */
1027 		  cp = strchr (cp, ':');
1028 		  if (cp != NULL)
1029 		    ++cp;
1030 		}
1031 	      while (cp != NULL);
1032 
1033 	      if (cp == NULL)
1034 		{
1035 		  buf = xmalloc (rpath_len + optarg_len + 2);
1036 		  sprintf (buf, "%s:%s", command_line.rpath, optarg);
1037 		  free (command_line.rpath);
1038 		  command_line.rpath = buf;
1039 		}
1040 	    }
1041 	  break;
1042 	case OPTION_RPATH_LINK:
1043 	  if (command_line.rpath_link == NULL)
1044 	    command_line.rpath_link = xstrdup (optarg);
1045 	  else
1046 	    {
1047 	      char *buf;
1048 
1049 	      buf = xmalloc (strlen (command_line.rpath_link)
1050 			     + strlen (optarg)
1051 			     + 2);
1052 	      sprintf (buf, "%s:%s", command_line.rpath_link, optarg);
1053 	      free (command_line.rpath_link);
1054 	      command_line.rpath_link = buf;
1055 	    }
1056 	  break;
1057 	case OPTION_RELAX:
1058 	  command_line.relax = TRUE;
1059 	  break;
1060 	case OPTION_RETAIN_SYMBOLS_FILE:
1061 	  add_keepsyms_file (optarg);
1062 	  break;
1063 	case 'S':
1064 	  link_info.strip = strip_debugger;
1065 	  break;
1066 	case 's':
1067 	  link_info.strip = strip_all;
1068 	  break;
1069 	case OPTION_STRIP_DISCARDED:
1070 	  link_info.strip_discarded = TRUE;
1071 	  break;
1072 	case OPTION_NO_STRIP_DISCARDED:
1073 	  link_info.strip_discarded = FALSE;
1074 	  break;
1075 	case OPTION_SHARED:
1076 	  if (config.has_shared)
1077 	    {
1078 	      link_info.shared = TRUE;
1079 	      link_info.pie = FALSE;
1080 	      /* When creating a shared library, the default
1081 		 behaviour is to ignore any unresolved references.  */
1082 	      if (link_info.unresolved_syms_in_objects == RM_NOT_YET_SET)
1083 		link_info.unresolved_syms_in_objects = RM_IGNORE;
1084 	      if (link_info.unresolved_syms_in_shared_libs == RM_NOT_YET_SET)
1085 		link_info.unresolved_syms_in_shared_libs = RM_IGNORE;
1086 	    }
1087 	  else
1088 	    einfo (_("%P%F: -shared not supported\n"));
1089 	  break;
1090 	case OPTION_PIE:
1091 	  if (config.has_shared)
1092 	    {
1093 	      link_info.shared = TRUE;
1094 	      link_info.pie = TRUE;
1095 	    }
1096 	  else
1097 	    einfo (_("%P%F: -pie not supported\n"));
1098 	  break;
1099 	case OPTION_NOPIE:
1100 	  if (config.has_shared)
1101 	    {
1102 	      link_info.shared = FALSE;
1103 	      link_info.pie = FALSE;
1104 	    }
1105 	  else
1106 	    einfo (_("%P%F: -nopie not supported\n"));
1107 	  break;
1108 	case 'h':		/* Used on Solaris.  */
1109 	case OPTION_SONAME:
1110 	  command_line.soname = optarg;
1111 	  break;
1112 	case OPTION_SORT_COMMON:
1113 	  config.sort_common = TRUE;
1114 	  break;
1115 	case OPTION_SORT_SECTION:
1116 	  if (strcmp (optarg, N_("name")) == 0)
1117 	    sort_section = by_name;
1118 	  else if (strcmp (optarg, N_("alignment")) == 0)
1119 	    sort_section = by_alignment;
1120 	  else
1121 	    einfo (_("%P%F: invalid section sorting option: %s\n"),
1122 		   optarg);
1123 	  break;
1124 	case OPTION_STATS:
1125 	  config.stats = TRUE;
1126 	  break;
1127 	case OPTION_SYMBOLIC:
1128 	  link_info.symbolic = TRUE;
1129 	  break;
1130 	case 't':
1131 	  trace_files = TRUE;
1132 	  break;
1133 	case 'T':
1134 	  ldfile_open_command_file (optarg);
1135 	  parser_input = input_script;
1136 	  yyparse ();
1137 	  break;
1138 	case OPTION_SECTION_START:
1139 	  {
1140 	    char *optarg2;
1141 	    char *sec_name;
1142 	    int len;
1143 
1144 	    /* Check for <something>=<somthing>...  */
1145 	    optarg2 = strchr (optarg, '=');
1146 	    if (optarg2 == NULL)
1147 	      einfo (_("%P%F: invalid argument to option"
1148 		       " \"--section-start\"\n"));
1149 
1150 	    optarg2++;
1151 
1152 	    /* So far so good.  Are all the args present?  */
1153 	    if ((*optarg == '\0') || (*optarg2 == '\0'))
1154 	      einfo (_("%P%F: missing argument(s) to option"
1155 		       " \"--section-start\"\n"));
1156 
1157 	    /* We must copy the section name as set_section_start
1158 	       doesn't do it for us.  */
1159 	    len = optarg2 - optarg;
1160 	    sec_name = xmalloc (len);
1161 	    memcpy (sec_name, optarg, len - 1);
1162 	    sec_name[len - 1] = 0;
1163 
1164 	    /* Then set it...  */
1165 	    set_section_start (sec_name, optarg2);
1166 	  }
1167 	  break;
1168 	case OPTION_TARGET_HELP:
1169 	  /* Mention any target specific options.  */
1170 	  ldemul_list_emulation_options (stdout);
1171 	  exit (0);
1172 	case OPTION_TBSS:
1173 	  set_segment_start (".bss", optarg);
1174 	  break;
1175 	case OPTION_TDATA:
1176 	  set_segment_start (".data", optarg);
1177 	  break;
1178 	case OPTION_TTEXT:
1179 	  set_segment_start (".text", optarg);
1180 	  break;
1181 	case OPTION_TRADITIONAL_FORMAT:
1182 	  link_info.traditional_format = TRUE;
1183 	  break;
1184 	case OPTION_TASK_LINK:
1185 	  link_info.task_link = TRUE;
1186 	  /* Fall through - do an implied -r option.  */
1187 	case OPTION_UR:
1188 	  link_info.relocatable = TRUE;
1189 	  config.build_constructors = TRUE;
1190 	  config.magic_demand_paged = FALSE;
1191 	  config.text_read_only = FALSE;
1192 	  config.dynamic_link = FALSE;
1193 	  break;
1194 	case 'u':
1195 	  ldlang_add_undef (optarg);
1196 	  break;
1197 	case OPTION_UNIQUE:
1198 	  if (optarg != NULL)
1199 	    lang_add_unique (optarg);
1200 	  else
1201 	    config.unique_orphan_sections = TRUE;
1202 	  break;
1203 	case OPTION_VERBOSE:
1204 	  ldversion (1);
1205 	  version_printed = TRUE;
1206 	  trace_file_tries = TRUE;
1207 	  overflow_cutoff_limit = -2;
1208 	  break;
1209 	case 'v':
1210 	  ldversion (0);
1211 	  version_printed = TRUE;
1212 	  break;
1213 	case 'V':
1214 	  ldversion (1);
1215 	  version_printed = TRUE;
1216 	  break;
1217 	case OPTION_VERSION:
1218 	  ldversion (2);
1219 	  xexit (0);
1220 	  break;
1221 	case OPTION_VERSION_SCRIPT:
1222 	  /* This option indicates a small script that only specifies
1223 	     version information.  Read it, but don't assume that
1224 	     we've seen a linker script.  */
1225 	  {
1226 	    FILE *hold_script_handle;
1227 
1228 	    hold_script_handle = saved_script_handle;
1229 	    ldfile_open_command_file (optarg);
1230 	    saved_script_handle = hold_script_handle;
1231 	    parser_input = input_version_script;
1232 	    yyparse ();
1233 	  }
1234 	  break;
1235 	case OPTION_VERSION_EXPORTS_SECTION:
1236 	  /* This option records a version symbol to be applied to the
1237 	     symbols listed for export to be found in the object files
1238 	     .exports sections.  */
1239 	  command_line.version_exports_section = optarg;
1240 	  break;
1241 	case OPTION_WARN_COMMON:
1242 	  config.warn_common = TRUE;
1243 	  break;
1244 	case OPTION_WARN_CONSTRUCTORS:
1245 	  config.warn_constructors = TRUE;
1246 	  break;
1247 	case OPTION_WARN_FATAL:
1248 	  config.fatal_warnings = TRUE;
1249 	  break;
1250 	case OPTION_WARN_MULTIPLE_GP:
1251 	  config.warn_multiple_gp = TRUE;
1252 	  break;
1253 	case OPTION_WARN_ONCE:
1254 	  config.warn_once = TRUE;
1255 	  break;
1256 	case OPTION_WARN_SECTION_ALIGN:
1257 	  config.warn_section_align = TRUE;
1258 	  break;
1259 	case OPTION_WARN_SHARED_TEXTREL:
1260 	  link_info.warn_shared_textrel = TRUE;
1261 	  break;
1262 	case OPTION_WHOLE_ARCHIVE:
1263 	  whole_archive = TRUE;
1264 	  break;
1265 	case OPTION_ADD_NEEDED:
1266 	  add_needed = TRUE;
1267 	  break;
1268 	case OPTION_NO_ADD_NEEDED:
1269 	  add_needed = FALSE;
1270 	  break;
1271 	case OPTION_AS_NEEDED:
1272 	  as_needed = TRUE;
1273 	  break;
1274 	case OPTION_NO_AS_NEEDED:
1275 	  as_needed = FALSE;
1276 	  break;
1277 	case OPTION_WRAP:
1278 	  add_wrap (optarg);
1279 	  break;
1280 	case OPTION_DISCARD_NONE:
1281 	  link_info.discard = discard_none;
1282 	  break;
1283 	case 'X':
1284 	  link_info.discard = discard_l;
1285 	  break;
1286 	case 'x':
1287 	  link_info.discard = discard_all;
1288 	  break;
1289 	case 'Y':
1290 	  if (strncmp (optarg, "P,", 2) == 0)
1291 	    optarg += 2;
1292 	  if (default_dirlist != NULL)
1293 	    free (default_dirlist);
1294 	  default_dirlist = xstrdup (optarg);
1295 	  break;
1296 	case 'y':
1297 	  add_ysym (optarg);
1298 	  break;
1299 	case 'Z':
1300 	  config.data_bss_contig = TRUE;
1301 	  break;
1302 	case OPTION_SPARE_DYNAMIC_TAGS:
1303 	  link_info.spare_dynamic_tags = strtoul (optarg, NULL, 0);
1304 	  break;
1305 	case OPTION_SPLIT_BY_RELOC:
1306 	  if (optarg != NULL)
1307 	    config.split_by_reloc = strtoul (optarg, NULL, 0);
1308 	  else
1309 	    config.split_by_reloc = 32768;
1310 	  break;
1311 	case OPTION_SPLIT_BY_FILE:
1312 	  if (optarg != NULL)
1313 	    config.split_by_file = bfd_scan_vma (optarg, NULL, 0);
1314 	  else
1315 	    config.split_by_file = 1;
1316 	  break;
1317 	case OPTION_CHECK_SECTIONS:
1318 	  command_line.check_section_addresses = TRUE;
1319 	  break;
1320 	case OPTION_NO_CHECK_SECTIONS:
1321 	  command_line.check_section_addresses = FALSE;
1322 	  break;
1323 	case OPTION_ACCEPT_UNKNOWN_INPUT_ARCH:
1324 	  command_line.accept_unknown_input_arch = TRUE;
1325 	  break;
1326 	case OPTION_NO_ACCEPT_UNKNOWN_INPUT_ARCH:
1327 	  command_line.accept_unknown_input_arch = FALSE;
1328 	  break;
1329 	case '(':
1330 	  if (ingroup)
1331 	    einfo (_("%P%F: may not nest groups (--help for usage)\n"));
1332 
1333 	  lang_enter_group ();
1334 	  ingroup = 1;
1335 	  break;
1336 	case ')':
1337 	  if (! ingroup)
1338 	    einfo (_("%P%F: group ended before it began (--help for usage)\n"));
1339 
1340 	  lang_leave_group ();
1341 	  ingroup = 0;
1342 	  break;
1343 
1344 	case OPTION_INIT:
1345 	  link_info.init_function = optarg;
1346 	  break;
1347 
1348 	case OPTION_FINI:
1349 	  link_info.fini_function = optarg;
1350 	  break;
1351 
1352 	case OPTION_REDUCE_MEMORY_OVERHEADS:
1353 	  link_info.reduce_memory_overheads = TRUE;
1354 	  if (config.hash_table_size == 0)
1355 	    config.hash_table_size = 1021;
1356 	  break;
1357 
1358         case OPTION_HASH_SIZE:
1359 	  {
1360 	    bfd_size_type new_size;
1361 
1362             new_size = strtoul (optarg, NULL, 0);
1363             if (new_size)
1364               config.hash_table_size = new_size;
1365             else
1366               einfo (_("%P%X: --hash-size needs a numeric argument\n"));
1367           }
1368           break;
1369 	}
1370     }
1371 
1372   if (ingroup)
1373     lang_leave_group ();
1374 
1375   if (default_dirlist != NULL)
1376     {
1377       set_default_dirlist (default_dirlist);
1378       free (default_dirlist);
1379     }
1380 
1381   if (link_info.unresolved_syms_in_objects == RM_NOT_YET_SET)
1382     /* FIXME: Should we allow emulations a chance to set this ?  */
1383     link_info.unresolved_syms_in_objects = how_to_report_unresolved_symbols;
1384 
1385   if (link_info.unresolved_syms_in_shared_libs == RM_NOT_YET_SET)
1386     /* FIXME: Should we allow emulations a chance to set this ?  */
1387     link_info.unresolved_syms_in_shared_libs = how_to_report_unresolved_symbols;
1388 }
1389 
1390 /* Add the (colon-separated) elements of DIRLIST_PTR to the
1391    library search path.  */
1392 
1393 static void
1394 set_default_dirlist (char *dirlist_ptr)
1395 {
1396   char *p;
1397 
1398   while (1)
1399     {
1400       p = strchr (dirlist_ptr, PATH_SEPARATOR);
1401       if (p != NULL)
1402 	*p = '\0';
1403       if (*dirlist_ptr != '\0')
1404 	ldfile_add_library_path (dirlist_ptr, TRUE);
1405       if (p == NULL)
1406 	break;
1407       dirlist_ptr = p + 1;
1408     }
1409 }
1410 
1411 static void
1412 set_section_start (char *sect, char *valstr)
1413 {
1414   const char *end;
1415   bfd_vma val = bfd_scan_vma (valstr, &end, 16);
1416   if (*end)
1417     einfo (_("%P%F: invalid hex number `%s'\n"), valstr);
1418   lang_section_start (sect, exp_intop (val), NULL);
1419 }
1420 
1421 static void
1422 set_segment_start (const char *section, char *valstr)
1423 {
1424   const char *name;
1425   const char *end;
1426   segment_type *seg;
1427 
1428   bfd_vma val = bfd_scan_vma (valstr, &end, 16);
1429   if (*end)
1430     einfo (_("%P%F: invalid hex number `%s'\n"), valstr);
1431   /* If we already have an entry for this segment, update the existing
1432      value.  */
1433   name = section + 1;
1434   for (seg = segments; seg; seg = seg->next)
1435     if (strcmp (seg->name, name) == 0)
1436       {
1437 	seg->value = val;
1438 	return;
1439       }
1440   /* There was no existing value so we must create a new segment
1441      entry.  */
1442   seg = stat_alloc (sizeof (*seg));
1443   seg->name = name;
1444   seg->value = val;
1445   seg->used = FALSE;
1446   /* Add it to the linked list of segments.  */
1447   seg->next = segments;
1448   segments = seg;
1449   /* Historically, -Ttext and friends set the base address of a
1450      particular section.  For backwards compatibility, we still do
1451      that.  If a SEGMENT_START directive is seen, the section address
1452      assignment will be disabled.  */
1453   lang_section_start (section, exp_intop (val), seg);
1454 }
1455 
1456 
1457 /* Print help messages for the options.  */
1458 
1459 static void
1460 help (void)
1461 {
1462   unsigned i;
1463   const char **targets, **pp;
1464   int len;
1465 
1466   printf (_("Usage: %s [options] file...\n"), program_name);
1467 
1468   printf (_("Options:\n"));
1469   for (i = 0; i < OPTION_COUNT; i++)
1470     {
1471       if (ld_options[i].doc != NULL)
1472 	{
1473 	  bfd_boolean comma;
1474 	  unsigned j;
1475 
1476 	  printf ("  ");
1477 
1478 	  comma = FALSE;
1479 	  len = 2;
1480 
1481 	  j = i;
1482 	  do
1483 	    {
1484 	      if (ld_options[j].shortopt != '\0'
1485 		  && ld_options[j].control != NO_HELP)
1486 		{
1487 		  printf ("%s-%c", comma ? ", " : "", ld_options[j].shortopt);
1488 		  len += (comma ? 2 : 0) + 2;
1489 		  if (ld_options[j].arg != NULL)
1490 		    {
1491 		      if (ld_options[j].opt.has_arg != optional_argument)
1492 			{
1493 			  printf (" ");
1494 			  ++len;
1495 			}
1496 		      printf ("%s", _(ld_options[j].arg));
1497 		      len += strlen (_(ld_options[j].arg));
1498 		    }
1499 		  comma = TRUE;
1500 		}
1501 	      ++j;
1502 	    }
1503 	  while (j < OPTION_COUNT && ld_options[j].doc == NULL);
1504 
1505 	  j = i;
1506 	  do
1507 	    {
1508 	      if (ld_options[j].opt.name != NULL
1509 		  && ld_options[j].control != NO_HELP)
1510 		{
1511 		  int two_dashes =
1512 		    (ld_options[j].control == TWO_DASHES
1513 		     || ld_options[j].control == EXACTLY_TWO_DASHES);
1514 
1515 		  printf ("%s-%s%s",
1516 			  comma ? ", " : "",
1517 			  two_dashes ? "-" : "",
1518 			  ld_options[j].opt.name);
1519 		  len += ((comma ? 2 : 0)
1520 			  + 1
1521 			  + (two_dashes ? 1 : 0)
1522 			  + strlen (ld_options[j].opt.name));
1523 		  if (ld_options[j].arg != NULL)
1524 		    {
1525 		      printf (" %s", _(ld_options[j].arg));
1526 		      len += 1 + strlen (_(ld_options[j].arg));
1527 		    }
1528 		  comma = TRUE;
1529 		}
1530 	      ++j;
1531 	    }
1532 	  while (j < OPTION_COUNT && ld_options[j].doc == NULL);
1533 
1534 	  if (len >= 30)
1535 	    {
1536 	      printf ("\n");
1537 	      len = 0;
1538 	    }
1539 
1540 	  for (; len < 30; len++)
1541 	    putchar (' ');
1542 
1543 	  printf ("%s\n", _(ld_options[i].doc));
1544 	}
1545     }
1546   printf (_("  @FILE"));
1547   for (len = strlen ("  @FILE"); len < 30; len++)
1548     putchar (' ');
1549   printf (_("Read options from FILE\n"));
1550 
1551   /* Note: Various tools (such as libtool) depend upon the
1552      format of the listings below - do not change them.  */
1553   /* xgettext:c-format */
1554   printf (_("%s: supported targets:"), program_name);
1555   targets = bfd_target_list ();
1556   for (pp = targets; *pp != NULL; pp++)
1557     printf (" %s", *pp);
1558   free (targets);
1559   printf ("\n");
1560 
1561   /* xgettext:c-format */
1562   printf (_("%s: supported emulations: "), program_name);
1563   ldemul_list_emulations (stdout);
1564   printf ("\n");
1565 
1566   /* xgettext:c-format */
1567   printf (_("%s: emulation specific options:\n"), program_name);
1568   ldemul_list_emulation_options (stdout);
1569   printf ("\n");
1570 
1571   printf (_("Report bugs to %s\n"), REPORT_BUGS_TO);
1572 }
1573