2003-10-08 Roland McGrath <roland@redhat.com>
[deliverable/binutils-gdb.git] / ld / lexsup.c
CommitLineData
252b5132 1/* Parse options for the GNU linker.
a2b64bed 2 Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
ae9a127f 3 2001, 2002, 2003
252b5132
RH
4 Free Software Foundation, Inc.
5
53b2a62f 6 This file is part of GLD, the Gnu Linker.
252b5132 7
53b2a62f
NC
8 GLD is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
252b5132 12
53b2a62f
NC
13 GLD is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
252b5132 17
53b2a62f
NC
18 You should have received a copy of the GNU General Public License
19 along with GLD; see the file COPYING. If not, write to the Free
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
21 02111-1307, USA. */
252b5132
RH
22
23#include "bfd.h"
24#include "sysdep.h"
25#include "libiberty.h"
26#include <stdio.h>
27#include <string.h>
3882b010 28#include "safe-ctype.h"
252b5132
RH
29#include "getopt.h"
30#include "bfdlink.h"
31#include "ld.h"
32#include "ldmain.h"
33#include "ldmisc.h"
34#include "ldexp.h"
35#include "ldlang.h"
df2a7313 36#include <ldgram.h>
252b5132
RH
37#include "ldlex.h"
38#include "ldfile.h"
39#include "ldver.h"
40#include "ldemul.h"
28c309a2 41#include "demangle.h"
252b5132
RH
42
43#ifndef PATH_SEPARATOR
44#if defined (__MSDOS__) || (defined (_WIN32) && ! defined (__CYGWIN32__))
45#define PATH_SEPARATOR ';'
46#else
47#define PATH_SEPARATOR ':'
48#endif
49#endif
50
5cc18311 51/* Somewhere above, sys/stat.h got included . . . . */
252b5132
RH
52#if !defined(S_ISDIR) && defined(S_IFDIR)
53#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
54#endif
55
1579bae1
AM
56static void set_default_dirlist (char *);
57static void set_section_start (char *, char *);
58static void help (void);
252b5132
RH
59
60/* Non-zero if we are processing a --defsym from the command line. */
61int parsing_defsym = 0;
62
63/* Codes used for the long options with no short synonyms. 150 isn't
64 special; it's just an arbitrary non-ASCII char value. */
560e09e9
NC
65enum option_values
66{
67 OPTION_ASSERT = 150,
68 OPTION_CALL_SHARED,
69 OPTION_CREF,
70 OPTION_DEFSYM,
71 OPTION_DEMANGLE,
72 OPTION_DYNAMIC_LINKER,
73 OPTION_EB,
74 OPTION_EL,
75 OPTION_EMBEDDED_RELOCS,
76 OPTION_EXPORT_DYNAMIC,
77 OPTION_HELP,
78 OPTION_IGNORE,
79 OPTION_MAP,
80 OPTION_NO_DEMANGLE,
81 OPTION_NO_KEEP_MEMORY,
82 OPTION_NO_WARN_MISMATCH,
83 OPTION_NOINHIBIT_EXEC,
84 OPTION_NON_SHARED,
85 OPTION_NO_WHOLE_ARCHIVE,
86 OPTION_OFORMAT,
87 OPTION_RELAX,
88 OPTION_RETAIN_SYMBOLS_FILE,
89 OPTION_RPATH,
90 OPTION_RPATH_LINK,
91 OPTION_SHARED,
92 OPTION_SONAME,
93 OPTION_SORT_COMMON,
94 OPTION_STATS,
95 OPTION_SYMBOLIC,
96 OPTION_TASK_LINK,
97 OPTION_TBSS,
98 OPTION_TDATA,
99 OPTION_TTEXT,
100 OPTION_TRADITIONAL_FORMAT,
101 OPTION_UR,
102 OPTION_VERBOSE,
103 OPTION_VERSION,
104 OPTION_VERSION_SCRIPT,
105 OPTION_VERSION_EXPORTS_SECTION,
106 OPTION_WARN_COMMON,
107 OPTION_WARN_CONSTRUCTORS,
108 OPTION_WARN_FATAL,
109 OPTION_WARN_MULTIPLE_GP,
110 OPTION_WARN_ONCE,
111 OPTION_WARN_SECTION_ALIGN,
112 OPTION_SPLIT_BY_RELOC,
113 OPTION_SPLIT_BY_FILE ,
114 OPTION_WHOLE_ARCHIVE,
115 OPTION_WRAP,
116 OPTION_FORCE_EXE_SUFFIX,
117 OPTION_GC_SECTIONS,
118 OPTION_NO_GC_SECTIONS,
119 OPTION_CHECK_SECTIONS,
120 OPTION_NO_CHECK_SECTIONS,
121 OPTION_MPC860C0,
122 OPTION_NO_UNDEFINED,
123 OPTION_INIT,
124 OPTION_FINI,
125 OPTION_SECTION_START,
126 OPTION_UNIQUE,
127 OPTION_TARGET_HELP,
128 OPTION_ALLOW_SHLIB_UNDEFINED,
129 OPTION_NO_ALLOW_SHLIB_UNDEFINED,
130 OPTION_ALLOW_MULTIPLE_DEFINITION,
131 OPTION_NO_UNDEFINED_VERSION,
132 OPTION_DISCARD_NONE,
133 OPTION_SPARE_DYNAMIC_TAGS,
134 OPTION_NO_DEFINE_COMMON,
135 OPTION_NOSTDLIB,
136 OPTION_NO_OMAGIC,
137 OPTION_STRIP_DISCARDED,
138 OPTION_NO_STRIP_DISCARDED,
139 OPTION_ACCEPT_UNKNOWN_INPUT_ARCH,
140 OPTION_NO_ACCEPT_UNKNOWN_INPUT_ARCH,
141 OPTION_PIE,
142 OPTION_UNRESOLVED_SYMBOLS,
143 OPTION_WARN_UNRESOLVED_SYMBOLS,
144 OPTION_ERROR_UNRESOLVED_SYMBOLS
145};
252b5132
RH
146
147/* The long options. This structure is used for both the option
148 parsing and the help text. */
149
b79e8c78
NC
150struct ld_option
151{
252b5132
RH
152 /* The long option information. */
153 struct option opt;
154 /* The short option with the same meaning ('\0' if none). */
155 char shortopt;
156 /* The name of the argument (NULL if none). */
157 const char *arg;
158 /* The documentation string. If this is NULL, this is a synonym for
159 the previous option. */
160 const char *doc;
e47d05ad
KH
161 enum {
162 /* Use one dash before long option name. */
163 ONE_DASH,
164 /* Use two dashes before long option name. */
165 TWO_DASHES,
e4897a32
NC
166 /* Only accept two dashes before the long option name.
167 This is an overloading of the use of this enum, since originally it
168 was only intended to tell the --help display function how to display
169 the long option name. This feature was added in order to resolve
170 the confusion about the -omagic command line switch. Is it setting
171 the output file name to "magic" or is it setting the NMAGIC flag on
172 the output ? It has been decided that it is setting the output file
173 name, and that if you want to set the NMAGIC flag you should use -N
174 or --omagic. */
175 EXACTLY_TWO_DASHES,
e47d05ad
KH
176 /* Don't mention this option in --help output. */
177 NO_HELP
178 } control;
252b5132
RH
179};
180
b79e8c78
NC
181static const struct ld_option ld_options[] =
182{
252b5132
RH
183 { {NULL, required_argument, NULL, '\0'},
184 'a', N_("KEYWORD"), N_("Shared library control for HP/UX compatibility"),
185 ONE_DASH },
186 { {"architecture", required_argument, NULL, 'A'},
187 'A', N_("ARCH"), N_("Set architecture") , TWO_DASHES },
188 { {"format", required_argument, NULL, 'b'},
189 'b', N_("TARGET"), N_("Specify target for following input files"), TWO_DASHES },
190 { {"mri-script", required_argument, NULL, 'c'},
191 'c', N_("FILE"), N_("Read MRI format linker script"), TWO_DASHES },
192 { {"dc", no_argument, NULL, 'd'},
193 'd', NULL, N_("Force common symbols to be defined"), ONE_DASH },
194 { {"dp", no_argument, NULL, 'd'},
195 '\0', NULL, NULL, ONE_DASH },
196 { {"entry", required_argument, NULL, 'e'},
197 'e', N_("ADDRESS"), N_("Set start address"), TWO_DASHES },
198 { {"export-dynamic", no_argument, NULL, OPTION_EXPORT_DYNAMIC},
199 'E', NULL, N_("Export all dynamic symbols"), TWO_DASHES },
200 { {"EB", no_argument, NULL, OPTION_EB},
201 '\0', NULL, N_("Link big-endian objects"), ONE_DASH },
202 { {"EL", no_argument, NULL, OPTION_EL},
203 '\0', NULL, N_("Link little-endian objects"), ONE_DASH },
204 { {"auxiliary", required_argument, NULL, 'f'},
205 'f', N_("SHLIB"), N_("Auxiliary filter for shared object symbol table"),
206 TWO_DASHES },
207 { {"filter", required_argument, NULL, 'F'},
208 'F', N_("SHLIB"), N_("Filter for shared object symbol table"), TWO_DASHES },
209 { {NULL, no_argument, NULL, '\0'},
210 'g', NULL, N_("Ignored"), ONE_DASH },
211 { {"gpsize", required_argument, NULL, 'G'},
212 'G', N_("SIZE"), N_("Small data size (if no size, same as --shared)"),
213 TWO_DASHES },
214 { {"soname", required_argument, NULL, OPTION_SONAME},
215 'h', N_("FILENAME"), N_("Set internal name of shared library"), ONE_DASH },
506eee22 216 { {"dynamic-linker", required_argument, NULL, OPTION_DYNAMIC_LINKER},
ff291a48 217 'I', N_("PROGRAM"), N_("Set PROGRAM as the dynamic linker to use"), TWO_DASHES },
252b5132
RH
218 { {"library", required_argument, NULL, 'l'},
219 'l', N_("LIBNAME"), N_("Search for library LIBNAME"), TWO_DASHES },
220 { {"library-path", required_argument, NULL, 'L'},
221 'L', N_("DIRECTORY"), N_("Add DIRECTORY to library search path"), TWO_DASHES },
222 { {NULL, required_argument, NULL, '\0'},
223 'm', N_("EMULATION"), N_("Set emulation"), ONE_DASH },
224 { {"print-map", no_argument, NULL, 'M'},
225 'M', NULL, N_("Print map file on standard output"), TWO_DASHES },
226 { {"nmagic", no_argument, NULL, 'n'},
227 'n', NULL, N_("Do not page align data"), TWO_DASHES },
228 { {"omagic", no_argument, NULL, 'N'},
229 'N', NULL, N_("Do not page align data, do not make text readonly"),
e4897a32 230 EXACTLY_TWO_DASHES },
63fd3b82
NC
231 { {"no-omagic", no_argument, NULL, OPTION_NO_OMAGIC},
232 '\0', NULL, N_("Page align data, make text readonly"), EXACTLY_TWO_DASHES },
252b5132 233 { {"output", required_argument, NULL, 'o'},
e4897a32 234 'o', N_("FILE"), N_("Set output file name"), EXACTLY_TWO_DASHES },
252b5132
RH
235 { {NULL, required_argument, NULL, '\0'},
236 'O', NULL, N_("Optimize output file"), ONE_DASH },
237 { {"Qy", no_argument, NULL, OPTION_IGNORE},
238 '\0', NULL, N_("Ignored for SVR4 compatibility"), ONE_DASH },
a712da20
NC
239 { {"emit-relocs", no_argument, NULL, 'q'},
240 'q', NULL, "Generate relocations in final output", TWO_DASHES },
1049f94e
AM
241 { {"relocatable", no_argument, NULL, 'r'},
242 'r', NULL, N_("Generate relocatable output"), TWO_DASHES },
252b5132
RH
243 { {NULL, no_argument, NULL, '\0'},
244 'i', NULL, NULL, ONE_DASH },
245 { {"just-symbols", required_argument, NULL, 'R'},
246 'R', N_("FILE"), N_("Just link symbols (if directory, same as --rpath)"),
247 TWO_DASHES },
248 { {"strip-all", no_argument, NULL, 's'},
249 's', NULL, N_("Strip all symbols"), TWO_DASHES },
250 { {"strip-debug", no_argument, NULL, 'S'},
251 'S', NULL, N_("Strip debugging symbols"), TWO_DASHES },
d5cd3933
AM
252 { {"strip-discarded", no_argument, NULL, OPTION_STRIP_DISCARDED},
253 '\0', NULL, N_("Strip symbols in discarded sections"), TWO_DASHES },
254 { {"no-strip-discarded", no_argument, NULL, OPTION_NO_STRIP_DISCARDED},
255 '\0', NULL, N_("Do not strip symbols in discarded sections"), TWO_DASHES },
252b5132
RH
256 { {"trace", no_argument, NULL, 't'},
257 't', NULL, N_("Trace file opens"), TWO_DASHES },
258 { {"script", required_argument, NULL, 'T'},
259 'T', N_("FILE"), N_("Read linker script"), TWO_DASHES },
260 { {"undefined", required_argument, NULL, 'u'},
261 'u', N_("SYMBOL"), N_("Start with undefined reference to SYMBOL"), TWO_DASHES },
577a0623
AM
262 { {"unique", optional_argument, NULL, OPTION_UNIQUE},
263 '\0', N_("[=SECTION]"), N_("Don't merge input [SECTION | orphan] sections"), TWO_DASHES },
252b5132
RH
264 { {"Ur", no_argument, NULL, OPTION_UR},
265 '\0', NULL, N_("Build global constructor/destructor tables"), ONE_DASH },
266 { {"version", no_argument, NULL, OPTION_VERSION},
267 'v', NULL, N_("Print version information"), TWO_DASHES },
268 { {NULL, no_argument, NULL, '\0'},
269 'V', NULL, N_("Print version and emulation information"), ONE_DASH },
270 { {"discard-all", no_argument, NULL, 'x'},
271 'x', NULL, N_("Discard all local symbols"), TWO_DASHES },
272 { {"discard-locals", no_argument, NULL, 'X'},
f5fa8ca2
JJ
273 'X', NULL, N_("Discard temporary local symbols (default)"), TWO_DASHES },
274 { {"discard-none", no_argument, NULL, OPTION_DISCARD_NONE},
275 '\0', NULL, N_("Don't discard any local symbols"), TWO_DASHES },
252b5132
RH
276 { {"trace-symbol", required_argument, NULL, 'y'},
277 'y', N_("SYMBOL"), N_("Trace mentions of SYMBOL"), TWO_DASHES },
278 { {NULL, required_argument, NULL, '\0'},
279 'Y', N_("PATH"), N_("Default search path for Solaris compatibility"), ONE_DASH },
252b5132
RH
280 { {"start-group", no_argument, NULL, '('},
281 '(', NULL, N_("Start a group"), TWO_DASHES },
282 { {"end-group", no_argument, NULL, ')'},
283 ')', NULL, N_("End a group"), TWO_DASHES },
312b768e
NC
284 { {"accept-unknown-input-arch", no_argument, NULL, OPTION_ACCEPT_UNKNOWN_INPUT_ARCH},
285 '\0', NULL, N_("Accept input files whose architecture cannot be determined"), TWO_DASHES },
286 { {"no-accept-unknown-input-arch", no_argument, NULL, OPTION_NO_ACCEPT_UNKNOWN_INPUT_ARCH},
287 '\0', NULL, N_("Reject input files whose architecture is unknown"), TWO_DASHES },
252b5132
RH
288 { {"assert", required_argument, NULL, OPTION_ASSERT},
289 '\0', N_("KEYWORD"), N_("Ignored for SunOS compatibility"), ONE_DASH },
290 { {"Bdynamic", no_argument, NULL, OPTION_CALL_SHARED},
291 '\0', NULL, N_("Link against shared libraries"), ONE_DASH },
292 { {"dy", no_argument, NULL, OPTION_CALL_SHARED},
293 '\0', NULL, NULL, ONE_DASH },
294 { {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
295 '\0', NULL, NULL, ONE_DASH },
296 { {"Bstatic", no_argument, NULL, OPTION_NON_SHARED},
297 '\0', NULL, N_("Do not link against shared libraries"), ONE_DASH },
298 { {"dn", no_argument, NULL, OPTION_NON_SHARED},
299 '\0', NULL, NULL, ONE_DASH },
300 { {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
301 '\0', NULL, NULL, ONE_DASH },
302 { {"static", no_argument, NULL, OPTION_NON_SHARED},
303 '\0', NULL, NULL, ONE_DASH },
304 { {"Bsymbolic", no_argument, NULL, OPTION_SYMBOLIC},
305 '\0', NULL, N_("Bind global references locally"), ONE_DASH },
306 { {"check-sections", no_argument, NULL, OPTION_CHECK_SECTIONS},
307 '\0', NULL, N_("Check section addresses for overlaps (default)"), TWO_DASHES },
308 { {"no-check-sections", no_argument, NULL, OPTION_NO_CHECK_SECTIONS},
309 '\0', NULL, N_("Do not check section addresses for overlaps"),
310 TWO_DASHES },
311 { {"cref", no_argument, NULL, OPTION_CREF},
312 '\0', NULL, N_("Output cross reference table"), TWO_DASHES },
313 { {"defsym", required_argument, NULL, OPTION_DEFSYM},
314 '\0', N_("SYMBOL=EXPRESSION"), N_("Define a symbol"), TWO_DASHES },
28c309a2
NC
315 { {"demangle", optional_argument, NULL, OPTION_DEMANGLE},
316 '\0', N_("[=STYLE]"), N_("Demangle symbol names [using STYLE]"), TWO_DASHES },
252b5132
RH
317 { {"embedded-relocs", no_argument, NULL, OPTION_EMBEDDED_RELOCS},
318 '\0', NULL, N_("Generate embedded relocs"), TWO_DASHES},
3dbf70a2
MM
319 { {"fini", required_argument, NULL, OPTION_FINI},
320 '\0', N_("SYMBOL"), N_("Call SYMBOL at unload-time"), ONE_DASH },
252b5132
RH
321 { {"force-exe-suffix", no_argument, NULL, OPTION_FORCE_EXE_SUFFIX},
322 '\0', NULL, N_("Force generation of file with .exe suffix"), TWO_DASHES},
323 { {"gc-sections", no_argument, NULL, OPTION_GC_SECTIONS},
324 '\0', NULL, N_("Remove unused sections (on some targets)"),
325 TWO_DASHES },
326 { {"no-gc-sections", no_argument, NULL, OPTION_NO_GC_SECTIONS},
327 '\0', NULL, N_("Don't remove unused sections (default)"),
328 TWO_DASHES },
329 { {"help", no_argument, NULL, OPTION_HELP},
330 '\0', NULL, N_("Print option help"), TWO_DASHES },
3dbf70a2
MM
331 { {"init", required_argument, NULL, OPTION_INIT},
332 '\0', N_("SYMBOL"), N_("Call SYMBOL at load-time"), ONE_DASH },
252b5132
RH
333 { {"Map", required_argument, NULL, OPTION_MAP},
334 '\0', N_("FILE"), N_("Write a map file"), ONE_DASH },
4818e05f
AM
335 { {"no-define-common", no_argument, NULL, OPTION_NO_DEFINE_COMMON},
336 '\0', NULL, N_("Do not define Common storage"), TWO_DASHES },
252b5132
RH
337 { {"no-demangle", no_argument, NULL, OPTION_NO_DEMANGLE },
338 '\0', NULL, N_("Do not demangle symbol names"), TWO_DASHES },
339 { {"no-keep-memory", no_argument, NULL, OPTION_NO_KEEP_MEMORY},
340 '\0', NULL, N_("Use less memory and more disk I/O"), TWO_DASHES },
341 { {"no-undefined", no_argument, NULL, OPTION_NO_UNDEFINED},
560e09e9 342 '\0', NULL, N_("Do not allow unresolved references in object files"), TWO_DASHES },
b79e8c78 343 { {"allow-shlib-undefined", no_argument, NULL, OPTION_ALLOW_SHLIB_UNDEFINED},
560e09e9 344 '\0', NULL, N_("Allow unresolved references in shared libaries"), TWO_DASHES },
ae9a127f 345 { {"no-allow-shlib-undefined", no_argument, NULL, OPTION_NO_ALLOW_SHLIB_UNDEFINED},
560e09e9 346 '\0', NULL, N_("Do not allow unresolved references in shared libs"), TWO_DASHES },
aa713662
L
347 { {"allow-multiple-definition", no_argument, NULL, OPTION_ALLOW_MULTIPLE_DEFINITION},
348 '\0', NULL, N_("Allow multiple definitions"), TWO_DASHES },
31941635
L
349 { {"no-undefined-version", no_argument, NULL, OPTION_NO_UNDEFINED_VERSION},
350 '\0', NULL, N_("Disallow undefined version"), TWO_DASHES },
252b5132
RH
351 { {"no-warn-mismatch", no_argument, NULL, OPTION_NO_WARN_MISMATCH},
352 '\0', NULL, N_("Don't warn about mismatched input files"), TWO_DASHES},
353 { {"no-whole-archive", no_argument, NULL, OPTION_NO_WHOLE_ARCHIVE},
354 '\0', NULL, N_("Turn off --whole-archive"), TWO_DASHES },
355 { {"noinhibit-exec", no_argument, NULL, OPTION_NOINHIBIT_EXEC},
356 '\0', NULL, N_("Create an output file even if errors occur"), TWO_DASHES },
357 { {"noinhibit_exec", no_argument, NULL, OPTION_NOINHIBIT_EXEC},
358 '\0', NULL, NULL, NO_HELP },
361b220e
CD
359 { {"nostdlib", no_argument, NULL, OPTION_NOSTDLIB},
360 '\0', NULL, N_("Only use library directories specified on\n\t\t\t\tthe command line"), ONE_DASH },
252b5132 361 { {"oformat", required_argument, NULL, OPTION_OFORMAT},
e4897a32 362 '\0', N_("TARGET"), N_("Specify target of output file"), EXACTLY_TWO_DASHES },
252b5132
RH
363 { {"qmagic", no_argument, NULL, OPTION_IGNORE},
364 '\0', NULL, N_("Ignored for Linux compatibility"), ONE_DASH },
365 { {"relax", no_argument, NULL, OPTION_RELAX},
366 '\0', NULL, N_("Relax branches on certain targets"), TWO_DASHES },
367 { {"retain-symbols-file", required_argument, NULL,
368 OPTION_RETAIN_SYMBOLS_FILE},
369 '\0', N_("FILE"), N_("Keep only symbols listed in FILE"), TWO_DASHES },
370 { {"rpath", required_argument, NULL, OPTION_RPATH},
371 '\0', N_("PATH"), N_("Set runtime shared library search path"), ONE_DASH },
372 { {"rpath-link", required_argument, NULL, OPTION_RPATH_LINK},
373 '\0', N_("PATH"), N_("Set link time shared library search path"), ONE_DASH },
374 { {"shared", no_argument, NULL, OPTION_SHARED},
375 '\0', NULL, N_("Create a shared library"), ONE_DASH },
376 { {"Bshareable", no_argument, NULL, OPTION_SHARED }, /* FreeBSD. */
377 '\0', NULL, NULL, ONE_DASH },
36af4a4e
JJ
378 { {"pie", no_argument, NULL, OPTION_PIE},
379 '\0', NULL, N_("Create a position independent executable"), ONE_DASH },
380 { {"pic-executable", no_argument, NULL, OPTION_PIE},
381 '\0', NULL, NULL, TWO_DASHES },
252b5132
RH
382 { {"sort-common", no_argument, NULL, OPTION_SORT_COMMON},
383 '\0', NULL, N_("Sort common symbols by size"), TWO_DASHES },
384 { {"sort_common", no_argument, NULL, OPTION_SORT_COMMON},
385 '\0', NULL, NULL, NO_HELP },
db6751f2
JJ
386 { {"spare-dynamic-tags", required_argument, NULL, OPTION_SPARE_DYNAMIC_TAGS},
387 '\0', N_("COUNT"), N_("How many tags to reserve in .dynamic section"), TWO_DASHES },
a854a4a7
AM
388 { {"split-by-file", optional_argument, NULL, OPTION_SPLIT_BY_FILE},
389 '\0', N_("[=SIZE]"), N_("Split output sections every SIZE octets"), TWO_DASHES },
390 { {"split-by-reloc", optional_argument, NULL, OPTION_SPLIT_BY_RELOC},
391 '\0', N_("[=COUNT]"), N_("Split output sections every COUNT relocs"), TWO_DASHES },
252b5132
RH
392 { {"stats", no_argument, NULL, OPTION_STATS},
393 '\0', NULL, N_("Print memory usage statistics"), TWO_DASHES },
ea20a7da
CC
394 { {"target-help", no_argument, NULL, OPTION_TARGET_HELP},
395 '\0', NULL, N_("Display target specific options"), TWO_DASHES },
252b5132
RH
396 { {"task-link", required_argument, NULL, OPTION_TASK_LINK},
397 '\0', N_("SYMBOL"), N_("Do task level linking"), TWO_DASHES },
398 { {"traditional-format", no_argument, NULL, OPTION_TRADITIONAL_FORMAT},
399 '\0', NULL, N_("Use same format as native linker"), TWO_DASHES },
176355da
NC
400 { {"section-start", required_argument, NULL, OPTION_SECTION_START},
401 '\0', N_("SECTION=ADDRESS"), N_("Set address of named section"), TWO_DASHES },
252b5132
RH
402 { {"Tbss", required_argument, NULL, OPTION_TBSS},
403 '\0', N_("ADDRESS"), N_("Set address of .bss section"), ONE_DASH },
404 { {"Tdata", required_argument, NULL, OPTION_TDATA},
405 '\0', N_("ADDRESS"), N_("Set address of .data section"), ONE_DASH },
406 { {"Ttext", required_argument, NULL, OPTION_TTEXT},
407 '\0', N_("ADDRESS"), N_("Set address of .text section"), ONE_DASH },
560e09e9
NC
408 { {"unresolved-symbols=<method>", required_argument, NULL, OPTION_UNRESOLVED_SYMBOLS},
409 '\0', NULL, N_("How to handle unresolved symbols. <method> is:\n\t\t\t\tignore-all, report-all, ignore-in-object-files,\n\t\t\t\tignore-in-shared-libs"),
410 TWO_DASHES },
252b5132
RH
411 { {"verbose", no_argument, NULL, OPTION_VERBOSE},
412 '\0', NULL, N_("Output lots of information during link"), TWO_DASHES },
413 { {"dll-verbose", no_argument, NULL, OPTION_VERBOSE}, /* Linux. */
414 '\0', NULL, NULL, NO_HELP },
415 { {"version-script", required_argument, NULL, OPTION_VERSION_SCRIPT },
416 '\0', N_("FILE"), N_("Read version information script"), TWO_DASHES },
417 { {"version-exports-section", required_argument, NULL,
418 OPTION_VERSION_EXPORTS_SECTION },
b3043ee4 419 '\0', N_("SYMBOL"), N_("Take export symbols list from .exports, using\n\t\t\t\tSYMBOL as the version."),
252b5132
RH
420 TWO_DASHES },
421 { {"warn-common", no_argument, NULL, OPTION_WARN_COMMON},
422 '\0', NULL, N_("Warn about duplicate common symbols"), TWO_DASHES },
423 { {"warn-constructors", no_argument, NULL, OPTION_WARN_CONSTRUCTORS},
424 '\0', NULL, N_("Warn if global constructors/destructors are seen"),
425 TWO_DASHES },
426 { {"warn-multiple-gp", no_argument, NULL, OPTION_WARN_MULTIPLE_GP},
427 '\0', NULL, N_("Warn if the multiple GP values are used"), TWO_DASHES },
428 { {"warn-once", no_argument, NULL, OPTION_WARN_ONCE},
429 '\0', NULL, N_("Warn only once per undefined symbol"), TWO_DASHES },
430 { {"warn-section-align", no_argument, NULL, OPTION_WARN_SECTION_ALIGN},
431 '\0', NULL, N_("Warn if start of section changes due to alignment"),
432 TWO_DASHES },
560e09e9
NC
433 { {"warn-unresolved-symbols", no_argument, NULL, OPTION_WARN_UNRESOLVED_SYMBOLS},
434 '\0', NULL, N_("Report unresolved symbols as warnings"), TWO_DASHES },
435 { {"error-unresolved-symbols", no_argument, NULL, OPTION_ERROR_UNRESOLVED_SYMBOLS},
436 '\0', NULL, N_("Report unresolved symbols as errors"), TWO_DASHES },
7ce691ae
C
437 { {"fatal-warnings", no_argument, NULL, OPTION_WARN_FATAL},
438 '\0', NULL, N_("Treat warnings as errors"),
439 TWO_DASHES },
252b5132
RH
440 { {"whole-archive", no_argument, NULL, OPTION_WHOLE_ARCHIVE},
441 '\0', NULL, N_("Include all objects from following archives"), TWO_DASHES },
442 { {"wrap", required_argument, NULL, OPTION_WRAP},
443 '\0', N_("SYMBOL"), N_("Use wrapper functions for SYMBOL"), TWO_DASHES },
444 { {"mpc860c0", optional_argument, NULL, OPTION_MPC860C0},
b3043ee4 445 '\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
446};
447
e4897a32 448#define OPTION_COUNT ARRAY_SIZE (ld_options)
252b5132 449
3d9f6277
ILT
450/* Test STRING for containing a string of digits that form a number
451 between MIN and MAX. The return value is the number or ERR. */
452
453static int
1579bae1 454is_num (const char *string, int min, int max, int err)
252b5132
RH
455{
456 int result = 0;
457
3d9f6277 458 for (; *string; ++string)
252b5132 459 {
3882b010 460 if (! ISDIGIT (*string))
3d9f6277
ILT
461 {
462 result = err;
463 break;
464 }
465 result = result * 10 + (*string - '0');
252b5132 466 }
252b5132
RH
467 if (result < min || result > max)
468 result = err;
469
470 return result;
471}
472
473void
1579bae1 474parse_args (unsigned argc, char **argv)
252b5132 475{
e4897a32
NC
476 unsigned i;
477 int is, il, irl;
252b5132
RH
478 int ingroup = 0;
479 char *default_dirlist = NULL;
3bcf5557
AM
480 char *shortopts;
481 struct option *longopts;
482 struct option *really_longopts;
252b5132 483 int last_optind;
560e09e9 484 enum report_method how_to_report_unresolved_symbols = RM_GENERATE_ERROR;
252b5132 485
1579bae1
AM
486 shortopts = xmalloc (OPTION_COUNT * 3 + 2);
487 longopts = xmalloc (sizeof (*longopts) * (OPTION_COUNT + 1));
488 really_longopts = xmalloc (sizeof (*really_longopts) * (OPTION_COUNT + 1));
3bcf5557 489
252b5132
RH
490 /* Starting the short option string with '-' is for programs that
491 expect options and other ARGV-elements in any order and that care about
492 the ordering of the two. We describe each non-option ARGV-element
493 as if it were the argument of an option with character code 1. */
494 shortopts[0] = '-';
495 is = 1;
496 il = 0;
e4897a32 497 irl = 0;
252b5132
RH
498 for (i = 0; i < OPTION_COUNT; i++)
499 {
500 if (ld_options[i].shortopt != '\0')
501 {
502 shortopts[is] = ld_options[i].shortopt;
503 ++is;
504 if (ld_options[i].opt.has_arg == required_argument
505 || ld_options[i].opt.has_arg == optional_argument)
506 {
507 shortopts[is] = ':';
508 ++is;
509 if (ld_options[i].opt.has_arg == optional_argument)
510 {
511 shortopts[is] = ':';
512 ++is;
513 }
514 }
515 }
516 if (ld_options[i].opt.name != NULL)
517 {
e4897a32
NC
518 if (ld_options[i].control == EXACTLY_TWO_DASHES)
519 {
520 really_longopts[irl] = ld_options[i].opt;
521 ++irl;
522 }
523 else
524 {
525 longopts[il] = ld_options[i].opt;
526 ++il;
527 }
252b5132
RH
528 }
529 }
530 shortopts[is] = '\0';
531 longopts[il].name = NULL;
e4897a32 532 really_longopts[irl].name = NULL;
252b5132 533
3bcf5557
AM
534 ldemul_add_options (is, &shortopts, il, &longopts, irl, &really_longopts);
535
252b5132
RH
536 /* The -G option is ambiguous on different platforms. Sometimes it
537 specifies the largest data size to put into the small data
538 section. Sometimes it is equivalent to --shared. Unfortunately,
539 the first form takes an argument, while the second does not.
540
541 We need to permit the --shared form because on some platforms,
542 such as Solaris, gcc -shared will pass -G to the linker.
543
544 To permit either usage, we look through the argument list. If we
545 find -G not followed by a number, we change it into --shared.
546 This will work for most normal cases. */
547 for (i = 1; i < argc; i++)
548 if (strcmp (argv[i], "-G") == 0
549 && (i + 1 >= argc
3882b010 550 || ! ISDIGIT (argv[i + 1][0])))
252b5132
RH
551 argv[i] = (char *) "--shared";
552
553 /* Because we permit long options to start with a single dash, and
554 we have a --library option, and the -l option is conventionally
555 used with an immediately following argument, we can have bad
556 results if somebody tries to use -l with a library whose name
557 happens to start with "ibrary", as in -li. We avoid problems by
558 simply turning -l into --library. This means that users will
559 have to use two dashes in order to use --library, which is OK
560 since that's how it is documented.
561
562 FIXME: It's possible that this problem can arise for other short
563 options as well, although the user does always have the recourse
564 of adding a space between the option and the argument. */
565 for (i = 1; i < argc; i++)
566 {
567 if (argv[i][0] == '-'
568 && argv[i][1] == 'l'
569 && argv[i][2] != '\0')
570 {
571 char *n;
572
1579bae1 573 n = xmalloc (strlen (argv[i]) + 20);
252b5132
RH
574 sprintf (n, "--library=%s", argv[i] + 2);
575 argv[i] = n;
576 }
577 }
578
579 last_optind = -1;
580 while (1)
581 {
89894c62 582 int longind;
252b5132
RH
583 int optc;
584
585 /* Using last_optind lets us avoid calling ldemul_parse_args
586 multiple times on a single option, which would lead to
587 confusion in the internal static variables maintained by
588 getopt. This could otherwise happen for an argument like
589 -nx, in which the -n is parsed as a single option, and we
590 loop around to pick up the -x. */
591 if (optind != last_optind)
89894c62
AM
592 if (ldemul_parse_args (argc, argv))
593 continue;
252b5132
RH
594
595 /* getopt_long_only is like getopt_long, but '-' as well as '--'
596 can indicate a long option. */
0fc3347a 597 opterr = 0;
89894c62 598 last_optind = optind;
252b5132 599 optc = getopt_long_only (argc, argv, shortopts, longopts, &longind);
0fc3347a
NC
600 if (optc == '?')
601 {
89894c62
AM
602 optind = last_optind;
603 optc = getopt_long (argc, argv, "-", really_longopts, &longind);
0fc3347a 604 }
b7ed8fad 605
3bcf5557
AM
606 if (ldemul_handle_option (optc))
607 continue;
608
252b5132
RH
609 if (optc == -1)
610 break;
0fc3347a 611
252b5132
RH
612 switch (optc)
613 {
0fc3347a 614 case '?':
89894c62 615 einfo (_("%P: unrecognized option '%s'\n"), argv[last_optind]);
252b5132 616 default:
210a7bd1
NC
617 einfo (_("%P%F: use the --help option for usage information\n"));
618
252b5132 619 case 1: /* File name. */
1579bae1 620 lang_add_input_file (optarg, lang_input_file_is_file_enum, NULL);
252b5132
RH
621 break;
622
623 case OPTION_IGNORE:
624 break;
625 case 'a':
626 /* For HP/UX compatibility. Actually -a shared should mean
1579bae1
AM
627 ``use only shared libraries'' but, then, we don't
628 currently support shared libraries on HP/UX anyhow. */
252b5132 629 if (strcmp (optarg, "archive") == 0)
b34976b6 630 config.dynamic_link = FALSE;
252b5132
RH
631 else if (strcmp (optarg, "shared") == 0
632 || strcmp (optarg, "default") == 0)
b34976b6 633 config.dynamic_link = TRUE;
252b5132
RH
634 else
635 einfo (_("%P%F: unrecognized -a option `%s'\n"), optarg);
636 break;
637 case OPTION_ASSERT:
638 /* FIXME: We just ignore these, but we should handle them. */
639 if (strcmp (optarg, "definitions") == 0)
640 ;
641 else if (strcmp (optarg, "nodefinitions") == 0)
642 ;
643 else if (strcmp (optarg, "nosymbolic") == 0)
644 ;
645 else if (strcmp (optarg, "pure-text") == 0)
646 ;
647 else
648 einfo (_("%P%F: unrecognized -assert option `%s'\n"), optarg);
649 break;
650 case 'A':
651 ldfile_add_arch (optarg);
652 break;
653 case 'b':
654 lang_add_target (optarg);
655 break;
656 case 'c':
657 ldfile_open_command_file (optarg);
658 parser_input = input_mri_script;
659 yyparse ();
660 break;
661 case OPTION_CALL_SHARED:
b34976b6 662 config.dynamic_link = TRUE;
85477099 663 /* When linking against shared libraries, the default behaviour is
e62eb3ee
NC
664 to report any unresolved references. Although strictly speaking
665 it is not a failure to encounter unresolved symbols at link time
666 - the symbol *might* be available at load time - it is a strong
667 indication that the resulting executable will not work. Plus it
668 is necessary for the correct execution of the autoconf package,
669 which needs to be able to detect functions that are not provided
670 by the host OS. */
560e09e9 671 if (link_info.unresolved_syms_in_objects == RM_NOT_YET_SET)
e62eb3ee 672 link_info.unresolved_syms_in_objects = how_to_report_unresolved_symbols;
560e09e9 673 if (link_info.unresolved_syms_in_shared_libs == RM_NOT_YET_SET)
85477099 674 link_info.unresolved_syms_in_shared_libs = how_to_report_unresolved_symbols;
252b5132
RH
675 break;
676 case OPTION_NON_SHARED:
b34976b6 677 config.dynamic_link = FALSE;
560e09e9
NC
678 /* When linking against static libraries, the default
679 behaviour is to report any unresolved references. */
680 if (link_info.unresolved_syms_in_objects == RM_NOT_YET_SET)
681 link_info.unresolved_syms_in_objects = how_to_report_unresolved_symbols;
682 if (link_info.unresolved_syms_in_shared_libs == RM_NOT_YET_SET)
683 link_info.unresolved_syms_in_shared_libs = how_to_report_unresolved_symbols;
252b5132
RH
684 break;
685 case OPTION_CREF:
b34976b6
AM
686 command_line.cref = TRUE;
687 link_info.notice_all = TRUE;
252b5132
RH
688 break;
689 case 'd':
b34976b6 690 command_line.force_common_definition = TRUE;
252b5132
RH
691 break;
692 case OPTION_DEFSYM:
693 lex_string = optarg;
694 lex_redirect (optarg);
695 parser_input = input_defsym;
696 parsing_defsym = 1;
697 yyparse ();
698 parsing_defsym = 0;
699 lex_string = NULL;
700 break;
701 case OPTION_DEMANGLE:
b34976b6 702 demangling = TRUE;
28c309a2
NC
703 if (optarg != NULL)
704 {
705 enum demangling_styles style;
5cc18311 706
28c309a2 707 style = cplus_demangle_name_to_style (optarg);
5cc18311 708 if (style == unknown_demangling)
28c309a2
NC
709 einfo (_("%F%P: unknown demangling style `%s'"),
710 optarg);
5cc18311 711
28c309a2 712 cplus_demangle_set_style (style);
e47d05ad 713 }
252b5132 714 break;
506eee22 715 case 'I': /* Used on Solaris. */
252b5132
RH
716 case OPTION_DYNAMIC_LINKER:
717 command_line.interpreter = optarg;
718 break;
719 case OPTION_EB:
720 command_line.endian = ENDIAN_BIG;
721 break;
722 case OPTION_EL:
723 command_line.endian = ENDIAN_LITTLE;
724 break;
725 case OPTION_EMBEDDED_RELOCS:
b34976b6 726 command_line.embedded_relocs = TRUE;
252b5132
RH
727 break;
728 case OPTION_EXPORT_DYNAMIC:
729 case 'E': /* HP/UX compatibility. */
b34976b6 730 link_info.export_dynamic = TRUE;
252b5132
RH
731 break;
732 case 'e':
b34976b6 733 lang_add_entry (optarg, TRUE);
252b5132
RH
734 break;
735 case 'f':
736 if (command_line.auxiliary_filters == NULL)
737 {
1579bae1 738 command_line.auxiliary_filters = xmalloc (2 * sizeof (char *));
252b5132
RH
739 command_line.auxiliary_filters[0] = optarg;
740 command_line.auxiliary_filters[1] = NULL;
741 }
742 else
743 {
744 int c;
745 char **p;
746
747 c = 0;
748 for (p = command_line.auxiliary_filters; *p != NULL; p++)
749 ++c;
1579bae1
AM
750 command_line.auxiliary_filters
751 = xrealloc (command_line.auxiliary_filters,
752 (c + 2) * sizeof (char *));
252b5132
RH
753 command_line.auxiliary_filters[c] = optarg;
754 command_line.auxiliary_filters[c + 1] = NULL;
755 }
756 break;
757 case 'F':
758 command_line.filter_shlib = optarg;
759 break;
760 case OPTION_FORCE_EXE_SUFFIX:
b34976b6 761 command_line.force_exe_suffix = TRUE;
252b5132
RH
762 break;
763 case 'G':
764 {
765 char *end;
766 g_switch_value = strtoul (optarg, &end, 0);
767 if (*end)
768 einfo (_("%P%F: invalid number `%s'\n"), optarg);
769 }
770 break;
771 case 'g':
772 /* Ignore. */
773 break;
774 case OPTION_GC_SECTIONS:
b34976b6 775 command_line.gc_sections = TRUE;
252b5132
RH
776 break;
777 case OPTION_HELP:
778 help ();
779 xexit (0);
780 break;
781 case 'L':
b34976b6 782 ldfile_add_library_path (optarg, TRUE);
252b5132
RH
783 break;
784 case 'l':
1579bae1 785 lang_add_input_file (optarg, lang_input_file_is_l_enum, NULL);
252b5132
RH
786 break;
787 case 'M':
788 config.map_filename = "-";
789 break;
790 case 'm':
791 /* Ignore. Was handled in a pre-parse. */
792 break;
793 case OPTION_MAP:
794 config.map_filename = optarg;
795 break;
796 case 'N':
b34976b6
AM
797 config.text_read_only = FALSE;
798 config.magic_demand_paged = FALSE;
799 config.dynamic_link = FALSE;
252b5132 800 break;
63fd3b82 801 case OPTION_NO_OMAGIC:
b34976b6
AM
802 config.text_read_only = TRUE;
803 config.magic_demand_paged = TRUE;
804 /* NB/ Does not set dynamic_link to TRUE.
63fd3b82
NC
805 Use --call-shared or -Bdynamic for this. */
806 break;
252b5132 807 case 'n':
b34976b6
AM
808 config.magic_demand_paged = FALSE;
809 config.dynamic_link = FALSE;
252b5132 810 break;
4818e05f 811 case OPTION_NO_DEFINE_COMMON:
b34976b6 812 command_line.inhibit_common_definition = TRUE;
4818e05f 813 break;
252b5132 814 case OPTION_NO_DEMANGLE:
b34976b6 815 demangling = FALSE;
252b5132
RH
816 break;
817 case OPTION_NO_GC_SECTIONS:
b34976b6 818 command_line.gc_sections = FALSE;
252b5132
RH
819 break;
820 case OPTION_NO_KEEP_MEMORY:
b34976b6 821 link_info.keep_memory = FALSE;
252b5132
RH
822 break;
823 case OPTION_NO_UNDEFINED:
560e09e9 824 link_info.unresolved_syms_in_objects = how_to_report_unresolved_symbols;
252b5132 825 break;
b79e8c78 826 case OPTION_ALLOW_SHLIB_UNDEFINED:
560e09e9 827 link_info.unresolved_syms_in_shared_libs = RM_IGNORE;
b79e8c78 828 break;
ae9a127f 829 case OPTION_NO_ALLOW_SHLIB_UNDEFINED:
560e09e9 830 link_info.unresolved_syms_in_shared_libs = how_to_report_unresolved_symbols;
ae9a127f 831 break;
560e09e9
NC
832 case OPTION_UNRESOLVED_SYMBOLS:
833 if (strcmp (optarg, "ignore-all") == 0)
834 {
835 link_info.unresolved_syms_in_objects = RM_IGNORE;
836 link_info.unresolved_syms_in_shared_libs = RM_IGNORE;
837 }
838 else if (strcmp (optarg, "report-all") == 0)
839 {
840 link_info.unresolved_syms_in_objects = how_to_report_unresolved_symbols;
841 link_info.unresolved_syms_in_shared_libs = how_to_report_unresolved_symbols;
842 }
843 else if (strcmp (optarg, "ignore-in-object-files") == 0)
844 {
845 link_info.unresolved_syms_in_objects = RM_IGNORE;
846 link_info.unresolved_syms_in_shared_libs = how_to_report_unresolved_symbols;
847 }
848 else if (strcmp (optarg, "ignore-in-shared-libs") == 0)
849 {
850 link_info.unresolved_syms_in_objects = how_to_report_unresolved_symbols;
851 link_info.unresolved_syms_in_shared_libs = RM_IGNORE;
852 }
853 else
854 einfo (_("%P%F: bad --unresolved-symbols option: %s\n"), optarg);
855 break;
856 case OPTION_WARN_UNRESOLVED_SYMBOLS:
857 how_to_report_unresolved_symbols = RM_GENERATE_WARNING;
858 if (link_info.unresolved_syms_in_objects == RM_GENERATE_ERROR)
859 link_info.unresolved_syms_in_objects = RM_GENERATE_WARNING;
860 if (link_info.unresolved_syms_in_shared_libs == RM_GENERATE_ERROR)
861 link_info.unresolved_syms_in_shared_libs = RM_GENERATE_WARNING;
862 break;
863
864 case OPTION_ERROR_UNRESOLVED_SYMBOLS:
865 how_to_report_unresolved_symbols = RM_GENERATE_ERROR;
866 if (link_info.unresolved_syms_in_objects == RM_GENERATE_WARNING)
867 link_info.unresolved_syms_in_objects = RM_GENERATE_ERROR;
868 if (link_info.unresolved_syms_in_shared_libs == RM_GENERATE_WARNING)
869 link_info.unresolved_syms_in_shared_libs = RM_GENERATE_ERROR;
870 break;
aa713662 871 case OPTION_ALLOW_MULTIPLE_DEFINITION:
b34976b6 872 link_info.allow_multiple_definition = TRUE;
aa713662 873 break;
31941635 874 case OPTION_NO_UNDEFINED_VERSION:
b34976b6 875 link_info.allow_undefined_version = FALSE;
31941635 876 break;
252b5132 877 case OPTION_NO_WARN_MISMATCH:
b34976b6 878 command_line.warn_mismatch = FALSE;
252b5132
RH
879 break;
880 case OPTION_NOINHIBIT_EXEC:
b34976b6 881 force_make_executable = TRUE;
252b5132 882 break;
361b220e 883 case OPTION_NOSTDLIB:
b34976b6 884 config.only_cmd_line_lib_dirs = TRUE;
361b220e 885 break;
252b5132 886 case OPTION_NO_WHOLE_ARCHIVE:
b34976b6 887 whole_archive = FALSE;
252b5132
RH
888 break;
889 case 'O':
890 /* FIXME "-O<non-digits> <value>" used to set the address of
891 section <non-digits>. Was this for compatibility with
892 something, or can we create a new option to do that
893 (with a syntax similar to -defsym)?
894 getopt can't handle two args to an option without kludges. */
895
896 /* Enable optimizations of output files. */
b34976b6 897 link_info.optimize = strtoul (optarg, NULL, 0) ? TRUE : FALSE;
252b5132
RH
898 break;
899 case 'o':
5cc18311 900 lang_add_output (optarg, 0);
252b5132
RH
901 break;
902 case OPTION_OFORMAT:
1579bae1 903 lang_add_output_format (optarg, NULL, NULL, 0);
252b5132 904 break;
a712da20 905 case 'q':
b34976b6 906 link_info.emitrelocations = TRUE;
a712da20 907 break;
8c5ff972 908 case 'i':
252b5132 909 case 'r':
210a7bd1
NC
910 if (optind == last_optind)
911 /* This can happen if the user put "-rpath,a" on the command
912 line. (Or something similar. The comma is important).
913 Getopt becomes confused and thinks that this is a -r option
914 but it cannot parse the text after the -r so it refuses to
915 increment the optind counter. Detect this case and issue
916 an error message here. We cannot just make this a warning,
917 increment optind, and continue because getopt is too confused
918 and will seg-fault the next time around. */
919 einfo(_("%P%F: bad -rpath option\n"));
b7a26f91 920
1049f94e 921 link_info.relocatable = TRUE;
b34976b6
AM
922 config.build_constructors = FALSE;
923 config.magic_demand_paged = FALSE;
924 config.text_read_only = FALSE;
925 config.dynamic_link = FALSE;
252b5132
RH
926 break;
927 case 'R':
928 /* The GNU linker traditionally uses -R to mean to include
929 only the symbols from a file. The Solaris linker uses -R
930 to set the path used by the runtime linker to find
931 libraries. This is the GNU linker -rpath argument. We
932 try to support both simultaneously by checking the file
933 named. If it is a directory, rather than a regular file,
934 we assume -rpath was meant. */
935 {
936 struct stat s;
937
938 if (stat (optarg, &s) >= 0
939 && ! S_ISDIR (s.st_mode))
940 {
941 lang_add_input_file (optarg,
942 lang_input_file_is_symbols_only_enum,
1579bae1 943 NULL);
252b5132
RH
944 break;
945 }
946 }
947 /* Fall through. */
948 case OPTION_RPATH:
949 if (command_line.rpath == NULL)
d1b2b2dc 950 command_line.rpath = xstrdup (optarg);
252b5132
RH
951 else
952 {
953 size_t rpath_len = strlen (command_line.rpath);
954 size_t optarg_len = strlen (optarg);
955 char *buf;
956 char *cp = command_line.rpath;
957
958 /* First see whether OPTARG is already in the path. */
959 do
960 {
961 size_t idx = 0;
210a7bd1 962
252b5132
RH
963 while (optarg[idx] != '\0' && optarg[idx] == cp[idx])
964 ++idx;
965 if (optarg[idx] == '\0'
966 && (cp[idx] == '\0' || cp[idx] == ':'))
967 /* We found it. */
968 break;
969
970 /* Not yet found. */
971 cp = strchr (cp, ':');
972 if (cp != NULL)
973 ++cp;
974 }
975 while (cp != NULL);
976
977 if (cp == NULL)
978 {
979 buf = xmalloc (rpath_len + optarg_len + 2);
980 sprintf (buf, "%s:%s", command_line.rpath, optarg);
981 free (command_line.rpath);
982 command_line.rpath = buf;
983 }
984 }
985 break;
986 case OPTION_RPATH_LINK:
987 if (command_line.rpath_link == NULL)
d1b2b2dc 988 command_line.rpath_link = xstrdup (optarg);
252b5132
RH
989 else
990 {
991 char *buf;
992
993 buf = xmalloc (strlen (command_line.rpath_link)
994 + strlen (optarg)
995 + 2);
996 sprintf (buf, "%s:%s", command_line.rpath_link, optarg);
997 free (command_line.rpath_link);
998 command_line.rpath_link = buf;
999 }
1000 break;
1001 case OPTION_RELAX:
b34976b6 1002 command_line.relax = TRUE;
252b5132
RH
1003 break;
1004 case OPTION_RETAIN_SYMBOLS_FILE:
1005 add_keepsyms_file (optarg);
1006 break;
1007 case 'S':
1008 link_info.strip = strip_debugger;
1009 break;
1010 case 's':
1011 link_info.strip = strip_all;
1012 break;
d5cd3933
AM
1013 case OPTION_STRIP_DISCARDED:
1014 link_info.strip_discarded = TRUE;
1015 break;
1016 case OPTION_NO_STRIP_DISCARDED:
1017 link_info.strip_discarded = FALSE;
1018 break;
252b5132
RH
1019 case OPTION_SHARED:
1020 if (config.has_shared)
560e09e9
NC
1021 {
1022 link_info.shared = TRUE;
1023 /* When creating a shared library, the default
1024 behaviour is to ignore any unresolved references. */
1025 if (link_info.unresolved_syms_in_objects == RM_NOT_YET_SET)
1026 link_info.unresolved_syms_in_objects = RM_IGNORE;
1027 if (link_info.unresolved_syms_in_shared_libs == RM_NOT_YET_SET)
1028 link_info.unresolved_syms_in_shared_libs = RM_IGNORE;
1029 }
252b5132
RH
1030 else
1031 einfo (_("%P%F: -shared not supported\n"));
1032 break;
36af4a4e
JJ
1033 case OPTION_PIE:
1034 if (config.has_shared)
1035 {
1036 link_info.shared = TRUE;
1037 link_info.pie = TRUE;
1038 }
1039 else
1040 einfo (_("%P%F: -pie not supported\n"));
1041 break;
252b5132
RH
1042 case 'h': /* Used on Solaris. */
1043 case OPTION_SONAME:
1044 command_line.soname = optarg;
1045 break;
1046 case OPTION_SORT_COMMON:
b34976b6 1047 config.sort_common = TRUE;
252b5132
RH
1048 break;
1049 case OPTION_STATS:
b34976b6 1050 config.stats = TRUE;
252b5132
RH
1051 break;
1052 case OPTION_SYMBOLIC:
b34976b6 1053 link_info.symbolic = TRUE;
252b5132
RH
1054 break;
1055 case 't':
b34976b6 1056 trace_files = TRUE;
252b5132
RH
1057 break;
1058 case 'T':
1059 ldfile_open_command_file (optarg);
1060 parser_input = input_script;
1061 yyparse ();
1062 break;
176355da
NC
1063 case OPTION_SECTION_START:
1064 {
1065 char *optarg2;
227aeb07
AM
1066 char *sec_name;
1067 int len;
176355da
NC
1068
1069 /* Check for <something>=<somthing>... */
1070 optarg2 = strchr (optarg, '=');
1071 if (optarg2 == NULL)
210a7bd1 1072 einfo (_("%P%F: invalid argument to option \"--section-start\"\n"));
176355da 1073
e47d05ad 1074 optarg2++;
5cc18311 1075
176355da
NC
1076 /* So far so good. Are all the args present? */
1077 if ((*optarg == '\0') || (*optarg2 == '\0'))
210a7bd1 1078 einfo (_("%P%F: missing argument(s) to option \"--section-start\"\n"));
176355da 1079
227aeb07
AM
1080 /* We must copy the section name as set_section_start
1081 doesn't do it for us. */
1082 len = optarg2 - optarg;
1083 sec_name = xmalloc (len);
1084 memcpy (sec_name, optarg, len - 1);
1085 sec_name[len - 1] = 0;
176355da
NC
1086
1087 /* Then set it... */
227aeb07 1088 set_section_start (sec_name, optarg2);
176355da
NC
1089 }
1090 break;
ea20a7da
CC
1091 case OPTION_TARGET_HELP:
1092 /* Mention any target specific options. */
b7a26f91
KH
1093 ldemul_list_emulation_options (stdout);
1094 exit (0);
252b5132
RH
1095 case OPTION_TBSS:
1096 set_section_start (".bss", optarg);
1097 break;
1098 case OPTION_TDATA:
1099 set_section_start (".data", optarg);
1100 break;
1101 case OPTION_TTEXT:
1102 set_section_start (".text", optarg);
1103 break;
1104 case OPTION_TRADITIONAL_FORMAT:
b34976b6 1105 link_info.traditional_format = TRUE;
252b5132
RH
1106 break;
1107 case OPTION_TASK_LINK:
b34976b6 1108 link_info.task_link = TRUE;
252b5132
RH
1109 /* Fall through - do an implied -r option. */
1110 case OPTION_UR:
1049f94e 1111 link_info.relocatable = TRUE;
b34976b6
AM
1112 config.build_constructors = TRUE;
1113 config.magic_demand_paged = FALSE;
1114 config.text_read_only = FALSE;
1115 config.dynamic_link = FALSE;
252b5132
RH
1116 break;
1117 case 'u':
1118 ldlang_add_undef (optarg);
1119 break;
a854a4a7 1120 case OPTION_UNIQUE:
577a0623
AM
1121 if (optarg != NULL)
1122 lang_add_unique (optarg);
1123 else
b34976b6 1124 config.unique_orphan_sections = TRUE;
a854a4a7 1125 break;
252b5132
RH
1126 case OPTION_VERBOSE:
1127 ldversion (1);
b34976b6
AM
1128 version_printed = TRUE;
1129 trace_file_tries = TRUE;
8aae64e6 1130 overflow_cutoff_limit = -2;
252b5132
RH
1131 break;
1132 case 'v':
1133 ldversion (0);
b34976b6 1134 version_printed = TRUE;
252b5132
RH
1135 break;
1136 case 'V':
1137 ldversion (1);
b34976b6 1138 version_printed = TRUE;
252b5132
RH
1139 break;
1140 case OPTION_VERSION:
d32820f2 1141 ldversion (2);
252b5132
RH
1142 xexit (0);
1143 break;
1144 case OPTION_VERSION_SCRIPT:
1145 /* This option indicates a small script that only specifies
1579bae1
AM
1146 version information. Read it, but don't assume that
1147 we've seen a linker script. */
252b5132 1148 {
b7a26f91 1149 FILE *hold_script_handle;
252b5132 1150
b9a8de1e 1151 hold_script_handle = saved_script_handle;
252b5132 1152 ldfile_open_command_file (optarg);
b9a8de1e 1153 saved_script_handle = hold_script_handle;
252b5132
RH
1154 parser_input = input_version_script;
1155 yyparse ();
1156 }
1157 break;
1158 case OPTION_VERSION_EXPORTS_SECTION:
1159 /* This option records a version symbol to be applied to the
1160 symbols listed for export to be found in the object files
1161 .exports sections. */
1162 command_line.version_exports_section = optarg;
1163 break;
1164 case OPTION_WARN_COMMON:
b34976b6 1165 config.warn_common = TRUE;
252b5132
RH
1166 break;
1167 case OPTION_WARN_CONSTRUCTORS:
b34976b6 1168 config.warn_constructors = TRUE;
252b5132 1169 break;
7ce691ae 1170 case OPTION_WARN_FATAL:
b34976b6 1171 config.fatal_warnings = TRUE;
7ce691ae 1172 break;
252b5132 1173 case OPTION_WARN_MULTIPLE_GP:
b34976b6 1174 config.warn_multiple_gp = TRUE;
252b5132
RH
1175 break;
1176 case OPTION_WARN_ONCE:
b34976b6 1177 config.warn_once = TRUE;
252b5132
RH
1178 break;
1179 case OPTION_WARN_SECTION_ALIGN:
b34976b6 1180 config.warn_section_align = TRUE;
252b5132
RH
1181 break;
1182 case OPTION_WHOLE_ARCHIVE:
b34976b6 1183 whole_archive = TRUE;
252b5132
RH
1184 break;
1185 case OPTION_WRAP:
1186 add_wrap (optarg);
1187 break;
f5fa8ca2
JJ
1188 case OPTION_DISCARD_NONE:
1189 link_info.discard = discard_none;
1190 break;
252b5132
RH
1191 case 'X':
1192 link_info.discard = discard_l;
1193 break;
1194 case 'x':
1195 link_info.discard = discard_all;
1196 break;
1197 case 'Y':
1198 if (strncmp (optarg, "P,", 2) == 0)
1199 optarg += 2;
5ed6aba4
NC
1200 if (default_dirlist != NULL)
1201 free (default_dirlist);
252b5132
RH
1202 default_dirlist = xstrdup (optarg);
1203 break;
1204 case 'y':
1205 add_ysym (optarg);
1206 break;
db6751f2
JJ
1207 case OPTION_SPARE_DYNAMIC_TAGS:
1208 link_info.spare_dynamic_tags = strtoul (optarg, NULL, 0);
1209 break;
252b5132 1210 case OPTION_SPLIT_BY_RELOC:
a854a4a7
AM
1211 if (optarg != NULL)
1212 config.split_by_reloc = strtoul (optarg, NULL, 0);
1213 else
1214 config.split_by_reloc = 32768;
5cc18311 1215 break;
252b5132 1216 case OPTION_SPLIT_BY_FILE:
a854a4a7
AM
1217 if (optarg != NULL)
1218 config.split_by_file = bfd_scan_vma (optarg, NULL, 0);
1219 else
1220 config.split_by_file = 1;
5cc18311 1221 break;
252b5132 1222 case OPTION_CHECK_SECTIONS:
b34976b6 1223 command_line.check_section_addresses = TRUE;
252b5132
RH
1224 break;
1225 case OPTION_NO_CHECK_SECTIONS:
b34976b6 1226 command_line.check_section_addresses = FALSE;
252b5132 1227 break;
312b768e
NC
1228 case OPTION_ACCEPT_UNKNOWN_INPUT_ARCH:
1229 command_line.accept_unknown_input_arch = TRUE;
1230 break;
1231 case OPTION_NO_ACCEPT_UNKNOWN_INPUT_ARCH:
1232 command_line.accept_unknown_input_arch = FALSE;
1233 break;
252b5132
RH
1234 case '(':
1235 if (ingroup)
210a7bd1
NC
1236 einfo (_("%P%F: may not nest groups (--help for usage)\n"));
1237
252b5132
RH
1238 lang_enter_group ();
1239 ingroup = 1;
1240 break;
1241 case ')':
1242 if (! ingroup)
210a7bd1
NC
1243 einfo (_("%P%F: group ended before it began (--help for usage)\n"));
1244
252b5132
RH
1245 lang_leave_group ();
1246 ingroup = 0;
1247 break;
e47d05ad 1248 case OPTION_MPC860C0:
210a7bd1
NC
1249 /* Default value (in bytes). */
1250 link_info.mpc860c0 = 20;
e47d05ad
KH
1251 if (optarg)
1252 {
1253 unsigned words;
252b5132 1254
e47d05ad
KH
1255 words = is_num (optarg, 1, 10, 0);
1256 if (words == 0)
210a7bd1
NC
1257 einfo (_("%P%F: invalid argument to option \"mpc860c0\"\n"));
1258
1259 /* Convert words to bytes. */
1260 link_info.mpc860c0 = words * 4;
e47d05ad 1261 }
b34976b6 1262 command_line.relax = TRUE;
e47d05ad 1263 break;
3dbf70a2
MM
1264
1265 case OPTION_INIT:
1266 link_info.init_function = optarg;
1267 break;
5cc18311 1268
3dbf70a2
MM
1269 case OPTION_FINI:
1270 link_info.fini_function = optarg;
1271 break;
252b5132
RH
1272 }
1273 }
1274
1275 if (ingroup)
1276 lang_leave_group ();
1277
1278 if (default_dirlist != NULL)
5ed6aba4
NC
1279 {
1280 set_default_dirlist (default_dirlist);
1281 free (default_dirlist);
1282 }
560e09e9
NC
1283
1284 if (link_info.unresolved_syms_in_objects == RM_NOT_YET_SET)
1285 /* FIXME: Should we allow emulations a chance to set this ? */
1286 link_info.unresolved_syms_in_objects = how_to_report_unresolved_symbols;
1287
1288 if (link_info.unresolved_syms_in_shared_libs == RM_NOT_YET_SET)
1289 /* FIXME: Should we allow emulations a chance to set this ? */
1290 link_info.unresolved_syms_in_shared_libs = how_to_report_unresolved_symbols;
252b5132
RH
1291}
1292
1293/* Add the (colon-separated) elements of DIRLIST_PTR to the
1294 library search path. */
1295
1296static void
1579bae1 1297set_default_dirlist (char *dirlist_ptr)
252b5132
RH
1298{
1299 char *p;
1300
1301 while (1)
1302 {
1303 p = strchr (dirlist_ptr, PATH_SEPARATOR);
1304 if (p != NULL)
1305 *p = '\0';
1306 if (*dirlist_ptr != '\0')
b34976b6 1307 ldfile_add_library_path (dirlist_ptr, TRUE);
252b5132
RH
1308 if (p == NULL)
1309 break;
1310 dirlist_ptr = p + 1;
1311 }
1312}
1313
1314static void
1579bae1 1315set_section_start (char *sect, char *valstr)
252b5132 1316{
93697284
AM
1317 const char *end;
1318 bfd_vma val = bfd_scan_vma (valstr, &end, 16);
252b5132
RH
1319 if (*end)
1320 einfo (_("%P%F: invalid hex number `%s'\n"), valstr);
1321 lang_section_start (sect, exp_intop (val));
1322}
1323\f
1324/* Print help messages for the options. */
1325
1326static void
1579bae1 1327help (void)
252b5132 1328{
e4897a32 1329 unsigned i;
252b5132
RH
1330 const char **targets, **pp;
1331
1332 printf (_("Usage: %s [options] file...\n"), program_name);
1333
1334 printf (_("Options:\n"));
1335 for (i = 0; i < OPTION_COUNT; i++)
1336 {
1337 if (ld_options[i].doc != NULL)
1338 {
b34976b6 1339 bfd_boolean comma;
252b5132 1340 int len;
e4897a32 1341 unsigned j;
252b5132
RH
1342
1343 printf (" ");
1344
b34976b6 1345 comma = FALSE;
252b5132
RH
1346 len = 2;
1347
1348 j = i;
1349 do
1350 {
1351 if (ld_options[j].shortopt != '\0'
1352 && ld_options[j].control != NO_HELP)
1353 {
1354 printf ("%s-%c", comma ? ", " : "", ld_options[j].shortopt);
1355 len += (comma ? 2 : 0) + 2;
1356 if (ld_options[j].arg != NULL)
1357 {
1358 if (ld_options[j].opt.has_arg != optional_argument)
1359 {
1360 printf (" ");
1361 ++len;
1362 }
1363 printf ("%s", _(ld_options[j].arg));
1364 len += strlen (_(ld_options[j].arg));
1365 }
b34976b6 1366 comma = TRUE;
252b5132
RH
1367 }
1368 ++j;
1369 }
1370 while (j < OPTION_COUNT && ld_options[j].doc == NULL);
1371
1372 j = i;
1373 do
1374 {
1375 if (ld_options[j].opt.name != NULL
1376 && ld_options[j].control != NO_HELP)
1377 {
e4897a32
NC
1378 int two_dashes =
1379 (ld_options[j].control == TWO_DASHES
1380 || ld_options[j].control == EXACTLY_TWO_DASHES);
b7a26f91 1381
252b5132
RH
1382 printf ("%s-%s%s",
1383 comma ? ", " : "",
e4897a32 1384 two_dashes ? "-" : "",
252b5132
RH
1385 ld_options[j].opt.name);
1386 len += ((comma ? 2 : 0)
1387 + 1
e4897a32 1388 + (two_dashes ? 1 : 0)
252b5132
RH
1389 + strlen (ld_options[j].opt.name));
1390 if (ld_options[j].arg != NULL)
1391 {
1392 printf (" %s", _(ld_options[j].arg));
1393 len += 1 + strlen (_(ld_options[j].arg));
1394 }
b34976b6 1395 comma = TRUE;
252b5132
RH
1396 }
1397 ++j;
1398 }
1399 while (j < OPTION_COUNT && ld_options[j].doc == NULL);
1400
1401 if (len >= 30)
1402 {
1403 printf ("\n");
1404 len = 0;
1405 }
1406
1407 for (; len < 30; len++)
1408 putchar (' ');
1409
1410 printf ("%s\n", _(ld_options[i].doc));
1411 }
1412 }
1413
f75692fe
NC
1414 /* Note: Various tools (such as libtool) depend upon the
1415 format of the listings below - do not change them. */
252b5132 1416 /* xgettext:c-format */
f75692fe 1417 printf (_("%s: supported targets:"), program_name);
252b5132
RH
1418 targets = bfd_target_list ();
1419 for (pp = targets; *pp != NULL; pp++)
1420 printf (" %s", *pp);
1421 free (targets);
1422 printf ("\n");
1423
1424 /* xgettext:c-format */
f75692fe 1425 printf (_("%s: supported emulations: "), program_name);
252b5132
RH
1426 ldemul_list_emulations (stdout);
1427 printf ("\n");
1428
1429 /* xgettext:c-format */
1430 printf (_("%s: emulation specific options:\n"), program_name);
1431 ldemul_list_emulation_options (stdout);
1432 printf ("\n");
c20f4f8c 1433
8ad3436c 1434 printf (_("Report bugs to %s\n"), REPORT_BUGS_TO);
252b5132 1435}
This page took 0.322507 seconds and 4 git commands to generate.