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