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