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