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