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