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