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