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