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