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