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