1 /* Parse options for the GNU linker.
2 Copyright (C) 1991, 92, 93, 94, 95, 96, 97, 98, 1999
3 Free Software Foundation, Inc.
5 This file is part of GLD, the Gnu Linker.
7 GLD is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GLD is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GLD; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24 #include "libiberty.h"
41 #ifndef PATH_SEPARATOR
42 #if defined (__MSDOS__) || (defined (_WIN32) && ! defined (__CYGWIN32__))
43 #define PATH_SEPARATOR ';'
45 #define PATH_SEPARATOR ':'
49 /* Somewhere above, sys/stat.h got included . . . . */
50 #if !defined(S_ISDIR) && defined(S_IFDIR)
51 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
54 /* Omit args to avoid the possibility of clashing with a system header
55 that might disagree about consts. */
56 unsigned long strtoul ();
58 static void set_default_dirlist PARAMS ((char *dirlist_ptr));
59 static void set_section_start PARAMS ((char *sect, char *valstr));
60 static void help PARAMS ((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. */
68 #define OPTION_ASSERT 150
69 #define OPTION_CALL_SHARED (OPTION_ASSERT + 1)
70 #define OPTION_CREF (OPTION_CALL_SHARED + 1)
71 #define OPTION_DEFSYM (OPTION_CREF + 1)
72 #define OPTION_DEMANGLE (OPTION_DEFSYM + 1)
73 #define OPTION_DYNAMIC_LINKER (OPTION_DEMANGLE + 1)
74 #define OPTION_EB (OPTION_DYNAMIC_LINKER + 1)
75 #define OPTION_EL (OPTION_EB + 1)
76 #define OPTION_EMBEDDED_RELOCS (OPTION_EL + 1)
77 #define OPTION_EXPORT_DYNAMIC (OPTION_EMBEDDED_RELOCS + 1)
78 #define OPTION_HELP (OPTION_EXPORT_DYNAMIC + 1)
79 #define OPTION_IGNORE (OPTION_HELP + 1)
80 #define OPTION_MAP (OPTION_IGNORE + 1)
81 #define OPTION_NO_DEMANGLE (OPTION_MAP + 1)
82 #define OPTION_NO_KEEP_MEMORY (OPTION_NO_DEMANGLE + 1)
83 #define OPTION_NO_WARN_MISMATCH (OPTION_NO_KEEP_MEMORY + 1)
84 #define OPTION_NOINHIBIT_EXEC (OPTION_NO_WARN_MISMATCH + 1)
85 #define OPTION_NON_SHARED (OPTION_NOINHIBIT_EXEC + 1)
86 #define OPTION_NO_WHOLE_ARCHIVE (OPTION_NON_SHARED + 1)
87 #define OPTION_OFORMAT (OPTION_NO_WHOLE_ARCHIVE + 1)
88 #define OPTION_RELAX (OPTION_OFORMAT + 1)
89 #define OPTION_RETAIN_SYMBOLS_FILE (OPTION_RELAX + 1)
90 #define OPTION_RPATH (OPTION_RETAIN_SYMBOLS_FILE + 1)
91 #define OPTION_RPATH_LINK (OPTION_RPATH + 1)
92 #define OPTION_SHARED (OPTION_RPATH_LINK + 1)
93 #define OPTION_SONAME (OPTION_SHARED + 1)
94 #define OPTION_SORT_COMMON (OPTION_SONAME + 1)
95 #define OPTION_STATS (OPTION_SORT_COMMON + 1)
96 #define OPTION_SYMBOLIC (OPTION_STATS + 1)
97 #define OPTION_TASK_LINK (OPTION_SYMBOLIC + 1)
98 #define OPTION_TBSS (OPTION_TASK_LINK + 1)
99 #define OPTION_TDATA (OPTION_TBSS + 1)
100 #define OPTION_TTEXT (OPTION_TDATA + 1)
101 #define OPTION_TRADITIONAL_FORMAT (OPTION_TTEXT + 1)
102 #define OPTION_UR (OPTION_TRADITIONAL_FORMAT + 1)
103 #define OPTION_VERBOSE (OPTION_UR + 1)
104 #define OPTION_VERSION (OPTION_VERBOSE + 1)
105 #define OPTION_VERSION_SCRIPT (OPTION_VERSION + 1)
106 #define OPTION_VERSION_EXPORTS_SECTION (OPTION_VERSION_SCRIPT + 1)
107 #define OPTION_WARN_COMMON (OPTION_VERSION_EXPORTS_SECTION + 1)
108 #define OPTION_WARN_CONSTRUCTORS (OPTION_WARN_COMMON + 1)
109 #define OPTION_WARN_MULTIPLE_GP (OPTION_WARN_CONSTRUCTORS + 1)
110 #define OPTION_WARN_ONCE (OPTION_WARN_MULTIPLE_GP + 1)
111 #define OPTION_WARN_SECTION_ALIGN (OPTION_WARN_ONCE + 1)
112 #define OPTION_SPLIT_BY_RELOC (OPTION_WARN_SECTION_ALIGN + 1)
113 #define OPTION_SPLIT_BY_FILE (OPTION_SPLIT_BY_RELOC + 1)
114 #define OPTION_WHOLE_ARCHIVE (OPTION_SPLIT_BY_FILE + 1)
115 #define OPTION_WRAP (OPTION_WHOLE_ARCHIVE + 1)
116 #define OPTION_FORCE_EXE_SUFFIX (OPTION_WRAP + 1)
117 #define OPTION_GC_SECTIONS (OPTION_FORCE_EXE_SUFFIX + 1)
118 #define OPTION_NO_GC_SECTIONS (OPTION_GC_SECTIONS + 1)
119 #define OPTION_CHECK_SECTIONS (OPTION_NO_GC_SECTIONS + 1)
120 #define OPTION_NO_CHECK_SECTIONS (OPTION_CHECK_SECTIONS + 1)
121 #define OPTION_MPC860C0 (OPTION_NO_CHECK_SECTIONS + 1)
122 #define OPTION_NO_UNDEFINED (OPTION_MPC860C0 + 1)
123 #define OPTION_INIT (OPTION_NO_UNDEFINED + 1)
124 #define OPTION_FINI (OPTION_INIT + 1)
126 /* The long options. This structure is used for both the option
127 parsing and the help text. */
131 /* The long option information. */
133 /* The short option with the same meaning ('\0' if none). */
135 /* The name of the argument (NULL if none). */
137 /* The documentation string. If this is NULL, this is a synonym for
138 the previous option. */
142 /* Use one dash before long option name. */
144 /* Use two dashes before long option name. */
146 /* Don't mention this option in --help output. */
151 static const struct ld_option ld_options[] =
153 { {NULL, required_argument, NULL, '\0'},
154 'a', N_("KEYWORD"), N_("Shared library control for HP/UX compatibility"),
156 { {"architecture", required_argument, NULL, 'A'},
157 'A', N_("ARCH"), N_("Set architecture") , TWO_DASHES },
158 { {"format", required_argument, NULL, 'b'},
159 'b', N_("TARGET"), N_("Specify target for following input files"), TWO_DASHES },
160 { {"mri-script", required_argument, NULL, 'c'},
161 'c', N_("FILE"), N_("Read MRI format linker script"), TWO_DASHES },
162 { {"dc", no_argument, NULL, 'd'},
163 'd', NULL, N_("Force common symbols to be defined"), ONE_DASH },
164 { {"dp", no_argument, NULL, 'd'},
165 '\0', NULL, NULL, ONE_DASH },
166 { {"entry", required_argument, NULL, 'e'},
167 'e', N_("ADDRESS"), N_("Set start address"), TWO_DASHES },
168 { {"export-dynamic", no_argument, NULL, OPTION_EXPORT_DYNAMIC},
169 'E', NULL, N_("Export all dynamic symbols"), TWO_DASHES },
170 { {"EB", no_argument, NULL, OPTION_EB},
171 '\0', NULL, N_("Link big-endian objects"), ONE_DASH },
172 { {"EL", no_argument, NULL, OPTION_EL},
173 '\0', NULL, N_("Link little-endian objects"), ONE_DASH },
174 { {"auxiliary", required_argument, NULL, 'f'},
175 'f', N_("SHLIB"), N_("Auxiliary filter for shared object symbol table"),
177 { {"filter", required_argument, NULL, 'F'},
178 'F', N_("SHLIB"), N_("Filter for shared object symbol table"), TWO_DASHES },
179 { {NULL, no_argument, NULL, '\0'},
180 'g', NULL, N_("Ignored"), ONE_DASH },
181 { {"gpsize", required_argument, NULL, 'G'},
182 'G', N_("SIZE"), N_("Small data size (if no size, same as --shared)"),
184 { {"soname", required_argument, NULL, OPTION_SONAME},
185 'h', N_("FILENAME"), N_("Set internal name of shared library"), ONE_DASH },
186 { {"library", required_argument, NULL, 'l'},
187 'l', N_("LIBNAME"), N_("Search for library LIBNAME"), TWO_DASHES },
188 { {"library-path", required_argument, NULL, 'L'},
189 'L', N_("DIRECTORY"), N_("Add DIRECTORY to library search path"), TWO_DASHES },
190 { {NULL, required_argument, NULL, '\0'},
191 'm', N_("EMULATION"), N_("Set emulation"), ONE_DASH },
192 { {"print-map", no_argument, NULL, 'M'},
193 'M', NULL, N_("Print map file on standard output"), TWO_DASHES },
194 { {"nmagic", no_argument, NULL, 'n'},
195 'n', NULL, N_("Do not page align data"), TWO_DASHES },
196 { {"omagic", no_argument, NULL, 'N'},
197 'N', NULL, N_("Do not page align data, do not make text readonly"),
199 { {"output", required_argument, NULL, 'o'},
200 'o', N_("FILE"), N_("Set output file name"), TWO_DASHES },
201 { {NULL, required_argument, NULL, '\0'},
202 'O', NULL, N_("Optimize output file"), ONE_DASH },
203 { {"Qy", no_argument, NULL, OPTION_IGNORE},
204 '\0', NULL, N_("Ignored for SVR4 compatibility"), ONE_DASH },
205 { {"relocateable", no_argument, NULL, 'r'},
206 'r', NULL, N_("Generate relocateable output"), TWO_DASHES },
207 { {NULL, no_argument, NULL, '\0'},
208 'i', NULL, NULL, ONE_DASH },
209 { {"just-symbols", required_argument, NULL, 'R'},
210 'R', N_("FILE"), N_("Just link symbols (if directory, same as --rpath)"),
212 { {"strip-all", no_argument, NULL, 's'},
213 's', NULL, N_("Strip all symbols"), TWO_DASHES },
214 { {"strip-debug", no_argument, NULL, 'S'},
215 'S', NULL, N_("Strip debugging symbols"), TWO_DASHES },
216 { {"trace", no_argument, NULL, 't'},
217 't', NULL, N_("Trace file opens"), TWO_DASHES },
218 { {"script", required_argument, NULL, 'T'},
219 'T', N_("FILE"), N_("Read linker script"), TWO_DASHES },
220 { {"undefined", required_argument, NULL, 'u'},
221 'u', N_("SYMBOL"), N_("Start with undefined reference to SYMBOL"), TWO_DASHES },
222 { {"Ur", no_argument, NULL, OPTION_UR},
223 '\0', NULL, N_("Build global constructor/destructor tables"), ONE_DASH },
224 { {"version", no_argument, NULL, OPTION_VERSION},
225 'v', NULL, N_("Print version information"), TWO_DASHES },
226 { {NULL, no_argument, NULL, '\0'},
227 'V', NULL, N_("Print version and emulation information"), ONE_DASH },
228 { {"discard-all", no_argument, NULL, 'x'},
229 'x', NULL, N_("Discard all local symbols"), TWO_DASHES },
230 { {"discard-locals", no_argument, NULL, 'X'},
231 'X', NULL, N_("Discard temporary local symbols"), TWO_DASHES },
232 { {"trace-symbol", required_argument, NULL, 'y'},
233 'y', N_("SYMBOL"), N_("Trace mentions of SYMBOL"), TWO_DASHES },
234 { {NULL, required_argument, NULL, '\0'},
235 'Y', N_("PATH"), N_("Default search path for Solaris compatibility"), ONE_DASH },
236 { {NULL, required_argument, NULL, '\0'},
237 'z', N_("KEYWORD"), N_("Ignored for Solaris compatibility"), ONE_DASH },
238 { {"start-group", no_argument, NULL, '('},
239 '(', NULL, N_("Start a group"), TWO_DASHES },
240 { {"end-group", no_argument, NULL, ')'},
241 ')', NULL, N_("End a group"), TWO_DASHES },
242 { {"assert", required_argument, NULL, OPTION_ASSERT},
243 '\0', N_("KEYWORD"), N_("Ignored for SunOS compatibility"), ONE_DASH },
244 { {"Bdynamic", no_argument, NULL, OPTION_CALL_SHARED},
245 '\0', NULL, N_("Link against shared libraries"), ONE_DASH },
246 { {"dy", no_argument, NULL, OPTION_CALL_SHARED},
247 '\0', NULL, NULL, ONE_DASH },
248 { {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
249 '\0', NULL, NULL, ONE_DASH },
250 { {"Bstatic", no_argument, NULL, OPTION_NON_SHARED},
251 '\0', NULL, N_("Do not link against shared libraries"), ONE_DASH },
252 { {"dn", no_argument, NULL, OPTION_NON_SHARED},
253 '\0', NULL, NULL, ONE_DASH },
254 { {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
255 '\0', NULL, NULL, ONE_DASH },
256 { {"static", no_argument, NULL, OPTION_NON_SHARED},
257 '\0', NULL, NULL, ONE_DASH },
258 { {"Bsymbolic", no_argument, NULL, OPTION_SYMBOLIC},
259 '\0', NULL, N_("Bind global references locally"), ONE_DASH },
260 { {"check-sections", no_argument, NULL, OPTION_CHECK_SECTIONS},
261 '\0', NULL, N_("Check section addresses for overlaps (default)"), TWO_DASHES },
262 { {"no-check-sections", no_argument, NULL, OPTION_NO_CHECK_SECTIONS},
263 '\0', NULL, N_("Do not check section addresses for overlaps"),
265 { {"cref", no_argument, NULL, OPTION_CREF},
266 '\0', NULL, N_("Output cross reference table"), TWO_DASHES },
267 { {"defsym", required_argument, NULL, OPTION_DEFSYM},
268 '\0', N_("SYMBOL=EXPRESSION"), N_("Define a symbol"), TWO_DASHES },
269 { {"demangle", no_argument, NULL, OPTION_DEMANGLE},
270 '\0', NULL, N_("Demangle symbol names"), TWO_DASHES },
271 { {"dynamic-linker", required_argument, NULL, OPTION_DYNAMIC_LINKER},
272 '\0', N_("PROGRAM"), N_("Set the dynamic linker to use"), TWO_DASHES },
273 { {"embedded-relocs", no_argument, NULL, OPTION_EMBEDDED_RELOCS},
274 '\0', NULL, N_("Generate embedded relocs"), TWO_DASHES},
275 { {"fini", required_argument, NULL, OPTION_FINI},
276 '\0', N_("SYMBOL"), N_("Call SYMBOL at unload-time"), ONE_DASH },
277 { {"force-exe-suffix", no_argument, NULL, OPTION_FORCE_EXE_SUFFIX},
278 '\0', NULL, N_("Force generation of file with .exe suffix"), TWO_DASHES},
279 { {"gc-sections", no_argument, NULL, OPTION_GC_SECTIONS},
280 '\0', NULL, N_("Remove unused sections (on some targets)"),
282 { {"no-gc-sections", no_argument, NULL, OPTION_NO_GC_SECTIONS},
283 '\0', NULL, N_("Don't remove unused sections (default)"),
285 { {"help", no_argument, NULL, OPTION_HELP},
286 '\0', NULL, N_("Print option help"), TWO_DASHES },
287 { {"init", required_argument, NULL, OPTION_INIT},
288 '\0', N_("SYMBOL"), N_("Call SYMBOL at load-time"), ONE_DASH },
289 { {"Map", required_argument, NULL, OPTION_MAP},
290 '\0', N_("FILE"), N_("Write a map file"), ONE_DASH },
291 { {"no-demangle", no_argument, NULL, OPTION_NO_DEMANGLE },
292 '\0', NULL, N_("Do not demangle symbol names"), TWO_DASHES },
293 { {"no-keep-memory", no_argument, NULL, OPTION_NO_KEEP_MEMORY},
294 '\0', NULL, N_("Use less memory and more disk I/O"), TWO_DASHES },
295 { {"no-undefined", no_argument, NULL, OPTION_NO_UNDEFINED},
296 '\0', NULL, N_("Allow no undefined symbols"), TWO_DASHES },
297 { {"no-warn-mismatch", no_argument, NULL, OPTION_NO_WARN_MISMATCH},
298 '\0', NULL, N_("Don't warn about mismatched input files"), TWO_DASHES},
299 { {"no-whole-archive", no_argument, NULL, OPTION_NO_WHOLE_ARCHIVE},
300 '\0', NULL, N_("Turn off --whole-archive"), TWO_DASHES },
301 { {"noinhibit-exec", no_argument, NULL, OPTION_NOINHIBIT_EXEC},
302 '\0', NULL, N_("Create an output file even if errors occur"), TWO_DASHES },
303 { {"noinhibit_exec", no_argument, NULL, OPTION_NOINHIBIT_EXEC},
304 '\0', NULL, NULL, NO_HELP },
305 { {"oformat", required_argument, NULL, OPTION_OFORMAT},
306 '\0', N_("TARGET"), N_("Specify target of output file"), TWO_DASHES },
307 { {"qmagic", no_argument, NULL, OPTION_IGNORE},
308 '\0', NULL, N_("Ignored for Linux compatibility"), ONE_DASH },
309 { {"relax", no_argument, NULL, OPTION_RELAX},
310 '\0', NULL, N_("Relax branches on certain targets"), TWO_DASHES },
311 { {"retain-symbols-file", required_argument, NULL,
312 OPTION_RETAIN_SYMBOLS_FILE},
313 '\0', N_("FILE"), N_("Keep only symbols listed in FILE"), TWO_DASHES },
314 { {"rpath", required_argument, NULL, OPTION_RPATH},
315 '\0', N_("PATH"), N_("Set runtime shared library search path"), ONE_DASH },
316 { {"rpath-link", required_argument, NULL, OPTION_RPATH_LINK},
317 '\0', N_("PATH"), N_("Set link time shared library search path"), ONE_DASH },
318 { {"shared", no_argument, NULL, OPTION_SHARED},
319 '\0', NULL, N_("Create a shared library"), ONE_DASH },
320 { {"Bshareable", no_argument, NULL, OPTION_SHARED }, /* FreeBSD. */
321 '\0', NULL, NULL, ONE_DASH },
322 { {"sort-common", no_argument, NULL, OPTION_SORT_COMMON},
323 '\0', NULL, N_("Sort common symbols by size"), TWO_DASHES },
324 { {"sort_common", no_argument, NULL, OPTION_SORT_COMMON},
325 '\0', NULL, NULL, NO_HELP },
326 { {"split-by-file", no_argument, NULL, OPTION_SPLIT_BY_FILE},
327 '\0', NULL, N_("Split output sections for each file"), TWO_DASHES },
328 { {"split-by-reloc", required_argument, NULL, OPTION_SPLIT_BY_RELOC},
329 '\0', N_("COUNT"), N_("Split output sections every COUNT relocs"), TWO_DASHES },
330 { {"stats", no_argument, NULL, OPTION_STATS},
331 '\0', NULL, N_("Print memory usage statistics"), TWO_DASHES },
332 { {"task-link", required_argument, NULL, OPTION_TASK_LINK},
333 '\0', N_("SYMBOL"), N_("Do task level linking"), TWO_DASHES },
334 { {"traditional-format", no_argument, NULL, OPTION_TRADITIONAL_FORMAT},
335 '\0', NULL, N_("Use same format as native linker"), TWO_DASHES },
336 { {"Tbss", required_argument, NULL, OPTION_TBSS},
337 '\0', N_("ADDRESS"), N_("Set address of .bss section"), ONE_DASH },
338 { {"Tdata", required_argument, NULL, OPTION_TDATA},
339 '\0', N_("ADDRESS"), N_("Set address of .data section"), ONE_DASH },
340 { {"Ttext", required_argument, NULL, OPTION_TTEXT},
341 '\0', N_("ADDRESS"), N_("Set address of .text section"), ONE_DASH },
342 { {"verbose", no_argument, NULL, OPTION_VERBOSE},
343 '\0', NULL, N_("Output lots of information during link"), TWO_DASHES },
344 { {"dll-verbose", no_argument, NULL, OPTION_VERBOSE}, /* Linux. */
345 '\0', NULL, NULL, NO_HELP },
346 { {"version-script", required_argument, NULL, OPTION_VERSION_SCRIPT },
347 '\0', N_("FILE"), N_("Read version information script"), TWO_DASHES },
348 { {"version-exports-section", required_argument, NULL,
349 OPTION_VERSION_EXPORTS_SECTION },
350 '\0', N_("SYMBOL"), N_("Take export symbols list from .exports, using\n\t\t\t\tSYMBOL as the version."),
352 { {"warn-common", no_argument, NULL, OPTION_WARN_COMMON},
353 '\0', NULL, N_("Warn about duplicate common symbols"), TWO_DASHES },
354 { {"warn-constructors", no_argument, NULL, OPTION_WARN_CONSTRUCTORS},
355 '\0', NULL, N_("Warn if global constructors/destructors are seen"),
357 { {"warn-multiple-gp", no_argument, NULL, OPTION_WARN_MULTIPLE_GP},
358 '\0', NULL, N_("Warn if the multiple GP values are used"), TWO_DASHES },
359 { {"warn-once", no_argument, NULL, OPTION_WARN_ONCE},
360 '\0', NULL, N_("Warn only once per undefined symbol"), TWO_DASHES },
361 { {"warn-section-align", no_argument, NULL, OPTION_WARN_SECTION_ALIGN},
362 '\0', NULL, N_("Warn if start of section changes due to alignment"),
364 { {"whole-archive", no_argument, NULL, OPTION_WHOLE_ARCHIVE},
365 '\0', NULL, N_("Include all objects from following archives"), TWO_DASHES },
366 { {"wrap", required_argument, NULL, OPTION_WRAP},
367 '\0', N_("SYMBOL"), N_("Use wrapper functions for SYMBOL"), TWO_DASHES },
368 { {"mpc860c0", optional_argument, NULL, OPTION_MPC860C0},
369 '\0', N_("[=WORDS]"), N_("Modify problematic branches in last WORDS (1-10,\n\t\t\t\tdefault 5) words of a page"), TWO_DASHES }
372 #define OPTION_COUNT ((int) (sizeof ld_options / sizeof ld_options[0]))
374 /* Test "string" for containing a string of digits that form a number
375 between "min" and "max". The return value is the number or "err". */
377 int is_num( char *string, int min, int max, int err)
381 for ( ; *string; ++string)
383 if (!isdigit(*string))
388 result = result * 10 + (*string - '0');
390 if (result < min || result > max)
397 parse_args (argc, argv)
403 char *default_dirlist = NULL;
404 char shortopts[OPTION_COUNT * 3 + 2];
405 struct option longopts[OPTION_COUNT + 1];
408 /* Starting the short option string with '-' is for programs that
409 expect options and other ARGV-elements in any order and that care about
410 the ordering of the two. We describe each non-option ARGV-element
411 as if it were the argument of an option with character code 1. */
415 for (i = 0; i < OPTION_COUNT; i++)
417 if (ld_options[i].shortopt != '\0')
419 shortopts[is] = ld_options[i].shortopt;
421 if (ld_options[i].opt.has_arg == required_argument
422 || ld_options[i].opt.has_arg == optional_argument)
426 if (ld_options[i].opt.has_arg == optional_argument)
433 if (ld_options[i].opt.name != NULL)
435 longopts[il] = ld_options[i].opt;
439 shortopts[is] = '\0';
440 longopts[il].name = NULL;
442 /* The -G option is ambiguous on different platforms. Sometimes it
443 specifies the largest data size to put into the small data
444 section. Sometimes it is equivalent to --shared. Unfortunately,
445 the first form takes an argument, while the second does not.
447 We need to permit the --shared form because on some platforms,
448 such as Solaris, gcc -shared will pass -G to the linker.
450 To permit either usage, we look through the argument list. If we
451 find -G not followed by a number, we change it into --shared.
452 This will work for most normal cases. */
453 for (i = 1; i < argc; i++)
454 if (strcmp (argv[i], "-G") == 0
456 || ! isdigit ((unsigned char) argv[i + 1][0])))
457 argv[i] = (char *) "--shared";
459 /* Because we permit long options to start with a single dash, and
460 we have a --library option, and the -l option is conventionally
461 used with an immediately following argument, we can have bad
462 results if somebody tries to use -l with a library whose name
463 happens to start with "ibrary", as in -li. We avoid problems by
464 simply turning -l into --library. This means that users will
465 have to use two dashes in order to use --library, which is OK
466 since that's how it is documented.
468 FIXME: It's possible that this problem can arise for other short
469 options as well, although the user does always have the recourse
470 of adding a space between the option and the argument. */
471 for (i = 1; i < argc; i++)
473 if (argv[i][0] == '-'
475 && argv[i][2] != '\0')
479 n = (char *) xmalloc (strlen (argv[i]) + 20);
480 sprintf (n, "--library=%s", argv[i] + 2);
491 /* Using last_optind lets us avoid calling ldemul_parse_args
492 multiple times on a single option, which would lead to
493 confusion in the internal static variables maintained by
494 getopt. This could otherwise happen for an argument like
495 -nx, in which the -n is parsed as a single option, and we
496 loop around to pick up the -x. */
497 if (optind != last_optind)
499 if (ldemul_parse_args (argc, argv))
501 last_optind = optind;
504 /* getopt_long_only is like getopt_long, but '-' as well as '--'
505 can indicate a long option. */
506 optc = getopt_long_only (argc, argv, shortopts, longopts, &longind);
514 case 1: /* File name. */
515 lang_add_input_file (optarg, lang_input_file_is_file_enum,
522 /* For HP/UX compatibility. Actually -a shared should mean
523 ``use only shared libraries'' but, then, we don't
524 currently support shared libraries on HP/UX anyhow. */
525 if (strcmp (optarg, "archive") == 0)
526 config.dynamic_link = false;
527 else if (strcmp (optarg, "shared") == 0
528 || strcmp (optarg, "default") == 0)
529 config.dynamic_link = true;
531 einfo (_("%P%F: unrecognized -a option `%s'\n"), optarg);
534 /* FIXME: We just ignore these, but we should handle them. */
535 if (strcmp (optarg, "definitions") == 0)
537 else if (strcmp (optarg, "nodefinitions") == 0)
539 else if (strcmp (optarg, "nosymbolic") == 0)
541 else if (strcmp (optarg, "pure-text") == 0)
544 einfo (_("%P%F: unrecognized -assert option `%s'\n"), optarg);
547 ldfile_add_arch (optarg);
550 lang_add_target (optarg);
553 ldfile_open_command_file (optarg);
554 parser_input = input_mri_script;
557 case OPTION_CALL_SHARED:
558 config.dynamic_link = true;
560 case OPTION_NON_SHARED:
561 config.dynamic_link = false;
564 command_line.cref = true;
565 link_info.notice_all = true;
568 command_line.force_common_definition = true;
572 lex_redirect (optarg);
573 parser_input = input_defsym;
579 case OPTION_DEMANGLE:
582 case OPTION_DYNAMIC_LINKER:
583 command_line.interpreter = optarg;
586 command_line.endian = ENDIAN_BIG;
589 command_line.endian = ENDIAN_LITTLE;
591 case OPTION_EMBEDDED_RELOCS:
592 command_line.embedded_relocs = true;
594 case OPTION_EXPORT_DYNAMIC:
595 case 'E': /* HP/UX compatibility. */
596 command_line.export_dynamic = true;
599 lang_add_entry (optarg, true);
602 if (command_line.auxiliary_filters == NULL)
604 command_line.auxiliary_filters =
605 (char **) xmalloc (2 * sizeof (char *));
606 command_line.auxiliary_filters[0] = optarg;
607 command_line.auxiliary_filters[1] = NULL;
615 for (p = command_line.auxiliary_filters; *p != NULL; p++)
617 command_line.auxiliary_filters =
618 (char **) xrealloc (command_line.auxiliary_filters,
619 (c + 2) * sizeof (char *));
620 command_line.auxiliary_filters[c] = optarg;
621 command_line.auxiliary_filters[c + 1] = NULL;
625 command_line.filter_shlib = optarg;
627 case OPTION_FORCE_EXE_SUFFIX:
628 command_line.force_exe_suffix = true;
633 g_switch_value = strtoul (optarg, &end, 0);
635 einfo (_("%P%F: invalid number `%s'\n"), optarg);
641 case OPTION_GC_SECTIONS:
642 command_line.gc_sections = true;
649 ldfile_add_library_path (optarg, true);
652 lang_add_input_file (optarg, lang_input_file_is_l_enum,
656 config.map_filename = "-";
659 /* Ignore. Was handled in a pre-parse. */
662 config.map_filename = optarg;
665 config.text_read_only = false;
666 config.magic_demand_paged = false;
667 config.dynamic_link = false;
670 config.magic_demand_paged = false;
671 config.dynamic_link = false;
673 case OPTION_NO_DEMANGLE:
676 case OPTION_NO_GC_SECTIONS:
677 command_line.gc_sections = false;
679 case OPTION_NO_KEEP_MEMORY:
680 link_info.keep_memory = false;
682 case OPTION_NO_UNDEFINED:
683 link_info.no_undefined = true;
685 case OPTION_NO_WARN_MISMATCH:
686 command_line.warn_mismatch = false;
688 case OPTION_NOINHIBIT_EXEC:
689 force_make_executable = true;
691 case OPTION_NO_WHOLE_ARCHIVE:
692 whole_archive = false;
695 /* FIXME "-O<non-digits> <value>" used to set the address of
696 section <non-digits>. Was this for compatibility with
697 something, or can we create a new option to do that
698 (with a syntax similar to -defsym)?
699 getopt can't handle two args to an option without kludges. */
701 /* Enable optimizations of output files. */
702 link_info.optimize = strtoul (optarg, NULL, 0) ? true : false;
705 lang_add_output (optarg, 0);
708 lang_add_output_format (optarg, (char *) NULL, (char *) NULL, 0);
712 link_info.relocateable = true;
713 config.build_constructors = false;
714 config.magic_demand_paged = false;
715 config.text_read_only = false;
716 config.dynamic_link = false;
719 /* The GNU linker traditionally uses -R to mean to include
720 only the symbols from a file. The Solaris linker uses -R
721 to set the path used by the runtime linker to find
722 libraries. This is the GNU linker -rpath argument. We
723 try to support both simultaneously by checking the file
724 named. If it is a directory, rather than a regular file,
725 we assume -rpath was meant. */
729 if (stat (optarg, &s) >= 0
730 && ! S_ISDIR (s.st_mode))
732 lang_add_input_file (optarg,
733 lang_input_file_is_symbols_only_enum,
740 if (command_line.rpath == NULL)
741 command_line.rpath = buystring (optarg);
744 size_t rpath_len = strlen (command_line.rpath);
745 size_t optarg_len = strlen (optarg);
747 char *cp = command_line.rpath;
749 /* First see whether OPTARG is already in the path. */
753 while (optarg[idx] != '\0' && optarg[idx] == cp[idx])
755 if (optarg[idx] == '\0'
756 && (cp[idx] == '\0' || cp[idx] == ':'))
761 cp = strchr (cp, ':');
769 buf = xmalloc (rpath_len + optarg_len + 2);
770 sprintf (buf, "%s:%s", command_line.rpath, optarg);
771 free (command_line.rpath);
772 command_line.rpath = buf;
776 case OPTION_RPATH_LINK:
777 if (command_line.rpath_link == NULL)
778 command_line.rpath_link = buystring (optarg);
783 buf = xmalloc (strlen (command_line.rpath_link)
786 sprintf (buf, "%s:%s", command_line.rpath_link, optarg);
787 free (command_line.rpath_link);
788 command_line.rpath_link = buf;
792 command_line.relax = true;
794 case OPTION_RETAIN_SYMBOLS_FILE:
795 add_keepsyms_file (optarg);
798 link_info.strip = strip_debugger;
801 link_info.strip = strip_all;
804 if (config.has_shared)
805 link_info.shared = true;
807 einfo (_("%P%F: -shared not supported\n"));
809 case 'h': /* Used on Solaris. */
811 command_line.soname = optarg;
813 case OPTION_SORT_COMMON:
814 config.sort_common = true;
819 case OPTION_SYMBOLIC:
820 link_info.symbolic = true;
826 ldfile_open_command_file (optarg);
827 parser_input = input_script;
831 set_section_start (".bss", optarg);
834 set_section_start (".data", optarg);
837 set_section_start (".text", optarg);
839 case OPTION_TRADITIONAL_FORMAT:
840 link_info.traditional_format = true;
842 case OPTION_TASK_LINK:
843 link_info.task_link = true;
844 /* Fall through - do an implied -r option. */
846 link_info.relocateable = true;
847 config.build_constructors = true;
848 config.magic_demand_paged = false;
849 config.text_read_only = false;
850 config.dynamic_link = false;
853 ldlang_add_undef (optarg);
857 version_printed = true;
858 trace_file_tries = true;
862 version_printed = true;
866 version_printed = true;
869 /* This output is intended to follow the GNU standards document. */
870 printf ("GNU ld %s\n", ld_program_version);
871 printf (_("Copyright 1997 Free Software Foundation, Inc.\n"));
873 This program is free software; you may redistribute it under the terms of\n\
874 the GNU General Public License. This program has absolutely no warranty.\n"));
876 ld_emulation_xfer_type **ptr = ld_emulations;
878 printf (_(" Supported emulations:\n"));
881 printf (" %s\n", (*ptr)->emulation_name);
887 case OPTION_VERSION_SCRIPT:
888 /* This option indicates a small script that only specifies
889 version information. Read it, but don't assume that
890 we've seen a linker script. */
892 boolean hold_had_script;
894 hold_had_script = had_script;
895 ldfile_open_command_file (optarg);
896 had_script = hold_had_script;
897 parser_input = input_version_script;
901 case OPTION_VERSION_EXPORTS_SECTION:
902 /* This option records a version symbol to be applied to the
903 symbols listed for export to be found in the object files
904 .exports sections. */
905 command_line.version_exports_section = optarg;
907 case OPTION_WARN_COMMON:
908 config.warn_common = true;
910 case OPTION_WARN_CONSTRUCTORS:
911 config.warn_constructors = true;
913 case OPTION_WARN_MULTIPLE_GP:
914 config.warn_multiple_gp = true;
916 case OPTION_WARN_ONCE:
917 config.warn_once = true;
919 case OPTION_WARN_SECTION_ALIGN:
920 config.warn_section_align = true;
922 case OPTION_WHOLE_ARCHIVE:
923 whole_archive = true;
929 link_info.discard = discard_l;
932 link_info.discard = discard_all;
935 if (strncmp (optarg, "P,", 2) == 0)
937 default_dirlist = xstrdup (optarg);
943 /* We accept and ignore this option for Solaris
944 compatibility. Actually, on Solaris, optarg is not
945 ignored. Someday we should handle it correctly. FIXME. */
947 case OPTION_SPLIT_BY_RELOC:
948 config.split_by_reloc = atoi (optarg);
950 case OPTION_SPLIT_BY_FILE:
951 config.split_by_file = true;
953 case OPTION_CHECK_SECTIONS:
954 command_line.check_section_addresses = true;
956 case OPTION_NO_CHECK_SECTIONS:
957 command_line.check_section_addresses = false;
963 _("%s: may not nest groups (--help for usage)\n"),
974 _("%s: group ended before it began (--help for usage)\n"),
981 case OPTION_MPC860C0:
982 link_info.mpc860c0 = 20; /* default value (in bytes) */
987 words = is_num (optarg, 1, 10, 0);
990 fprintf (stderr, _("Invalid argument to option \"mpc860c0\"\n"));
993 link_info.mpc860c0 = words * 4; /* convert words to bytes */
995 command_line.relax = true;
999 link_info.init_function = optarg;
1003 link_info.fini_function = optarg;
1009 lang_leave_group ();
1011 if (default_dirlist != NULL)
1012 set_default_dirlist (default_dirlist);
1016 /* Add the (colon-separated) elements of DIRLIST_PTR to the
1017 library search path. */
1020 set_default_dirlist (dirlist_ptr)
1027 p = strchr (dirlist_ptr, PATH_SEPARATOR);
1030 if (*dirlist_ptr != '\0')
1031 ldfile_add_library_path (dirlist_ptr, true);
1034 dirlist_ptr = p + 1;
1039 set_section_start (sect, valstr)
1040 char *sect, *valstr;
1043 unsigned long val = strtoul (valstr, &end, 16);
1045 einfo (_("%P%F: invalid hex number `%s'\n"), valstr);
1046 lang_section_start (sect, exp_intop (val));
1049 /* Print help messages for the options. */
1055 const char **targets, **pp;
1057 printf (_("Usage: %s [options] file...\n"), program_name);
1059 printf (_("Options:\n"));
1060 for (i = 0; i < OPTION_COUNT; i++)
1062 if (ld_options[i].doc != NULL)
1076 if (ld_options[j].shortopt != '\0'
1077 && ld_options[j].control != NO_HELP)
1079 printf ("%s-%c", comma ? ", " : "", ld_options[j].shortopt);
1080 len += (comma ? 2 : 0) + 2;
1081 if (ld_options[j].arg != NULL)
1083 if (ld_options[j].opt.has_arg != optional_argument)
1088 printf ("%s", _(ld_options[j].arg));
1089 len += strlen (_(ld_options[j].arg));
1095 while (j < OPTION_COUNT && ld_options[j].doc == NULL);
1100 if (ld_options[j].opt.name != NULL
1101 && ld_options[j].control != NO_HELP)
1105 ld_options[j].control == TWO_DASHES ? "-" : "",
1106 ld_options[j].opt.name);
1107 len += ((comma ? 2 : 0)
1109 + (ld_options[j].control == TWO_DASHES ? 1 : 0)
1110 + strlen (ld_options[j].opt.name));
1111 if (ld_options[j].arg != NULL)
1113 printf (" %s", _(ld_options[j].arg));
1114 len += 1 + strlen (_(ld_options[j].arg));
1120 while (j < OPTION_COUNT && ld_options[j].doc == NULL);
1128 for (; len < 30; len++)
1131 printf ("%s\n", _(ld_options[i].doc));
1135 /* Note: Various tools (such as libtool) depend upon the
1136 format of the listings below - do not change them. */
1137 /* xgettext:c-format */
1138 printf (_("%s: supported targets:"), program_name);
1139 targets = bfd_target_list ();
1140 for (pp = targets; *pp != NULL; pp++)
1141 printf (" %s", *pp);
1145 /* xgettext:c-format */
1146 printf (_("%s: supported emulations: "), program_name);
1147 ldemul_list_emulations (stdout);
1150 /* xgettext:c-format */
1151 printf (_("%s: emulation specific options:\n"), program_name);
1152 ldemul_list_emulation_options (stdout);
1155 printf (_("\nReport bugs to bug-gnu-utils@gnu.org\n"));