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