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