xref: /netbsd-src/external/gpl3/binutils/dist/ld/lexsup.c (revision dd7241df2fae9da4ea2bd20a68f001fa86ecf909)
1 /* Parse options for the GNU linker.
2    Copyright (C) 1991-2024 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 "filenames.h"
26 #include <stdio.h>
27 #include <string.h>
28 #include <errno.h>
29 #include "safe-ctype.h"
30 #include "getopt.h"
31 #include "bfdlink.h"
32 #include "ctf-api.h"
33 #include "ld.h"
34 #include "ldmain.h"
35 #include "ldmisc.h"
36 #include "ldexp.h"
37 #include "ldlang.h"
38 #include <ldgram.h>
39 #include "ldlex.h"
40 #include "ldfile.h"
41 #include "ldver.h"
42 #include "ldemul.h"
43 #include "demangle.h"
44 #if BFD_SUPPORTS_PLUGINS
45 #include "plugin.h"
46 #endif /* BFD_SUPPORTS_PLUGINS */
47 
48 #ifndef PATH_SEPARATOR
49 #if defined (__MSDOS__) || (defined (_WIN32) && ! defined (__CYGWIN32__))
50 #define PATH_SEPARATOR ';'
51 #else
52 #define PATH_SEPARATOR ':'
53 #endif
54 #endif
55 
56 /* Somewhere above, sys/stat.h got included . . . .  */
57 #if !defined(S_ISDIR) && defined(S_IFDIR)
58 #define	S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
59 #endif
60 
61 static void set_default_dirlist (char *);
62 static void set_section_start (char *, char *);
63 static void set_segment_start (const char *, char *);
64 static void help (void);
65 
66 /* The long options.  This structure is used for both the option
67    parsing and the help text.  */
68 
69 enum control_enum {
70   /* Use one dash before long option name.  */
71   ONE_DASH = 1,
72   /* Use two dashes before long option name.  */
73   TWO_DASHES = 2,
74   /* Only accept two dashes before the long option name.
75      This is an overloading of the use of this enum, since originally it
76      was only intended to tell the --help display function how to display
77      the long option name.  This feature was added in order to resolve
78      the confusion about the -omagic command line switch.  Is it setting
79      the output file name to "magic" or is it setting the NMAGIC flag on
80      the output ?  It has been decided that it is setting the output file
81      name, and that if you want to set the NMAGIC flag you should use -N
82      or --omagic.  */
83   EXACTLY_TWO_DASHES,
84   /* Don't mention this option in --help output.  */
85   NO_HELP
86 };
87 
88 struct ld_option
89 {
90   /* The long option information.  */
91   struct option opt;
92   /* The short option with the same meaning ('\0' if none).  */
93   char shortopt;
94   /* The name of the argument (NULL if none).  */
95   const char *arg;
96   /* The documentation string.  If this is NULL, this is a synonym for
97      the previous option.  */
98   const char *doc;
99   enum control_enum control;
100 };
101 
102 static const struct ld_option ld_options[] =
103 {
104   { {NULL, required_argument, NULL, '\0'},
105     'a', N_("KEYWORD"), N_("Shared library control for HP/UX compatibility"),
106     ONE_DASH },
107   { {"architecture", required_argument, NULL, 'A'},
108     'A', N_("ARCH"), N_("Set architecture") , EXACTLY_TWO_DASHES },
109   { {"format", required_argument, NULL, 'b'},
110     'b', N_("TARGET"), N_("Specify target for following input files"),
111     EXACTLY_TWO_DASHES },
112   { {"mri-script", required_argument, NULL, 'c'},
113     'c', N_("FILE"), N_("Read MRI format linker script"), EXACTLY_TWO_DASHES },
114   { {"dc", no_argument, NULL, 'd'},
115     'd', NULL, N_("Force common symbols to be defined"), ONE_DASH },
116   { {"dp", no_argument, NULL, 'd'},
117     '\0', NULL, NULL, ONE_DASH },
118   { {"dependency-file", required_argument, NULL, OPTION_DEPENDENCY_FILE},
119     '\0', N_("FILE"), N_("Write dependency file"), TWO_DASHES },
120   { {"force-group-allocation", no_argument, NULL,
121      OPTION_FORCE_GROUP_ALLOCATION},
122     '\0', NULL, N_("Force group members out of groups"), TWO_DASHES },
123   { {"entry", required_argument, NULL, 'e'},
124     'e', N_("ADDRESS"), N_("Set start address"), TWO_DASHES },
125   { {"export-dynamic", no_argument, NULL, OPTION_EXPORT_DYNAMIC},
126     'E', NULL, N_("Export all dynamic symbols"), TWO_DASHES },
127   { {"no-export-dynamic", no_argument, NULL, OPTION_NO_EXPORT_DYNAMIC},
128     '\0', NULL, N_("Undo the effect of --export-dynamic"), TWO_DASHES },
129   { {"enable-non-contiguous-regions", no_argument, NULL, OPTION_NON_CONTIGUOUS_REGIONS},
130     '\0', NULL, N_("Enable support of non-contiguous memory regions"), TWO_DASHES },
131   { {"enable-non-contiguous-regions-warnings", no_argument, NULL, OPTION_NON_CONTIGUOUS_REGIONS_WARNINGS},
132     '\0', NULL, N_("Enable warnings when --enable-non-contiguous-regions may cause unexpected behaviour"), TWO_DASHES },
133   { {"disable-linker-version", no_argument, NULL, OPTION_DISABLE_LINKER_VERSION},
134     '\0', NULL, N_("Disable the LINKER_VERSION linker script directive"), TWO_DASHES },
135   { {"enable-linker-version", no_argument, NULL, OPTION_ENABLE_LINKER_VERSION},
136     '\0', NULL, N_("Enable the LINKER_VERSION linker script directive"), TWO_DASHES },
137   { {"EB", no_argument, NULL, OPTION_EB},
138     '\0', NULL, N_("Link big-endian objects"), ONE_DASH },
139   { {"EL", no_argument, NULL, OPTION_EL},
140     '\0', NULL, N_("Link little-endian objects"), ONE_DASH },
141   { {"auxiliary", required_argument, NULL, 'f'},
142     'f', N_("SHLIB"), N_("Auxiliary filter for shared object symbol table"),
143     TWO_DASHES },
144   { {"filter", required_argument, NULL, 'F'},
145     'F', N_("SHLIB"), N_("Filter for shared object symbol table"),
146     TWO_DASHES },
147   { {NULL, no_argument, NULL, '\0'},
148     'g', NULL, N_("Ignored"), ONE_DASH },
149   { {"gpsize", required_argument, NULL, 'G'},
150     'G', N_("SIZE"), N_("Small data size (if no size, same as --shared)"),
151     TWO_DASHES },
152   { {"soname", required_argument, NULL, OPTION_SONAME},
153     'h', N_("FILENAME"), N_("Set internal name of shared library"), ONE_DASH },
154   { {"dynamic-linker", required_argument, NULL, OPTION_DYNAMIC_LINKER},
155     'I', N_("PROGRAM"), N_("Set PROGRAM as the dynamic linker to use"),
156     TWO_DASHES },
157   { {"no-dynamic-linker", no_argument, NULL, OPTION_NO_DYNAMIC_LINKER},
158     '\0', NULL, N_("Produce an executable with no program interpreter header"),
159     TWO_DASHES },
160   { {"library", required_argument, NULL, 'l'},
161     'l', N_("LIBNAME"), N_("Search for library LIBNAME"), TWO_DASHES },
162   { {"library-path", required_argument, NULL, 'L'},
163     'L', N_("DIRECTORY"), N_("Add DIRECTORY to library search path"),
164     TWO_DASHES },
165   { {"sysroot=<DIRECTORY>", required_argument, NULL, OPTION_SYSROOT},
166     '\0', NULL, N_("Override the default sysroot location"), TWO_DASHES },
167   { {NULL, required_argument, NULL, '\0'},
168     'm', N_("EMULATION"), N_("Set emulation"), ONE_DASH },
169   { {"print-map", no_argument, NULL, 'M'},
170     'M', NULL, N_("Print map file on standard output"), TWO_DASHES },
171   { {"nmagic", no_argument, NULL, 'n'},
172     'n', NULL, N_("Do not page align data"), TWO_DASHES },
173   { {"omagic", no_argument, NULL, 'N'},
174     'N', NULL, N_("Do not page align data, do not make text readonly"),
175     EXACTLY_TWO_DASHES },
176   { {"no-omagic", no_argument, NULL, OPTION_NO_OMAGIC},
177     '\0', NULL, N_("Page align data, make text readonly"),
178     EXACTLY_TWO_DASHES },
179   { {"output", required_argument, NULL, 'o'},
180     'o', N_("FILE"), N_("Set output file name"), EXACTLY_TWO_DASHES },
181   { {NULL, required_argument, NULL, '\0'},
182     'O', NULL, N_("Optimize output file"), ONE_DASH },
183   { {"out-implib", required_argument, NULL, OPTION_OUT_IMPLIB},
184     '\0', N_("FILE"), N_("Generate import library"), TWO_DASHES },
185 #if BFD_SUPPORTS_PLUGINS
186   { {"plugin", required_argument, NULL, OPTION_PLUGIN},
187     '\0', N_("PLUGIN"), N_("Load named plugin"), ONE_DASH },
188   { {"plugin-opt", required_argument, NULL, OPTION_PLUGIN_OPT},
189     '\0', N_("ARG"), N_("Send arg to last-loaded plugin"), ONE_DASH },
190   { {"flto", optional_argument, NULL, OPTION_IGNORE},
191     '\0', NULL, N_("Ignored for GCC LTO option compatibility"),
192     ONE_DASH },
193   { {"flto-partition=", required_argument, NULL, OPTION_IGNORE},
194     '\0', NULL, N_("Ignored for GCC LTO option compatibility"),
195     ONE_DASH },
196 #else
197   { {"plugin", required_argument, NULL, OPTION_IGNORE},
198     '\0', N_("PLUGIN"), N_("Load named plugin (ignored)"), ONE_DASH },
199   { {"plugin-opt", required_argument, NULL, OPTION_IGNORE},
200     '\0', N_("ARG"), N_("Send arg to last-loaded plugin (ignored)"), ONE_DASH },
201 #endif /* BFD_SUPPORTS_PLUGINS */
202   { {"fuse-ld=", required_argument, NULL, OPTION_IGNORE},
203     '\0', NULL, N_("Ignored for GCC linker option compatibility"),
204     ONE_DASH },
205   { {"map-whole-files", optional_argument, NULL, OPTION_IGNORE},
206     '\0', NULL, N_("Ignored for gold option compatibility"),
207     TWO_DASHES },
208   { {"no-map-whole-files", optional_argument, NULL, OPTION_IGNORE},
209     '\0', NULL, N_("Ignored for gold option compatibility"),
210     TWO_DASHES },
211   { {"Qy", no_argument, NULL, OPTION_IGNORE},
212     '\0', NULL, N_("Ignored for SVR4 compatibility"), ONE_DASH },
213   { {"emit-relocs", no_argument, NULL, 'q'},
214     'q', NULL, "Generate relocations in final output", TWO_DASHES },
215   { {"relocatable", no_argument, NULL, 'r'},
216     'r', NULL, N_("Generate relocatable output"), TWO_DASHES },
217   { {NULL, no_argument, NULL, '\0'},
218     'i', NULL, NULL, ONE_DASH },
219   { {"just-symbols", required_argument, NULL, 'R'},
220     'R', N_("FILE"), N_("Just link symbols (if directory, same as --rpath)"),
221     TWO_DASHES },
222 
223   { {"remap-inputs-file", required_argument, NULL, OPTION_REMAP_INPUTS_FILE},
224     '\0', N_("FILE"), "Provide a FILE containing input remapings", TWO_DASHES },
225   { {"remap-inputs", required_argument, NULL, OPTION_REMAP_INPUTS},
226     '\0', N_("PATTERN=FILE"), "Remap input files matching PATTERN to FILE", TWO_DASHES },
227 
228   { {"strip-all", no_argument, NULL, 's'},
229     's', NULL, N_("Strip all symbols"), TWO_DASHES },
230   { {"strip-debug", no_argument, NULL, 'S'},
231     'S', NULL, N_("Strip debugging symbols"), TWO_DASHES },
232   { {"strip-discarded", no_argument, NULL, OPTION_STRIP_DISCARDED},
233     '\0', NULL, N_("Strip symbols in discarded sections"), TWO_DASHES },
234   { {"no-strip-discarded", no_argument, NULL, OPTION_NO_STRIP_DISCARDED},
235     '\0', NULL, N_("Do not strip symbols in discarded sections"), TWO_DASHES },
236   { {"trace", no_argument, NULL, 't'},
237     't', NULL, N_("Trace file opens"), TWO_DASHES },
238   { {"script", required_argument, NULL, 'T'},
239     'T', N_("FILE"), N_("Read linker script"), TWO_DASHES },
240   { {"default-script", required_argument, NULL, OPTION_DEFAULT_SCRIPT},
241     '\0', N_("FILE"), N_("Read default linker script"), TWO_DASHES },
242   { {"dT", required_argument, NULL, OPTION_DEFAULT_SCRIPT},
243     '\0', NULL, NULL, ONE_DASH },
244   { {"undefined", required_argument, NULL, 'u'},
245     'u', N_("SYMBOL"), N_("Start with undefined reference to SYMBOL"),
246     TWO_DASHES },
247   { {"require-defined", required_argument, NULL, OPTION_REQUIRE_DEFINED_SYMBOL},
248     '\0', N_("SYMBOL"), N_("Require SYMBOL be defined in the final output"),
249     TWO_DASHES },
250   { {"unique", optional_argument, NULL, OPTION_UNIQUE},
251     '\0', N_("[=SECTION]"),
252     N_("Don't merge input [SECTION | orphan] sections"), TWO_DASHES },
253   { {"Ur", no_argument, NULL, OPTION_UR},
254     '\0', NULL, N_("Build global constructor/destructor tables"), ONE_DASH },
255   { {"version", no_argument, NULL, OPTION_VERSION},
256     'v', NULL, N_("Print version information"), TWO_DASHES },
257   { {NULL, no_argument, NULL, '\0'},
258     'V', NULL, N_("Print version and emulation information"), ONE_DASH },
259   { {"discard-all", no_argument, NULL, 'x'},
260     'x', NULL, N_("Discard all local symbols"), TWO_DASHES },
261   { {"discard-locals", no_argument, NULL, 'X'},
262     'X', NULL, N_("Discard temporary local symbols (default)"), TWO_DASHES },
263   { {"discard-none", no_argument, NULL, OPTION_DISCARD_NONE},
264     '\0', NULL, N_("Don't discard any local symbols"), TWO_DASHES },
265   { {"trace-symbol", required_argument, NULL, 'y'},
266     'y', N_("SYMBOL"), N_("Trace mentions of SYMBOL"), TWO_DASHES },
267   { {NULL, required_argument, NULL, '\0'},
268     'Y', N_("PATH"), N_("Default search path for Solaris compatibility"),
269     ONE_DASH },
270   { {"start-group", no_argument, NULL, '('},
271     '(', NULL, N_("Start a group"), TWO_DASHES },
272   { {"end-group", no_argument, NULL, ')'},
273     ')', NULL, N_("End a group"), TWO_DASHES },
274   { {"accept-unknown-input-arch", no_argument, NULL,
275      OPTION_ACCEPT_UNKNOWN_INPUT_ARCH},
276     '\0', NULL,
277     N_("Accept input files whose architecture cannot be determined"),
278     TWO_DASHES },
279   { {"no-accept-unknown-input-arch", no_argument, NULL,
280      OPTION_NO_ACCEPT_UNKNOWN_INPUT_ARCH},
281     '\0', NULL, N_("Reject input files whose architecture is unknown"),
282     TWO_DASHES },
283 
284   /* The next two options are deprecated because of their similarity to
285      --as-needed and --no-as-needed.  They have been replaced by
286      --copy-dt-needed-entries and --no-copy-dt-needed-entries.  */
287   { {"add-needed", no_argument, NULL, OPTION_ADD_DT_NEEDED_FOR_DYNAMIC},
288     '\0', NULL, NULL, NO_HELP },
289   { {"no-add-needed", no_argument, NULL, OPTION_NO_ADD_DT_NEEDED_FOR_DYNAMIC},
290     '\0', NULL, NULL, NO_HELP },
291 
292   { {"as-needed", no_argument, NULL, OPTION_ADD_DT_NEEDED_FOR_REGULAR},
293     '\0', NULL, N_("Only set DT_NEEDED for following dynamic libs if used"),
294     TWO_DASHES },
295   { {"no-as-needed", no_argument, NULL, OPTION_NO_ADD_DT_NEEDED_FOR_REGULAR},
296     '\0', NULL, N_("Always set DT_NEEDED for dynamic libraries mentioned on\n"
297 		   "                                the command line"),
298     TWO_DASHES },
299   { {"assert", required_argument, NULL, OPTION_ASSERT},
300     '\0', N_("KEYWORD"), N_("Ignored for SunOS compatibility"), ONE_DASH },
301   { {"Bdynamic", no_argument, NULL, OPTION_CALL_SHARED},
302     '\0', NULL, N_("Link against shared libraries"), ONE_DASH },
303   { {"dy", no_argument, NULL, OPTION_CALL_SHARED},
304     '\0', NULL, NULL, ONE_DASH },
305   { {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
306     '\0', NULL, NULL, ONE_DASH },
307   { {"Bstatic", no_argument, NULL, OPTION_NON_SHARED},
308     '\0', NULL, N_("Do not link against shared libraries"), ONE_DASH },
309   { {"dn", no_argument, NULL, OPTION_NON_SHARED},
310     '\0', NULL, NULL, ONE_DASH },
311   { {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
312     '\0', NULL, NULL, ONE_DASH },
313   { {"static", no_argument, NULL, OPTION_NON_SHARED},
314     '\0', NULL, NULL, ONE_DASH },
315   { {"Bno-symbolic", no_argument, NULL, OPTION_NO_SYMBOLIC},
316     '\0', NULL, N_("Don't bind global references locally"), ONE_DASH },
317   { {"Bsymbolic", no_argument, NULL, OPTION_SYMBOLIC},
318     '\0', NULL, N_("Bind global references locally"), ONE_DASH },
319   { {"Bsymbolic-functions", no_argument, NULL, OPTION_SYMBOLIC_FUNCTIONS},
320     '\0', NULL, N_("Bind global function references locally"), ONE_DASH },
321   { {"check-sections", no_argument, NULL, OPTION_CHECK_SECTIONS},
322     '\0', NULL, N_("Check section addresses for overlaps (default)"),
323     TWO_DASHES },
324   { {"no-check-sections", no_argument, NULL, OPTION_NO_CHECK_SECTIONS},
325     '\0', NULL, N_("Do not check section addresses for overlaps"),
326     TWO_DASHES },
327   { {"copy-dt-needed-entries", no_argument, NULL,
328      OPTION_ADD_DT_NEEDED_FOR_DYNAMIC},
329     '\0', NULL, N_("Copy DT_NEEDED links mentioned inside DSOs that follow"),
330     TWO_DASHES },
331   { {"no-copy-dt-needed-entries", no_argument, NULL,
332      OPTION_NO_ADD_DT_NEEDED_FOR_DYNAMIC},
333     '\0', NULL, N_("Do not copy DT_NEEDED links mentioned inside DSOs that follow"),
334     TWO_DASHES },
335 
336   { {"cref", no_argument, NULL, OPTION_CREF},
337     '\0', NULL, N_("Output cross reference table"), TWO_DASHES },
338   { {"defsym", required_argument, NULL, OPTION_DEFSYM},
339     '\0', N_("SYMBOL=EXPRESSION"), N_("Define a symbol"), TWO_DASHES },
340   { {"demangle", optional_argument, NULL, OPTION_DEMANGLE},
341     '\0', N_("[=STYLE]"), N_("Demangle symbol names [using STYLE]"),
342     TWO_DASHES },
343   { {"disable-multiple-abs-defs", no_argument, NULL,
344      OPTION_DISABLE_MULTIPLE_DEFS_ABS},
345     '\0', NULL, N_("Do not allow multiple definitions with symbols included\n"
346 		   "                                in filename invoked by -R "
347 		   "or --just-symbols"),
348     TWO_DASHES},
349   { {"embedded-relocs", no_argument, NULL, OPTION_EMBEDDED_RELOCS},
350     '\0', NULL, N_("Generate embedded relocs"), TWO_DASHES},
351   { {"fatal-warnings", no_argument, NULL, OPTION_WARN_FATAL},
352     '\0', NULL, N_("Treat warnings as errors"),
353     TWO_DASHES },
354   { {"no-fatal-warnings", no_argument, NULL, OPTION_NO_WARN_FATAL},
355     '\0', NULL, N_("Do not treat warnings as errors (default)"),
356     TWO_DASHES },
357   { {"fini", required_argument, NULL, OPTION_FINI},
358     '\0', N_("SYMBOL"), N_("Call SYMBOL at unload-time"), ONE_DASH },
359   { {"force-exe-suffix", no_argument, NULL, OPTION_FORCE_EXE_SUFFIX},
360     '\0', NULL, N_("Force generation of file with .exe suffix"), TWO_DASHES},
361   { {"gc-sections", no_argument, NULL, OPTION_GC_SECTIONS},
362     '\0', NULL, N_("Remove unused sections (on some targets)"),
363     TWO_DASHES },
364   { {"no-gc-sections", no_argument, NULL, OPTION_NO_GC_SECTIONS},
365     '\0', NULL, N_("Don't remove unused sections (default)"),
366     TWO_DASHES },
367   { {"print-gc-sections", no_argument, NULL, OPTION_PRINT_GC_SECTIONS},
368     '\0', NULL, N_("List removed unused sections on stderr"),
369     TWO_DASHES },
370   { {"no-print-gc-sections", no_argument, NULL, OPTION_NO_PRINT_GC_SECTIONS},
371     '\0', NULL, N_("Do not list removed unused sections"),
372     TWO_DASHES },
373   { {"gc-keep-exported", no_argument, NULL, OPTION_GC_KEEP_EXPORTED},
374     '\0', NULL, N_("Keep exported symbols when removing unused sections"),
375     TWO_DASHES },
376   { {"hash-size=<NUMBER>", required_argument, NULL, OPTION_HASH_SIZE},
377     '\0', NULL, N_("Set default hash table size close to <NUMBER>"),
378     TWO_DASHES },
379   { {"help", no_argument, NULL, OPTION_HELP},
380     '\0', NULL, N_("Print option help"), TWO_DASHES },
381   { {"init", required_argument, NULL, OPTION_INIT},
382     '\0', N_("SYMBOL"), N_("Call SYMBOL at load-time"), ONE_DASH },
383   { {"Map", required_argument, NULL, OPTION_MAP},
384     '\0', N_("FILE/DIR"), N_("Write a linker map to FILE or DIR/<outputname>.map"), ONE_DASH },
385   { {"no-define-common", no_argument, NULL, OPTION_NO_DEFINE_COMMON},
386     '\0', NULL, N_("Do not define Common storage"), TWO_DASHES },
387   { {"no-demangle", no_argument, NULL, OPTION_NO_DEMANGLE },
388     '\0', NULL, N_("Do not demangle symbol names"), TWO_DASHES },
389   { {"no-keep-memory", no_argument, NULL, OPTION_NO_KEEP_MEMORY},
390     '\0', NULL, N_("Use less memory and more disk I/O"), TWO_DASHES },
391   { {"no-undefined", no_argument, NULL, OPTION_NO_UNDEFINED},
392     '\0', NULL, N_("Do not allow unresolved references in object files"),
393     TWO_DASHES },
394   { {"no-warnings", no_argument, NULL, OPTION_NO_WARNINGS},
395     'w', NULL, N_("Do not display any warning or error messages"),
396     TWO_DASHES },
397   { {"allow-shlib-undefined", no_argument, NULL, OPTION_ALLOW_SHLIB_UNDEFINED},
398     '\0', NULL, N_("Allow unresolved references in shared libraries"),
399     TWO_DASHES },
400   { {"no-allow-shlib-undefined", no_argument, NULL,
401      OPTION_NO_ALLOW_SHLIB_UNDEFINED},
402     '\0', NULL, N_("Do not allow unresolved references in shared libs"),
403     TWO_DASHES },
404   { {"allow-multiple-definition", no_argument, NULL,
405      OPTION_ALLOW_MULTIPLE_DEFINITION},
406     '\0', NULL, N_("Allow multiple definitions"), TWO_DASHES },
407 #if SUPPORT_ERROR_HANDLING_SCRIPT
408   { {"error-handling-script", required_argument, NULL,
409      OPTION_ERROR_HANDLING_SCRIPT},
410     '\0', N_("SCRIPT"), N_("Provide a script to help with undefined symbol errors"), TWO_DASHES},
411 #endif
412   { {"undefined-version", no_argument, NULL, OPTION_UNDEFINED_VERSION},
413     '\0', NULL, N_("Allow undefined version"), EXACTLY_TWO_DASHES },
414   { {"no-undefined-version", no_argument, NULL, OPTION_NO_UNDEFINED_VERSION},
415     '\0', NULL, N_("Disallow undefined version"), TWO_DASHES },
416   { {"default-symver", no_argument, NULL, OPTION_DEFAULT_SYMVER},
417     '\0', NULL, N_("Create default symbol version"), TWO_DASHES },
418   { {"default-imported-symver", no_argument, NULL,
419       OPTION_DEFAULT_IMPORTED_SYMVER},
420     '\0', NULL, N_("Create default symbol version for imported symbols"),
421     TWO_DASHES },
422   { {"no-warn-mismatch", no_argument, NULL, OPTION_NO_WARN_MISMATCH},
423     '\0', NULL, N_("Don't warn about mismatched input files"), TWO_DASHES},
424   { {"no-warn-search-mismatch", no_argument, NULL,
425      OPTION_NO_WARN_SEARCH_MISMATCH},
426     '\0', NULL, N_("Don't warn on finding an incompatible library"),
427     TWO_DASHES},
428   { {"no-whole-archive", no_argument, NULL, OPTION_NO_WHOLE_ARCHIVE},
429     '\0', NULL, N_("Turn off --whole-archive"), TWO_DASHES },
430   { {"noinhibit-exec", no_argument, NULL, OPTION_NOINHIBIT_EXEC},
431     '\0', NULL, N_("Create an output file even if errors occur"),
432     TWO_DASHES },
433   { {"noinhibit_exec", no_argument, NULL, OPTION_NOINHIBIT_EXEC},
434     '\0', NULL, NULL, NO_HELP },
435   { {"nostdlib", no_argument, NULL, OPTION_NOSTDLIB},
436     '\0', NULL, N_("Only use library directories specified on\n"
437 		   "                                the command line"),
438     ONE_DASH },
439   { {"oformat", required_argument, NULL, OPTION_OFORMAT},
440     '\0', N_("TARGET"), N_("Specify target of output file"),
441     EXACTLY_TWO_DASHES },
442   { {"print-output-format", no_argument, NULL, OPTION_PRINT_OUTPUT_FORMAT},
443     '\0', NULL, N_("Print default output format"), TWO_DASHES },
444   { {"print-sysroot", no_argument, NULL, OPTION_PRINT_SYSROOT},
445     '\0', NULL, N_("Print current sysroot"), TWO_DASHES },
446   { {"qmagic", no_argument, NULL, OPTION_IGNORE},
447     '\0', NULL, N_("Ignored for Linux compatibility"), ONE_DASH },
448   { {"reduce-memory-overheads", no_argument, NULL,
449      OPTION_REDUCE_MEMORY_OVERHEADS},
450     '\0', NULL, N_("Reduce memory overheads, possibly taking much longer"),
451     TWO_DASHES },
452   { {"max-cache-size=SIZE", required_argument, NULL,
453     OPTION_MAX_CACHE_SIZE},
454     '\0', NULL, N_("Set the maximum cache size to SIZE bytes"),
455     TWO_DASHES },
456   { {"relax", no_argument, NULL, OPTION_RELAX},
457     '\0', NULL, N_("Reduce code size by using target specific optimizations"), TWO_DASHES },
458   { {"no-relax", no_argument, NULL, OPTION_NO_RELAX},
459     '\0', NULL, N_("Do not use relaxation techniques to reduce code size"), TWO_DASHES },
460   { {"retain-symbols-file", required_argument, NULL,
461      OPTION_RETAIN_SYMBOLS_FILE},
462     '\0', N_("FILE"), N_("Keep only symbols listed in FILE"), TWO_DASHES },
463   { {"rpath", required_argument, NULL, OPTION_RPATH},
464     '\0', N_("PATH"), N_("Set runtime shared library search path"), ONE_DASH },
465   { {"rpath-link", required_argument, NULL, OPTION_RPATH_LINK},
466     '\0', N_("PATH"), N_("Set link time shared library search path"),
467     ONE_DASH },
468   { {"shared", no_argument, NULL, OPTION_SHARED},
469     '\0', NULL, N_("Create a shared library"), ONE_DASH },
470   { {"Bshareable", no_argument, NULL, OPTION_SHARED }, /* FreeBSD, NetBSD.  */
471     '\0', NULL, NULL, ONE_DASH },
472   { {"pie", no_argument, NULL, OPTION_PIE},
473     '\0', NULL, N_("Create a position independent executable"), ONE_DASH },
474   { {"pic-executable", no_argument, NULL, OPTION_PIE},
475     '\0', NULL, NULL, TWO_DASHES },
476   { {"no-pie", no_argument, NULL, OPTION_NO_PIE},
477     '\0', NULL, N_("Create a position dependent executable (default)"), ONE_DASH },
478   { {"sort-common", optional_argument, NULL, OPTION_SORT_COMMON},
479     '\0', N_("[=ascending|descending]"),
480     N_("Sort common symbols by alignment [in specified order]"),
481     TWO_DASHES },
482   { {"sort_common", no_argument, NULL, OPTION_SORT_COMMON},
483     '\0', NULL, NULL, NO_HELP },
484   { {"sort-section", required_argument, NULL, OPTION_SORT_SECTION},
485     '\0', N_("name|alignment"),
486     N_("Sort sections by name or maximum alignment"), TWO_DASHES },
487   { {"spare-dynamic-tags", required_argument, NULL, OPTION_SPARE_DYNAMIC_TAGS},
488     '\0', N_("COUNT"), N_("How many tags to reserve in .dynamic section"),
489     TWO_DASHES },
490   { {"split-by-file", optional_argument, NULL, OPTION_SPLIT_BY_FILE},
491     '\0', N_("[=SIZE]"), N_("Split output sections every SIZE octets"),
492     TWO_DASHES },
493   { {"split-by-reloc", optional_argument, NULL, OPTION_SPLIT_BY_RELOC},
494     '\0', N_("[=COUNT]"), N_("Split output sections every COUNT relocs"),
495     TWO_DASHES },
496   { {"stats", no_argument, NULL, OPTION_STATS},
497     '\0', NULL, N_("Print memory usage statistics"), TWO_DASHES },
498   { {"target-help", no_argument, NULL, OPTION_TARGET_HELP},
499     '\0', NULL, N_("Display target specific options"), TWO_DASHES },
500   { {"task-link", required_argument, NULL, OPTION_TASK_LINK},
501     '\0', N_("SYMBOL"), N_("Do task level linking"), TWO_DASHES },
502   { {"traditional-format", no_argument, NULL, OPTION_TRADITIONAL_FORMAT},
503     '\0', NULL, N_("Use same format as native linker"), TWO_DASHES },
504   { {"section-start", required_argument, NULL, OPTION_SECTION_START},
505     '\0', N_("SECTION=ADDRESS"), N_("Set address of named section"),
506     TWO_DASHES },
507   { {"Tbss", required_argument, NULL, OPTION_TBSS},
508     '\0', N_("ADDRESS"), N_("Set address of .bss section"), ONE_DASH },
509   { {"Tdata", required_argument, NULL, OPTION_TDATA},
510     '\0', N_("ADDRESS"), N_("Set address of .data section"), ONE_DASH },
511   { {"Ttext", required_argument, NULL, OPTION_TTEXT},
512     '\0', N_("ADDRESS"), N_("Set address of .text section"), ONE_DASH },
513   { {"Ttext-segment", required_argument, NULL, OPTION_TTEXT_SEGMENT},
514     '\0', N_("ADDRESS"), N_("Set address of text segment"), ONE_DASH },
515   { {"Trodata-segment", required_argument, NULL, OPTION_TRODATA_SEGMENT},
516     '\0', N_("ADDRESS"), N_("Set address of rodata segment"), ONE_DASH },
517   { {"Tldata-segment", required_argument, NULL, OPTION_TLDATA_SEGMENT},
518     '\0', N_("ADDRESS"), N_("Set address of ldata segment"), ONE_DASH },
519   { {"unresolved-symbols=<method>", required_argument, NULL,
520      OPTION_UNRESOLVED_SYMBOLS},
521     '\0', NULL, N_("How to handle unresolved symbols.  <method> is:\n"
522 		   "                                ignore-all, report-all, ignore-in-object-files,\n"
523 		   "                                ignore-in-shared-libs"),
524     TWO_DASHES },
525   { {"verbose", optional_argument, NULL, OPTION_VERBOSE},
526     '\0', N_("[=NUMBER]"),
527     N_("Output lots of information during link"), TWO_DASHES },
528   { {"dll-verbose", no_argument, NULL, OPTION_VERBOSE}, /* Linux.  */
529     '\0', NULL, NULL, NO_HELP },
530   { {"version-script", required_argument, NULL, OPTION_VERSION_SCRIPT },
531     '\0', N_("FILE"), N_("Read version information script"), TWO_DASHES },
532   { {"version-exports-section", required_argument, NULL,
533      OPTION_VERSION_EXPORTS_SECTION },
534     '\0', N_("SYMBOL"), N_("Take export symbols list from .exports, using\n"
535 			   "                                SYMBOL as the version."),
536     TWO_DASHES },
537   { {"dynamic-list-data", no_argument, NULL, OPTION_DYNAMIC_LIST_DATA},
538     '\0', NULL, N_("Add data symbols to dynamic list"), TWO_DASHES },
539   { {"dynamic-list-cpp-new", no_argument, NULL, OPTION_DYNAMIC_LIST_CPP_NEW},
540     '\0', NULL, N_("Use C++ operator new/delete dynamic list"), TWO_DASHES },
541   { {"dynamic-list-cpp-typeinfo", no_argument, NULL, OPTION_DYNAMIC_LIST_CPP_TYPEINFO},
542     '\0', NULL, N_("Use C++ typeinfo dynamic list"), TWO_DASHES },
543   { {"dynamic-list", required_argument, NULL, OPTION_DYNAMIC_LIST},
544     '\0', N_("FILE"), N_("Read dynamic list"), TWO_DASHES },
545   { {"export-dynamic-symbol", required_argument, NULL, OPTION_EXPORT_DYNAMIC_SYMBOL},
546     '\0', N_("SYMBOL"), N_("Export the specified symbol"), EXACTLY_TWO_DASHES },
547   { {"export-dynamic-symbol-list", required_argument, NULL, OPTION_EXPORT_DYNAMIC_SYMBOL_LIST},
548     '\0', N_("FILE"), N_("Read export dynamic symbol list"), EXACTLY_TWO_DASHES },
549   { {"warn-common", no_argument, NULL, OPTION_WARN_COMMON},
550     '\0', NULL, N_("Warn about duplicate common symbols"), TWO_DASHES },
551   { {"warn-constructors", no_argument, NULL, OPTION_WARN_CONSTRUCTORS},
552     '\0', NULL, N_("Warn if global constructors/destructors are seen"),
553     TWO_DASHES },
554 
555   { {"error-execstack", no_argument, NULL, OPTION_ERROR_EXECSTACK},
556     '\0', NULL, NULL, TWO_DASHES },
557   { {"no-error-execstack", no_argument, NULL, OPTION_NO_ERROR_EXECSTACK},
558     '\0', NULL, NULL, TWO_DASHES },
559   { {"warn-execstack-objects", no_argument, NULL, OPTION_WARN_EXECSTACK_OBJECTS},
560     '\0', NULL, NULL, TWO_DASHES },
561   { {"warn-execstack", no_argument, NULL, OPTION_WARN_EXECSTACK},
562     '\0', NULL, NULL, TWO_DASHES },
563   { {"no-warn-execstack", no_argument, NULL, OPTION_NO_WARN_EXECSTACK},
564     '\0', NULL, NULL, TWO_DASHES },
565 
566   { {"error-rwx-segments", no_argument, NULL, OPTION_ERROR_RWX_SEGMENTS},
567     '\0', NULL, NULL, TWO_DASHES },
568   { {"no-error-rwx-segments", no_argument, NULL, OPTION_NO_ERROR_RWX_SEGMENTS},
569     '\0', NULL, NULL, TWO_DASHES },
570   { {"warn-rwx-segments", no_argument, NULL, OPTION_WARN_RWX_SEGMENTS},
571     '\0', NULL, NULL, TWO_DASHES },
572   { {"no-warn-rwx-segments", no_argument, NULL, OPTION_NO_WARN_RWX_SEGMENTS},
573     '\0', NULL, NULL, TWO_DASHES },
574 
575   { {"warn-multiple-gp", no_argument, NULL, OPTION_WARN_MULTIPLE_GP},
576     '\0', NULL, N_("Warn if the multiple GP values are used"), TWO_DASHES },
577   { {"warn-once", no_argument, NULL, OPTION_WARN_ONCE},
578     '\0', NULL, N_("Warn only once per undefined symbol"), TWO_DASHES },
579   { {"warn-section-align", no_argument, NULL, OPTION_WARN_SECTION_ALIGN},
580     '\0', NULL, N_("Warn if start of section changes due to alignment"),
581     TWO_DASHES },
582   { {"warn-textrel", no_argument, NULL, OPTION_WARN_TEXTREL},
583     '\0', NULL,
584 #if DEFAULT_LD_TEXTREL_CHECK_WARNING
585     N_("Warn if output has DT_TEXTREL (default)"),
586 #else
587     N_("Warn if output has DT_TEXTREL"),
588 #endif
589     TWO_DASHES },
590   { {"warn-shared-textrel", no_argument, NULL, OPTION_WARN_TEXTREL},
591     '\0', NULL, NULL, NO_HELP },
592   { {"warn-alternate-em", no_argument, NULL, OPTION_WARN_ALTERNATE_EM},
593     '\0', NULL, N_("Warn if an object has alternate ELF machine code"),
594     TWO_DASHES },
595   { {"warn-unresolved-symbols", no_argument, NULL,
596      OPTION_WARN_UNRESOLVED_SYMBOLS},
597     '\0', NULL, N_("Report unresolved symbols as warnings"), TWO_DASHES },
598   { {"error-unresolved-symbols", no_argument, NULL,
599      OPTION_ERROR_UNRESOLVED_SYMBOLS},
600     '\0', NULL, N_("Report unresolved symbols as errors"), TWO_DASHES },
601   { {"whole-archive", no_argument, NULL, OPTION_WHOLE_ARCHIVE},
602     '\0', NULL, N_("Include all objects from following archives"),
603     TWO_DASHES },
604   { {"Bforcearchive", no_argument, NULL, OPTION_WHOLE_ARCHIVE},
605       '\0', NULL, NULL, TWO_DASHES },	/* NetBSD.  */
606   { {"wrap", required_argument, NULL, OPTION_WRAP},
607     '\0', N_("SYMBOL"), N_("Use wrapper functions for SYMBOL"), TWO_DASHES },
608   { {"ignore-unresolved-symbol", required_argument, NULL,
609     OPTION_IGNORE_UNRESOLVED_SYMBOL},
610     '\0', N_("SYMBOL"),
611     N_("Unresolved SYMBOL will not cause an error or warning"), TWO_DASHES },
612   { {"push-state", no_argument, NULL, OPTION_PUSH_STATE},
613     '\0', NULL, N_("Push state of flags governing input file handling"),
614     TWO_DASHES },
615   { {"pop-state", no_argument, NULL, OPTION_POP_STATE},
616     '\0', NULL, N_("Pop state of flags governing input file handling"),
617     TWO_DASHES },
618   { {"print-memory-usage", no_argument, NULL, OPTION_PRINT_MEMORY_USAGE},
619     '\0', NULL, N_("Report target memory usage"), TWO_DASHES },
620   { {"orphan-handling", required_argument, NULL, OPTION_ORPHAN_HANDLING},
621     '\0', N_("=MODE"), N_("Control how orphan sections are handled."),
622     TWO_DASHES },
623   { {"print-map-discarded", no_argument, NULL, OPTION_PRINT_MAP_DISCARDED},
624     '\0', NULL, N_("Show discarded sections in map file output (default)"),
625     TWO_DASHES },
626   { {"no-print-map-discarded", no_argument, NULL, OPTION_NO_PRINT_MAP_DISCARDED},
627     '\0', NULL, N_("Do not show discarded sections in map file output"),
628     TWO_DASHES },
629   { {"print-map-locals", no_argument, NULL, OPTION_PRINT_MAP_LOCALS},
630     '\0', NULL, N_("Show local symbols in map file output"),
631     TWO_DASHES },
632   { {"no-print-map-locals", no_argument, NULL, OPTION_NO_PRINT_MAP_LOCALS},
633     '\0', NULL, N_("Do not show local symbols in map file output (default)"),
634     TWO_DASHES },
635   { {"ctf-variables", no_argument, NULL, OPTION_CTF_VARIABLES},
636     '\0', NULL, N_("Emit names and types of static variables in CTF"),
637     TWO_DASHES },
638   { {"no-ctf-variables", no_argument, NULL, OPTION_NO_CTF_VARIABLES},
639     '\0', NULL, N_("Do not emit names and types of static variables in CTF"),
640     TWO_DASHES },
641   { {"ctf-share-types=<method>", required_argument, NULL,
642      OPTION_CTF_SHARE_TYPES},
643     '\0', NULL, N_("How to share CTF types between translation units.\n"
644 		   "                                <method> is: share-unconflicted (default),\n"
645 		   "                                             share-duplicated"),
646     TWO_DASHES },
647 };
648 
649 #define OPTION_COUNT ARRAY_SIZE (ld_options)
650 
651 void
parse_args(unsigned argc,char ** argv)652 parse_args (unsigned argc, char **argv)
653 {
654   unsigned i;
655   int is, il, irl;
656   int ingroup = 0;
657   char *default_dirlist = NULL;
658   char *shortopts;
659   struct option *longopts;
660   struct option *really_longopts;
661   int last_optind;
662   enum symbolic_enum
663   {
664     symbolic_unset = 0,
665     symbolic,
666     symbolic_functions,
667   } opt_symbolic = symbolic_unset;
668   enum dynamic_list_enum
669   {
670     dynamic_list_unset = 0,
671     dynamic_list_data,
672     dynamic_list
673   } opt_dynamic_list = dynamic_list_unset;
674   struct bfd_elf_dynamic_list *export_list = NULL;
675 
676   shortopts = (char *) xmalloc (OPTION_COUNT * 3 + 2);
677   longopts = (struct option *)
678       xmalloc (sizeof (*longopts) * (OPTION_COUNT + 1));
679   really_longopts = (struct option *)
680       xmalloc (sizeof (*really_longopts) * (OPTION_COUNT + 1));
681 
682   /* Starting the short option string with '-' is for programs that
683      expect options and other ARGV-elements in any order and that care about
684      the ordering of the two.  We describe each non-option ARGV-element
685      as if it were the argument of an option with character code 1.  */
686   shortopts[0] = '-';
687   is = 1;
688   il = 0;
689   irl = 0;
690   for (i = 0; i < OPTION_COUNT; i++)
691     {
692       if (ld_options[i].shortopt != '\0')
693 	{
694 	  shortopts[is] = ld_options[i].shortopt;
695 	  ++is;
696 	  if (ld_options[i].opt.has_arg == required_argument
697 	      || ld_options[i].opt.has_arg == optional_argument)
698 	    {
699 	      shortopts[is] = ':';
700 	      ++is;
701 	      if (ld_options[i].opt.has_arg == optional_argument)
702 		{
703 		  shortopts[is] = ':';
704 		  ++is;
705 		}
706 	    }
707 	}
708       if (ld_options[i].opt.name != NULL)
709 	{
710 	  if (ld_options[i].control == EXACTLY_TWO_DASHES)
711 	    {
712 	      really_longopts[irl] = ld_options[i].opt;
713 	      ++irl;
714 	    }
715 	  else
716 	    {
717 	      longopts[il] = ld_options[i].opt;
718 	      ++il;
719 	    }
720 	}
721     }
722   shortopts[is] = '\0';
723   longopts[il].name = NULL;
724   really_longopts[irl].name = NULL;
725 
726   ldemul_add_options (is, &shortopts, il, &longopts, irl, &really_longopts);
727 
728   /* The -G option is ambiguous on different platforms.  Sometimes it
729      specifies the largest data size to put into the small data
730      section.  Sometimes it is equivalent to --shared.  Unfortunately,
731      the first form takes an argument, while the second does not.
732 
733      We need to permit the --shared form because on some platforms,
734      such as Solaris, gcc -shared will pass -G to the linker.
735 
736      To permit either usage, we look through the argument list.  If we
737      find -G not followed by a number, we change it into --shared.
738      This will work for most normal cases.  */
739   for (i = 1; i < argc; i++)
740     if (strcmp (argv[i], "-G") == 0
741 	&& (i + 1 >= argc
742 	    || ! ISDIGIT (argv[i + 1][0])))
743       argv[i] = (char *) "--shared";
744 
745   /* Because we permit long options to start with a single dash, and
746      we have a --library option, and the -l option is conventionally
747      used with an immediately following argument, we can have bad
748      results if somebody tries to use -l with a library whose name
749      happens to start with "ibrary", as in -li.  We avoid problems by
750      simply turning -l into --library.  This means that users will
751      have to use two dashes in order to use --library, which is OK
752      since that's how it is documented.
753 
754      FIXME: It's possible that this problem can arise for other short
755      options as well, although the user does always have the recourse
756      of adding a space between the option and the argument.  */
757   for (i = 1; i < argc; i++)
758     {
759       if (argv[i][0] == '-'
760 	  && argv[i][1] == 'l'
761 	  && argv[i][2] != '\0')
762 	{
763 	  char *n;
764 
765 	  n = (char *) xmalloc (strlen (argv[i]) + 20);
766 	  sprintf (n, "--library=%s", argv[i] + 2);
767 	  argv[i] = n;
768 	}
769     }
770 
771   last_optind = -1;
772   while (1)
773     {
774       int longind = 0;
775       int optc;
776       static unsigned int defsym_count;
777 
778       /* Using last_optind lets us avoid calling ldemul_parse_args
779 	 multiple times on a single option, which would lead to
780 	 confusion in the internal static variables maintained by
781 	 getopt.  This could otherwise happen for an argument like
782 	 -nx, in which the -n is parsed as a single option, and we
783 	 loop around to pick up the -x.  */
784       if (optind != last_optind)
785 	if (ldemul_parse_args (argc, argv))
786 	  continue;
787 
788       /* getopt_long_only is like getopt_long, but '-' as well as '--'
789 	 can indicate a long option.  */
790       opterr = 0;
791       last_optind = optind;
792       optc = getopt_long_only (argc, argv, shortopts, longopts, &longind);
793       if (optc == '?')
794 	{
795 	  optind = last_optind;
796 	  optc = getopt_long (argc, argv, "-", really_longopts, &longind);
797 	}
798       /* Attempt to detect grouped short options,  eg: "-non-start".
799 	 Accepting such options is error prone as it is not clear if the user
800 	 intended "-n -o n-start" or "--non-start".  */
801       else if (longind == 0  /* This is a short option.  */
802 	       && optc > 32  /* It is a valid option.  */
803         /* The character is not the second character of argv[last_optind].  */
804 	       && optc != argv[last_optind][1])
805 	{
806 	  if (optarg)
807 	    einfo (_("%F%P: Error: unable to disambiguate: %s (did you mean -%s ?)\n"),
808 		   argv[last_optind], argv[last_optind]);
809 	  else
810 	    einfo (_("%P: Warning: grouped short command line options are deprecated: %s\n"), argv[last_optind]);
811 	}
812 
813       if (ldemul_handle_option (optc))
814 	continue;
815 
816       if (optc == -1)
817 	break;
818 
819       switch (optc)
820 	{
821 	case '?':
822 	  {
823 	    /* If the last word on the command line is an option that
824 	       requires an argument, getopt will refuse to recognise it.
825 	       Try to catch such options here and issue a more helpful
826 	       error message than just "unrecognized option".  */
827 	    int opt;
828 
829 	    for (opt = ARRAY_SIZE (ld_options); opt--;)
830 	      if (ld_options[opt].opt.has_arg == required_argument
831 		  /* FIXME: There are a few short options that do not
832 		     have long equivalents, but which require arguments.
833 		     We should handle them too.  */
834 		  && ld_options[opt].opt.name != NULL
835 		  && strcmp (argv[last_optind] + ld_options[opt].control, ld_options[opt].opt.name) == 0)
836 		{
837 		  einfo (_("%P: %s: missing argument\n"), argv[last_optind]);
838 		  break;
839 		}
840 
841 	    if (opt == -1)
842 	      einfo (_("%P: unrecognized option '%s'\n"), argv[last_optind]);
843 	  }
844 	  /* Fall through.  */
845 
846 	default:
847 	  einfo (_("%F%P: use the --help option for usage information\n"));
848 	  break;
849 
850 	case 1:			/* File name.  */
851 	  lang_add_input_file (optarg, lang_input_file_is_file_enum, NULL);
852 	  break;
853 
854 	case OPTION_IGNORE:
855 	  break;
856 	case 'a':
857 	  /* For HP/UX compatibility.  Actually -a shared should mean
858 	     ``use only shared libraries'' but, then, we don't
859 	     currently support shared libraries on HP/UX anyhow.  */
860 	  if (strcmp (optarg, "archive") == 0)
861 	    input_flags.dynamic = false;
862 	  else if (strcmp (optarg, "shared") == 0
863 		   || strcmp (optarg, "default") == 0)
864 	    input_flags.dynamic = true;
865 	  else
866 	    einfo (_("%F%P: unrecognized -a option `%s'\n"), optarg);
867 	  break;
868 	case OPTION_ASSERT:
869 	  /* FIXME: We just ignore these, but we should handle them.  */
870 	  if (strcmp (optarg, "definitions") == 0)
871 	    ;
872 	  else if (strcmp (optarg, "nodefinitions") == 0)
873 	    ;
874 	  else if (strcmp (optarg, "nosymbolic") == 0)
875 	    ;
876 	  else if (strcmp (optarg, "pure-text") == 0)
877 	    ;
878 	  else
879 	    einfo (_("%F%P: unrecognized -assert option `%s'\n"), optarg);
880 	  break;
881 	case 'A':
882 	  ldfile_add_arch (optarg);
883 	  break;
884 	case 'b':
885 	  lang_add_target (optarg);
886 	  break;
887 	case 'c':
888 	  ldfile_open_command_file (optarg);
889 	  parser_input = input_mri_script;
890 	  yyparse ();
891 	  break;
892 	case OPTION_CALL_SHARED:
893 	  input_flags.dynamic = true;
894 	  break;
895 	case OPTION_NON_SHARED:
896 	  input_flags.dynamic = false;
897 	  break;
898 	case OPTION_CREF:
899 	  command_line.cref = true;
900 	  link_info.notice_all = true;
901 	  break;
902 	case 'd':
903 	  command_line.force_common_definition = true;
904 	  break;
905 	case OPTION_FORCE_GROUP_ALLOCATION:
906 	  command_line.force_group_allocation = true;
907 	  break;
908 	case OPTION_DEFSYM:
909 	  lex_string = optarg;
910 	  lex_redirect (optarg, "--defsym", ++defsym_count);
911 	  parser_input = input_defsym;
912 	  yyparse ();
913 	  lex_string = NULL;
914 	  break;
915 	case OPTION_DEMANGLE:
916 	  demangling = true;
917 	  if (optarg != NULL)
918 	    {
919 	      enum demangling_styles style;
920 
921 	      style = cplus_demangle_name_to_style (optarg);
922 	      if (style == unknown_demangling)
923 		einfo (_("%F%P: unknown demangling style `%s'\n"),
924 		       optarg);
925 
926 	      cplus_demangle_set_style (style);
927 	    }
928 	  break;
929 	case 'I':		/* Used on Solaris.  */
930 	case OPTION_DYNAMIC_LINKER:
931 	  command_line.interpreter = optarg;
932 	  link_info.nointerp = 0;
933 	  break;
934 	case OPTION_NO_DYNAMIC_LINKER:
935 	  link_info.nointerp = 1;
936 	  break;
937 	case OPTION_SYSROOT:
938 	  /* Already handled in ldmain.c.  */
939 	  break;
940 	case OPTION_EB:
941 	  command_line.endian = ENDIAN_BIG;
942 	  break;
943 	case OPTION_EL:
944 	  command_line.endian = ENDIAN_LITTLE;
945 	  break;
946 	case OPTION_EMBEDDED_RELOCS:
947 	  command_line.embedded_relocs = true;
948 	  break;
949 	case OPTION_EXPORT_DYNAMIC:
950 	case 'E': /* HP/UX compatibility.  */
951 	  link_info.export_dynamic = true;
952 	  break;
953 	case OPTION_NO_EXPORT_DYNAMIC:
954 	  link_info.export_dynamic = false;
955 	  break;
956 	case OPTION_NON_CONTIGUOUS_REGIONS:
957 	  link_info.non_contiguous_regions = true;
958 	  break;
959 	case OPTION_NON_CONTIGUOUS_REGIONS_WARNINGS:
960 	  link_info.non_contiguous_regions_warnings = true;
961 	  break;
962 
963 	case OPTION_ERROR_EXECSTACK:
964 	  link_info.error_execstack = 1;
965 	  break;
966 	case OPTION_NO_ERROR_EXECSTACK:
967 	  link_info.error_execstack = 0;
968 	  break;
969 	case OPTION_WARN_EXECSTACK_OBJECTS:
970 	  link_info.warn_execstack = 2;
971 	  break;
972 	case OPTION_WARN_EXECSTACK:
973 	  link_info.warn_execstack = 1;
974 	  break;
975 	case OPTION_NO_WARN_EXECSTACK:
976 	  link_info.warn_execstack = 0;
977 	  break;
978 
979 	case OPTION_ERROR_RWX_SEGMENTS:
980 	  link_info.warn_is_error_for_rwx_segments = 1;
981 	  break;
982 	case OPTION_NO_ERROR_RWX_SEGMENTS:
983 	  link_info.warn_is_error_for_rwx_segments = 0;
984 	  break;
985 	case OPTION_WARN_RWX_SEGMENTS:
986 	  link_info.no_warn_rwx_segments = 0;
987 	  link_info.user_warn_rwx_segments = 1;
988 	  break;
989 	case OPTION_NO_WARN_RWX_SEGMENTS:
990 	  link_info.no_warn_rwx_segments = 1;
991 	  link_info.user_warn_rwx_segments = 1;
992 	  break;
993 
994 	case 'e':
995 	  lang_add_entry (optarg, true);
996 	  break;
997 	case 'f':
998 	  if (command_line.auxiliary_filters == NULL)
999 	    {
1000 	      command_line.auxiliary_filters = (char **)
1001 		xmalloc (2 * sizeof (char *));
1002 	      command_line.auxiliary_filters[0] = optarg;
1003 	      command_line.auxiliary_filters[1] = NULL;
1004 	    }
1005 	  else
1006 	    {
1007 	      int c;
1008 	      char **p;
1009 
1010 	      c = 0;
1011 	      for (p = command_line.auxiliary_filters; *p != NULL; p++)
1012 		++c;
1013 	      command_line.auxiliary_filters = (char **)
1014 		xrealloc (command_line.auxiliary_filters,
1015 			  (c + 2) * sizeof (char *));
1016 	      command_line.auxiliary_filters[c] = optarg;
1017 	      command_line.auxiliary_filters[c + 1] = NULL;
1018 	    }
1019 	  break;
1020 	case 'F':
1021 	  command_line.filter_shlib = optarg;
1022 	  break;
1023 	case OPTION_FORCE_EXE_SUFFIX:
1024 	  command_line.force_exe_suffix = true;
1025 	  break;
1026 	case 'G':
1027 	  {
1028 	    char *end;
1029 	    g_switch_value = strtoul (optarg, &end, 0);
1030 	    if (*end)
1031 	      einfo (_("%F%P: invalid number `%s'\n"), optarg);
1032 	  }
1033 	  break;
1034 	case 'g':
1035 	  /* Ignore.  */
1036 	  break;
1037 	case OPTION_GC_SECTIONS:
1038 	  link_info.gc_sections = true;
1039 	  break;
1040 	case OPTION_PRINT_GC_SECTIONS:
1041 	  link_info.print_gc_sections = true;
1042 	  break;
1043 	case OPTION_GC_KEEP_EXPORTED:
1044 	  link_info.gc_keep_exported = true;
1045 	  break;
1046 	case OPTION_HELP:
1047 	  help ();
1048 	  xexit (0);
1049 	  break;
1050 	case 'L':
1051 	  ldfile_add_library_path (optarg, true);
1052 	  break;
1053 	case 'l':
1054 	  lang_add_input_file (optarg, lang_input_file_is_l_enum, NULL);
1055 	  break;
1056 	case 'M':
1057 	  config.map_filename = "-";
1058 	  break;
1059 	case 'm':
1060 	  /* Ignore.  Was handled in a pre-parse.   */
1061 	  break;
1062 	case OPTION_MAP:
1063 	  config.map_filename = optarg;
1064 	  break;
1065 	case 'N':
1066 	  config.text_read_only = false;
1067 	  config.magic_demand_paged = false;
1068 	  input_flags.dynamic = false;
1069 	  break;
1070 	case OPTION_NO_OMAGIC:
1071 	  config.text_read_only = true;
1072 	  config.magic_demand_paged = true;
1073 	  /* NB/ Does not set input_flags.dynamic to TRUE.
1074 	     Use --call-shared or -Bdynamic for this.  */
1075 	  break;
1076 	case 'n':
1077 	  config.text_read_only = true;
1078 	  config.magic_demand_paged = false;
1079 	  input_flags.dynamic = false;
1080 	  break;
1081 	case OPTION_NO_DEFINE_COMMON:
1082 	  link_info.inhibit_common_definition = true;
1083 	  break;
1084 	case OPTION_NO_DEMANGLE:
1085 	  demangling = false;
1086 	  break;
1087 	case OPTION_NO_GC_SECTIONS:
1088 	  link_info.gc_sections = false;
1089 	  break;
1090 	case OPTION_NO_PRINT_GC_SECTIONS:
1091 	  link_info.print_gc_sections = false;
1092 	  break;
1093 	case OPTION_NO_KEEP_MEMORY:
1094 	  link_info.keep_memory = false;
1095 	  break;
1096 	case OPTION_NO_UNDEFINED:
1097 	  link_info.unresolved_syms_in_objects = RM_DIAGNOSE;
1098 	  break;
1099 	case OPTION_ALLOW_SHLIB_UNDEFINED:
1100 	  link_info.unresolved_syms_in_shared_libs = RM_IGNORE;
1101 	  break;
1102 	case OPTION_NO_ALLOW_SHLIB_UNDEFINED:
1103 	  link_info.unresolved_syms_in_shared_libs = RM_DIAGNOSE;
1104 	  break;
1105 	case OPTION_UNRESOLVED_SYMBOLS:
1106 	  if (strcmp (optarg, "ignore-all") == 0)
1107 	    {
1108 	      link_info.unresolved_syms_in_objects = RM_IGNORE;
1109 	      link_info.unresolved_syms_in_shared_libs = RM_IGNORE;
1110 	    }
1111 	  else if (strcmp (optarg, "report-all") == 0)
1112 	    {
1113 	      link_info.unresolved_syms_in_objects = RM_DIAGNOSE;
1114 	      link_info.unresolved_syms_in_shared_libs = RM_DIAGNOSE;
1115 	    }
1116 	  else if (strcmp (optarg, "ignore-in-object-files") == 0)
1117 	    {
1118 	      link_info.unresolved_syms_in_objects = RM_IGNORE;
1119 	      link_info.unresolved_syms_in_shared_libs = RM_DIAGNOSE;
1120 	    }
1121 	  else if (strcmp (optarg, "ignore-in-shared-libs") == 0)
1122 	    {
1123 	      link_info.unresolved_syms_in_objects = RM_DIAGNOSE;
1124 	      link_info.unresolved_syms_in_shared_libs = RM_IGNORE;
1125 	    }
1126 	  else
1127 	    einfo (_("%F%P: bad --unresolved-symbols option: %s\n"), optarg);
1128 	  break;
1129 	case OPTION_WARN_UNRESOLVED_SYMBOLS:
1130 	  link_info.warn_unresolved_syms = true;
1131 	  break;
1132 	case OPTION_ERROR_UNRESOLVED_SYMBOLS:
1133 	  link_info.warn_unresolved_syms = false;
1134 	  break;
1135 	case OPTION_ALLOW_MULTIPLE_DEFINITION:
1136 	  link_info.allow_multiple_definition = true;
1137 	  break;
1138 
1139 #if SUPPORT_ERROR_HANDLING_SCRIPT
1140 	case OPTION_ERROR_HANDLING_SCRIPT:
1141 	  /* FIXME: Should we warn if the script is being overridden by another ?
1142 	     Or maybe they should be chained together ?  */
1143 	  error_handling_script = optarg;
1144 	  break;
1145 #endif
1146 
1147 	case OPTION_ENABLE_LINKER_VERSION:
1148 	  enable_linker_version = true;
1149 	  break;
1150 	case OPTION_DISABLE_LINKER_VERSION:
1151 	  enable_linker_version = false;
1152 	  break;
1153 
1154 	case OPTION_UNDEFINED_VERSION:
1155 	  link_info.allow_undefined_version = true;
1156 	  break;
1157 	case OPTION_NO_UNDEFINED_VERSION:
1158 	  link_info.allow_undefined_version = false;
1159 	  break;
1160 	case OPTION_DEFAULT_SYMVER:
1161 	  link_info.create_default_symver = true;
1162 	  break;
1163 	case OPTION_DEFAULT_IMPORTED_SYMVER:
1164 	  link_info.default_imported_symver = true;
1165 	  break;
1166 	case OPTION_NO_WARN_MISMATCH:
1167 	  command_line.warn_mismatch = false;
1168 	  break;
1169 	case OPTION_NO_WARN_SEARCH_MISMATCH:
1170 	  command_line.warn_search_mismatch = false;
1171 	  break;
1172 	case OPTION_NOINHIBIT_EXEC:
1173 	  force_make_executable = true;
1174 	  break;
1175 	case OPTION_NOSTDLIB:
1176 	  config.only_cmd_line_lib_dirs = true;
1177 	  break;
1178 	case OPTION_NO_WHOLE_ARCHIVE:
1179 	  input_flags.whole_archive = false;
1180 	  break;
1181 	case 'O':
1182 	  /* FIXME "-O<non-digits> <value>" used to set the address of
1183 	     section <non-digits>.  Was this for compatibility with
1184 	     something, or can we create a new option to do that
1185 	     (with a syntax similar to -defsym)?
1186 	     getopt can't handle two args to an option without kludges.  */
1187 
1188 	  /* Enable optimizations of output files.  */
1189 	  link_info.optimize = strtoul (optarg, NULL, 0) != 0;
1190 	  break;
1191 	case 'o':
1192 	  lang_add_output (optarg, 0);
1193 	  break;
1194 	case OPTION_OFORMAT:
1195 	  lang_add_output_format (optarg, NULL, NULL, 0);
1196 	  break;
1197 	case OPTION_OUT_IMPLIB:
1198 	  command_line.out_implib_filename = xstrdup (optarg);
1199 	  break;
1200 	case OPTION_PRINT_SYSROOT:
1201 	  if (*ld_sysroot)
1202 	    puts (ld_sysroot);
1203 	  xexit (0);
1204 	  break;
1205 	case OPTION_PRINT_OUTPUT_FORMAT:
1206 	  command_line.print_output_format = true;
1207 	  break;
1208 #if BFD_SUPPORTS_PLUGINS
1209 	case OPTION_PLUGIN:
1210 	  plugin_opt_plugin (optarg);
1211 	  break;
1212 	case OPTION_PLUGIN_OPT:
1213 	  if (plugin_opt_plugin_arg (optarg))
1214 	    einfo (_("%F%P: bad -plugin-opt option\n"));
1215 	  break;
1216 #endif /* BFD_SUPPORTS_PLUGINS */
1217 	case 'q':
1218 	  link_info.emitrelocations = true;
1219 	  break;
1220 	case 'i':
1221 	case 'r':
1222 	  if (optind == last_optind)
1223 	    /* This can happen if the user put "-rpath,a" on the command
1224 	       line.  (Or something similar.  The comma is important).
1225 	       Getopt becomes confused and thinks that this is a -r option
1226 	       but it cannot parse the text after the -r so it refuses to
1227 	       increment the optind counter.  Detect this case and issue
1228 	       an error message here.  We cannot just make this a warning,
1229 	       increment optind, and continue because getopt is too confused
1230 	       and will seg-fault the next time around.  */
1231 	    einfo(_("%F%P: unrecognised option: %s\n"), argv[optind]);
1232 
1233 	  if (bfd_link_pic (&link_info))
1234 	    einfo (_("%F%P: -r and %s may not be used together\n"),
1235 		     bfd_link_dll (&link_info) ? "-shared" : "-pie");
1236 
1237 	  link_info.type = type_relocatable;
1238 	  config.build_constructors = false;
1239 	  config.magic_demand_paged = false;
1240 	  config.text_read_only = false;
1241 	  input_flags.dynamic = false;
1242 	  break;
1243 	case 'R':
1244 	  /* The GNU linker traditionally uses -R to mean to include
1245 	     only the symbols from a file.  The Solaris linker uses -R
1246 	     to set the path used by the runtime linker to find
1247 	     libraries.  This is the GNU linker -rpath argument.  We
1248 	     try to support both simultaneously by checking the file
1249 	     named.  If it is a directory, rather than a regular file,
1250 	     we assume -rpath was meant.  */
1251 	  {
1252 	    struct stat s;
1253 
1254 	    if (stat (optarg, &s) >= 0
1255 		&& ! S_ISDIR (s.st_mode))
1256 	      {
1257 		lang_add_input_file (optarg,
1258 				     lang_input_file_is_symbols_only_enum,
1259 				     NULL);
1260 		break;
1261 	      }
1262 	  }
1263 	  /* Fall through.  */
1264 	case OPTION_RPATH:
1265 	  if (command_line.rpath == NULL)
1266 	    command_line.rpath = xstrdup (optarg);
1267 	  else
1268 	    {
1269 	      size_t rpath_len = strlen (command_line.rpath);
1270 	      size_t optarg_len = strlen (optarg);
1271 	      char *buf;
1272 	      char *cp = command_line.rpath;
1273 
1274 	      /* First see whether OPTARG is already in the path.  */
1275 	      do
1276 		{
1277 		  if (strncmp (optarg, cp, optarg_len) == 0
1278 		      && (cp[optarg_len] == 0
1279 			  || cp[optarg_len] == config.rpath_separator))
1280 		    /* We found it.  */
1281 		    break;
1282 
1283 		  /* Not yet found.  */
1284 		  cp = strchr (cp, config.rpath_separator);
1285 		  if (cp != NULL)
1286 		    ++cp;
1287 		}
1288 	      while (cp != NULL);
1289 
1290 	      if (cp == NULL)
1291 		{
1292 		  buf = (char *) xmalloc (rpath_len + optarg_len + 2);
1293 		  sprintf (buf, "%s%c%s", command_line.rpath,
1294 			   config.rpath_separator, optarg);
1295 		  free (command_line.rpath);
1296 		  command_line.rpath = buf;
1297 		}
1298 	    }
1299 	  break;
1300 	case OPTION_RPATH_LINK:
1301 	  if (command_line.rpath_link == NULL)
1302 	    command_line.rpath_link = xstrdup (optarg);
1303 	  else
1304 	    {
1305 	      char *buf;
1306 
1307 	      buf = (char *) xmalloc (strlen (command_line.rpath_link)
1308 				      + strlen (optarg)
1309 				      + 2);
1310 	      sprintf (buf, "%s%c%s", command_line.rpath_link,
1311 		       config.rpath_separator, optarg);
1312 	      free (command_line.rpath_link);
1313 	      command_line.rpath_link = buf;
1314 	    }
1315 	  break;
1316 	case OPTION_NO_RELAX:
1317 	  DISABLE_RELAXATION;
1318 	  break;
1319 	case OPTION_RELAX:
1320 	  ENABLE_RELAXATION;
1321 	  break;
1322 	case OPTION_RETAIN_SYMBOLS_FILE:
1323 	  add_keepsyms_file (optarg);
1324 	  break;
1325 	case 'S':
1326 	  link_info.strip = strip_debugger;
1327 	  break;
1328 	case 's':
1329 	  link_info.strip = strip_all;
1330 	  break;
1331 	case OPTION_STRIP_DISCARDED:
1332 	  link_info.strip_discarded = true;
1333 	  break;
1334 	case OPTION_NO_STRIP_DISCARDED:
1335 	  link_info.strip_discarded = false;
1336 	  break;
1337 	case OPTION_DISABLE_MULTIPLE_DEFS_ABS:
1338 	  link_info.prohibit_multiple_definition_absolute = true;
1339 	  break;
1340 	case OPTION_SHARED:
1341 	  if (config.has_shared)
1342 	    {
1343 	      if (bfd_link_relocatable (&link_info))
1344 		einfo (_("%F%P: -r and %s may not be used together\n"),
1345 		       "-shared");
1346 
1347 	      link_info.type = type_dll;
1348 	      /* When creating a shared library, the default
1349 		 behaviour is to ignore any unresolved references.  */
1350 	      if (link_info.unresolved_syms_in_objects == RM_NOT_YET_SET)
1351 		link_info.unresolved_syms_in_objects = RM_IGNORE;
1352 	      if (link_info.unresolved_syms_in_shared_libs == RM_NOT_YET_SET)
1353 		link_info.unresolved_syms_in_shared_libs = RM_IGNORE;
1354 	    }
1355 	  else
1356 	    einfo (_("%F%P: -shared not supported\n"));
1357 	  break;
1358 	case OPTION_NO_PIE:
1359 	  link_info.type = type_pde;
1360 	  break;
1361 	case OPTION_PIE:
1362 	  if (config.has_shared)
1363 	    {
1364 	      if (bfd_link_relocatable (&link_info))
1365 		einfo (_("%F%P: -r and %s may not be used together\n"), "-pie");
1366 
1367 	      link_info.type = type_pie;
1368 	    }
1369 	  else
1370 	    einfo (_("%F%P: -pie not supported\n"));
1371 	  break;
1372 	case 'h':		/* Used on Solaris.  */
1373 	case OPTION_SONAME:
1374 	  if (optarg[0] == '\0' && command_line.soname
1375 	      && command_line.soname[0])
1376 	    einfo (_("%P: SONAME must not be empty string; keeping previous one\n"));
1377 	  else
1378 	    command_line.soname = optarg;
1379 	  break;
1380 	case OPTION_SORT_COMMON:
1381 	  if (optarg == NULL
1382 	      || strcmp (optarg, N_("descending")) == 0)
1383 	    config.sort_common = sort_descending;
1384 	  else if (strcmp (optarg, N_("ascending")) == 0)
1385 	    config.sort_common = sort_ascending;
1386 	  else
1387 	    einfo (_("%F%P: invalid common section sorting option: %s\n"),
1388 		   optarg);
1389 	  break;
1390 	case OPTION_SORT_SECTION:
1391 	  if (strcmp (optarg, N_("name")) == 0)
1392 	    sort_section = by_name;
1393 	  else if (strcmp (optarg, N_("alignment")) == 0)
1394 	    sort_section = by_alignment;
1395 	  else
1396 	    einfo (_("%F%P: invalid section sorting option: %s\n"),
1397 		   optarg);
1398 	  break;
1399 	case OPTION_STATS:
1400 	  config.stats = true;
1401 	  break;
1402 	case OPTION_NO_SYMBOLIC:
1403 	  opt_symbolic = symbolic_unset;
1404 	  break;
1405 	case OPTION_SYMBOLIC:
1406 	  opt_symbolic = symbolic;
1407 	  break;
1408 	case OPTION_SYMBOLIC_FUNCTIONS:
1409 	  opt_symbolic = symbolic_functions;
1410 	  break;
1411 	case 't':
1412 	  ++trace_files;
1413 	  break;
1414 	case 'T':
1415 	  previous_script_handle = saved_script_handle;
1416 	  ldfile_open_script_file (optarg);
1417 	  parser_input = input_script;
1418 	  yyparse ();
1419 	  previous_script_handle = NULL;
1420 	  break;
1421 	case OPTION_DEFAULT_SCRIPT:
1422 	  command_line.default_script = optarg;
1423 	  break;
1424 	case OPTION_SECTION_START:
1425 	  {
1426 	    char *optarg2;
1427 	    char *sec_name;
1428 	    int len;
1429 
1430 	    /* Check for <something>=<somthing>...  */
1431 	    optarg2 = strchr (optarg, '=');
1432 	    if (optarg2 == NULL)
1433 	      einfo (_("%F%P: invalid argument to option"
1434 		       " \"--section-start\"\n"));
1435 
1436 	    optarg2++;
1437 
1438 	    /* So far so good.  Are all the args present?  */
1439 	    if ((*optarg == '\0') || (*optarg2 == '\0'))
1440 	      einfo (_("%F%P: missing argument(s) to option"
1441 		       " \"--section-start\"\n"));
1442 
1443 	    /* We must copy the section name as set_section_start
1444 	       doesn't do it for us.  */
1445 	    len = optarg2 - optarg;
1446 	    sec_name = (char *) xmalloc (len);
1447 	    memcpy (sec_name, optarg, len - 1);
1448 	    sec_name[len - 1] = 0;
1449 
1450 	    /* Then set it...  */
1451 	    set_section_start (sec_name, optarg2);
1452 	  }
1453 	  break;
1454 	case OPTION_TARGET_HELP:
1455 	  /* Mention any target specific options.  */
1456 	  ldemul_list_emulation_options (stdout);
1457 	  exit (0);
1458 	case OPTION_TBSS:
1459 	  set_segment_start (".bss", optarg);
1460 	  break;
1461 	case OPTION_TDATA:
1462 	  set_segment_start (".data", optarg);
1463 	  break;
1464 	case OPTION_TTEXT:
1465 	  set_segment_start (".text", optarg);
1466 	  break;
1467 	case OPTION_TTEXT_SEGMENT:
1468 	  set_segment_start (".text-segment", optarg);
1469 	  break;
1470 	case OPTION_TRODATA_SEGMENT:
1471 	  set_segment_start (".rodata-segment", optarg);
1472 	  break;
1473 	case OPTION_TLDATA_SEGMENT:
1474 	  set_segment_start (".ldata-segment", optarg);
1475 	  break;
1476 	case OPTION_TRADITIONAL_FORMAT:
1477 	  link_info.traditional_format = true;
1478 	  break;
1479 	case OPTION_TASK_LINK:
1480 	  link_info.task_link = true;
1481 	  /* Fall through.  */
1482 	case OPTION_UR:
1483 	  if (bfd_link_pic (&link_info))
1484 	    einfo (_("%F%P: -r and %s may not be used together\n"),
1485 		     bfd_link_dll (&link_info) ? "-shared" : "-pie");
1486 
1487 	  link_info.type = type_relocatable;
1488 	  config.build_constructors = true;
1489 	  config.magic_demand_paged = false;
1490 	  config.text_read_only = false;
1491 	  input_flags.dynamic = false;
1492 	  break;
1493 	case 'u':
1494 	  ldlang_add_undef (optarg, true);
1495 	  break;
1496 	case OPTION_REQUIRE_DEFINED_SYMBOL:
1497 	  ldlang_add_require_defined (optarg);
1498 	  break;
1499 	case OPTION_UNIQUE:
1500 	  if (optarg != NULL)
1501 	    lang_add_unique (optarg);
1502 	  else
1503 	    config.unique_orphan_sections = true;
1504 	  break;
1505 	case OPTION_VERBOSE:
1506 	  ldversion (1);
1507 	  version_printed = true;
1508 	  verbose = true;
1509 	  overflow_cutoff_limit = -2;
1510 	  if (optarg != NULL)
1511 	    {
1512 	      char *end;
1513 	      int level ATTRIBUTE_UNUSED = strtoul (optarg, &end, 0);
1514 	      if (*end)
1515 		einfo (_("%F%P: invalid number `%s'\n"), optarg);
1516 #if BFD_SUPPORTS_PLUGINS
1517 	      report_plugin_symbols = level > 1;
1518 #endif /* BFD_SUPPORTS_PLUGINS */
1519 	    }
1520 	  break;
1521 	case 'v':
1522 	  ldversion (0);
1523 	  version_printed = true;
1524 	  break;
1525 	case 'V':
1526 	  ldversion (1);
1527 	  version_printed = true;
1528 	  break;
1529 	case OPTION_VERSION:
1530 	  ldversion (2);
1531 	  xexit (0);
1532 	  break;
1533 	case OPTION_VERSION_SCRIPT:
1534 	  /* This option indicates a small script that only specifies
1535 	     version information.  Read it, but don't assume that
1536 	     we've seen a linker script.  */
1537 	  {
1538 	    FILE *hold_script_handle;
1539 
1540 	    hold_script_handle = saved_script_handle;
1541 	    ldfile_open_command_file (optarg);
1542 	    saved_script_handle = hold_script_handle;
1543 	    parser_input = input_version_script;
1544 	    yyparse ();
1545 	  }
1546 	  break;
1547 	case OPTION_VERSION_EXPORTS_SECTION:
1548 	  /* This option records a version symbol to be applied to the
1549 	     symbols listed for export to be found in the object files
1550 	     .exports sections.  */
1551 	  command_line.version_exports_section = optarg;
1552 	  break;
1553 	case OPTION_DYNAMIC_LIST_DATA:
1554 	  opt_dynamic_list = dynamic_list_data;
1555 	  break;
1556 	case OPTION_DYNAMIC_LIST_CPP_TYPEINFO:
1557 	  lang_append_dynamic_list_cpp_typeinfo ();
1558 	  if (opt_dynamic_list != dynamic_list_data)
1559 	    opt_dynamic_list = dynamic_list;
1560 	  break;
1561 	case OPTION_DYNAMIC_LIST_CPP_NEW:
1562 	  lang_append_dynamic_list_cpp_new ();
1563 	  if (opt_dynamic_list != dynamic_list_data)
1564 	    opt_dynamic_list = dynamic_list;
1565 	  break;
1566 	case OPTION_DYNAMIC_LIST:
1567 	  /* This option indicates a small script that only specifies
1568 	     a dynamic list.  Read it, but don't assume that we've
1569 	     seen a linker script.  */
1570 	  {
1571 	    FILE *hold_script_handle;
1572 
1573 	    hold_script_handle = saved_script_handle;
1574 	    ldfile_open_command_file (optarg);
1575 	    saved_script_handle = hold_script_handle;
1576 	    parser_input = input_dynamic_list;
1577 	    current_dynamic_list_p = &link_info.dynamic_list;
1578 	    yyparse ();
1579 	  }
1580 	  if (opt_dynamic_list != dynamic_list_data)
1581 	    opt_dynamic_list = dynamic_list;
1582 	  break;
1583 	case OPTION_EXPORT_DYNAMIC_SYMBOL:
1584 	  {
1585 	    struct bfd_elf_version_expr *expr
1586 	      = lang_new_vers_pattern (NULL, xstrdup (optarg), NULL,
1587 				       false);
1588 	    lang_append_dynamic_list (&export_list, expr);
1589 	  }
1590 	  break;
1591 	case OPTION_EXPORT_DYNAMIC_SYMBOL_LIST:
1592 	  /* This option indicates a small script that only specifies
1593 	     an export list.  Read it, but don't assume that we've
1594 	     seen a linker script.  */
1595 	  {
1596 	    FILE *hold_script_handle;
1597 
1598 	    hold_script_handle = saved_script_handle;
1599 	    ldfile_open_command_file (optarg);
1600 	    saved_script_handle = hold_script_handle;
1601 	    parser_input = input_dynamic_list;
1602 	    current_dynamic_list_p = &export_list;
1603 	    yyparse ();
1604 	  }
1605 	  break;
1606 	case OPTION_WARN_COMMON:
1607 	  config.warn_common = true;
1608 	  break;
1609 	case OPTION_WARN_CONSTRUCTORS:
1610 	  config.warn_constructors = true;
1611 	  break;
1612 	case OPTION_WARN_FATAL:
1613 	  config.fatal_warnings = true;
1614 	  break;
1615 	case OPTION_NO_WARN_FATAL:
1616 	  config.fatal_warnings = false;
1617 	  break;
1618 	case OPTION_NO_WARNINGS:
1619 	case 'w':
1620 	  config.no_warnings = true;
1621 	  config.fatal_warnings = false;
1622 	  break;
1623 	case OPTION_WARN_MULTIPLE_GP:
1624 	  config.warn_multiple_gp = true;
1625 	  break;
1626 	case OPTION_WARN_ONCE:
1627 	  config.warn_once = true;
1628 	  break;
1629 	case OPTION_WARN_SECTION_ALIGN:
1630 	  config.warn_section_align = true;
1631 	  break;
1632 	case OPTION_WARN_TEXTREL:
1633 	  link_info.textrel_check = textrel_check_warning;
1634 	  break;
1635 	case OPTION_WARN_ALTERNATE_EM:
1636 	  link_info.warn_alternate_em = true;
1637 	  break;
1638 	case OPTION_WHOLE_ARCHIVE:
1639 	  input_flags.whole_archive = true;
1640 	  break;
1641 	case OPTION_ADD_DT_NEEDED_FOR_DYNAMIC:
1642 	  input_flags.add_DT_NEEDED_for_dynamic = true;
1643 	  break;
1644 	case OPTION_NO_ADD_DT_NEEDED_FOR_DYNAMIC:
1645 	  input_flags.add_DT_NEEDED_for_dynamic = false;
1646 	  break;
1647 	case OPTION_ADD_DT_NEEDED_FOR_REGULAR:
1648 	  input_flags.add_DT_NEEDED_for_regular = true;
1649 	  break;
1650 	case OPTION_NO_ADD_DT_NEEDED_FOR_REGULAR:
1651 	  input_flags.add_DT_NEEDED_for_regular = false;
1652 	  break;
1653 	case OPTION_WRAP:
1654 	  add_wrap (optarg);
1655 	  break;
1656 	case OPTION_IGNORE_UNRESOLVED_SYMBOL:
1657 	  add_ignoresym (&link_info, optarg);
1658 	  break;
1659 	case OPTION_DISCARD_NONE:
1660 	  link_info.discard = discard_none;
1661 	  break;
1662 	case 'X':
1663 	  link_info.discard = discard_l;
1664 	  break;
1665 	case 'x':
1666 	  link_info.discard = discard_all;
1667 	  break;
1668 	case 'Y':
1669 	  if (startswith (optarg, "P,"))
1670 	    optarg += 2;
1671 	  free (default_dirlist);
1672 	  default_dirlist = xstrdup (optarg);
1673 	  break;
1674 	case 'y':
1675 	  add_ysym (optarg);
1676 	  break;
1677 	case OPTION_SPARE_DYNAMIC_TAGS:
1678 	  link_info.spare_dynamic_tags = strtoul (optarg, NULL, 0);
1679 	  break;
1680 	case OPTION_SPLIT_BY_RELOC:
1681 	  if (optarg != NULL)
1682 	    config.split_by_reloc = strtoul (optarg, NULL, 0);
1683 	  else
1684 	    config.split_by_reloc = 32768;
1685 	  break;
1686 	case OPTION_SPLIT_BY_FILE:
1687 	  if (optarg != NULL)
1688 	    config.split_by_file = bfd_scan_vma (optarg, NULL, 0);
1689 	  else
1690 	    config.split_by_file = 1;
1691 	  break;
1692 	case OPTION_CHECK_SECTIONS:
1693 	  command_line.check_section_addresses = 1;
1694 	  break;
1695 	case OPTION_NO_CHECK_SECTIONS:
1696 	  command_line.check_section_addresses = 0;
1697 	  break;
1698 	case OPTION_ACCEPT_UNKNOWN_INPUT_ARCH:
1699 	  command_line.accept_unknown_input_arch = true;
1700 	  break;
1701 	case OPTION_NO_ACCEPT_UNKNOWN_INPUT_ARCH:
1702 	  command_line.accept_unknown_input_arch = false;
1703 	  break;
1704 	case '(':
1705 	  lang_enter_group ();
1706 	  ingroup++;
1707 	  break;
1708 	case ')':
1709 	  if (! ingroup)
1710 	    einfo (_("%F%P: group ended before it began (--help for usage)\n"));
1711 
1712 	  lang_leave_group ();
1713 	  ingroup--;
1714 	  break;
1715 
1716 	case OPTION_INIT:
1717 	  link_info.init_function = optarg;
1718 	  break;
1719 
1720 	case OPTION_FINI:
1721 	  link_info.fini_function = optarg;
1722 	  break;
1723 
1724 	case OPTION_REMAP_INPUTS_FILE:
1725 	  if (! ldfile_add_remap_file (optarg))
1726 	    einfo (_("%F%P: failed to add remap file %s\n"), optarg);
1727 	  break;
1728 
1729 	case OPTION_REMAP_INPUTS:
1730 	  {
1731 	    char *optarg2 = strchr (optarg, '=');
1732 	    if (optarg2 == NULL)
1733 	      /* FIXME: Should we allow --remap-inputs=@myfile as a synonym
1734 		 for --remap-inputs-file=myfile ?  */
1735 	      einfo (_("%F%P: invalid argument to option --remap-inputs\n"));
1736 	    size_t len = optarg2 - optarg;
1737 	    char * pattern = xmalloc (len + 1);
1738 	    memcpy (pattern, optarg, len);
1739 	    pattern[len] = 0;
1740 	    ldfile_add_remap (pattern, optarg2 + 1);
1741 	    free (pattern);
1742 	  }
1743 	  break;
1744 
1745 	case OPTION_REDUCE_MEMORY_OVERHEADS:
1746 	  link_info.reduce_memory_overheads = true;
1747 	  if (config.hash_table_size == 0)
1748 	    config.hash_table_size = 1021;
1749 	  break;
1750 
1751 	case OPTION_MAX_CACHE_SIZE:
1752 	  {
1753 	    char *end;
1754 	    bfd_size_type cache_size = strtoul (optarg, &end, 0);
1755 	    if (*end != '\0')
1756 	      einfo (_("%F%P: invalid cache memory size: %s\n"),
1757 		     optarg);
1758 	    link_info.max_cache_size = cache_size;
1759 	  }
1760 	  break;
1761 
1762 	case OPTION_HASH_SIZE:
1763 	  {
1764 	    bfd_size_type new_size;
1765 
1766 	    new_size = strtoul (optarg, NULL, 0);
1767 	    if (new_size)
1768 	      config.hash_table_size = new_size;
1769 	    else
1770 	      einfo (_("%X%P: --hash-size needs a numeric argument\n"));
1771 	  }
1772 	  break;
1773 
1774 	case OPTION_PUSH_STATE:
1775 	  input_flags.pushed = xmemdup (&input_flags,
1776 					sizeof (input_flags),
1777 					sizeof (input_flags));
1778 	  break;
1779 
1780 	case OPTION_POP_STATE:
1781 	  if (input_flags.pushed == NULL)
1782 	    einfo (_("%F%P: no state pushed before popping\n"));
1783 	  else
1784 	    {
1785 	      struct lang_input_statement_flags *oldp = input_flags.pushed;
1786 	      memcpy (&input_flags, oldp, sizeof (input_flags));
1787 	      free (oldp);
1788 	    }
1789 	  break;
1790 
1791 	case OPTION_PRINT_MEMORY_USAGE:
1792 	  command_line.print_memory_usage = true;
1793 	  break;
1794 
1795 	case OPTION_ORPHAN_HANDLING:
1796 	  if (strcasecmp (optarg, "place") == 0)
1797 	    config.orphan_handling = orphan_handling_place;
1798 	  else if (strcasecmp (optarg, "warn") == 0)
1799 	    config.orphan_handling = orphan_handling_warn;
1800 	  else if (strcasecmp (optarg, "error") == 0)
1801 	    config.orphan_handling = orphan_handling_error;
1802 	  else if (strcasecmp (optarg, "discard") == 0)
1803 	    config.orphan_handling = orphan_handling_discard;
1804 	  else
1805 	    einfo (_("%F%P: invalid argument to option"
1806 		     " \"--orphan-handling\"\n"));
1807 	  break;
1808 
1809 	case OPTION_NO_PRINT_MAP_DISCARDED:
1810 	  config.print_map_discarded = false;
1811 	  break;
1812 
1813 	case OPTION_PRINT_MAP_DISCARDED:
1814 	  config.print_map_discarded = true;
1815 	  break;
1816 
1817 	case OPTION_NO_PRINT_MAP_LOCALS:
1818 	  config.print_map_locals = false;
1819 	  break;
1820 
1821 	case OPTION_PRINT_MAP_LOCALS:
1822 	  config.print_map_locals = true;
1823 	  break;
1824 
1825 	case OPTION_DEPENDENCY_FILE:
1826 	  config.dependency_file = optarg;
1827 	  break;
1828 
1829 	case OPTION_CTF_VARIABLES:
1830 	  config.ctf_variables = true;
1831 	  break;
1832 
1833 	case OPTION_NO_CTF_VARIABLES:
1834 	  config.ctf_variables = false;
1835 	  break;
1836 
1837 	case OPTION_CTF_SHARE_TYPES:
1838 	  if (strcmp (optarg, "share-unconflicted") == 0)
1839 	    config.ctf_share_duplicated = false;
1840 	  else if (strcmp (optarg, "share-duplicated") == 0)
1841 	    config.ctf_share_duplicated = true;
1842 	  else
1843 	    einfo (_("%F%P: bad --ctf-share-types option: %s\n"), optarg);
1844 	  break;
1845 	}
1846     }
1847 
1848   free (really_longopts);
1849   free (longopts);
1850   free (shortopts);
1851 
1852   /* Run a couple of checks on the map filename.  */
1853   if (config.map_filename)
1854     {
1855       char * new_name = NULL;
1856       char * percent;
1857       int    res = 0;
1858 
1859       if (config.map_filename[0] == 0)
1860 	{
1861 	  einfo (_("%P: no file/directory name provided for map output; ignored\n"));
1862 	  config.map_filename = NULL;
1863 	}
1864       else if (strcmp (config.map_filename, "-") == 0)
1865 	; /* Write to stdout.  Handled in main().  */
1866       else if ((percent = strchr (config.map_filename, '%')) != NULL)
1867 	{
1868 	  /* FIXME: Check for a second % character and issue an error ?  */
1869 
1870 	  /* Construct a map file by replacing the % character with the (full)
1871 	     output filename.  If the % character was the last character in
1872 	     the original map filename then add a .map extension.  */
1873 	  percent[0] = 0;
1874 	  res = asprintf (&new_name, "%s%s%s", config.map_filename,
1875 			  output_filename,
1876 			  percent[1] ? percent + 1 : ".map");
1877 
1878 	  /* FIXME: Should we ensure that any directory components in new_name exist ?  */
1879 	}
1880       else
1881 	{
1882 	  struct stat s;
1883 
1884 	  /* If the map filename is actually a directory then create
1885 	     a file inside it, based upon the output filename.  */
1886 	  if (stat (config.map_filename, &s) < 0)
1887 	    {
1888 	      if (errno != ENOENT)
1889 		einfo (_("%P: cannot stat linker map file: %E\n"));
1890 	    }
1891 	  else if (S_ISDIR (s.st_mode))
1892 	    {
1893 	      char lastc = config.map_filename[strlen (config.map_filename) - 1];
1894 	      res = asprintf (&new_name, "%s%s%s.map",
1895 			      config.map_filename,
1896 			      IS_DIR_SEPARATOR (lastc) ? "" : "/",
1897 			      lbasename (output_filename));
1898 	    }
1899 	  else if (! S_ISREG (s.st_mode))
1900 	    {
1901 	      einfo (_("%P: linker map file is not a regular file\n"));
1902 	      config.map_filename = NULL;
1903 	    }
1904 	  /* else FIXME: Check write permission ?  */
1905 	}
1906 
1907       if (res < 0)
1908 	{
1909 	  /* If the asprintf failed then something is probably very
1910 	     wrong.  Better to halt now rather than continue on
1911 	     into more problems.  */
1912 	  einfo (_("%P%F: cannot create name for linker map file: %E\n"));
1913 	}
1914       else if (new_name != NULL)
1915 	{
1916 	  /* This is a trivial memory leak.  */
1917 	  config.map_filename = new_name;
1918 	}
1919     }
1920 
1921   if (command_line.soname && command_line.soname[0] == '\0')
1922     {
1923       einfo (_("%P: SONAME must not be empty string; ignored\n"));
1924       command_line.soname = NULL;
1925     }
1926 
1927   while (ingroup)
1928     {
1929       einfo (_("%P: missing --end-group; added as last command line option\n"));
1930       lang_leave_group ();
1931       ingroup--;
1932     }
1933 
1934   if (default_dirlist != NULL)
1935     {
1936       set_default_dirlist (default_dirlist);
1937       free (default_dirlist);
1938     }
1939 
1940   if (link_info.unresolved_syms_in_objects == RM_NOT_YET_SET)
1941     /* FIXME: Should we allow emulations a chance to set this ?  */
1942     link_info.unresolved_syms_in_objects = RM_DIAGNOSE;
1943 
1944   if (link_info.unresolved_syms_in_shared_libs == RM_NOT_YET_SET)
1945     /* FIXME: Should we allow emulations a chance to set this ?  */
1946     link_info.unresolved_syms_in_shared_libs = RM_DIAGNOSE;
1947 
1948   if (bfd_link_relocatable (&link_info)
1949       && command_line.check_section_addresses < 0)
1950     command_line.check_section_addresses = 0;
1951 
1952   if (export_list)
1953     {
1954       struct bfd_elf_version_expr *head = export_list->head.list;
1955       struct bfd_elf_version_expr *next;
1956 
1957       /* For --export-dynamic-symbol[-list]:
1958 	 1. When building executable, treat like --dynamic-list.
1959 	 2. When building shared object:
1960 	    a. If -Bsymbolic or --dynamic-list are used, treat like
1961 	       --dynamic-list.
1962 	    b. Otherwise, ignored.
1963        */
1964       if (!bfd_link_relocatable (&link_info)
1965 	  && (bfd_link_executable (&link_info)
1966 	      || opt_symbolic != symbolic_unset
1967 	      || opt_dynamic_list != dynamic_list_unset))
1968 	{
1969 	  /* Append the export list to link_info.dynamic_list.  */
1970 	  if (link_info.dynamic_list)
1971 	    {
1972 	      for (next = head; next->next != NULL; next = next->next)
1973 		;
1974 	      next->next = link_info.dynamic_list->head.list;
1975 	      link_info.dynamic_list->head.list = head;
1976 	    }
1977 	  else
1978 	    link_info.dynamic_list = export_list;
1979 
1980 	  if (opt_dynamic_list != dynamic_list_data)
1981 	    opt_dynamic_list = dynamic_list;
1982 	}
1983       else
1984 	{
1985 	  /* Free the export list.  */
1986 	  for (; head->next != NULL; head = next)
1987 	    {
1988 	      next = head->next;
1989 	      free (head);
1990 	    }
1991 	  free (export_list);
1992 	}
1993     }
1994 
1995   switch (opt_dynamic_list)
1996     {
1997     case dynamic_list_unset:
1998       break;
1999     case dynamic_list_data:
2000       link_info.dynamic_data = true;
2001       /* Fall through.  */
2002     case dynamic_list:
2003       link_info.dynamic = true;
2004       opt_symbolic = symbolic_unset;
2005       break;
2006     }
2007 
2008   /* -Bsymbolic and -Bsymbols-functions are for shared library output.  */
2009   if (bfd_link_dll (&link_info))
2010     switch (opt_symbolic)
2011       {
2012       case symbolic_unset:
2013 	break;
2014       case symbolic:
2015 	link_info.symbolic = true;
2016 	if (link_info.dynamic_list)
2017 	  {
2018 	    struct bfd_elf_version_expr *ent, *next;
2019 	    for (ent = link_info.dynamic_list->head.list; ent; ent = next)
2020 	      {
2021 		next = ent->next;
2022 		free (ent);
2023 	      }
2024 	    free (link_info.dynamic_list);
2025 	    link_info.dynamic_list = NULL;
2026 	  }
2027 	break;
2028       case symbolic_functions:
2029 	link_info.dynamic = true;
2030 	link_info.dynamic_data = true;
2031 	break;
2032       }
2033 
2034   /* -z nosectionheader implies --strip-all.  */
2035   if (config.no_section_header)
2036     {
2037       if (bfd_link_relocatable (&link_info))
2038 	einfo (_("%F%P: -r and -z nosectionheader may not be used together\n"));
2039 
2040       link_info.strip = strip_all;
2041     }
2042 
2043   if (!bfd_link_dll (&link_info))
2044     {
2045       if (command_line.filter_shlib)
2046 	einfo (_("%F%P: -F may not be used without -shared\n"));
2047       if (command_line.auxiliary_filters)
2048 	einfo (_("%F%P: -f may not be used without -shared\n"));
2049     }
2050 
2051   /* Treat ld -r -s as ld -r -S -x (i.e., strip all local symbols).  I
2052      don't see how else this can be handled, since in this case we
2053      must preserve all externally visible symbols.  */
2054   if (bfd_link_relocatable (&link_info) && link_info.strip == strip_all)
2055     {
2056       link_info.strip = strip_debugger;
2057       if (link_info.discard == discard_sec_merge)
2058 	link_info.discard = discard_all;
2059     }
2060 }
2061 
2062 /* Add the (colon-separated) elements of DIRLIST_PTR to the
2063    library search path.  */
2064 
2065 static void
set_default_dirlist(char * dirlist_ptr)2066 set_default_dirlist (char *dirlist_ptr)
2067 {
2068   char *p;
2069 
2070   while (1)
2071     {
2072       p = strchr (dirlist_ptr, PATH_SEPARATOR);
2073       if (p != NULL)
2074 	*p = '\0';
2075       if (*dirlist_ptr != '\0')
2076 	ldfile_add_library_path (dirlist_ptr, true);
2077       if (p == NULL)
2078 	break;
2079       dirlist_ptr = p + 1;
2080     }
2081 }
2082 
2083 static void
set_section_start(char * sect,char * valstr)2084 set_section_start (char *sect, char *valstr)
2085 {
2086   const char *end;
2087   bfd_vma val = bfd_scan_vma (valstr, &end, 16);
2088   if (*end)
2089     einfo (_("%F%P: invalid hex number `%s'\n"), valstr);
2090   lang_section_start (sect, exp_intop (val), NULL);
2091 }
2092 
2093 static void
set_segment_start(const char * section,char * valstr)2094 set_segment_start (const char *section, char *valstr)
2095 {
2096   const char *name;
2097   const char *end;
2098   segment_type *seg;
2099 
2100   bfd_vma val = bfd_scan_vma (valstr, &end, 16);
2101   if (*end)
2102     einfo (_("%F%P: invalid hex number `%s'\n"), valstr);
2103   /* If we already have an entry for this segment, update the existing
2104      value.  */
2105   name = section + 1;
2106   for (seg = segments; seg; seg = seg->next)
2107     if (strcmp (seg->name, name) == 0)
2108       {
2109 	seg->value = val;
2110 	lang_section_start (section, exp_intop (val), seg);
2111 	return;
2112       }
2113   /* There was no existing value so we must create a new segment
2114      entry.  */
2115   seg = stat_alloc (sizeof (*seg));
2116   seg->name = name;
2117   seg->value = val;
2118   seg->used = false;
2119   /* Add it to the linked list of segments.  */
2120   seg->next = segments;
2121   segments = seg;
2122   /* Historically, -Ttext and friends set the base address of a
2123      particular section.  For backwards compatibility, we still do
2124      that.  If a SEGMENT_START directive is seen, the section address
2125      assignment will be disabled.  */
2126   lang_section_start (section, exp_intop (val), seg);
2127 }
2128 
2129 static void
elf_shlib_list_options(FILE * file)2130 elf_shlib_list_options (FILE *file)
2131 {
2132   fprintf (file, _("\
2133   --audit=AUDITLIB            Specify a library to use for auditing\n"));
2134   fprintf (file, _("\
2135   -Bgroup                     Selects group name lookup rules for DSO\n"));
2136   fprintf (file, _("\
2137   --disable-new-dtags         Disable new dynamic tags\n"));
2138   fprintf (file, _("\
2139   --enable-new-dtags          Enable new dynamic tags\n"));
2140   fprintf (file, _("\
2141   --eh-frame-hdr              Create .eh_frame_hdr section\n"));
2142   fprintf (file, _("\
2143   --no-eh-frame-hdr           Do not create .eh_frame_hdr section\n"));
2144   fprintf (file, _("\
2145   --exclude-libs=LIBS         Make all symbols in LIBS hidden\n"));
2146   fprintf (file, _("\
2147   --hash-style=STYLE          Set hash style to sysv/gnu/both.  Default: "));
2148   if (DEFAULT_EMIT_SYSV_HASH)
2149     {
2150       /* Note - these strings are not translated as
2151 	 they are keywords not descriptive text.  */
2152       if (DEFAULT_EMIT_GNU_HASH)
2153 	fprintf (file, "both\n");
2154       else
2155 	fprintf (file, "sysv\n");
2156     }
2157   else
2158     {
2159       if (DEFAULT_EMIT_GNU_HASH)
2160 	fprintf (file, "gnu\n");
2161       else
2162 	/* FIXME: Can this happen ?  */
2163 	fprintf (file, "none\n");
2164     }
2165   fprintf (file, _("\
2166   -P AUDITLIB, --depaudit=AUDITLIB\n" "\
2167                               Specify a library to use for auditing dependencies\n"));
2168   fprintf (file, _("\
2169   -z combreloc                Merge dynamic relocs into one section and sort\n"));
2170   fprintf (file, _("\
2171   -z nocombreloc              Don't merge dynamic relocs into one section\n"));
2172   fprintf (file, _("\
2173   -z global                   Make symbols in DSO available for subsequently\n\
2174                                 loaded objects\n"));
2175   fprintf (file, _("\
2176   -z initfirst                Mark DSO to be initialized first at runtime\n"));
2177   fprintf (file, _("\
2178   -z interpose                Mark object to interpose all DSOs but executable\n"));
2179   fprintf (file, _("\
2180   -z unique                   Mark DSO to be loaded at most once by default, and only in the main namespace\n"));
2181   fprintf (file, _("\
2182   -z nounique                 Don't mark DSO as a loadable at most once\n"));
2183   fprintf (file, _("\
2184   -z lazy                     Mark object lazy runtime binding (default)\n"));
2185   fprintf (file, _("\
2186   -z loadfltr                 Mark object requiring immediate process\n"));
2187   fprintf (file, _("\
2188   -z nocopyreloc              Don't create copy relocs\n"));
2189   fprintf (file, _("\
2190   -z nodefaultlib             Mark object not to use default search paths\n"));
2191   fprintf (file, _("\
2192   -z nodelete                 Mark DSO non-deletable at runtime\n"));
2193   fprintf (file, _("\
2194   -z nodlopen                 Mark DSO not available to dlopen\n"));
2195   fprintf (file, _("\
2196   -z nodump                   Mark DSO not available to dldump\n"));
2197   fprintf (file, _("\
2198   -z now                      Mark object non-lazy runtime binding\n"));
2199   fprintf (file, _("\
2200   -z origin                   Mark object requiring immediate $ORIGIN\n\
2201                                 processing at runtime\n"));
2202 #if DEFAULT_LD_Z_RELRO
2203   fprintf (file, _("\
2204   -z relro                    Create RELRO program header (default)\n"));
2205   fprintf (file, _("\
2206   -z norelro                  Don't create RELRO program header\n"));
2207 #else
2208   fprintf (file, _("\
2209   -z relro                    Create RELRO program header\n"));
2210   fprintf (file, _("\
2211   -z norelro                  Don't create RELRO program header (default)\n"));
2212 #endif
2213 #if DEFAULT_LD_Z_SEPARATE_CODE
2214   fprintf (file, _("\
2215   -z separate-code            Create separate code program header (default)\n"));
2216   fprintf (file, _("\
2217   -z noseparate-code          Don't create separate code program header\n"));
2218 #else
2219   fprintf (file, _("\
2220   -z separate-code            Create separate code program header\n"));
2221   fprintf (file, _("\
2222   -z noseparate-code          Don't create separate code program header (default)\n"));
2223 #endif
2224   fprintf (file, _("\
2225   -z common                   Generate common symbols with STT_COMMON type\n"));
2226   fprintf (file, _("\
2227   -z nocommon                 Generate common symbols with STT_OBJECT type\n"));
2228   if (link_info.textrel_check == textrel_check_error)
2229     fprintf (file, _("\
2230   -z text                     Treat DT_TEXTREL in output as error (default)\n"));
2231   else
2232     fprintf (file, _("\
2233   -z text                     Treat DT_TEXTREL in output as error\n"));
2234   if (link_info.textrel_check == textrel_check_none)
2235     {
2236       fprintf (file, _("\
2237   -z notext                   Don't treat DT_TEXTREL in output as error (default)\n"));
2238       fprintf (file, _("\
2239   -z textoff                  Don't treat DT_TEXTREL in output as error (default)\n"));
2240     }
2241   else
2242     {
2243       fprintf (file, _("\
2244   -z notext                   Don't treat DT_TEXTREL in output as error\n"));
2245       fprintf (file, _("\
2246   -z textoff                  Don't treat DT_TEXTREL in output as error\n"));
2247     }
2248 }
2249 
2250 static void
elf_static_list_options(FILE * file)2251 elf_static_list_options (FILE *file)
2252 {
2253   fprintf (file, _("\
2254   --build-id[=STYLE]          Generate build ID note\n"));
2255   fprintf (file, _("\
2256   --package-metadata[=JSON]   Generate package metadata note\n"));
2257   fprintf (file, _("\
2258   --compress-debug-sections=[none|zlib|zlib-gnu|zlib-gabi|zstd]\n\
2259 			      Compress DWARF debug sections\n"));
2260   fprintf (file, _("\
2261                                 Default: %s\n"),
2262 	   bfd_get_compression_algorithm_name (config.compress_debug));
2263   fprintf (file, _("\
2264   -z common-page-size=SIZE    Set common page size to SIZE\n"));
2265   fprintf (file, _("\
2266   -z max-page-size=SIZE       Set maximum page size to SIZE\n"));
2267   fprintf (file, _("\
2268   -z defs                     Report unresolved symbols in object files\n"));
2269   fprintf (file, _("\
2270   -z undefs                   Ignore unresolved symbols in object files\n"));
2271   fprintf (file, _("\
2272   -z muldefs                  Allow multiple definitions\n"));
2273   fprintf (file, _("\
2274   -z stack-size=SIZE          Set size of stack segment\n"));
2275 
2276   fprintf (file, _("\
2277   -z execstack                Mark executable as requiring executable stack\n"));
2278   fprintf (file, _("\
2279   -z noexecstack              Mark executable as not requiring executable stack\n"));
2280   fprintf (file, _("\
2281   --warn-execstack-objects    Generate a warning if an object file requests an executable stack\n"));
2282 #if DEFAULT_LD_WARN_EXECSTACK == 0
2283   fprintf (file, _("\
2284   --warn-execstack            Generate a warning if creating an executable stack\n"));
2285 #else
2286   fprintf (file, _("\
2287   --warn-execstack            Generate a warning if creating an executable stack (default)\n"));
2288 #endif
2289 #if DEFAULT_LD_WARN_EXECSTACK == 0
2290   fprintf (file, _("\
2291   --no-warn-execstack         Do not generate a warning if creating an executable stack (default)\n"));
2292 #else
2293   fprintf (file, _("\
2294   --no-warn-execstack         Do not generate a warning if creating an executable stack\n"));
2295 #endif
2296   fprintf (file, _("\
2297   --error-execstack           Turn warnings about executable stacks into errors\n"));
2298   fprintf (file, _("\
2299   --no-error-execstack         Do not turn warnings about executable stacks into errors\n"));
2300 
2301 #if DEFAULT_LD_WARN_RWX_SEGMENTS
2302   fprintf (file, _("\
2303   --warn-rwx-segments         Generate a warning if a LOAD segment has RWX permissions (default)\n"));
2304   fprintf (file, _("\
2305   --no-warn-rwx-segments      Do not generate a warning if a LOAD segments has RWX permissions\n"));
2306 #else
2307   fprintf (file, _("\
2308   --warn-rwx-segments         Generate a warning if a LOAD segment has RWX permissions\n"));
2309   fprintf (file, _("\
2310   --no-warn-rwx-segments      Do not generate a warning if a LOAD segments has RWX permissions (default)\n"));
2311 #endif
2312   fprintf (file, _("\
2313   --error-rwx-segments        Turn warnings about loadable RWX segments into errors\n"));
2314   fprintf (file, _("\
2315   --no-error-rwx-segments     Do not turn warnings about loadable RWX segments into errors\n"));
2316 
2317   fprintf (file, _("\
2318   -z unique-symbol            Avoid duplicated local symbol names\n"));
2319   fprintf (file, _("\
2320   -z nounique-symbol          Keep duplicated local symbol names (default)\n"));
2321   fprintf (file, _("\
2322   -z globalaudit              Mark executable requiring global auditing\n"));
2323   fprintf (file, _("\
2324   -z start-stop-gc            Enable garbage collection on __start/__stop\n"));
2325   fprintf (file, _("\
2326   -z nostart-stop-gc          Don't garbage collect __start/__stop (default)\n"));
2327   fprintf (file, _("\
2328   -z start-stop-visibility=V  Set visibility of built-in __start/__stop symbols\n\
2329                                 to DEFAULT, PROTECTED, HIDDEN or INTERNAL\n"));
2330   fprintf (file, _("\
2331   -z sectionheader            Generate section header (default)\n"));
2332   fprintf (file, _("\
2333   -z nosectionheader          Do not generate section header\n"));
2334 }
2335 
2336 static void
elf_plt_unwind_list_options(FILE * file)2337 elf_plt_unwind_list_options (FILE *file)
2338 {
2339   fprintf (file, _("\
2340   --ld-generated-unwind-info  Generate exception handling info for PLT\n"));
2341   fprintf (file, _("\
2342   --no-ld-generated-unwind-info\n\
2343                               Don't generate exception handling info for PLT\n"));
2344 }
2345 
2346 static void
ld_list_options(FILE * file,bool elf,bool shlib,bool plt_unwind)2347 ld_list_options (FILE *file, bool elf, bool shlib, bool plt_unwind)
2348 {
2349   if (!elf)
2350     return;
2351   printf (_("ELF emulations:\n"));
2352   if (plt_unwind)
2353     elf_plt_unwind_list_options (file);
2354   elf_static_list_options (file);
2355   if (shlib)
2356     elf_shlib_list_options (file);
2357 }
2358 
2359 
2360 /* Print help messages for the options.  */
2361 
2362 static void
help(void)2363 help (void)
2364 {
2365   unsigned i;
2366   const char **targets, **pp;
2367   int len;
2368 
2369   printf (_("Usage: %s [options] file...\n"), program_name);
2370 
2371   printf (_("Options:\n"));
2372   for (i = 0; i < OPTION_COUNT; i++)
2373     {
2374       if (ld_options[i].doc != NULL)
2375 	{
2376 	  bool comma;
2377 	  unsigned j;
2378 
2379 	  printf ("  ");
2380 
2381 	  comma = false;
2382 	  len = 2;
2383 
2384 	  j = i;
2385 	  do
2386 	    {
2387 	      if (ld_options[j].shortopt != '\0'
2388 		  && ld_options[j].control != NO_HELP)
2389 		{
2390 		  printf ("%s-%c", comma ? ", " : "", ld_options[j].shortopt);
2391 		  len += (comma ? 2 : 0) + 2;
2392 		  if (ld_options[j].arg != NULL)
2393 		    {
2394 		      if (ld_options[j].opt.has_arg != optional_argument)
2395 			{
2396 			  printf (" ");
2397 			  ++len;
2398 			}
2399 		      printf ("%s", _(ld_options[j].arg));
2400 		      len += strlen (_(ld_options[j].arg));
2401 		    }
2402 		  comma = true;
2403 		}
2404 	      ++j;
2405 	    }
2406 	  while (j < OPTION_COUNT && ld_options[j].doc == NULL);
2407 
2408 	  j = i;
2409 	  do
2410 	    {
2411 	      if (ld_options[j].opt.name != NULL
2412 		  && ld_options[j].control != NO_HELP)
2413 		{
2414 		  int two_dashes =
2415 		    (ld_options[j].control == TWO_DASHES
2416 		     || ld_options[j].control == EXACTLY_TWO_DASHES);
2417 
2418 		  printf ("%s-%s%s",
2419 			  comma ? ", " : "",
2420 			  two_dashes ? "-" : "",
2421 			  ld_options[j].opt.name);
2422 		  len += ((comma ? 2 : 0)
2423 			  + 1
2424 			  + (two_dashes ? 1 : 0)
2425 			  + strlen (ld_options[j].opt.name));
2426 		  if (ld_options[j].arg != NULL)
2427 		    {
2428 		      printf (" %s", _(ld_options[j].arg));
2429 		      len += 1 + strlen (_(ld_options[j].arg));
2430 		    }
2431 		  comma = true;
2432 		}
2433 	      ++j;
2434 	    }
2435 	  while (j < OPTION_COUNT && ld_options[j].doc == NULL);
2436 
2437 	  if (len >= 30)
2438 	    {
2439 	      printf ("\n");
2440 	      len = 0;
2441 	    }
2442 
2443 	  for (; len < 30; len++)
2444 	    putchar (' ');
2445 
2446 	  printf ("%s\n", _(ld_options[i].doc));
2447 	}
2448     }
2449   printf (_("  @FILE"));
2450   for (len = strlen ("  @FILE"); len < 30; len++)
2451     putchar (' ');
2452   printf (_("Read options from FILE\n"));
2453 
2454   /* Note: Various tools (such as libtool) depend upon the
2455      format of the listings below - do not change them.  */
2456   /* xgettext:c-format */
2457   printf (_("%s: supported targets:"), program_name);
2458   targets = bfd_target_list ();
2459   for (pp = targets; *pp != NULL; pp++)
2460     printf (" %s", *pp);
2461   free (targets);
2462   printf ("\n");
2463 
2464   /* xgettext:c-format */
2465   printf (_("%s: supported emulations: "), program_name);
2466   ldemul_list_emulations (stdout);
2467   printf ("\n");
2468 
2469   /* xgettext:c-format */
2470   printf (_("%s: emulation specific options:\n"), program_name);
2471   ld_list_options (stdout, ELF_LIST_OPTIONS, ELF_SHLIB_LIST_OPTIONS,
2472 		   ELF_PLT_UNWIND_LIST_OPTIONS);
2473   ldemul_list_emulation_options (stdout);
2474   printf ("\n");
2475 
2476   if (REPORT_BUGS_TO[0])
2477     printf (_("Report bugs to %s\n"), REPORT_BUGS_TO);
2478 }
2479