ld: Extend options for altering orphan handling behaviour.
[external/binutils.git] / ld / lexsup.c
1 /* Parse options for the GNU linker.
2    Copyright (C) 1991-2015 Free Software Foundation, Inc.
3
4    This file is part of the GNU Binutils.
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19    MA 02110-1301, USA.  */
20
21 #include "sysdep.h"
22 #include "bfd.h"
23 #include "bfdver.h"
24 #include "libiberty.h"
25 #include <stdio.h>
26 #include <string.h>
27 #include "safe-ctype.h"
28 #include "getopt.h"
29 #include "bfdlink.h"
30 #include "ld.h"
31 #include "ldmain.h"
32 #include "ldmisc.h"
33 #include "ldexp.h"
34 #include "ldlang.h"
35 #include <ldgram.h>
36 #include "ldlex.h"
37 #include "ldfile.h"
38 #include "ldver.h"
39 #include "ldemul.h"
40 #include "demangle.h"
41 #ifdef ENABLE_PLUGINS
42 #include "plugin.h"
43 #endif /* ENABLE_PLUGINS */
44
45 #ifndef PATH_SEPARATOR
46 #if defined (__MSDOS__) || (defined (_WIN32) && ! defined (__CYGWIN32__))
47 #define PATH_SEPARATOR ';'
48 #else
49 #define PATH_SEPARATOR ':'
50 #endif
51 #endif
52
53 /* Somewhere above, sys/stat.h got included . . . .  */
54 #if !defined(S_ISDIR) && defined(S_IFDIR)
55 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
56 #endif
57
58 static void set_default_dirlist (char *);
59 static void set_section_start (char *, char *);
60 static void set_segment_start (const char *, char *);
61 static void help (void);
62
63 /* The long options.  This structure is used for both the option
64    parsing and the help text.  */
65
66 enum control_enum {
67   /* Use one dash before long option name.  */
68   ONE_DASH,
69   /* Use two dashes before long option name.  */
70   TWO_DASHES,
71   /* Only accept two dashes before the long option name.
72      This is an overloading of the use of this enum, since originally it
73      was only intended to tell the --help display function how to display
74      the long option name.  This feature was added in order to resolve
75      the confusion about the -omagic command line switch.  Is it setting
76      the output file name to "magic" or is it setting the NMAGIC flag on
77      the output ?  It has been decided that it is setting the output file
78      name, and that if you want to set the NMAGIC flag you should use -N
79      or --omagic.  */
80   EXACTLY_TWO_DASHES,
81   /* Don't mention this option in --help output.  */
82   NO_HELP
83 };
84
85 struct ld_option
86 {
87   /* The long option information.  */
88   struct option opt;
89   /* The short option with the same meaning ('\0' if none).  */
90   char shortopt;
91   /* The name of the argument (NULL if none).  */
92   const char *arg;
93   /* The documentation string.  If this is NULL, this is a synonym for
94      the previous option.  */
95   const char *doc;
96   enum control_enum control;
97 };
98
99 static const struct ld_option ld_options[] =
100 {
101   { {NULL, required_argument, NULL, '\0'},
102     'a', N_("KEYWORD"), N_("Shared library control for HP/UX compatibility"),
103     ONE_DASH },
104   { {"architecture", required_argument, NULL, 'A'},
105     'A', N_("ARCH"), N_("Set architecture") , TWO_DASHES },
106   { {"format", required_argument, NULL, 'b'},
107     'b', N_("TARGET"), N_("Specify target for following input files"),
108     TWO_DASHES },
109   { {"mri-script", required_argument, NULL, 'c'},
110     'c', N_("FILE"), N_("Read MRI format linker script"), TWO_DASHES },
111   { {"dc", no_argument, NULL, 'd'},
112     'd', NULL, N_("Force common symbols to be defined"), ONE_DASH },
113   { {"dp", no_argument, NULL, 'd'},
114     '\0', NULL, NULL, ONE_DASH },
115   { {"entry", required_argument, NULL, 'e'},
116     'e', N_("ADDRESS"), N_("Set start address"), TWO_DASHES },
117   { {"export-dynamic", no_argument, NULL, OPTION_EXPORT_DYNAMIC},
118     'E', NULL, N_("Export all dynamic symbols"), TWO_DASHES },
119   { {"no-export-dynamic", no_argument, NULL, OPTION_NO_EXPORT_DYNAMIC},
120     '\0', NULL, N_("Undo the effect of --export-dynamic"), TWO_DASHES },
121   { {"EB", no_argument, NULL, OPTION_EB},
122     '\0', NULL, N_("Link big-endian objects"), ONE_DASH },
123   { {"EL", no_argument, NULL, OPTION_EL},
124     '\0', NULL, N_("Link little-endian objects"), ONE_DASH },
125   { {"auxiliary", required_argument, NULL, 'f'},
126     'f', N_("SHLIB"), N_("Auxiliary filter for shared object symbol table"),
127     TWO_DASHES },
128   { {"filter", required_argument, NULL, 'F'},
129     'F', N_("SHLIB"), N_("Filter for shared object symbol table"),
130     TWO_DASHES },
131   { {NULL, no_argument, NULL, '\0'},
132     'g', NULL, N_("Ignored"), ONE_DASH },
133   { {"gpsize", required_argument, NULL, 'G'},
134     'G', N_("SIZE"), N_("Small data size (if no size, same as --shared)"),
135     TWO_DASHES },
136   { {"soname", required_argument, NULL, OPTION_SONAME},
137     'h', N_("FILENAME"), N_("Set internal name of shared library"), ONE_DASH },
138   { {"dynamic-linker", required_argument, NULL, OPTION_DYNAMIC_LINKER},
139     'I', N_("PROGRAM"), N_("Set PROGRAM as the dynamic linker to use"),
140     TWO_DASHES },
141   { {"library", required_argument, NULL, 'l'},
142     'l', N_("LIBNAME"), N_("Search for library LIBNAME"), TWO_DASHES },
143   { {"library-path", required_argument, NULL, 'L'},
144     'L', N_("DIRECTORY"), N_("Add DIRECTORY to library search path"),
145     TWO_DASHES },
146   { {"sysroot=<DIRECTORY>", required_argument, NULL, OPTION_SYSROOT},
147     '\0', NULL, N_("Override the default sysroot location"), TWO_DASHES },
148   { {NULL, required_argument, NULL, '\0'},
149     'm', N_("EMULATION"), N_("Set emulation"), ONE_DASH },
150   { {"print-map", no_argument, NULL, 'M'},
151     'M', NULL, N_("Print map file on standard output"), TWO_DASHES },
152   { {"nmagic", no_argument, NULL, 'n'},
153     'n', NULL, N_("Do not page align data"), TWO_DASHES },
154   { {"omagic", no_argument, NULL, 'N'},
155     'N', NULL, N_("Do not page align data, do not make text readonly"),
156     EXACTLY_TWO_DASHES },
157   { {"no-omagic", no_argument, NULL, OPTION_NO_OMAGIC},
158     '\0', NULL, N_("Page align data, make text readonly"),
159     EXACTLY_TWO_DASHES },
160   { {"output", required_argument, NULL, 'o'},
161     'o', N_("FILE"), N_("Set output file name"), EXACTLY_TWO_DASHES },
162   { {NULL, required_argument, NULL, '\0'},
163     'O', NULL, N_("Optimize output file"), ONE_DASH },
164 #ifdef ENABLE_PLUGINS
165   { {"plugin", required_argument, NULL, OPTION_PLUGIN},
166     '\0', N_("PLUGIN"), N_("Load named plugin"), ONE_DASH },
167   { {"plugin-opt", required_argument, NULL, OPTION_PLUGIN_OPT},
168     '\0', N_("ARG"), N_("Send arg to last-loaded plugin"), ONE_DASH },
169   { {"flto", optional_argument, NULL, OPTION_IGNORE},
170     '\0', NULL, N_("Ignored for GCC LTO option compatibility"),
171     ONE_DASH },
172   { {"flto-partition=", required_argument, NULL, OPTION_IGNORE},
173     '\0', NULL, N_("Ignored for GCC LTO option compatibility"),
174     ONE_DASH },
175 #endif /* ENABLE_PLUGINS */
176   { {"fuse-ld=", required_argument, NULL, OPTION_IGNORE},
177     '\0', NULL, N_("Ignored for GCC linker option compatibility"),
178     ONE_DASH },
179   { {"map-whole-files", optional_argument, NULL, OPTION_IGNORE},
180     '\0', NULL, N_("Ignored for gold option compatibility"),
181     TWO_DASHES },
182   { {"no-map-whole-files", optional_argument, NULL, OPTION_IGNORE},
183     '\0', NULL, N_("Ignored for gold option compatibility"),
184     TWO_DASHES },
185   { {"Qy", no_argument, NULL, OPTION_IGNORE},
186     '\0', NULL, N_("Ignored for SVR4 compatibility"), ONE_DASH },
187   { {"emit-relocs", no_argument, NULL, 'q'},
188     'q', NULL, "Generate relocations in final output", TWO_DASHES },
189   { {"relocatable", no_argument, NULL, 'r'},
190     'r', NULL, N_("Generate relocatable output"), TWO_DASHES },
191   { {NULL, no_argument, NULL, '\0'},
192     'i', NULL, NULL, ONE_DASH },
193   { {"just-symbols", required_argument, NULL, 'R'},
194     'R', N_("FILE"), N_("Just link symbols (if directory, same as --rpath)"),
195     TWO_DASHES },
196   { {"strip-all", no_argument, NULL, 's'},
197     's', NULL, N_("Strip all symbols"), TWO_DASHES },
198   { {"strip-debug", no_argument, NULL, 'S'},
199     'S', NULL, N_("Strip debugging symbols"), TWO_DASHES },
200   { {"strip-discarded", no_argument, NULL, OPTION_STRIP_DISCARDED},
201     '\0', NULL, N_("Strip symbols in discarded sections"), TWO_DASHES },
202   { {"no-strip-discarded", no_argument, NULL, OPTION_NO_STRIP_DISCARDED},
203     '\0', NULL, N_("Do not strip symbols in discarded sections"), TWO_DASHES },
204   { {"trace", no_argument, NULL, 't'},
205     't', NULL, N_("Trace file opens"), TWO_DASHES },
206   { {"script", required_argument, NULL, 'T'},
207     'T', N_("FILE"), N_("Read linker script"), TWO_DASHES },
208   { {"default-script", required_argument, NULL, OPTION_DEFAULT_SCRIPT},
209     '\0', N_("FILE"), N_("Read default linker script"), TWO_DASHES },
210   { {"dT", required_argument, NULL, OPTION_DEFAULT_SCRIPT},
211     '\0', NULL, NULL, ONE_DASH },
212   { {"undefined", required_argument, NULL, 'u'},
213     'u', N_("SYMBOL"), N_("Start with undefined reference to SYMBOL"),
214     TWO_DASHES },
215   { {"require-defined", required_argument, NULL, OPTION_REQUIRE_DEFINED_SYMBOL},
216     '\0', N_("SYMBOL"), N_("Require SYMBOL be defined in the final output"),
217     TWO_DASHES },
218   { {"unique", optional_argument, NULL, OPTION_UNIQUE},
219     '\0', N_("[=SECTION]"),
220     N_("Don't merge input [SECTION | orphan] sections"), TWO_DASHES },
221   { {"Ur", no_argument, NULL, OPTION_UR},
222     '\0', NULL, N_("Build global constructor/destructor tables"), ONE_DASH },
223   { {"version", no_argument, NULL, OPTION_VERSION},
224     'v', NULL, N_("Print version information"), TWO_DASHES },
225   { {NULL, no_argument, NULL, '\0'},
226     'V', NULL, N_("Print version and emulation information"), ONE_DASH },
227   { {"discard-all", no_argument, NULL, 'x'},
228     'x', NULL, N_("Discard all local symbols"), TWO_DASHES },
229   { {"discard-locals", no_argument, NULL, 'X'},
230     'X', NULL, N_("Discard temporary local symbols (default)"), TWO_DASHES },
231   { {"discard-none", no_argument, NULL, OPTION_DISCARD_NONE},
232     '\0', NULL, N_("Don't discard any local symbols"), TWO_DASHES },
233   { {"trace-symbol", required_argument, NULL, 'y'},
234     'y', N_("SYMBOL"), N_("Trace mentions of SYMBOL"), TWO_DASHES },
235   { {NULL, required_argument, NULL, '\0'},
236     'Y', N_("PATH"), N_("Default search path for Solaris compatibility"),
237     ONE_DASH },
238   { {"start-group", no_argument, NULL, '('},
239     '(', NULL, N_("Start a group"), TWO_DASHES },
240   { {"end-group", no_argument, NULL, ')'},
241     ')', NULL, N_("End a group"), TWO_DASHES },
242   { {"accept-unknown-input-arch", no_argument, NULL,
243      OPTION_ACCEPT_UNKNOWN_INPUT_ARCH},
244     '\0', NULL,
245     N_("Accept input files whose architecture cannot be determined"),
246     TWO_DASHES },
247   { {"no-accept-unknown-input-arch", no_argument, NULL,
248      OPTION_NO_ACCEPT_UNKNOWN_INPUT_ARCH},
249     '\0', NULL, N_("Reject input files whose architecture is unknown"),
250     TWO_DASHES },
251
252   /* The next two options are deprecated because of their similarity to
253      --as-needed and --no-as-needed.  They have been replaced by
254      --copy-dt-needed-entries and --no-copy-dt-needed-entries.  */
255   { {"add-needed", no_argument, NULL, OPTION_ADD_DT_NEEDED_FOR_DYNAMIC},
256     '\0', NULL, NULL, NO_HELP },
257   { {"no-add-needed", no_argument, NULL, OPTION_NO_ADD_DT_NEEDED_FOR_DYNAMIC},
258     '\0', NULL, NULL, NO_HELP },
259
260   { {"as-needed", no_argument, NULL, OPTION_ADD_DT_NEEDED_FOR_REGULAR},
261     '\0', NULL, N_("Only set DT_NEEDED for following dynamic libs if used"),
262     TWO_DASHES },
263   { {"no-as-needed", no_argument, NULL, OPTION_NO_ADD_DT_NEEDED_FOR_REGULAR},
264     '\0', NULL, N_("Always set DT_NEEDED for dynamic libraries mentioned on\n"
265                    "                                the command line"),
266     TWO_DASHES },
267   { {"assert", required_argument, NULL, OPTION_ASSERT},
268     '\0', N_("KEYWORD"), N_("Ignored for SunOS compatibility"), ONE_DASH },
269   { {"Bdynamic", no_argument, NULL, OPTION_CALL_SHARED},
270     '\0', NULL, N_("Link against shared libraries"), ONE_DASH },
271   { {"dy", no_argument, NULL, OPTION_CALL_SHARED},
272     '\0', NULL, NULL, ONE_DASH },
273   { {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
274     '\0', NULL, NULL, ONE_DASH },
275   { {"Bstatic", no_argument, NULL, OPTION_NON_SHARED},
276     '\0', NULL, N_("Do not link against shared libraries"), ONE_DASH },
277   { {"dn", no_argument, NULL, OPTION_NON_SHARED},
278     '\0', NULL, NULL, ONE_DASH },
279   { {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
280     '\0', NULL, NULL, ONE_DASH },
281   { {"static", no_argument, NULL, OPTION_NON_SHARED},
282     '\0', NULL, NULL, ONE_DASH },
283   { {"Bsymbolic", no_argument, NULL, OPTION_SYMBOLIC},
284     '\0', NULL, N_("Bind global references locally"), ONE_DASH },
285   { {"Bsymbolic-functions", no_argument, NULL, OPTION_SYMBOLIC_FUNCTIONS},
286     '\0', NULL, N_("Bind global function references locally"), ONE_DASH },
287   { {"check-sections", no_argument, NULL, OPTION_CHECK_SECTIONS},
288     '\0', NULL, N_("Check section addresses for overlaps (default)"),
289     TWO_DASHES },
290   { {"no-check-sections", no_argument, NULL, OPTION_NO_CHECK_SECTIONS},
291     '\0', NULL, N_("Do not check section addresses for overlaps"),
292     TWO_DASHES },
293   { {"copy-dt-needed-entries", no_argument, NULL,
294      OPTION_ADD_DT_NEEDED_FOR_DYNAMIC},
295     '\0', NULL, N_("Copy DT_NEEDED links mentioned inside DSOs that follow"),
296     TWO_DASHES },
297   { {"no-copy-dt-needed-entries", no_argument, NULL,
298      OPTION_NO_ADD_DT_NEEDED_FOR_DYNAMIC},
299     '\0', NULL, N_("Do not copy DT_NEEDED links mentioned inside DSOs that follow"),
300     TWO_DASHES },
301
302   { {"cref", no_argument, NULL, OPTION_CREF},
303     '\0', NULL, N_("Output cross reference table"), TWO_DASHES },
304   { {"defsym", required_argument, NULL, OPTION_DEFSYM},
305     '\0', N_("SYMBOL=EXPRESSION"), N_("Define a symbol"), TWO_DASHES },
306   { {"demangle", optional_argument, NULL, OPTION_DEMANGLE},
307     '\0', N_("[=STYLE]"), N_("Demangle symbol names [using STYLE]"),
308     TWO_DASHES },
309   { {"embedded-relocs", no_argument, NULL, OPTION_EMBEDDED_RELOCS},
310     '\0', NULL, N_("Generate embedded relocs"), TWO_DASHES},
311   { {"fatal-warnings", no_argument, NULL, OPTION_WARN_FATAL},
312     '\0', NULL, N_("Treat warnings as errors"),
313     TWO_DASHES },
314   { {"no-fatal-warnings", no_argument, NULL, OPTION_NO_WARN_FATAL},
315     '\0', NULL, N_("Do not treat warnings as errors (default)"),
316     TWO_DASHES },
317   { {"fini", required_argument, NULL, OPTION_FINI},
318     '\0', N_("SYMBOL"), N_("Call SYMBOL at unload-time"), ONE_DASH },
319   { {"force-exe-suffix", no_argument, NULL, OPTION_FORCE_EXE_SUFFIX},
320     '\0', NULL, N_("Force generation of file with .exe suffix"), TWO_DASHES},
321   { {"gc-sections", no_argument, NULL, OPTION_GC_SECTIONS},
322     '\0', NULL, N_("Remove unused sections (on some targets)"),
323     TWO_DASHES },
324   { {"no-gc-sections", no_argument, NULL, OPTION_NO_GC_SECTIONS},
325     '\0', NULL, N_("Don't remove unused sections (default)"),
326     TWO_DASHES },
327   { {"print-gc-sections", no_argument, NULL, OPTION_PRINT_GC_SECTIONS},
328     '\0', NULL, N_("List removed unused sections on stderr"),
329     TWO_DASHES },
330   { {"no-print-gc-sections", no_argument, NULL, OPTION_NO_PRINT_GC_SECTIONS},
331     '\0', NULL, N_("Do not list removed unused sections"),
332     TWO_DASHES },
333   { {"hash-size=<NUMBER>", required_argument, NULL, OPTION_HASH_SIZE},
334     '\0', NULL, N_("Set default hash table size close to <NUMBER>"),
335     TWO_DASHES },
336   { {"help", no_argument, NULL, OPTION_HELP},
337     '\0', NULL, N_("Print option help"), TWO_DASHES },
338   { {"init", required_argument, NULL, OPTION_INIT},
339     '\0', N_("SYMBOL"), N_("Call SYMBOL at load-time"), ONE_DASH },
340   { {"Map", required_argument, NULL, OPTION_MAP},
341     '\0', N_("FILE"), N_("Write a map file"), ONE_DASH },
342   { {"no-define-common", no_argument, NULL, OPTION_NO_DEFINE_COMMON},
343     '\0', NULL, N_("Do not define Common storage"), TWO_DASHES },
344   { {"no-demangle", no_argument, NULL, OPTION_NO_DEMANGLE },
345     '\0', NULL, N_("Do not demangle symbol names"), TWO_DASHES },
346   { {"no-keep-memory", no_argument, NULL, OPTION_NO_KEEP_MEMORY},
347     '\0', NULL, N_("Use less memory and more disk I/O"), TWO_DASHES },
348   { {"no-undefined", no_argument, NULL, OPTION_NO_UNDEFINED},
349     '\0', NULL, N_("Do not allow unresolved references in object files"),
350     TWO_DASHES },
351   { {"allow-shlib-undefined", no_argument, NULL, OPTION_ALLOW_SHLIB_UNDEFINED},
352     '\0', NULL, N_("Allow unresolved references in shared libraries"),
353     TWO_DASHES },
354   { {"no-allow-shlib-undefined", no_argument, NULL,
355      OPTION_NO_ALLOW_SHLIB_UNDEFINED},
356     '\0', NULL, N_("Do not allow unresolved references in shared libs"),
357     TWO_DASHES },
358   { {"allow-multiple-definition", no_argument, NULL,
359      OPTION_ALLOW_MULTIPLE_DEFINITION},
360     '\0', NULL, N_("Allow multiple definitions"), TWO_DASHES },
361   { {"no-undefined-version", no_argument, NULL, OPTION_NO_UNDEFINED_VERSION},
362     '\0', NULL, N_("Disallow undefined version"), TWO_DASHES },
363   { {"default-symver", no_argument, NULL, OPTION_DEFAULT_SYMVER},
364     '\0', NULL, N_("Create default symbol version"), TWO_DASHES },
365   { {"default-imported-symver", no_argument, NULL,
366       OPTION_DEFAULT_IMPORTED_SYMVER},
367     '\0', NULL, N_("Create default symbol version for imported symbols"),
368     TWO_DASHES },
369   { {"no-warn-mismatch", no_argument, NULL, OPTION_NO_WARN_MISMATCH},
370     '\0', NULL, N_("Don't warn about mismatched input files"), TWO_DASHES},
371   { {"no-warn-search-mismatch", no_argument, NULL,
372      OPTION_NO_WARN_SEARCH_MISMATCH},
373     '\0', NULL, N_("Don't warn on finding an incompatible library"),
374     TWO_DASHES},
375   { {"no-whole-archive", no_argument, NULL, OPTION_NO_WHOLE_ARCHIVE},
376     '\0', NULL, N_("Turn off --whole-archive"), TWO_DASHES },
377   { {"noinhibit-exec", no_argument, NULL, OPTION_NOINHIBIT_EXEC},
378     '\0', NULL, N_("Create an output file even if errors occur"),
379     TWO_DASHES },
380   { {"noinhibit_exec", no_argument, NULL, OPTION_NOINHIBIT_EXEC},
381     '\0', NULL, NULL, NO_HELP },
382   { {"nostdlib", no_argument, NULL, OPTION_NOSTDLIB},
383     '\0', NULL, N_("Only use library directories specified on\n"
384                    "                                the command line"),
385     ONE_DASH },
386   { {"oformat", required_argument, NULL, OPTION_OFORMAT},
387     '\0', N_("TARGET"), N_("Specify target of output file"),
388     EXACTLY_TWO_DASHES },
389   { {"print-output-format", no_argument, NULL, OPTION_PRINT_OUTPUT_FORMAT},
390     '\0', NULL, N_("Print default output format"), TWO_DASHES },
391   { {"print-sysroot", no_argument, NULL, OPTION_PRINT_SYSROOT},
392     '\0', NULL, N_("Print current sysroot"), TWO_DASHES },
393   { {"qmagic", no_argument, NULL, OPTION_IGNORE},
394     '\0', NULL, N_("Ignored for Linux compatibility"), ONE_DASH },
395   { {"reduce-memory-overheads", no_argument, NULL,
396      OPTION_REDUCE_MEMORY_OVERHEADS},
397     '\0', NULL, N_("Reduce memory overheads, possibly taking much longer"),
398     TWO_DASHES },
399   { {"relax", no_argument, NULL, OPTION_RELAX},
400     '\0', NULL, N_("Reduce code size by using target specific optimizations"), TWO_DASHES },
401   { {"no-relax", no_argument, NULL, OPTION_NO_RELAX},
402     '\0', NULL, N_("Do not use relaxation techniques to reduce code size"), TWO_DASHES },
403   { {"retain-symbols-file", required_argument, NULL,
404      OPTION_RETAIN_SYMBOLS_FILE},
405     '\0', N_("FILE"), N_("Keep only symbols listed in FILE"), TWO_DASHES },
406   { {"rpath", required_argument, NULL, OPTION_RPATH},
407     '\0', N_("PATH"), N_("Set runtime shared library search path"), ONE_DASH },
408   { {"rpath-link", required_argument, NULL, OPTION_RPATH_LINK},
409     '\0', N_("PATH"), N_("Set link time shared library search path"),
410     ONE_DASH },
411   { {"shared", no_argument, NULL, OPTION_SHARED},
412     '\0', NULL, N_("Create a shared library"), ONE_DASH },
413   { {"Bshareable", no_argument, NULL, OPTION_SHARED }, /* FreeBSD.  */
414     '\0', NULL, NULL, ONE_DASH },
415   { {"pie", no_argument, NULL, OPTION_PIE},
416     '\0', NULL, N_("Create a position independent executable"), ONE_DASH },
417   { {"pic-executable", no_argument, NULL, OPTION_PIE},
418     '\0', NULL, NULL, TWO_DASHES },
419   { {"sort-common", optional_argument, NULL, OPTION_SORT_COMMON},
420     '\0', N_("[=ascending|descending]"),
421     N_("Sort common symbols by alignment [in specified order]"),
422     TWO_DASHES },
423   { {"sort_common", no_argument, NULL, OPTION_SORT_COMMON},
424     '\0', NULL, NULL, NO_HELP },
425   { {"sort-section", required_argument, NULL, OPTION_SORT_SECTION},
426     '\0', N_("name|alignment"),
427     N_("Sort sections by name or maximum alignment"), TWO_DASHES },
428   { {"spare-dynamic-tags", required_argument, NULL, OPTION_SPARE_DYNAMIC_TAGS},
429     '\0', N_("COUNT"), N_("How many tags to reserve in .dynamic section"),
430     TWO_DASHES },
431   { {"split-by-file", optional_argument, NULL, OPTION_SPLIT_BY_FILE},
432     '\0', N_("[=SIZE]"), N_("Split output sections every SIZE octets"),
433     TWO_DASHES },
434   { {"split-by-reloc", optional_argument, NULL, OPTION_SPLIT_BY_RELOC},
435     '\0', N_("[=COUNT]"), N_("Split output sections every COUNT relocs"),
436     TWO_DASHES },
437   { {"stats", no_argument, NULL, OPTION_STATS},
438     '\0', NULL, N_("Print memory usage statistics"), TWO_DASHES },
439   { {"target-help", no_argument, NULL, OPTION_TARGET_HELP},
440     '\0', NULL, N_("Display target specific options"), TWO_DASHES },
441   { {"task-link", required_argument, NULL, OPTION_TASK_LINK},
442     '\0', N_("SYMBOL"), N_("Do task level linking"), TWO_DASHES },
443   { {"traditional-format", no_argument, NULL, OPTION_TRADITIONAL_FORMAT},
444     '\0', NULL, N_("Use same format as native linker"), TWO_DASHES },
445   { {"section-start", required_argument, NULL, OPTION_SECTION_START},
446     '\0', N_("SECTION=ADDRESS"), N_("Set address of named section"),
447     TWO_DASHES },
448   { {"Tbss", required_argument, NULL, OPTION_TBSS},
449     '\0', N_("ADDRESS"), N_("Set address of .bss section"), ONE_DASH },
450   { {"Tdata", required_argument, NULL, OPTION_TDATA},
451     '\0', N_("ADDRESS"), N_("Set address of .data section"), ONE_DASH },
452   { {"Ttext", required_argument, NULL, OPTION_TTEXT},
453     '\0', N_("ADDRESS"), N_("Set address of .text section"), ONE_DASH },
454   { {"Ttext-segment", required_argument, NULL, OPTION_TTEXT_SEGMENT},
455     '\0', N_("ADDRESS"), N_("Set address of text segment"), ONE_DASH },
456   { {"Trodata-segment", required_argument, NULL, OPTION_TRODATA_SEGMENT},
457     '\0', N_("ADDRESS"), N_("Set address of rodata segment"), ONE_DASH },
458   { {"Tldata-segment", required_argument, NULL, OPTION_TLDATA_SEGMENT},
459     '\0', N_("ADDRESS"), N_("Set address of ldata segment"), ONE_DASH },
460   { {"unresolved-symbols=<method>", required_argument, NULL,
461      OPTION_UNRESOLVED_SYMBOLS},
462     '\0', NULL, N_("How to handle unresolved symbols.  <method> is:\n"
463                    "                                ignore-all, report-all, ignore-in-object-files,\n"
464                    "                                ignore-in-shared-libs"),
465     TWO_DASHES },
466   { {"verbose", optional_argument, NULL, OPTION_VERBOSE},
467     '\0', N_("[=NUMBER]"),
468     N_("Output lots of information during link"), TWO_DASHES },
469   { {"dll-verbose", no_argument, NULL, OPTION_VERBOSE}, /* Linux.  */
470     '\0', NULL, NULL, NO_HELP },
471   { {"version-script", required_argument, NULL, OPTION_VERSION_SCRIPT },
472     '\0', N_("FILE"), N_("Read version information script"), TWO_DASHES },
473   { {"version-exports-section", required_argument, NULL,
474      OPTION_VERSION_EXPORTS_SECTION },
475     '\0', N_("SYMBOL"), N_("Take export symbols list from .exports, using\n"
476                            "                                SYMBOL as the version."),
477     TWO_DASHES },
478   { {"dynamic-list-data", no_argument, NULL, OPTION_DYNAMIC_LIST_DATA},
479     '\0', NULL, N_("Add data symbols to dynamic list"), TWO_DASHES },
480   { {"dynamic-list-cpp-new", no_argument, NULL, OPTION_DYNAMIC_LIST_CPP_NEW},
481     '\0', NULL, N_("Use C++ operator new/delete dynamic list"), TWO_DASHES },
482   { {"dynamic-list-cpp-typeinfo", no_argument, NULL, OPTION_DYNAMIC_LIST_CPP_TYPEINFO},
483     '\0', NULL, N_("Use C++ typeinfo dynamic list"), TWO_DASHES },
484   { {"dynamic-list", required_argument, NULL, OPTION_DYNAMIC_LIST},
485     '\0', N_("FILE"), N_("Read dynamic list"), 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-alternate-em", no_argument, NULL, OPTION_WARN_ALTERNATE_EM},
502     '\0', NULL, N_("Warn if an object has alternate ELF machine code"),
503     TWO_DASHES },
504   { {"warn-unresolved-symbols", no_argument, NULL,
505      OPTION_WARN_UNRESOLVED_SYMBOLS},
506     '\0', NULL, N_("Report unresolved symbols as warnings"), TWO_DASHES },
507   { {"error-unresolved-symbols", no_argument, NULL,
508      OPTION_ERROR_UNRESOLVED_SYMBOLS},
509     '\0', NULL, N_("Report unresolved symbols as errors"), TWO_DASHES },
510   { {"whole-archive", no_argument, NULL, OPTION_WHOLE_ARCHIVE},
511     '\0', NULL, N_("Include all objects from following archives"),
512     TWO_DASHES },
513   { {"wrap", required_argument, NULL, OPTION_WRAP},
514     '\0', N_("SYMBOL"), N_("Use wrapper functions for SYMBOL"), TWO_DASHES },
515   { {"ignore-unresolved-symbol", required_argument, NULL,
516     OPTION_IGNORE_UNRESOLVED_SYMBOL},
517     '\0', N_("SYMBOL"),
518     N_("Unresolved SYMBOL will not cause an error or warning"), TWO_DASHES },
519   { {"push-state", no_argument, NULL, OPTION_PUSH_STATE},
520     '\0', NULL, N_("Push state of flags governing input file handling"),
521     TWO_DASHES },
522   { {"pop-state", no_argument, NULL, OPTION_POP_STATE},
523     '\0', NULL, N_("Pop state of flags governing input file handling"),
524     TWO_DASHES },
525   { {"print-memory-usage", no_argument, NULL, OPTION_PRINT_MEMORY_USAGE},
526     '\0', NULL, N_("Report target memory usage"), TWO_DASHES },
527   { {"orphan-handling", required_argument, NULL, OPTION_ORPHAN_HANDLING},
528     '\0', N_("=MODE"), N_("Control how orphan sections are handled."),
529     TWO_DASHES },
530 };
531
532 #define OPTION_COUNT ARRAY_SIZE (ld_options)
533
534 void
535 parse_args (unsigned argc, char **argv)
536 {
537   unsigned i;
538   int is, il, irl;
539   int ingroup = 0;
540   char *default_dirlist = NULL;
541   char *shortopts;
542   struct option *longopts;
543   struct option *really_longopts;
544   int last_optind;
545   enum report_method how_to_report_unresolved_symbols = RM_GENERATE_ERROR;
546
547   shortopts = (char *) xmalloc (OPTION_COUNT * 3 + 2);
548   longopts = (struct option *)
549       xmalloc (sizeof (*longopts) * (OPTION_COUNT + 1));
550   really_longopts = (struct option *)
551       malloc (sizeof (*really_longopts) * (OPTION_COUNT + 1));
552
553   /* Starting the short option string with '-' is for programs that
554      expect options and other ARGV-elements in any order and that care about
555      the ordering of the two.  We describe each non-option ARGV-element
556      as if it were the argument of an option with character code 1.  */
557   shortopts[0] = '-';
558   is = 1;
559   il = 0;
560   irl = 0;
561   for (i = 0; i < OPTION_COUNT; i++)
562     {
563       if (ld_options[i].shortopt != '\0')
564         {
565           shortopts[is] = ld_options[i].shortopt;
566           ++is;
567           if (ld_options[i].opt.has_arg == required_argument
568               || ld_options[i].opt.has_arg == optional_argument)
569             {
570               shortopts[is] = ':';
571               ++is;
572               if (ld_options[i].opt.has_arg == optional_argument)
573                 {
574                   shortopts[is] = ':';
575                   ++is;
576                 }
577             }
578         }
579       if (ld_options[i].opt.name != NULL)
580         {
581           if (ld_options[i].control == EXACTLY_TWO_DASHES)
582             {
583               really_longopts[irl] = ld_options[i].opt;
584               ++irl;
585             }
586           else
587             {
588               longopts[il] = ld_options[i].opt;
589               ++il;
590             }
591         }
592     }
593   shortopts[is] = '\0';
594   longopts[il].name = NULL;
595   really_longopts[irl].name = NULL;
596
597   ldemul_add_options (is, &shortopts, il, &longopts, irl, &really_longopts);
598
599   /* The -G option is ambiguous on different platforms.  Sometimes it
600      specifies the largest data size to put into the small data
601      section.  Sometimes it is equivalent to --shared.  Unfortunately,
602      the first form takes an argument, while the second does not.
603
604      We need to permit the --shared form because on some platforms,
605      such as Solaris, gcc -shared will pass -G to the linker.
606
607      To permit either usage, we look through the argument list.  If we
608      find -G not followed by a number, we change it into --shared.
609      This will work for most normal cases.  */
610   for (i = 1; i < argc; i++)
611     if (strcmp (argv[i], "-G") == 0
612         && (i + 1 >= argc
613             || ! ISDIGIT (argv[i + 1][0])))
614       argv[i] = (char *) "--shared";
615
616   /* Because we permit long options to start with a single dash, and
617      we have a --library option, and the -l option is conventionally
618      used with an immediately following argument, we can have bad
619      results if somebody tries to use -l with a library whose name
620      happens to start with "ibrary", as in -li.  We avoid problems by
621      simply turning -l into --library.  This means that users will
622      have to use two dashes in order to use --library, which is OK
623      since that's how it is documented.
624
625      FIXME: It's possible that this problem can arise for other short
626      options as well, although the user does always have the recourse
627      of adding a space between the option and the argument.  */
628   for (i = 1; i < argc; i++)
629     {
630       if (argv[i][0] == '-'
631           && argv[i][1] == 'l'
632           && argv[i][2] != '\0')
633         {
634           char *n;
635
636           n = (char *) xmalloc (strlen (argv[i]) + 20);
637           sprintf (n, "--library=%s", argv[i] + 2);
638           argv[i] = n;
639         }
640     }
641
642   last_optind = -1;
643   while (1)
644     {
645       int longind;
646       int optc;
647       static unsigned int defsym_count;
648
649       /* Using last_optind lets us avoid calling ldemul_parse_args
650          multiple times on a single option, which would lead to
651          confusion in the internal static variables maintained by
652          getopt.  This could otherwise happen for an argument like
653          -nx, in which the -n is parsed as a single option, and we
654          loop around to pick up the -x.  */
655       if (optind != last_optind)
656         if (ldemul_parse_args (argc, argv))
657           continue;
658
659       /* getopt_long_only is like getopt_long, but '-' as well as '--'
660          can indicate a long option.  */
661       opterr = 0;
662       last_optind = optind;
663       optc = getopt_long_only (argc, argv, shortopts, longopts, &longind);
664       if (optc == '?')
665         {
666           optind = last_optind;
667           optc = getopt_long (argc, argv, "-", really_longopts, &longind);
668         }
669
670       if (ldemul_handle_option (optc))
671         continue;
672
673       if (optc == -1)
674         break;
675
676       switch (optc)
677         {
678         case '?':
679           einfo (_("%P: unrecognized option '%s'\n"), argv[last_optind]);
680           /* Fall through.  */
681
682         default:
683           einfo (_("%P%F: use the --help option for usage information\n"));
684
685         case 1:                 /* File name.  */
686           lang_add_input_file (optarg, lang_input_file_is_file_enum, NULL);
687           break;
688
689         case OPTION_IGNORE:
690           break;
691         case 'a':
692           /* For HP/UX compatibility.  Actually -a shared should mean
693              ``use only shared libraries'' but, then, we don't
694              currently support shared libraries on HP/UX anyhow.  */
695           if (strcmp (optarg, "archive") == 0)
696             input_flags.dynamic = FALSE;
697           else if (strcmp (optarg, "shared") == 0
698                    || strcmp (optarg, "default") == 0)
699             input_flags.dynamic = TRUE;
700           else
701             einfo (_("%P%F: unrecognized -a option `%s'\n"), optarg);
702           break;
703         case OPTION_ASSERT:
704           /* FIXME: We just ignore these, but we should handle them.  */
705           if (strcmp (optarg, "definitions") == 0)
706             ;
707           else if (strcmp (optarg, "nodefinitions") == 0)
708             ;
709           else if (strcmp (optarg, "nosymbolic") == 0)
710             ;
711           else if (strcmp (optarg, "pure-text") == 0)
712             ;
713           else
714             einfo (_("%P%F: unrecognized -assert option `%s'\n"), optarg);
715           break;
716         case 'A':
717           ldfile_add_arch (optarg);
718           break;
719         case 'b':
720           lang_add_target (optarg);
721           break;
722         case 'c':
723           ldfile_open_command_file (optarg);
724           parser_input = input_mri_script;
725           yyparse ();
726           break;
727         case OPTION_CALL_SHARED:
728           input_flags.dynamic = TRUE;
729           break;
730         case OPTION_NON_SHARED:
731           input_flags.dynamic = FALSE;
732           break;
733         case OPTION_CREF:
734           command_line.cref = TRUE;
735           link_info.notice_all = TRUE;
736           break;
737         case 'd':
738           command_line.force_common_definition = TRUE;
739           break;
740         case OPTION_DEFSYM:
741           lex_string = optarg;
742           lex_redirect (optarg, "--defsym", ++defsym_count);
743           parser_input = input_defsym;
744           yyparse ();
745           lex_string = NULL;
746           break;
747         case OPTION_DEMANGLE:
748           demangling = TRUE;
749           if (optarg != NULL)
750             {
751               enum demangling_styles style;
752
753               style = cplus_demangle_name_to_style (optarg);
754               if (style == unknown_demangling)
755                 einfo (_("%F%P: unknown demangling style `%s'\n"),
756                        optarg);
757
758               cplus_demangle_set_style (style);
759             }
760           break;
761         case 'I':               /* Used on Solaris.  */
762         case OPTION_DYNAMIC_LINKER:
763           command_line.interpreter = optarg;
764           break;
765         case OPTION_SYSROOT:
766           /* Already handled in ldmain.c.  */
767           break;
768         case OPTION_EB:
769           command_line.endian = ENDIAN_BIG;
770           break;
771         case OPTION_EL:
772           command_line.endian = ENDIAN_LITTLE;
773           break;
774         case OPTION_EMBEDDED_RELOCS:
775           command_line.embedded_relocs = TRUE;
776           break;
777         case OPTION_EXPORT_DYNAMIC:
778         case 'E': /* HP/UX compatibility.  */
779           link_info.export_dynamic = TRUE;
780           break;
781         case OPTION_NO_EXPORT_DYNAMIC:
782           link_info.export_dynamic = FALSE;
783           break;
784         case 'e':
785           lang_add_entry (optarg, TRUE);
786           break;
787         case 'f':
788           if (command_line.auxiliary_filters == NULL)
789             {
790               command_line.auxiliary_filters = (char **)
791                   xmalloc (2 * sizeof (char *));
792               command_line.auxiliary_filters[0] = optarg;
793               command_line.auxiliary_filters[1] = NULL;
794             }
795           else
796             {
797               int c;
798               char **p;
799
800               c = 0;
801               for (p = command_line.auxiliary_filters; *p != NULL; p++)
802                 ++c;
803               command_line.auxiliary_filters = (char **)
804                   xrealloc (command_line.auxiliary_filters,
805                             (c + 2) * sizeof (char *));
806               command_line.auxiliary_filters[c] = optarg;
807               command_line.auxiliary_filters[c + 1] = NULL;
808             }
809           break;
810         case 'F':
811           command_line.filter_shlib = optarg;
812           break;
813         case OPTION_FORCE_EXE_SUFFIX:
814           command_line.force_exe_suffix = TRUE;
815           break;
816         case 'G':
817           {
818             char *end;
819             g_switch_value = strtoul (optarg, &end, 0);
820             if (*end)
821               einfo (_("%P%F: invalid number `%s'\n"), optarg);
822           }
823           break;
824         case 'g':
825           /* Ignore.  */
826           break;
827         case OPTION_GC_SECTIONS:
828           link_info.gc_sections = TRUE;
829           break;
830         case OPTION_PRINT_GC_SECTIONS:
831           link_info.print_gc_sections = TRUE;
832           break;
833         case OPTION_HELP:
834           help ();
835           xexit (0);
836           break;
837         case 'L':
838           ldfile_add_library_path (optarg, TRUE);
839           break;
840         case 'l':
841           lang_add_input_file (optarg, lang_input_file_is_l_enum, NULL);
842           break;
843         case 'M':
844           config.map_filename = "-";
845           break;
846         case 'm':
847           /* Ignore.  Was handled in a pre-parse.   */
848           break;
849         case OPTION_MAP:
850           config.map_filename = optarg;
851           break;
852         case 'N':
853           config.text_read_only = FALSE;
854           config.magic_demand_paged = FALSE;
855           input_flags.dynamic = FALSE;
856           break;
857         case OPTION_NO_OMAGIC:
858           config.text_read_only = TRUE;
859           config.magic_demand_paged = TRUE;
860           /* NB/ Does not set input_flags.dynamic to TRUE.
861              Use --call-shared or -Bdynamic for this.  */
862           break;
863         case 'n':
864           config.magic_demand_paged = FALSE;
865           input_flags.dynamic = FALSE;
866           break;
867         case OPTION_NO_DEFINE_COMMON:
868           command_line.inhibit_common_definition = TRUE;
869           break;
870         case OPTION_NO_DEMANGLE:
871           demangling = FALSE;
872           break;
873         case OPTION_NO_GC_SECTIONS:
874           link_info.gc_sections = FALSE;
875           break;
876         case OPTION_NO_PRINT_GC_SECTIONS:
877           link_info.print_gc_sections = FALSE;
878           break;
879         case OPTION_NO_KEEP_MEMORY:
880           link_info.keep_memory = FALSE;
881           break;
882         case OPTION_NO_UNDEFINED:
883           link_info.unresolved_syms_in_objects
884             = how_to_report_unresolved_symbols;
885           break;
886         case OPTION_ALLOW_SHLIB_UNDEFINED:
887           link_info.unresolved_syms_in_shared_libs = RM_IGNORE;
888           break;
889         case OPTION_NO_ALLOW_SHLIB_UNDEFINED:
890           link_info.unresolved_syms_in_shared_libs
891             = how_to_report_unresolved_symbols;
892           break;
893         case OPTION_UNRESOLVED_SYMBOLS:
894           if (strcmp (optarg, "ignore-all") == 0)
895             {
896               link_info.unresolved_syms_in_objects = RM_IGNORE;
897               link_info.unresolved_syms_in_shared_libs = RM_IGNORE;
898             }
899           else if (strcmp (optarg, "report-all") == 0)
900             {
901               link_info.unresolved_syms_in_objects
902                 = how_to_report_unresolved_symbols;
903               link_info.unresolved_syms_in_shared_libs
904                 = how_to_report_unresolved_symbols;
905             }
906           else if (strcmp (optarg, "ignore-in-object-files") == 0)
907             {
908               link_info.unresolved_syms_in_objects = RM_IGNORE;
909               link_info.unresolved_syms_in_shared_libs
910                 = how_to_report_unresolved_symbols;
911             }
912           else if (strcmp (optarg, "ignore-in-shared-libs") == 0)
913             {
914               link_info.unresolved_syms_in_objects
915                 = how_to_report_unresolved_symbols;
916               link_info.unresolved_syms_in_shared_libs = RM_IGNORE;
917             }
918           else
919             einfo (_("%P%F: bad --unresolved-symbols option: %s\n"), optarg);
920           break;
921         case OPTION_WARN_UNRESOLVED_SYMBOLS:
922           how_to_report_unresolved_symbols = RM_GENERATE_WARNING;
923           if (link_info.unresolved_syms_in_objects == RM_GENERATE_ERROR)
924             link_info.unresolved_syms_in_objects = RM_GENERATE_WARNING;
925           if (link_info.unresolved_syms_in_shared_libs == RM_GENERATE_ERROR)
926             link_info.unresolved_syms_in_shared_libs = RM_GENERATE_WARNING;
927           break;
928
929         case OPTION_ERROR_UNRESOLVED_SYMBOLS:
930           how_to_report_unresolved_symbols = RM_GENERATE_ERROR;
931           if (link_info.unresolved_syms_in_objects == RM_GENERATE_WARNING)
932             link_info.unresolved_syms_in_objects = RM_GENERATE_ERROR;
933           if (link_info.unresolved_syms_in_shared_libs == RM_GENERATE_WARNING)
934             link_info.unresolved_syms_in_shared_libs = RM_GENERATE_ERROR;
935           break;
936         case OPTION_ALLOW_MULTIPLE_DEFINITION:
937           link_info.allow_multiple_definition = TRUE;
938           break;
939         case OPTION_NO_UNDEFINED_VERSION:
940           link_info.allow_undefined_version = FALSE;
941           break;
942         case OPTION_DEFAULT_SYMVER:
943           link_info.create_default_symver = TRUE;
944           break;
945         case OPTION_DEFAULT_IMPORTED_SYMVER:
946           link_info.default_imported_symver = TRUE;
947           break;
948         case OPTION_NO_WARN_MISMATCH:
949           command_line.warn_mismatch = FALSE;
950           break;
951         case OPTION_NO_WARN_SEARCH_MISMATCH:
952           command_line.warn_search_mismatch = FALSE;
953           break;
954         case OPTION_NOINHIBIT_EXEC:
955           force_make_executable = TRUE;
956           break;
957         case OPTION_NOSTDLIB:
958           config.only_cmd_line_lib_dirs = TRUE;
959           break;
960         case OPTION_NO_WHOLE_ARCHIVE:
961           input_flags.whole_archive = FALSE;
962           break;
963         case 'O':
964           /* FIXME "-O<non-digits> <value>" used to set the address of
965              section <non-digits>.  Was this for compatibility with
966              something, or can we create a new option to do that
967              (with a syntax similar to -defsym)?
968              getopt can't handle two args to an option without kludges.  */
969
970           /* Enable optimizations of output files.  */
971           link_info.optimize = strtoul (optarg, NULL, 0) ? TRUE : FALSE;
972           break;
973         case 'o':
974           lang_add_output (optarg, 0);
975           break;
976         case OPTION_OFORMAT:
977           lang_add_output_format (optarg, NULL, NULL, 0);
978           break;
979         case OPTION_PRINT_SYSROOT:
980           if (*ld_sysroot)
981             puts (ld_sysroot);
982           xexit (0);
983           break;
984         case OPTION_PRINT_OUTPUT_FORMAT:
985           command_line.print_output_format = TRUE;
986           break;
987 #ifdef ENABLE_PLUGINS
988         case OPTION_PLUGIN:
989           plugin_opt_plugin (optarg);
990           break;
991         case OPTION_PLUGIN_OPT:
992           if (plugin_opt_plugin_arg (optarg))
993             einfo(_("%P%F: bad -plugin-opt option\n"));
994           break;
995 #endif /* ENABLE_PLUGINS */
996         case 'q':
997           link_info.emitrelocations = TRUE;
998           break;
999         case 'i':
1000         case 'r':
1001           if (optind == last_optind)
1002             /* This can happen if the user put "-rpath,a" on the command
1003                line.  (Or something similar.  The comma is important).
1004                Getopt becomes confused and thinks that this is a -r option
1005                but it cannot parse the text after the -r so it refuses to
1006                increment the optind counter.  Detect this case and issue
1007                an error message here.  We cannot just make this a warning,
1008                increment optind, and continue because getopt is too confused
1009                and will seg-fault the next time around.  */
1010             einfo(_("%P%F: unrecognised option: %s\n"), argv[optind]);
1011
1012           if (bfd_link_pic (&link_info))
1013             einfo (_("%P%F: -r and %s may not be used together\n"),
1014                      bfd_link_dll (&link_info) ? "-shared" : "-pie");
1015
1016           link_info.type = type_relocatable;
1017           config.build_constructors = FALSE;
1018           config.magic_demand_paged = FALSE;
1019           config.text_read_only = FALSE;
1020           input_flags.dynamic = FALSE;
1021           break;
1022         case 'R':
1023           /* The GNU linker traditionally uses -R to mean to include
1024              only the symbols from a file.  The Solaris linker uses -R
1025              to set the path used by the runtime linker to find
1026              libraries.  This is the GNU linker -rpath argument.  We
1027              try to support both simultaneously by checking the file
1028              named.  If it is a directory, rather than a regular file,
1029              we assume -rpath was meant.  */
1030           {
1031             struct stat s;
1032
1033             if (stat (optarg, &s) >= 0
1034                 && ! S_ISDIR (s.st_mode))
1035               {
1036                 lang_add_input_file (optarg,
1037                                      lang_input_file_is_symbols_only_enum,
1038                                      NULL);
1039                 break;
1040               }
1041           }
1042           /* Fall through.  */
1043         case OPTION_RPATH:
1044           if (command_line.rpath == NULL)
1045             command_line.rpath = xstrdup (optarg);
1046           else
1047             {
1048               size_t rpath_len = strlen (command_line.rpath);
1049               size_t optarg_len = strlen (optarg);
1050               char *buf;
1051               char *cp = command_line.rpath;
1052
1053               /* First see whether OPTARG is already in the path.  */
1054               do
1055                 {
1056                   if (strncmp (optarg, cp, optarg_len) == 0
1057                       && (cp[optarg_len] == 0
1058                           || cp[optarg_len] == config.rpath_separator))
1059                     /* We found it.  */
1060                     break;
1061
1062                   /* Not yet found.  */
1063                   cp = strchr (cp, config.rpath_separator);
1064                   if (cp != NULL)
1065                     ++cp;
1066                 }
1067               while (cp != NULL);
1068
1069               if (cp == NULL)
1070                 {
1071                   buf = (char *) xmalloc (rpath_len + optarg_len + 2);
1072                   sprintf (buf, "%s%c%s", command_line.rpath,
1073                            config.rpath_separator, optarg);
1074                   free (command_line.rpath);
1075                   command_line.rpath = buf;
1076                 }
1077             }
1078           break;
1079         case OPTION_RPATH_LINK:
1080           if (command_line.rpath_link == NULL)
1081             command_line.rpath_link = xstrdup (optarg);
1082           else
1083             {
1084               char *buf;
1085
1086               buf = (char *) xmalloc (strlen (command_line.rpath_link)
1087                                       + strlen (optarg)
1088                                       + 2);
1089               sprintf (buf, "%s%c%s", command_line.rpath_link,
1090                        config.rpath_separator, optarg);
1091               free (command_line.rpath_link);
1092               command_line.rpath_link = buf;
1093             }
1094           break;
1095         case OPTION_NO_RELAX:
1096           DISABLE_RELAXATION;
1097           break;
1098         case OPTION_RELAX:
1099           ENABLE_RELAXATION;
1100           break;
1101         case OPTION_RETAIN_SYMBOLS_FILE:
1102           add_keepsyms_file (optarg);
1103           break;
1104         case 'S':
1105           link_info.strip = strip_debugger;
1106           break;
1107         case 's':
1108           link_info.strip = strip_all;
1109           break;
1110         case OPTION_STRIP_DISCARDED:
1111           link_info.strip_discarded = TRUE;
1112           break;
1113         case OPTION_NO_STRIP_DISCARDED:
1114           link_info.strip_discarded = FALSE;
1115           break;
1116         case OPTION_SHARED:
1117           if (config.has_shared)
1118             {
1119               if (bfd_link_relocatable (&link_info))
1120                 einfo (_("%P%F: -r and -shared may not be used together\n"));
1121
1122               link_info.type = type_dll;
1123               /* When creating a shared library, the default
1124                  behaviour is to ignore any unresolved references.  */
1125               if (link_info.unresolved_syms_in_objects == RM_NOT_YET_SET)
1126                 link_info.unresolved_syms_in_objects = RM_IGNORE;
1127               if (link_info.unresolved_syms_in_shared_libs == RM_NOT_YET_SET)
1128                 link_info.unresolved_syms_in_shared_libs = RM_IGNORE;
1129             }
1130           else
1131             einfo (_("%P%F: -shared not supported\n"));
1132           break;
1133         case OPTION_PIE:
1134           if (config.has_shared)
1135             {
1136               if (bfd_link_relocatable (&link_info))
1137                 einfo (_("%P%F: -r and -pie may not be used together\n"));
1138
1139               link_info.type = type_pie;
1140             }
1141           else
1142             einfo (_("%P%F: -pie not supported\n"));
1143           break;
1144         case 'h':               /* Used on Solaris.  */
1145         case OPTION_SONAME:
1146           if (optarg[0] == '\0' && command_line.soname
1147               && command_line.soname[0])
1148             einfo (_("%P: SONAME must not be empty string; keeping previous one\n"));
1149           else
1150             command_line.soname = optarg;
1151           break;
1152         case OPTION_SORT_COMMON:
1153           if (optarg == NULL
1154               || strcmp (optarg, N_("descending")) == 0)
1155             config.sort_common = sort_descending;
1156           else if (strcmp (optarg, N_("ascending")) == 0)
1157             config.sort_common = sort_ascending;
1158           else
1159             einfo (_("%P%F: invalid common section sorting option: %s\n"),
1160                    optarg);
1161           break;
1162         case OPTION_SORT_SECTION:
1163           if (strcmp (optarg, N_("name")) == 0)
1164             sort_section = by_name;
1165           else if (strcmp (optarg, N_("alignment")) == 0)
1166             sort_section = by_alignment;
1167           else
1168             einfo (_("%P%F: invalid section sorting option: %s\n"),
1169                    optarg);
1170           break;
1171         case OPTION_STATS:
1172           config.stats = TRUE;
1173           break;
1174         case OPTION_SYMBOLIC:
1175           command_line.symbolic = symbolic;
1176           break;
1177         case OPTION_SYMBOLIC_FUNCTIONS:
1178           command_line.symbolic = symbolic_functions;
1179           break;
1180         case 't':
1181           trace_files = TRUE;
1182           break;
1183         case 'T':
1184           previous_script_handle = saved_script_handle;
1185           ldfile_open_command_file (optarg);
1186           parser_input = input_script;
1187           yyparse ();
1188           previous_script_handle = NULL;
1189           break;
1190         case OPTION_DEFAULT_SCRIPT:
1191           command_line.default_script = optarg;
1192           break;
1193         case OPTION_SECTION_START:
1194           {
1195             char *optarg2;
1196             char *sec_name;
1197             int len;
1198
1199             /* Check for <something>=<somthing>...  */
1200             optarg2 = strchr (optarg, '=');
1201             if (optarg2 == NULL)
1202               einfo (_("%P%F: invalid argument to option"
1203                        " \"--section-start\"\n"));
1204
1205             optarg2++;
1206
1207             /* So far so good.  Are all the args present?  */
1208             if ((*optarg == '\0') || (*optarg2 == '\0'))
1209               einfo (_("%P%F: missing argument(s) to option"
1210                        " \"--section-start\"\n"));
1211
1212             /* We must copy the section name as set_section_start
1213                doesn't do it for us.  */
1214             len = optarg2 - optarg;
1215             sec_name = (char *) xmalloc (len);
1216             memcpy (sec_name, optarg, len - 1);
1217             sec_name[len - 1] = 0;
1218
1219             /* Then set it...  */
1220             set_section_start (sec_name, optarg2);
1221           }
1222           break;
1223         case OPTION_TARGET_HELP:
1224           /* Mention any target specific options.  */
1225           ldemul_list_emulation_options (stdout);
1226           exit (0);
1227         case OPTION_TBSS:
1228           set_segment_start (".bss", optarg);
1229           break;
1230         case OPTION_TDATA:
1231           set_segment_start (".data", optarg);
1232           break;
1233         case OPTION_TTEXT:
1234           set_segment_start (".text", optarg);
1235           break;
1236         case OPTION_TTEXT_SEGMENT:
1237           set_segment_start (".text-segment", optarg);
1238           break;
1239         case OPTION_TRODATA_SEGMENT:
1240           set_segment_start (".rodata-segment", optarg);
1241           break;
1242         case OPTION_TLDATA_SEGMENT:
1243           set_segment_start (".ldata-segment", optarg);
1244           break;
1245         case OPTION_TRADITIONAL_FORMAT:
1246           link_info.traditional_format = TRUE;
1247           break;
1248         case OPTION_TASK_LINK:
1249           link_info.task_link = TRUE;
1250           /* Fall through - do an implied -r option.  */
1251         case OPTION_UR:
1252           if (bfd_link_pic (&link_info))
1253             einfo (_("%P%F: -r and %s may not be used together\n"),
1254                      bfd_link_dll (&link_info) ? "-shared" : "-pie");
1255
1256           link_info.type = type_relocatable;
1257           config.build_constructors = TRUE;
1258           config.magic_demand_paged = FALSE;
1259           config.text_read_only = FALSE;
1260           input_flags.dynamic = FALSE;
1261           break;
1262         case 'u':
1263           ldlang_add_undef (optarg, TRUE);
1264           break;
1265         case OPTION_REQUIRE_DEFINED_SYMBOL:
1266           ldlang_add_require_defined (optarg);
1267           break;
1268         case OPTION_UNIQUE:
1269           if (optarg != NULL)
1270             lang_add_unique (optarg);
1271           else
1272             config.unique_orphan_sections = TRUE;
1273           break;
1274         case OPTION_VERBOSE:
1275           ldversion (1);
1276           version_printed = TRUE;
1277           verbose = TRUE;
1278           overflow_cutoff_limit = -2;
1279           if (optarg != NULL)
1280             {
1281               char *end;
1282               int level ATTRIBUTE_UNUSED = strtoul (optarg, &end, 0);
1283               if (*end)
1284                 einfo (_("%P%F: invalid number `%s'\n"), optarg);
1285 #ifdef ENABLE_PLUGINS
1286               report_plugin_symbols = level > 1;
1287 #endif /* ENABLE_PLUGINS */
1288             }
1289           break;
1290         case 'v':
1291           ldversion (0);
1292           version_printed = TRUE;
1293           break;
1294         case 'V':
1295           ldversion (1);
1296           version_printed = TRUE;
1297           break;
1298         case OPTION_VERSION:
1299           ldversion (2);
1300           xexit (0);
1301           break;
1302         case OPTION_VERSION_SCRIPT:
1303           /* This option indicates a small script that only specifies
1304              version information.  Read it, but don't assume that
1305              we've seen a linker script.  */
1306           {
1307             FILE *hold_script_handle;
1308
1309             hold_script_handle = saved_script_handle;
1310             ldfile_open_command_file (optarg);
1311             saved_script_handle = hold_script_handle;
1312             parser_input = input_version_script;
1313             yyparse ();
1314           }
1315           break;
1316         case OPTION_VERSION_EXPORTS_SECTION:
1317           /* This option records a version symbol to be applied to the
1318              symbols listed for export to be found in the object files
1319              .exports sections.  */
1320           command_line.version_exports_section = optarg;
1321           break;
1322         case OPTION_DYNAMIC_LIST_DATA:
1323           command_line.dynamic_list = dynamic_list_data;
1324           if (command_line.symbolic == symbolic)
1325             command_line.symbolic = symbolic_unset;
1326           break;
1327         case OPTION_DYNAMIC_LIST_CPP_TYPEINFO:
1328           lang_append_dynamic_list_cpp_typeinfo ();
1329           if (command_line.dynamic_list != dynamic_list_data)
1330             command_line.dynamic_list = dynamic_list;
1331           if (command_line.symbolic == symbolic)
1332             command_line.symbolic = symbolic_unset;
1333           break;
1334         case OPTION_DYNAMIC_LIST_CPP_NEW:
1335           lang_append_dynamic_list_cpp_new ();
1336           if (command_line.dynamic_list != dynamic_list_data)
1337             command_line.dynamic_list = dynamic_list;
1338           if (command_line.symbolic == symbolic)
1339             command_line.symbolic = symbolic_unset;
1340           break;
1341         case OPTION_DYNAMIC_LIST:
1342           /* This option indicates a small script that only specifies
1343              a dynamic list.  Read it, but don't assume that we've
1344              seen a linker script.  */
1345           {
1346             FILE *hold_script_handle;
1347
1348             hold_script_handle = saved_script_handle;
1349             ldfile_open_command_file (optarg);
1350             saved_script_handle = hold_script_handle;
1351             parser_input = input_dynamic_list;
1352             yyparse ();
1353           }
1354           if (command_line.dynamic_list != dynamic_list_data)
1355             command_line.dynamic_list = dynamic_list;
1356           if (command_line.symbolic == symbolic)
1357             command_line.symbolic = symbolic_unset;
1358           break;
1359         case OPTION_WARN_COMMON:
1360           config.warn_common = TRUE;
1361           break;
1362         case OPTION_WARN_CONSTRUCTORS:
1363           config.warn_constructors = TRUE;
1364           break;
1365         case OPTION_WARN_FATAL:
1366           config.fatal_warnings = TRUE;
1367           break;
1368         case OPTION_NO_WARN_FATAL:
1369           config.fatal_warnings = FALSE;
1370           break;
1371         case OPTION_WARN_MULTIPLE_GP:
1372           config.warn_multiple_gp = TRUE;
1373           break;
1374         case OPTION_WARN_ONCE:
1375           config.warn_once = TRUE;
1376           break;
1377         case OPTION_WARN_SECTION_ALIGN:
1378           config.warn_section_align = TRUE;
1379           break;
1380         case OPTION_WARN_SHARED_TEXTREL:
1381           link_info.warn_shared_textrel = TRUE;
1382           break;
1383         case OPTION_WARN_ALTERNATE_EM:
1384           link_info.warn_alternate_em = TRUE;
1385           break;
1386         case OPTION_WHOLE_ARCHIVE:
1387           input_flags.whole_archive = TRUE;
1388           break;
1389         case OPTION_ADD_DT_NEEDED_FOR_DYNAMIC:
1390           input_flags.add_DT_NEEDED_for_dynamic = TRUE;
1391           break;
1392         case OPTION_NO_ADD_DT_NEEDED_FOR_DYNAMIC:
1393           input_flags.add_DT_NEEDED_for_dynamic = FALSE;
1394           break;
1395         case OPTION_ADD_DT_NEEDED_FOR_REGULAR:
1396           input_flags.add_DT_NEEDED_for_regular = TRUE;
1397           break;
1398         case OPTION_NO_ADD_DT_NEEDED_FOR_REGULAR:
1399           input_flags.add_DT_NEEDED_for_regular = FALSE;
1400           break;
1401         case OPTION_WRAP:
1402           add_wrap (optarg);
1403           break;
1404         case OPTION_IGNORE_UNRESOLVED_SYMBOL:
1405           add_ignoresym (&link_info, optarg);
1406           break;
1407         case OPTION_DISCARD_NONE:
1408           link_info.discard = discard_none;
1409           break;
1410         case 'X':
1411           link_info.discard = discard_l;
1412           break;
1413         case 'x':
1414           link_info.discard = discard_all;
1415           break;
1416         case 'Y':
1417           if (CONST_STRNEQ (optarg, "P,"))
1418             optarg += 2;
1419           if (default_dirlist != NULL)
1420             free (default_dirlist);
1421           default_dirlist = xstrdup (optarg);
1422           break;
1423         case 'y':
1424           add_ysym (optarg);
1425           break;
1426         case OPTION_SPARE_DYNAMIC_TAGS:
1427           link_info.spare_dynamic_tags = strtoul (optarg, NULL, 0);
1428           break;
1429         case OPTION_SPLIT_BY_RELOC:
1430           if (optarg != NULL)
1431             config.split_by_reloc = strtoul (optarg, NULL, 0);
1432           else
1433             config.split_by_reloc = 32768;
1434           break;
1435         case OPTION_SPLIT_BY_FILE:
1436           if (optarg != NULL)
1437             config.split_by_file = bfd_scan_vma (optarg, NULL, 0);
1438           else
1439             config.split_by_file = 1;
1440           break;
1441         case OPTION_CHECK_SECTIONS:
1442           command_line.check_section_addresses = 1;
1443           break;
1444         case OPTION_NO_CHECK_SECTIONS:
1445           command_line.check_section_addresses = 0;
1446           break;
1447         case OPTION_ACCEPT_UNKNOWN_INPUT_ARCH:
1448           command_line.accept_unknown_input_arch = TRUE;
1449           break;
1450         case OPTION_NO_ACCEPT_UNKNOWN_INPUT_ARCH:
1451           command_line.accept_unknown_input_arch = FALSE;
1452           break;
1453         case '(':
1454           lang_enter_group ();
1455           ingroup++;
1456           break;
1457         case ')':
1458           if (! ingroup)
1459             einfo (_("%P%F: group ended before it began (--help for usage)\n"));
1460
1461           lang_leave_group ();
1462           ingroup--;
1463           break;
1464
1465         case OPTION_INIT:
1466           link_info.init_function = optarg;
1467           break;
1468
1469         case OPTION_FINI:
1470           link_info.fini_function = optarg;
1471           break;
1472
1473         case OPTION_REDUCE_MEMORY_OVERHEADS:
1474           link_info.reduce_memory_overheads = TRUE;
1475           if (config.hash_table_size == 0)
1476             config.hash_table_size = 1021;
1477           break;
1478
1479         case OPTION_HASH_SIZE:
1480           {
1481             bfd_size_type new_size;
1482
1483             new_size = strtoul (optarg, NULL, 0);
1484             if (new_size)
1485               config.hash_table_size = new_size;
1486             else
1487               einfo (_("%P%X: --hash-size needs a numeric argument\n"));
1488           }
1489           break;
1490
1491         case OPTION_PUSH_STATE:
1492           input_flags.pushed = xmemdup (&input_flags,
1493                                         sizeof (input_flags),
1494                                         sizeof (input_flags));
1495           break;
1496
1497         case OPTION_POP_STATE:
1498           if (input_flags.pushed == NULL)
1499             einfo (_("%P%F: no state pushed before popping\n"));
1500           else
1501             {
1502               struct lang_input_statement_flags *oldp = input_flags.pushed;
1503               memcpy (&input_flags, oldp, sizeof (input_flags));
1504               free (oldp);
1505             }
1506           break;
1507
1508         case OPTION_PRINT_MEMORY_USAGE:
1509           command_line.print_memory_usage = TRUE;
1510           break;
1511
1512         case OPTION_ORPHAN_HANDLING:
1513           if (strcasecmp (optarg, "place") == 0)
1514             config.orphan_handling = orphan_handling_place;
1515           else if (strcasecmp (optarg, "warn") == 0)
1516             config.orphan_handling = orphan_handling_warn;
1517           else if (strcasecmp (optarg, "error") == 0)
1518             config.orphan_handling = orphan_handling_error;
1519           else if (strcasecmp (optarg, "discard") == 0)
1520             config.orphan_handling = orphan_handling_discard;
1521           else
1522             einfo (_("%P%F: invalid argument to option"
1523                      " \"--orphan-handling\"\n"));
1524           break;
1525         }
1526     }
1527
1528   if (command_line.soname && command_line.soname[0] == '\0')
1529     {
1530       einfo (_("%P: SONAME must not be empty string; ignored\n"));
1531       command_line.soname = NULL;
1532     }
1533
1534   while (ingroup)
1535     {
1536       lang_leave_group ();
1537       ingroup--;
1538     }
1539
1540   if (default_dirlist != NULL)
1541     {
1542       set_default_dirlist (default_dirlist);
1543       free (default_dirlist);
1544     }
1545
1546   if (link_info.unresolved_syms_in_objects == RM_NOT_YET_SET)
1547     /* FIXME: Should we allow emulations a chance to set this ?  */
1548     link_info.unresolved_syms_in_objects = how_to_report_unresolved_symbols;
1549
1550   if (link_info.unresolved_syms_in_shared_libs == RM_NOT_YET_SET)
1551     /* FIXME: Should we allow emulations a chance to set this ?  */
1552     link_info.unresolved_syms_in_shared_libs = how_to_report_unresolved_symbols;
1553
1554   if (bfd_link_relocatable (&link_info)
1555       && command_line.check_section_addresses < 0)
1556     command_line.check_section_addresses = 0;
1557
1558   /* We may have -Bsymbolic, -Bsymbolic-functions, --dynamic-list-data,
1559      --dynamic-list-cpp-new, --dynamic-list-cpp-typeinfo and
1560      --dynamic-list FILE.  -Bsymbolic and -Bsymbolic-functions are
1561      for shared libraries.  -Bsymbolic overrides all others and vice
1562      versa.  */
1563   switch (command_line.symbolic)
1564     {
1565     case symbolic_unset:
1566       break;
1567     case symbolic:
1568       /* -Bsymbolic is for shared library only.  */
1569       if (bfd_link_dll (&link_info))
1570         {
1571           link_info.symbolic = TRUE;
1572           /* Should we free the unused memory?  */
1573           link_info.dynamic_list = NULL;
1574           command_line.dynamic_list = dynamic_list_unset;
1575         }
1576       break;
1577     case symbolic_functions:
1578       /* -Bsymbolic-functions is for shared library only.  */
1579       if (bfd_link_dll (&link_info))
1580         command_line.dynamic_list = dynamic_list_data;
1581       break;
1582     }
1583
1584   switch (command_line.dynamic_list)
1585     {
1586     case dynamic_list_unset:
1587       break;
1588     case dynamic_list_data:
1589       link_info.dynamic_data = TRUE;
1590     case dynamic_list:
1591       link_info.dynamic = TRUE;
1592       break;
1593     }
1594
1595   if (!bfd_link_dll (&link_info))
1596     {
1597       if (command_line.filter_shlib)
1598         einfo (_("%P%F: -F may not be used without -shared\n"));
1599       if (command_line.auxiliary_filters)
1600         einfo (_("%P%F: -f may not be used without -shared\n"));
1601     }
1602
1603   /* Treat ld -r -s as ld -r -S -x (i.e., strip all local symbols).  I
1604      don't see how else this can be handled, since in this case we
1605      must preserve all externally visible symbols.  */
1606   if (bfd_link_relocatable (&link_info) && link_info.strip == strip_all)
1607     {
1608       link_info.strip = strip_debugger;
1609       if (link_info.discard == discard_sec_merge)
1610         link_info.discard = discard_all;
1611     }
1612 }
1613
1614 /* Add the (colon-separated) elements of DIRLIST_PTR to the
1615    library search path.  */
1616
1617 static void
1618 set_default_dirlist (char *dirlist_ptr)
1619 {
1620   char *p;
1621
1622   while (1)
1623     {
1624       p = strchr (dirlist_ptr, PATH_SEPARATOR);
1625       if (p != NULL)
1626         *p = '\0';
1627       if (*dirlist_ptr != '\0')
1628         ldfile_add_library_path (dirlist_ptr, TRUE);
1629       if (p == NULL)
1630         break;
1631       dirlist_ptr = p + 1;
1632     }
1633 }
1634
1635 static void
1636 set_section_start (char *sect, char *valstr)
1637 {
1638   const char *end;
1639   bfd_vma val = bfd_scan_vma (valstr, &end, 16);
1640   if (*end)
1641     einfo (_("%P%F: invalid hex number `%s'\n"), valstr);
1642   lang_section_start (sect, exp_intop (val), NULL);
1643 }
1644
1645 static void
1646 set_segment_start (const char *section, char *valstr)
1647 {
1648   const char *name;
1649   const char *end;
1650   segment_type *seg;
1651
1652   bfd_vma val = bfd_scan_vma (valstr, &end, 16);
1653   if (*end)
1654     einfo (_("%P%F: invalid hex number `%s'\n"), valstr);
1655   /* If we already have an entry for this segment, update the existing
1656      value.  */
1657   name = section + 1;
1658   for (seg = segments; seg; seg = seg->next)
1659     if (strcmp (seg->name, name) == 0)
1660       {
1661         seg->value = val;
1662         return;
1663       }
1664   /* There was no existing value so we must create a new segment
1665      entry.  */
1666   seg = (segment_type *) stat_alloc (sizeof (*seg));
1667   seg->name = name;
1668   seg->value = val;
1669   seg->used = FALSE;
1670   /* Add it to the linked list of segments.  */
1671   seg->next = segments;
1672   segments = seg;
1673   /* Historically, -Ttext and friends set the base address of a
1674      particular section.  For backwards compatibility, we still do
1675      that.  If a SEGMENT_START directive is seen, the section address
1676      assignment will be disabled.  */
1677   lang_section_start (section, exp_intop (val), seg);
1678 }
1679
1680 static void
1681 elf_shlib_list_options (FILE *file)
1682 {
1683   fprintf (file, _("\
1684   --audit=AUDITLIB            Specify a library to use for auditing\n"));
1685   fprintf (file, _("\
1686   -Bgroup                     Selects group name lookup rules for DSO\n"));
1687   fprintf (file, _("\
1688   --disable-new-dtags         Disable new dynamic tags\n"));
1689   fprintf (file, _("\
1690   --enable-new-dtags          Enable new dynamic tags\n"));
1691   fprintf (file, _("\
1692   --eh-frame-hdr              Create .eh_frame_hdr section\n"));
1693   fprintf (file, _("\
1694   --exclude-libs=LIBS         Make all symbols in LIBS hidden\n"));
1695   fprintf (file, _("\
1696   --hash-style=STYLE          Set hash style to sysv, gnu or both\n"));
1697   fprintf (file, _("\
1698   -P AUDITLIB, --depaudit=AUDITLIB\n" "\
1699                               Specify a library to use for auditing dependencies\n"));
1700   fprintf (file, _("\
1701   -z combreloc                Merge dynamic relocs into one section and sort\n"));
1702   fprintf (file, _("\
1703   -z nocombreloc              Don't merge dynamic relocs into one section\n"));
1704   fprintf (file, _("\
1705   -z global                   Make symbols in DSO available for subsequently\n\
1706                                loaded objects\n"));
1707   fprintf (file, _("\
1708   -z initfirst                Mark DSO to be initialized first at runtime\n"));
1709   fprintf (file, _("\
1710   -z interpose                Mark object to interpose all DSOs but executable\n"));
1711   fprintf (file, _("\
1712   -z lazy                     Mark object lazy runtime binding (default)\n"));
1713   fprintf (file, _("\
1714   -z loadfltr                 Mark object requiring immediate process\n"));
1715   fprintf (file, _("\
1716   -z nocopyreloc              Don't create copy relocs\n"));
1717   fprintf (file, _("\
1718   -z nodefaultlib             Mark object not to use default search paths\n"));
1719   fprintf (file, _("\
1720   -z nodelete                 Mark DSO non-deletable at runtime\n"));
1721   fprintf (file, _("\
1722   -z nodlopen                 Mark DSO not available to dlopen\n"));
1723   fprintf (file, _("\
1724   -z nodump                   Mark DSO not available to dldump\n"));
1725   fprintf (file, _("\
1726   -z now                      Mark object non-lazy runtime binding\n"));
1727   fprintf (file, _("\
1728   -z origin                   Mark object requiring immediate $ORIGIN\n\
1729                                 processing at runtime\n"));
1730   fprintf (file, _("\
1731   -z relro                    Create RELRO program header\n"));
1732   fprintf (file, _("\
1733   -z norelro                  Don't create RELRO program header\n"));
1734   fprintf (file, _("\
1735   -z stacksize=SIZE           Set size of stack segment\n"));
1736   fprintf (file, _("\
1737   -z text                     Treat DT_TEXTREL in shared object as error\n"));
1738   fprintf (file, _("\
1739   -z notext                   Don't treat DT_TEXTREL in shared object as error\n"));
1740   fprintf (file, _("\
1741   -z textoff                  Don't treat DT_TEXTREL in shared object as error\n"));
1742 }
1743
1744 static void
1745 elf_static_list_options (FILE *file)
1746 {
1747   fprintf (file, _("\
1748   --build-id[=STYLE]          Generate build ID note\n"));
1749   fprintf (file, _("\
1750   --compress-debug-sections=[none|zlib|zlib-gnu|zlib-gabi]\n\
1751                               Compress DWARF debug sections using zlib\n"));
1752   fprintf (file, _("\
1753   -z common-page-size=SIZE    Set common page size to SIZE\n"));
1754   fprintf (file, _("\
1755   -z max-page-size=SIZE       Set maximum page size to SIZE\n"));
1756   fprintf (file, _("\
1757   -z defs                     Report unresolved symbols in object files.\n"));
1758   fprintf (file, _("\
1759   -z muldefs                  Allow multiple definitions\n"));
1760   fprintf (file, _("\
1761   -z execstack                Mark executable as requiring executable stack\n"));
1762   fprintf (file, _("\
1763   -z noexecstack              Mark executable as not requiring executable stack\n"));
1764 }
1765
1766 static void
1767 elf_plt_unwind_list_options (FILE *file)
1768 {
1769   fprintf (file, _("\
1770   --ld-generated-unwind-info  Generate exception handling info for PLT\n\
1771   --no-ld-generated-unwind-info\n\
1772                               Don't generate exception handling info for PLT\n"));
1773 }
1774
1775 static void
1776 ld_list_options (FILE *file, bfd_boolean elf, bfd_boolean shlib,
1777                  bfd_boolean plt_unwind)
1778 {
1779   if (!elf)
1780     return;
1781   printf (_("ELF emulations:\n"));
1782   if (plt_unwind)
1783     elf_plt_unwind_list_options (file);
1784   elf_static_list_options (file);
1785   if (shlib)
1786     elf_shlib_list_options (file);
1787 }
1788
1789 \f
1790 /* Print help messages for the options.  */
1791
1792 static void
1793 help (void)
1794 {
1795   unsigned i;
1796   const char **targets, **pp;
1797   int len;
1798
1799   printf (_("Usage: %s [options] file...\n"), program_name);
1800
1801   printf (_("Options:\n"));
1802   for (i = 0; i < OPTION_COUNT; i++)
1803     {
1804       if (ld_options[i].doc != NULL)
1805         {
1806           bfd_boolean comma;
1807           unsigned j;
1808
1809           printf ("  ");
1810
1811           comma = FALSE;
1812           len = 2;
1813
1814           j = i;
1815           do
1816             {
1817               if (ld_options[j].shortopt != '\0'
1818                   && ld_options[j].control != NO_HELP)
1819                 {
1820                   printf ("%s-%c", comma ? ", " : "", ld_options[j].shortopt);
1821                   len += (comma ? 2 : 0) + 2;
1822                   if (ld_options[j].arg != NULL)
1823                     {
1824                       if (ld_options[j].opt.has_arg != optional_argument)
1825                         {
1826                           printf (" ");
1827                           ++len;
1828                         }
1829                       printf ("%s", _(ld_options[j].arg));
1830                       len += strlen (_(ld_options[j].arg));
1831                     }
1832                   comma = TRUE;
1833                 }
1834               ++j;
1835             }
1836           while (j < OPTION_COUNT && ld_options[j].doc == NULL);
1837
1838           j = i;
1839           do
1840             {
1841               if (ld_options[j].opt.name != NULL
1842                   && ld_options[j].control != NO_HELP)
1843                 {
1844                   int two_dashes =
1845                     (ld_options[j].control == TWO_DASHES
1846                      || ld_options[j].control == EXACTLY_TWO_DASHES);
1847
1848                   printf ("%s-%s%s",
1849                           comma ? ", " : "",
1850                           two_dashes ? "-" : "",
1851                           ld_options[j].opt.name);
1852                   len += ((comma ? 2 : 0)
1853                           + 1
1854                           + (two_dashes ? 1 : 0)
1855                           + strlen (ld_options[j].opt.name));
1856                   if (ld_options[j].arg != NULL)
1857                     {
1858                       printf (" %s", _(ld_options[j].arg));
1859                       len += 1 + strlen (_(ld_options[j].arg));
1860                     }
1861                   comma = TRUE;
1862                 }
1863               ++j;
1864             }
1865           while (j < OPTION_COUNT && ld_options[j].doc == NULL);
1866
1867           if (len >= 30)
1868             {
1869               printf ("\n");
1870               len = 0;
1871             }
1872
1873           for (; len < 30; len++)
1874             putchar (' ');
1875
1876           printf ("%s\n", _(ld_options[i].doc));
1877         }
1878     }
1879   printf (_("  @FILE"));
1880   for (len = strlen ("  @FILE"); len < 30; len++)
1881     putchar (' ');
1882   printf (_("Read options from FILE\n"));
1883
1884   /* Note: Various tools (such as libtool) depend upon the
1885      format of the listings below - do not change them.  */
1886   /* xgettext:c-format */
1887   printf (_("%s: supported targets:"), program_name);
1888   targets = bfd_target_list ();
1889   for (pp = targets; *pp != NULL; pp++)
1890     printf (" %s", *pp);
1891   free (targets);
1892   printf ("\n");
1893
1894   /* xgettext:c-format */
1895   printf (_("%s: supported emulations: "), program_name);
1896   ldemul_list_emulations (stdout);
1897   printf ("\n");
1898
1899   /* xgettext:c-format */
1900   printf (_("%s: emulation specific options:\n"), program_name);
1901   ld_list_options (stdout, ELF_LIST_OPTIONS, ELF_SHLIB_LIST_OPTIONS,
1902                    ELF_PLT_UNWIND_LIST_OPTIONS);
1903   ldemul_list_emulation_options (stdout);
1904   printf ("\n");
1905
1906   if (REPORT_BUGS_TO[0])
1907     printf (_("Report bugs to %s\n"), REPORT_BUGS_TO);
1908 }