Initial revision
[binutils.git] / ld / lexsup.c
blob7ee5087f1ac314771bbe510bcf2570f2c7694093
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)
10 any later version.
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
20 02111-1307, USA. */
22 #include "bfd.h"
23 #include "sysdep.h"
24 #include "libiberty.h"
25 #include <stdio.h>
26 #include <string.h>
27 #include <ctype.h>
28 #include "getopt.h"
29 #include "bfdlink.h"
30 #include "ld.h"
31 #include "ldmain.h"
32 #include "ldmisc.h"
33 #include "ldexp.h"
34 #include "ldlang.h"
35 #include "ldgram.h"
36 #include "ldlex.h"
37 #include "ldfile.h"
38 #include "ldver.h"
39 #include "ldemul.h"
41 #ifndef PATH_SEPARATOR
42 #if defined (__MSDOS__) || (defined (_WIN32) && ! defined (__CYGWIN32__))
43 #define PATH_SEPARATOR ';'
44 #else
45 #define PATH_SEPARATOR ':'
46 #endif
47 #endif
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)
52 #endif
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)
124 /* The long options. This structure is used for both the option
125 parsing and the help text. */
127 struct ld_option
129 /* The long option information. */
130 struct option opt;
131 /* The short option with the same meaning ('\0' if none). */
132 char shortopt;
133 /* The name of the argument (NULL if none). */
134 const char *arg;
135 /* The documentation string. If this is NULL, this is a synonym for
136 the previous option. */
137 const char *doc;
138 enum
140 /* Use one dash before long option name. */
141 ONE_DASH,
142 /* Use two dashes before long option name. */
143 TWO_DASHES,
144 /* Don't mention this option in --help output. */
145 NO_HELP
146 } control;
149 static const struct ld_option ld_options[] =
151 { {NULL, required_argument, NULL, '\0'},
152 'a', N_("KEYWORD"), N_("Shared library control for HP/UX compatibility"),
153 ONE_DASH },
154 { {"architecture", required_argument, NULL, 'A'},
155 'A', N_("ARCH"), N_("Set architecture") , TWO_DASHES },
156 { {"format", required_argument, NULL, 'b'},
157 'b', N_("TARGET"), N_("Specify target for following input files"), TWO_DASHES },
158 { {"mri-script", required_argument, NULL, 'c'},
159 'c', N_("FILE"), N_("Read MRI format linker script"), TWO_DASHES },
160 { {"dc", no_argument, NULL, 'd'},
161 'd', NULL, N_("Force common symbols to be defined"), ONE_DASH },
162 { {"dp", no_argument, NULL, 'd'},
163 '\0', NULL, NULL, ONE_DASH },
164 { {"entry", required_argument, NULL, 'e'},
165 'e', N_("ADDRESS"), N_("Set start address"), TWO_DASHES },
166 { {"export-dynamic", no_argument, NULL, OPTION_EXPORT_DYNAMIC},
167 'E', NULL, N_("Export all dynamic symbols"), TWO_DASHES },
168 { {"EB", no_argument, NULL, OPTION_EB},
169 '\0', NULL, N_("Link big-endian objects"), ONE_DASH },
170 { {"EL", no_argument, NULL, OPTION_EL},
171 '\0', NULL, N_("Link little-endian objects"), ONE_DASH },
172 { {"auxiliary", required_argument, NULL, 'f'},
173 'f', N_("SHLIB"), N_("Auxiliary filter for shared object symbol table"),
174 TWO_DASHES },
175 { {"filter", required_argument, NULL, 'F'},
176 'F', N_("SHLIB"), N_("Filter for shared object symbol table"), TWO_DASHES },
177 { {NULL, no_argument, NULL, '\0'},
178 'g', NULL, N_("Ignored"), ONE_DASH },
179 { {"gpsize", required_argument, NULL, 'G'},
180 'G', N_("SIZE"), N_("Small data size (if no size, same as --shared)"),
181 TWO_DASHES },
182 { {"soname", required_argument, NULL, OPTION_SONAME},
183 'h', N_("FILENAME"), N_("Set internal name of shared library"), ONE_DASH },
184 { {"library", required_argument, NULL, 'l'},
185 'l', N_("LIBNAME"), N_("Search for library LIBNAME"), TWO_DASHES },
186 { {"library-path", required_argument, NULL, 'L'},
187 'L', N_("DIRECTORY"), N_("Add DIRECTORY to library search path"), TWO_DASHES },
188 { {NULL, required_argument, NULL, '\0'},
189 'm', N_("EMULATION"), N_("Set emulation"), ONE_DASH },
190 { {"print-map", no_argument, NULL, 'M'},
191 'M', NULL, N_("Print map file on standard output"), TWO_DASHES },
192 { {"nmagic", no_argument, NULL, 'n'},
193 'n', NULL, N_("Do not page align data"), TWO_DASHES },
194 { {"omagic", no_argument, NULL, 'N'},
195 'N', NULL, N_("Do not page align data, do not make text readonly"),
196 TWO_DASHES },
197 { {"output", required_argument, NULL, 'o'},
198 'o', N_("FILE"), N_("Set output file name"), TWO_DASHES },
199 { {NULL, required_argument, NULL, '\0'},
200 'O', NULL, N_("Optimize output file"), ONE_DASH },
201 { {"Qy", no_argument, NULL, OPTION_IGNORE},
202 '\0', NULL, N_("Ignored for SVR4 compatibility"), ONE_DASH },
203 { {"relocateable", no_argument, NULL, 'r'},
204 'r', NULL, N_("Generate relocateable output"), TWO_DASHES },
205 { {NULL, no_argument, NULL, '\0'},
206 'i', NULL, NULL, ONE_DASH },
207 { {"just-symbols", required_argument, NULL, 'R'},
208 'R', N_("FILE"), N_("Just link symbols (if directory, same as --rpath)"),
209 TWO_DASHES },
210 { {"strip-all", no_argument, NULL, 's'},
211 's', NULL, N_("Strip all symbols"), TWO_DASHES },
212 { {"strip-debug", no_argument, NULL, 'S'},
213 'S', NULL, N_("Strip debugging symbols"), TWO_DASHES },
214 { {"trace", no_argument, NULL, 't'},
215 't', NULL, N_("Trace file opens"), TWO_DASHES },
216 { {"script", required_argument, NULL, 'T'},
217 'T', N_("FILE"), N_("Read linker script"), TWO_DASHES },
218 { {"undefined", required_argument, NULL, 'u'},
219 'u', N_("SYMBOL"), N_("Start with undefined reference to SYMBOL"), TWO_DASHES },
220 { {"Ur", no_argument, NULL, OPTION_UR},
221 '\0', NULL, N_("Build global constructor/destructor tables"), ONE_DASH },
222 { {"version", no_argument, NULL, OPTION_VERSION},
223 'v', NULL, N_("Print version information"), TWO_DASHES },
224 { {NULL, no_argument, NULL, '\0'},
225 'V', NULL, N_("Print version and emulation information"), ONE_DASH },
226 { {"discard-all", no_argument, NULL, 'x'},
227 'x', NULL, N_("Discard all local symbols"), TWO_DASHES },
228 { {"discard-locals", no_argument, NULL, 'X'},
229 'X', NULL, N_("Discard temporary local symbols"), TWO_DASHES },
230 { {"trace-symbol", required_argument, NULL, 'y'},
231 'y', N_("SYMBOL"), N_("Trace mentions of SYMBOL"), TWO_DASHES },
232 { {NULL, required_argument, NULL, '\0'},
233 'Y', N_("PATH"), N_("Default search path for Solaris compatibility"), ONE_DASH },
234 { {NULL, required_argument, NULL, '\0'},
235 'z', N_("KEYWORD"), N_("Ignored for Solaris compatibility"), ONE_DASH },
236 { {"start-group", no_argument, NULL, '('},
237 '(', NULL, N_("Start a group"), TWO_DASHES },
238 { {"end-group", no_argument, NULL, ')'},
239 ')', NULL, N_("End a group"), TWO_DASHES },
240 { {"assert", required_argument, NULL, OPTION_ASSERT},
241 '\0', N_("KEYWORD"), N_("Ignored for SunOS compatibility"), ONE_DASH },
242 { {"Bdynamic", no_argument, NULL, OPTION_CALL_SHARED},
243 '\0', NULL, N_("Link against shared libraries"), ONE_DASH },
244 { {"dy", no_argument, NULL, OPTION_CALL_SHARED},
245 '\0', NULL, NULL, ONE_DASH },
246 { {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
247 '\0', NULL, NULL, ONE_DASH },
248 { {"Bstatic", no_argument, NULL, OPTION_NON_SHARED},
249 '\0', NULL, N_("Do not link against shared libraries"), ONE_DASH },
250 { {"dn", no_argument, NULL, OPTION_NON_SHARED},
251 '\0', NULL, NULL, ONE_DASH },
252 { {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
253 '\0', NULL, NULL, ONE_DASH },
254 { {"static", no_argument, NULL, OPTION_NON_SHARED},
255 '\0', NULL, NULL, ONE_DASH },
256 { {"Bsymbolic", no_argument, NULL, OPTION_SYMBOLIC},
257 '\0', NULL, N_("Bind global references locally"), ONE_DASH },
258 { {"check-sections", no_argument, NULL, OPTION_CHECK_SECTIONS},
259 '\0', NULL, N_("Check section addresses for overlaps (default)"), TWO_DASHES },
260 { {"no-check-sections", no_argument, NULL, OPTION_NO_CHECK_SECTIONS},
261 '\0', NULL, N_("Do not check section addresses for overlaps"),
262 TWO_DASHES },
263 { {"cref", no_argument, NULL, OPTION_CREF},
264 '\0', NULL, N_("Output cross reference table"), TWO_DASHES },
265 { {"defsym", required_argument, NULL, OPTION_DEFSYM},
266 '\0', N_("SYMBOL=EXPRESSION"), N_("Define a symbol"), TWO_DASHES },
267 { {"demangle", no_argument, NULL, OPTION_DEMANGLE},
268 '\0', NULL, N_("Demangle symbol names"), TWO_DASHES },
269 { {"dynamic-linker", required_argument, NULL, OPTION_DYNAMIC_LINKER},
270 '\0', N_("PROGRAM"), N_("Set the dynamic linker to use"), TWO_DASHES },
271 { {"embedded-relocs", no_argument, NULL, OPTION_EMBEDDED_RELOCS},
272 '\0', NULL, N_("Generate embedded relocs"), TWO_DASHES},
273 { {"force-exe-suffix", no_argument, NULL, OPTION_FORCE_EXE_SUFFIX},
274 '\0', NULL, N_("Force generation of file with .exe suffix"), TWO_DASHES},
275 { {"gc-sections", no_argument, NULL, OPTION_GC_SECTIONS},
276 '\0', NULL, N_("Remove unused sections (on some targets)"),
277 TWO_DASHES },
278 { {"no-gc-sections", no_argument, NULL, OPTION_NO_GC_SECTIONS},
279 '\0', NULL, N_("Don't remove unused sections (default)"),
280 TWO_DASHES },
281 { {"help", no_argument, NULL, OPTION_HELP},
282 '\0', NULL, N_("Print option help"), TWO_DASHES },
283 { {"Map", required_argument, NULL, OPTION_MAP},
284 '\0', N_("FILE"), N_("Write a map file"), ONE_DASH },
285 { {"no-demangle", no_argument, NULL, OPTION_NO_DEMANGLE },
286 '\0', NULL, N_("Do not demangle symbol names"), TWO_DASHES },
287 { {"no-keep-memory", no_argument, NULL, OPTION_NO_KEEP_MEMORY},
288 '\0', NULL, N_("Use less memory and more disk I/O"), TWO_DASHES },
289 { {"no-undefined", no_argument, NULL, OPTION_NO_UNDEFINED},
290 '\0', NULL, N_("Allow no undefined symbols"), TWO_DASHES },
291 { {"no-warn-mismatch", no_argument, NULL, OPTION_NO_WARN_MISMATCH},
292 '\0', NULL, N_("Don't warn about mismatched input files"), TWO_DASHES},
293 { {"no-whole-archive", no_argument, NULL, OPTION_NO_WHOLE_ARCHIVE},
294 '\0', NULL, N_("Turn off --whole-archive"), TWO_DASHES },
295 { {"noinhibit-exec", no_argument, NULL, OPTION_NOINHIBIT_EXEC},
296 '\0', NULL, N_("Create an output file even if errors occur"), TWO_DASHES },
297 { {"noinhibit_exec", no_argument, NULL, OPTION_NOINHIBIT_EXEC},
298 '\0', NULL, NULL, NO_HELP },
299 { {"oformat", required_argument, NULL, OPTION_OFORMAT},
300 '\0', N_("TARGET"), N_("Specify target of output file"), TWO_DASHES },
301 { {"qmagic", no_argument, NULL, OPTION_IGNORE},
302 '\0', NULL, N_("Ignored for Linux compatibility"), ONE_DASH },
303 { {"relax", no_argument, NULL, OPTION_RELAX},
304 '\0', NULL, N_("Relax branches on certain targets"), TWO_DASHES },
305 { {"retain-symbols-file", required_argument, NULL,
306 OPTION_RETAIN_SYMBOLS_FILE},
307 '\0', N_("FILE"), N_("Keep only symbols listed in FILE"), TWO_DASHES },
308 { {"rpath", required_argument, NULL, OPTION_RPATH},
309 '\0', N_("PATH"), N_("Set runtime shared library search path"), ONE_DASH },
310 { {"rpath-link", required_argument, NULL, OPTION_RPATH_LINK},
311 '\0', N_("PATH"), N_("Set link time shared library search path"), ONE_DASH },
312 { {"shared", no_argument, NULL, OPTION_SHARED},
313 '\0', NULL, N_("Create a shared library"), ONE_DASH },
314 { {"Bshareable", no_argument, NULL, OPTION_SHARED }, /* FreeBSD. */
315 '\0', NULL, NULL, ONE_DASH },
316 { {"sort-common", no_argument, NULL, OPTION_SORT_COMMON},
317 '\0', NULL, N_("Sort common symbols by size"), TWO_DASHES },
318 { {"sort_common", no_argument, NULL, OPTION_SORT_COMMON},
319 '\0', NULL, NULL, NO_HELP },
320 { {"split-by-file", no_argument, NULL, OPTION_SPLIT_BY_FILE},
321 '\0', NULL, N_("Split output sections for each file"), TWO_DASHES },
322 { {"split-by-reloc", required_argument, NULL, OPTION_SPLIT_BY_RELOC},
323 '\0', N_("COUNT"), N_("Split output sections every COUNT relocs"), TWO_DASHES },
324 { {"stats", no_argument, NULL, OPTION_STATS},
325 '\0', NULL, N_("Print memory usage statistics"), TWO_DASHES },
326 { {"task-link", required_argument, NULL, OPTION_TASK_LINK},
327 '\0', N_("SYMBOL"), N_("Do task level linking"), TWO_DASHES },
328 { {"traditional-format", no_argument, NULL, OPTION_TRADITIONAL_FORMAT},
329 '\0', NULL, N_("Use same format as native linker"), TWO_DASHES },
330 { {"Tbss", required_argument, NULL, OPTION_TBSS},
331 '\0', N_("ADDRESS"), N_("Set address of .bss section"), ONE_DASH },
332 { {"Tdata", required_argument, NULL, OPTION_TDATA},
333 '\0', N_("ADDRESS"), N_("Set address of .data section"), ONE_DASH },
334 { {"Ttext", required_argument, NULL, OPTION_TTEXT},
335 '\0', N_("ADDRESS"), N_("Set address of .text section"), ONE_DASH },
336 { {"verbose", no_argument, NULL, OPTION_VERBOSE},
337 '\0', NULL, N_("Output lots of information during link"), TWO_DASHES },
338 { {"dll-verbose", no_argument, NULL, OPTION_VERBOSE}, /* Linux. */
339 '\0', NULL, NULL, NO_HELP },
340 { {"version-script", required_argument, NULL, OPTION_VERSION_SCRIPT },
341 '\0', N_("FILE"), N_("Read version information script"), TWO_DASHES },
342 { {"version-exports-section", required_argument, NULL,
343 OPTION_VERSION_EXPORTS_SECTION },
344 '\0', N_("SYMBOL"), N_("Take export symbols list from .exports, using SYMBOL as the version."),
345 TWO_DASHES },
346 { {"warn-common", no_argument, NULL, OPTION_WARN_COMMON},
347 '\0', NULL, N_("Warn about duplicate common symbols"), TWO_DASHES },
348 { {"warn-constructors", no_argument, NULL, OPTION_WARN_CONSTRUCTORS},
349 '\0', NULL, N_("Warn if global constructors/destructors are seen"),
350 TWO_DASHES },
351 { {"warn-multiple-gp", no_argument, NULL, OPTION_WARN_MULTIPLE_GP},
352 '\0', NULL, N_("Warn if the multiple GP values are used"), TWO_DASHES },
353 { {"warn-once", no_argument, NULL, OPTION_WARN_ONCE},
354 '\0', NULL, N_("Warn only once per undefined symbol"), TWO_DASHES },
355 { {"warn-section-align", no_argument, NULL, OPTION_WARN_SECTION_ALIGN},
356 '\0', NULL, N_("Warn if start of section changes due to alignment"),
357 TWO_DASHES },
358 { {"whole-archive", no_argument, NULL, OPTION_WHOLE_ARCHIVE},
359 '\0', NULL, N_("Include all objects from following archives"), TWO_DASHES },
360 { {"wrap", required_argument, NULL, OPTION_WRAP},
361 '\0', N_("SYMBOL"), N_("Use wrapper functions for SYMBOL"), TWO_DASHES },
362 { {"mpc860c0", optional_argument, NULL, OPTION_MPC860C0},
363 '\0', N_("[=WORDS]"), N_("Modify problematic branches in last WORDS (1-10, default 5) words of a page"), TWO_DASHES }
366 #define OPTION_COUNT ((int) (sizeof ld_options / sizeof ld_options[0]))
368 /* Test "string" for containing a string of digits that form a number
369 between "min" and "max". The return value is the number or "err". */
370 static
371 int is_num( char *string, int min, int max, int err)
373 int result = 0;
375 for ( ; *string; ++string)
377 if (!isdigit(*string))
379 result = err;
380 break;
382 result = result * 10 + (*string - '0');
384 if (result < min || result > max)
385 result = err;
387 return result;
390 void
391 parse_args (argc, argv)
392 int argc;
393 char **argv;
395 int i, is, il;
396 int ingroup = 0;
397 char *default_dirlist = NULL;
398 char shortopts[OPTION_COUNT * 3 + 2];
399 struct option longopts[OPTION_COUNT + 1];
400 int last_optind;
402 /* Starting the short option string with '-' is for programs that
403 expect options and other ARGV-elements in any order and that care about
404 the ordering of the two. We describe each non-option ARGV-element
405 as if it were the argument of an option with character code 1. */
406 shortopts[0] = '-';
407 is = 1;
408 il = 0;
409 for (i = 0; i < OPTION_COUNT; i++)
411 if (ld_options[i].shortopt != '\0')
413 shortopts[is] = ld_options[i].shortopt;
414 ++is;
415 if (ld_options[i].opt.has_arg == required_argument
416 || ld_options[i].opt.has_arg == optional_argument)
418 shortopts[is] = ':';
419 ++is;
420 if (ld_options[i].opt.has_arg == optional_argument)
422 shortopts[is] = ':';
423 ++is;
427 if (ld_options[i].opt.name != NULL)
429 longopts[il] = ld_options[i].opt;
430 ++il;
433 shortopts[is] = '\0';
434 longopts[il].name = NULL;
436 /* The -G option is ambiguous on different platforms. Sometimes it
437 specifies the largest data size to put into the small data
438 section. Sometimes it is equivalent to --shared. Unfortunately,
439 the first form takes an argument, while the second does not.
441 We need to permit the --shared form because on some platforms,
442 such as Solaris, gcc -shared will pass -G to the linker.
444 To permit either usage, we look through the argument list. If we
445 find -G not followed by a number, we change it into --shared.
446 This will work for most normal cases. */
447 for (i = 1; i < argc; i++)
448 if (strcmp (argv[i], "-G") == 0
449 && (i + 1 >= argc
450 || ! isdigit ((unsigned char) argv[i + 1][0])))
451 argv[i] = (char *) "--shared";
453 /* Because we permit long options to start with a single dash, and
454 we have a --library option, and the -l option is conventionally
455 used with an immediately following argument, we can have bad
456 results if somebody tries to use -l with a library whose name
457 happens to start with "ibrary", as in -li. We avoid problems by
458 simply turning -l into --library. This means that users will
459 have to use two dashes in order to use --library, which is OK
460 since that's how it is documented.
462 FIXME: It's possible that this problem can arise for other short
463 options as well, although the user does always have the recourse
464 of adding a space between the option and the argument. */
465 for (i = 1; i < argc; i++)
467 if (argv[i][0] == '-'
468 && argv[i][1] == 'l'
469 && argv[i][2] != '\0')
471 char *n;
473 n = (char *) xmalloc (strlen (argv[i]) + 20);
474 sprintf (n, "--library=%s", argv[i] + 2);
475 argv[i] = n;
479 last_optind = -1;
480 while (1)
482 int longind;
483 int optc;
485 /* Using last_optind lets us avoid calling ldemul_parse_args
486 multiple times on a single option, which would lead to
487 confusion in the internal static variables maintained by
488 getopt. This could otherwise happen for an argument like
489 -nx, in which the -n is parsed as a single option, and we
490 loop around to pick up the -x. */
491 if (optind != last_optind)
493 if (ldemul_parse_args (argc, argv))
494 continue;
495 last_optind = optind;
498 /* getopt_long_only is like getopt_long, but '-' as well as '--'
499 can indicate a long option. */
500 optc = getopt_long_only (argc, argv, shortopts, longopts, &longind);
502 if (optc == -1)
503 break;
504 switch (optc)
506 default:
507 xexit (1);
508 case 1: /* File name. */
509 lang_add_input_file (optarg, lang_input_file_is_file_enum,
510 (char *) NULL);
511 break;
513 case OPTION_IGNORE:
514 break;
515 case 'a':
516 /* For HP/UX compatibility. Actually -a shared should mean
517 ``use only shared libraries'' but, then, we don't
518 currently support shared libraries on HP/UX anyhow. */
519 if (strcmp (optarg, "archive") == 0)
520 config.dynamic_link = false;
521 else if (strcmp (optarg, "shared") == 0
522 || strcmp (optarg, "default") == 0)
523 config.dynamic_link = true;
524 else
525 einfo (_("%P%F: unrecognized -a option `%s'\n"), optarg);
526 break;
527 case OPTION_ASSERT:
528 /* FIXME: We just ignore these, but we should handle them. */
529 if (strcmp (optarg, "definitions") == 0)
531 else if (strcmp (optarg, "nodefinitions") == 0)
533 else if (strcmp (optarg, "nosymbolic") == 0)
535 else if (strcmp (optarg, "pure-text") == 0)
537 else
538 einfo (_("%P%F: unrecognized -assert option `%s'\n"), optarg);
539 break;
540 case 'A':
541 ldfile_add_arch (optarg);
542 break;
543 case 'b':
544 lang_add_target (optarg);
545 break;
546 case 'c':
547 ldfile_open_command_file (optarg);
548 parser_input = input_mri_script;
549 yyparse ();
550 break;
551 case OPTION_CALL_SHARED:
552 config.dynamic_link = true;
553 break;
554 case OPTION_NON_SHARED:
555 config.dynamic_link = false;
556 break;
557 case OPTION_CREF:
558 command_line.cref = true;
559 link_info.notice_all = true;
560 break;
561 case 'd':
562 command_line.force_common_definition = true;
563 break;
564 case OPTION_DEFSYM:
565 lex_string = optarg;
566 lex_redirect (optarg);
567 parser_input = input_defsym;
568 parsing_defsym = 1;
569 yyparse ();
570 parsing_defsym = 0;
571 lex_string = NULL;
572 break;
573 case OPTION_DEMANGLE:
574 demangling = true;
575 break;
576 case OPTION_DYNAMIC_LINKER:
577 command_line.interpreter = optarg;
578 break;
579 case OPTION_EB:
580 command_line.endian = ENDIAN_BIG;
581 break;
582 case OPTION_EL:
583 command_line.endian = ENDIAN_LITTLE;
584 break;
585 case OPTION_EMBEDDED_RELOCS:
586 command_line.embedded_relocs = true;
587 break;
588 case OPTION_EXPORT_DYNAMIC:
589 case 'E': /* HP/UX compatibility. */
590 command_line.export_dynamic = true;
591 break;
592 case 'e':
593 lang_add_entry (optarg, true);
594 break;
595 case 'f':
596 if (command_line.auxiliary_filters == NULL)
598 command_line.auxiliary_filters =
599 (char **) xmalloc (2 * sizeof (char *));
600 command_line.auxiliary_filters[0] = optarg;
601 command_line.auxiliary_filters[1] = NULL;
603 else
605 int c;
606 char **p;
608 c = 0;
609 for (p = command_line.auxiliary_filters; *p != NULL; p++)
610 ++c;
611 command_line.auxiliary_filters =
612 (char **) xrealloc (command_line.auxiliary_filters,
613 (c + 2) * sizeof (char *));
614 command_line.auxiliary_filters[c] = optarg;
615 command_line.auxiliary_filters[c + 1] = NULL;
617 break;
618 case 'F':
619 command_line.filter_shlib = optarg;
620 break;
621 case OPTION_FORCE_EXE_SUFFIX:
622 command_line.force_exe_suffix = true;
623 break;
624 case 'G':
626 char *end;
627 g_switch_value = strtoul (optarg, &end, 0);
628 if (*end)
629 einfo (_("%P%F: invalid number `%s'\n"), optarg);
631 break;
632 case 'g':
633 /* Ignore. */
634 break;
635 case OPTION_GC_SECTIONS:
636 command_line.gc_sections = true;
637 break;
638 case OPTION_HELP:
639 help ();
640 xexit (0);
641 break;
642 case 'L':
643 ldfile_add_library_path (optarg, true);
644 break;
645 case 'l':
646 lang_add_input_file (optarg, lang_input_file_is_l_enum,
647 (char *) NULL);
648 break;
649 case 'M':
650 config.map_filename = "-";
651 break;
652 case 'm':
653 /* Ignore. Was handled in a pre-parse. */
654 break;
655 case OPTION_MAP:
656 config.map_filename = optarg;
657 break;
658 case 'N':
659 config.text_read_only = false;
660 config.magic_demand_paged = false;
661 config.dynamic_link = false;
662 break;
663 case 'n':
664 config.magic_demand_paged = false;
665 config.dynamic_link = false;
666 break;
667 case OPTION_NO_DEMANGLE:
668 demangling = false;
669 break;
670 case OPTION_NO_GC_SECTIONS:
671 command_line.gc_sections = false;
672 break;
673 case OPTION_NO_KEEP_MEMORY:
674 link_info.keep_memory = false;
675 break;
676 case OPTION_NO_UNDEFINED:
677 link_info.no_undefined = true;
678 break;
679 case OPTION_NO_WARN_MISMATCH:
680 command_line.warn_mismatch = false;
681 break;
682 case OPTION_NOINHIBIT_EXEC:
683 force_make_executable = true;
684 break;
685 case OPTION_NO_WHOLE_ARCHIVE:
686 whole_archive = false;
687 break;
688 case 'O':
689 /* FIXME "-O<non-digits> <value>" used to set the address of
690 section <non-digits>. Was this for compatibility with
691 something, or can we create a new option to do that
692 (with a syntax similar to -defsym)?
693 getopt can't handle two args to an option without kludges. */
695 /* Enable optimizations of output files. */
696 link_info.optimize = strtoul (optarg, NULL, 0) ? true : false;
697 break;
698 case 'o':
699 lang_add_output (optarg, 0);
700 break;
701 case OPTION_OFORMAT:
702 lang_add_output_format (optarg, (char *) NULL, (char *) NULL, 0);
703 break;
704 case 'i':
705 case 'r':
706 link_info.relocateable = true;
707 config.build_constructors = false;
708 config.magic_demand_paged = false;
709 config.text_read_only = false;
710 config.dynamic_link = false;
711 break;
712 case 'R':
713 /* The GNU linker traditionally uses -R to mean to include
714 only the symbols from a file. The Solaris linker uses -R
715 to set the path used by the runtime linker to find
716 libraries. This is the GNU linker -rpath argument. We
717 try to support both simultaneously by checking the file
718 named. If it is a directory, rather than a regular file,
719 we assume -rpath was meant. */
721 struct stat s;
723 if (stat (optarg, &s) >= 0
724 && ! S_ISDIR (s.st_mode))
726 lang_add_input_file (optarg,
727 lang_input_file_is_symbols_only_enum,
728 (char *) NULL);
729 break;
732 /* Fall through. */
733 case OPTION_RPATH:
734 if (command_line.rpath == NULL)
735 command_line.rpath = buystring (optarg);
736 else
738 size_t rpath_len = strlen (command_line.rpath);
739 size_t optarg_len = strlen (optarg);
740 char *buf;
741 char *cp = command_line.rpath;
743 /* First see whether OPTARG is already in the path. */
746 size_t idx = 0;
747 while (optarg[idx] != '\0' && optarg[idx] == cp[idx])
748 ++idx;
749 if (optarg[idx] == '\0'
750 && (cp[idx] == '\0' || cp[idx] == ':'))
751 /* We found it. */
752 break;
754 /* Not yet found. */
755 cp = strchr (cp, ':');
756 if (cp != NULL)
757 ++cp;
759 while (cp != NULL);
761 if (cp == NULL)
763 buf = xmalloc (rpath_len + optarg_len + 2);
764 sprintf (buf, "%s:%s", command_line.rpath, optarg);
765 free (command_line.rpath);
766 command_line.rpath = buf;
769 break;
770 case OPTION_RPATH_LINK:
771 if (command_line.rpath_link == NULL)
772 command_line.rpath_link = buystring (optarg);
773 else
775 char *buf;
777 buf = xmalloc (strlen (command_line.rpath_link)
778 + strlen (optarg)
779 + 2);
780 sprintf (buf, "%s:%s", command_line.rpath_link, optarg);
781 free (command_line.rpath_link);
782 command_line.rpath_link = buf;
784 break;
785 case OPTION_RELAX:
786 command_line.relax = true;
787 break;
788 case OPTION_RETAIN_SYMBOLS_FILE:
789 add_keepsyms_file (optarg);
790 break;
791 case 'S':
792 link_info.strip = strip_debugger;
793 break;
794 case 's':
795 link_info.strip = strip_all;
796 break;
797 case OPTION_SHARED:
798 if (config.has_shared)
799 link_info.shared = true;
800 else
801 einfo (_("%P%F: -shared not supported\n"));
802 break;
803 case 'h': /* Used on Solaris. */
804 case OPTION_SONAME:
805 command_line.soname = optarg;
806 break;
807 case OPTION_SORT_COMMON:
808 config.sort_common = true;
809 break;
810 case OPTION_STATS:
811 config.stats = true;
812 break;
813 case OPTION_SYMBOLIC:
814 link_info.symbolic = true;
815 break;
816 case 't':
817 trace_files = true;
818 break;
819 case 'T':
820 ldfile_open_command_file (optarg);
821 parser_input = input_script;
822 yyparse ();
823 break;
824 case OPTION_TBSS:
825 set_section_start (".bss", optarg);
826 break;
827 case OPTION_TDATA:
828 set_section_start (".data", optarg);
829 break;
830 case OPTION_TTEXT:
831 set_section_start (".text", optarg);
832 break;
833 case OPTION_TRADITIONAL_FORMAT:
834 link_info.traditional_format = true;
835 break;
836 case OPTION_TASK_LINK:
837 link_info.task_link = true;
838 /* Fall through - do an implied -r option. */
839 case OPTION_UR:
840 link_info.relocateable = true;
841 config.build_constructors = true;
842 config.magic_demand_paged = false;
843 config.text_read_only = false;
844 config.dynamic_link = false;
845 break;
846 case 'u':
847 ldlang_add_undef (optarg);
848 break;
849 case OPTION_VERBOSE:
850 ldversion (1);
851 version_printed = true;
852 trace_file_tries = true;
853 break;
854 case 'v':
855 ldversion (0);
856 version_printed = true;
857 break;
858 case 'V':
859 ldversion (1);
860 version_printed = true;
861 break;
862 case OPTION_VERSION:
863 /* This output is intended to follow the GNU standards document. */
864 printf ("GNU ld %s\n", ld_program_version);
865 printf (_("Copyright 1997 Free Software Foundation, Inc.\n"));
866 printf (_("\
867 This program is free software; you may redistribute it under the terms of\n\
868 the GNU General Public License. This program has absolutely no warranty.\n"));
870 ld_emulation_xfer_type **ptr = ld_emulations;
872 printf (_(" Supported emulations:\n"));
873 while (*ptr)
875 printf (" %s\n", (*ptr)->emulation_name);
876 ptr++;
879 xexit (0);
880 break;
881 case OPTION_VERSION_SCRIPT:
882 /* This option indicates a small script that only specifies
883 version information. Read it, but don't assume that
884 we've seen a linker script. */
886 boolean hold_had_script;
888 hold_had_script = had_script;
889 ldfile_open_command_file (optarg);
890 had_script = hold_had_script;
891 parser_input = input_version_script;
892 yyparse ();
894 break;
895 case OPTION_VERSION_EXPORTS_SECTION:
896 /* This option records a version symbol to be applied to the
897 symbols listed for export to be found in the object files
898 .exports sections. */
899 command_line.version_exports_section = optarg;
900 break;
901 case OPTION_WARN_COMMON:
902 config.warn_common = true;
903 break;
904 case OPTION_WARN_CONSTRUCTORS:
905 config.warn_constructors = true;
906 break;
907 case OPTION_WARN_MULTIPLE_GP:
908 config.warn_multiple_gp = true;
909 break;
910 case OPTION_WARN_ONCE:
911 config.warn_once = true;
912 break;
913 case OPTION_WARN_SECTION_ALIGN:
914 config.warn_section_align = true;
915 break;
916 case OPTION_WHOLE_ARCHIVE:
917 whole_archive = true;
918 break;
919 case OPTION_WRAP:
920 add_wrap (optarg);
921 break;
922 case 'X':
923 link_info.discard = discard_l;
924 break;
925 case 'x':
926 link_info.discard = discard_all;
927 break;
928 case 'Y':
929 if (strncmp (optarg, "P,", 2) == 0)
930 optarg += 2;
931 default_dirlist = xstrdup (optarg);
932 break;
933 case 'y':
934 add_ysym (optarg);
935 break;
936 case 'z':
937 /* We accept and ignore this option for Solaris
938 compatibility. Actually, on Solaris, optarg is not
939 ignored. Someday we should handle it correctly. FIXME. */
940 break;
941 case OPTION_SPLIT_BY_RELOC:
942 config.split_by_reloc = atoi (optarg);
943 break;
944 case OPTION_SPLIT_BY_FILE:
945 config.split_by_file = true;
946 break;
947 case OPTION_CHECK_SECTIONS:
948 command_line.check_section_addresses = true;
949 break;
950 case OPTION_NO_CHECK_SECTIONS:
951 command_line.check_section_addresses = false;
952 break;
953 case '(':
954 if (ingroup)
956 fprintf (stderr,
957 _("%s: may not nest groups (--help for usage)\n"),
958 program_name);
959 xexit (1);
961 lang_enter_group ();
962 ingroup = 1;
963 break;
964 case ')':
965 if (! ingroup)
967 fprintf (stderr,
968 _("%s: group ended before it began (--help for usage)\n"),
969 program_name);
970 xexit (1);
972 lang_leave_group ();
973 ingroup = 0;
974 break;
975 case OPTION_MPC860C0:
976 link_info.mpc860c0 = 20; /* default value (in bytes) */
977 if (optarg)
979 unsigned words;
981 words = is_num (optarg, 1, 10, 0);
982 if (words == 0)
984 fprintf (stderr, _("Invalid argument to option \"mpc860c0\"\n"));
985 xexit (1);
987 link_info.mpc860c0 = words * 4; /* convert words to bytes */
989 command_line.relax = true;
990 break;
994 if (ingroup)
995 lang_leave_group ();
997 if (default_dirlist != NULL)
998 set_default_dirlist (default_dirlist);
1002 /* Add the (colon-separated) elements of DIRLIST_PTR to the
1003 library search path. */
1005 static void
1006 set_default_dirlist (dirlist_ptr)
1007 char *dirlist_ptr;
1009 char *p;
1011 while (1)
1013 p = strchr (dirlist_ptr, PATH_SEPARATOR);
1014 if (p != NULL)
1015 *p = '\0';
1016 if (*dirlist_ptr != '\0')
1017 ldfile_add_library_path (dirlist_ptr, true);
1018 if (p == NULL)
1019 break;
1020 dirlist_ptr = p + 1;
1024 static void
1025 set_section_start (sect, valstr)
1026 char *sect, *valstr;
1028 char *end;
1029 unsigned long val = strtoul (valstr, &end, 16);
1030 if (*end)
1031 einfo (_("%P%F: invalid hex number `%s'\n"), valstr);
1032 lang_section_start (sect, exp_intop (val));
1035 /* Print help messages for the options. */
1037 static void
1038 help ()
1040 int i;
1041 const char **targets, **pp;
1043 printf (_("Usage: %s [options] file...\n"), program_name);
1045 printf (_("Options:\n"));
1046 for (i = 0; i < OPTION_COUNT; i++)
1048 if (ld_options[i].doc != NULL)
1050 boolean comma;
1051 int len;
1052 int j;
1054 printf (" ");
1056 comma = false;
1057 len = 2;
1059 j = i;
1062 if (ld_options[j].shortopt != '\0'
1063 && ld_options[j].control != NO_HELP)
1065 printf ("%s-%c", comma ? ", " : "", ld_options[j].shortopt);
1066 len += (comma ? 2 : 0) + 2;
1067 if (ld_options[j].arg != NULL)
1069 if (ld_options[j].opt.has_arg != optional_argument)
1071 printf (" ");
1072 ++len;
1074 printf ("%s", _(ld_options[j].arg));
1075 len += strlen (_(ld_options[j].arg));
1077 comma = true;
1079 ++j;
1081 while (j < OPTION_COUNT && ld_options[j].doc == NULL);
1083 j = i;
1086 if (ld_options[j].opt.name != NULL
1087 && ld_options[j].control != NO_HELP)
1089 printf ("%s-%s%s",
1090 comma ? ", " : "",
1091 ld_options[j].control == TWO_DASHES ? "-" : "",
1092 ld_options[j].opt.name);
1093 len += ((comma ? 2 : 0)
1095 + (ld_options[j].control == TWO_DASHES ? 1 : 0)
1096 + strlen (ld_options[j].opt.name));
1097 if (ld_options[j].arg != NULL)
1099 printf (" %s", _(ld_options[j].arg));
1100 len += 1 + strlen (_(ld_options[j].arg));
1102 comma = true;
1104 ++j;
1106 while (j < OPTION_COUNT && ld_options[j].doc == NULL);
1108 if (len >= 30)
1110 printf ("\n");
1111 len = 0;
1114 for (; len < 30; len++)
1115 putchar (' ');
1117 printf ("%s\n", _(ld_options[i].doc));
1121 /* xgettext:c-format */
1122 printf (_("%s: supported targets:"), program_name);
1123 targets = bfd_target_list ();
1124 for (pp = targets; *pp != NULL; pp++)
1125 printf (" %s", *pp);
1126 free (targets);
1127 printf ("\n");
1129 /* xgettext:c-format */
1130 printf (_("%s: supported emulations: "), program_name);
1131 ldemul_list_emulations (stdout);
1132 printf ("\n");
1134 /* xgettext:c-format */
1135 printf (_("%s: emulation specific options:\n"), program_name);
1136 ldemul_list_emulation_options (stdout);
1137 printf ("\n");
1139 printf (_("\nReport bugs to bug-gnu-utils@gnu.org\n"));