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