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