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