1 /* Parse options for the GNU linker.
2 Copyright (C) 1991, 92, 93, 94, 1995 Free Software Foundation, Inc.
4 This file is part of GLD, the Gnu Linker.
6 GLD is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GLD is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GLD; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
38 /* Somewhere above, sys/stat.h got included . . . . */
39 #if !defined(S_ISDIR) && defined(S_IFDIR)
40 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
43 /* Omit args to avoid the possibility of clashing with a system header
44 that might disagree about consts. */
45 unsigned long strtoul ();
47 static void set_default_dirlist PARAMS ((char *dirlist_ptr));
48 static void set_section_start PARAMS ((char *sect, char *valstr));
50 /* Non-zero if we are processing a --defsym from the command line. */
51 int parsing_defsym = 0;
54 parse_args (argc, argv)
60 char *default_dirlist = NULL;
62 /* Starting the short option string with '-' is for programs that
63 expect options and other ARGV-elements in any order and that care about
64 the ordering of the two. We describe each non-option ARGV-element
65 as if it were the argument of an option with character code 1. */
67 const char *shortopts =
68 "-a:A:b:c:de:F::G:gh:iL:l:Mm:NnO:o:R:rSsT:tu:VvXxY:y:z:()";
70 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
72 #define OPTION_ASSERT 150
73 #define OPTION_CALL_SHARED (OPTION_ASSERT + 1)
74 #define OPTION_DEFSYM (OPTION_CALL_SHARED + 1)
75 #define OPTION_DYNAMIC_LINKER (OPTION_DEFSYM + 1)
76 #define OPTION_EB (OPTION_DYNAMIC_LINKER + 1)
77 #define OPTION_EL (OPTION_EB + 1)
78 #define OPTION_EMBEDDED_RELOCS (OPTION_EL + 1)
79 #define OPTION_EXPORT_DYNAMIC (OPTION_EMBEDDED_RELOCS + 1)
80 #define OPTION_HELP (OPTION_EXPORT_DYNAMIC + 1)
81 #define OPTION_IGNORE (OPTION_HELP + 1)
82 #define OPTION_MAP (OPTION_IGNORE + 1)
83 #define OPTION_NO_KEEP_MEMORY (OPTION_MAP + 1)
84 #define OPTION_NOINHIBIT_EXEC (OPTION_NO_KEEP_MEMORY + 1)
85 #define OPTION_NON_SHARED (OPTION_NOINHIBIT_EXEC + 1)
86 #define OPTION_NO_WHOLE_ARCHIVE (OPTION_NON_SHARED + 1)
87 #define OPTION_OFORMAT (OPTION_NO_WHOLE_ARCHIVE + 1)
88 #define OPTION_RELAX (OPTION_OFORMAT + 1)
89 #define OPTION_RETAIN_SYMBOLS_FILE (OPTION_RELAX + 1)
90 #define OPTION_RPATH (OPTION_RETAIN_SYMBOLS_FILE + 1)
91 #define OPTION_RPATH_LINK (OPTION_RPATH + 1)
92 #define OPTION_SHARED (OPTION_RPATH_LINK + 1)
93 #define OPTION_SONAME (OPTION_SHARED + 1)
94 #define OPTION_SORT_COMMON (OPTION_SONAME + 1)
95 #define OPTION_STATS (OPTION_SORT_COMMON + 1)
96 #define OPTION_SYMBOLIC (OPTION_STATS + 1)
97 #define OPTION_TBSS (OPTION_SYMBOLIC + 1)
98 #define OPTION_TDATA (OPTION_TBSS + 1)
99 #define OPTION_TTEXT (OPTION_TDATA + 1)
100 #define OPTION_TRADITIONAL_FORMAT (OPTION_TTEXT + 1)
101 #define OPTION_UR (OPTION_TRADITIONAL_FORMAT + 1)
102 #define OPTION_VERBOSE (OPTION_UR + 1)
103 #define OPTION_VERSION (OPTION_VERBOSE + 1)
104 #define OPTION_WARN_COMMON (OPTION_VERSION + 1)
105 #define OPTION_WARN_CONSTRUCTORS (OPTION_WARN_COMMON + 1)
106 #define OPTION_WARN_ONCE (OPTION_WARN_CONSTRUCTORS + 1)
107 #define OPTION_SPLIT_BY_RELOC (OPTION_WARN_ONCE + 1)
108 #define OPTION_SPLIT_BY_FILE (OPTION_SPLIT_BY_RELOC + 1)
109 #define OPTION_WHOLE_ARCHIVE (OPTION_SPLIT_BY_FILE + 1)
112 static struct option longopts[] = {
113 /* Sorted alphabeticaly, except for the PE options grouped at the end. */
114 {"assert", required_argument, NULL, OPTION_ASSERT},
115 {"Bdynamic", no_argument, NULL, OPTION_CALL_SHARED},
116 {"Bstatic", no_argument, NULL, OPTION_NON_SHARED},
117 {"Bsymbolic", no_argument, NULL, OPTION_SYMBOLIC},
118 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
119 {"dc", no_argument, NULL, 'd'},
120 {"defsym", required_argument, NULL, OPTION_DEFSYM},
121 {"dll-verbose", no_argument, NULL, OPTION_VERSION}, /* Linux. */
122 {"dn", no_argument, NULL, OPTION_NON_SHARED},
123 {"dp", no_argument, NULL, 'd'},
124 {"dy", no_argument, NULL, OPTION_CALL_SHARED},
125 {"dynamic-linker", required_argument, NULL, OPTION_DYNAMIC_LINKER},
126 {"EB", no_argument, NULL, OPTION_EB},
127 {"EL", no_argument, NULL, OPTION_EL},
128 {"embedded-relocs", no_argument, NULL, OPTION_EMBEDDED_RELOCS},
129 {"end-group", no_argument, NULL, ')'},
130 {"export-dynamic", no_argument, NULL, OPTION_EXPORT_DYNAMIC},
131 {"format", required_argument, NULL, 'b'},
132 {"help", no_argument, NULL, OPTION_HELP},
133 {"Map", required_argument, NULL, OPTION_MAP},
134 {"no-keep-memory", no_argument, NULL, OPTION_NO_KEEP_MEMORY},
135 {"no-whole-archive", no_argument, NULL, OPTION_NO_WHOLE_ARCHIVE},
136 {"noinhibit-exec", no_argument, NULL, OPTION_NOINHIBIT_EXEC},
137 {"noinhibit_exec", no_argument, NULL, OPTION_NOINHIBIT_EXEC},
138 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
139 {"oformat", required_argument, NULL, OPTION_OFORMAT},
140 {"Qy", no_argument, NULL, OPTION_IGNORE},
141 {"qmagic", no_argument, NULL, OPTION_IGNORE}, /* Linux compatibility. */
142 {"relax", no_argument, NULL, OPTION_RELAX},
143 {"retain-symbols-file", required_argument, NULL, OPTION_RETAIN_SYMBOLS_FILE},
144 {"rpath", required_argument, NULL, OPTION_RPATH},
145 {"rpath-link", required_argument, NULL, OPTION_RPATH_LINK},
146 {"shared", no_argument, NULL, OPTION_SHARED},
147 {"soname", required_argument, NULL, OPTION_SONAME},
148 {"sort-common", no_argument, NULL, OPTION_SORT_COMMON},
149 {"sort_common", no_argument, NULL, OPTION_SORT_COMMON},
150 {"start-group", no_argument, NULL, '('},
151 {"stats", no_argument, NULL, OPTION_STATS},
152 {"static", no_argument, NULL, OPTION_NON_SHARED},
153 {"Tbss", required_argument, NULL, OPTION_TBSS},
154 {"Tdata", required_argument, NULL, OPTION_TDATA},
155 {"Ttext", required_argument, NULL, OPTION_TTEXT},
156 {"traditional-format", no_argument, NULL, OPTION_TRADITIONAL_FORMAT},
157 {"Ur", no_argument, NULL, OPTION_UR},
158 {"verbose", no_argument, NULL, OPTION_VERBOSE},
159 {"version", no_argument, NULL, OPTION_VERSION},
160 {"warn-common", no_argument, NULL, OPTION_WARN_COMMON},
161 {"warn-constructors", no_argument, NULL, OPTION_WARN_CONSTRUCTORS},
162 {"warn-once", no_argument, NULL, OPTION_WARN_ONCE},
163 {"split-by-reloc", required_argument, NULL, OPTION_SPLIT_BY_RELOC},
164 {"split-by-file", no_argument, NULL, OPTION_SPLIT_BY_FILE},
165 {"whole-archive", no_argument, NULL, OPTION_WHOLE_ARCHIVE},
168 {NULL, no_argument, NULL, 0}
171 /* The -G option is ambiguous on different platforms. Sometimes it
172 specifies the largest data size to put into the small data
173 section. Sometimes it is equivalent to --shared. Unfortunately,
174 the first form takes an argument, while the second does not.
176 We need to permit the --shared form because on some platforms,
177 such as Solaris, gcc -shared will pass -G to the linker.
179 To permit either usage, we look through the argument list. If we
180 find -G not followed by a number, we change it into --shared.
181 This will work for most normal cases. */
182 for (i = 1; i < argc; i++)
183 if (strcmp (argv[i], "-G") == 0
185 || ! isdigit (argv[i + 1][0])))
186 argv[i] = (char *) "--shared";
190 /* getopt_long_only is like getopt_long, but '-' as well as '--' can
191 indicate a long option. */
195 if (ldemul_parse_args (argc, argv))
198 optc = getopt_long_only (argc, argv, shortopts, longopts, &longind);
206 case 1: /* File name. */
207 lang_add_input_file (optarg, lang_input_file_is_file_enum,
214 /* For HP/UX compatibility. Actually -a shared should mean
215 ``use only shared libraries'' but, then, we don't
216 currently support shared libraries on HP/UX anyhow. */
217 if (strcmp (optarg, "archive") == 0)
218 config.dynamic_link = false;
219 else if (strcmp (optarg, "shared") == 0
220 || strcmp (optarg, "default") == 0)
221 config.dynamic_link = true;
223 einfo ("%P%F: unrecognized -a option `%s'\n", optarg);
226 /* FIXME: We just ignore these, but we should handle them. */
227 if (strcmp (optarg, "definitions") == 0)
229 else if (strcmp (optarg, "nodefinitions") == 0)
231 else if (strcmp (optarg, "nosymbolic") == 0)
233 else if (strcmp (optarg, "pure-text") == 0)
236 einfo ("%P%F: unrecognized -assert option `%s'\n", optarg);
239 ldfile_add_arch (optarg);
242 lang_add_target (optarg);
245 ldfile_open_command_file (optarg);
246 parser_input = input_mri_script;
249 case OPTION_CALL_SHARED:
250 config.dynamic_link = true;
252 case OPTION_NON_SHARED:
253 config.dynamic_link = false;
256 command_line.force_common_definition = true;
260 lex_redirect (optarg);
261 parser_input = input_defsym;
267 case OPTION_DYNAMIC_LINKER:
268 command_line.interpreter = optarg;
271 command_line.endian = ENDIAN_BIG;
274 command_line.endian = ENDIAN_LITTLE;
276 case OPTION_EMBEDDED_RELOCS:
277 command_line.embedded_relocs = true;
279 case OPTION_EXPORT_DYNAMIC:
280 command_line.export_dynamic = true;
283 lang_add_entry (optarg, true);
291 g_switch_value = strtoul (optarg, &end, 0);
293 einfo ("%P%F: invalid number `%s'\n", optarg);
304 ldfile_add_library_path (optarg, true);
307 lang_add_input_file (optarg, lang_input_file_is_l_enum,
311 config.map_filename = "-";
314 /* Ignore. Was handled in a pre-parse. */
317 config.map_filename = optarg;
320 config.text_read_only = false;
321 config.magic_demand_paged = false;
322 config.dynamic_link = false;
325 config.magic_demand_paged = false;
326 config.dynamic_link = false;
328 case OPTION_NO_KEEP_MEMORY:
329 link_info.keep_memory = false;
331 case OPTION_NOINHIBIT_EXEC:
332 force_make_executable = true;
334 case OPTION_NO_WHOLE_ARCHIVE:
335 whole_archive = false;
338 /* FIXME "-O<non-digits> <value>" used to set the address of
339 section <non-digits>. Was this for compatibility with
340 something, or can we create a new option to do that
341 (with a syntax similar to -defsym)?
342 getopt can't handle two args to an option without kludges. */
345 lang_add_output (optarg, 0);
348 lang_add_output_format (optarg, (char *) NULL, (char *) NULL, 0);
352 link_info.relocateable = true;
353 config.build_constructors = false;
354 config.magic_demand_paged = false;
355 config.text_read_only = false;
356 config.dynamic_link = false;
359 /* The GNU linker traditionally uses -R to mean to include
360 only the symbols from a file. The Solaris linker uses -R
361 to set the path used by the runtime linker to find
362 libraries. This is the GNU linker -rpath argument. We
363 try to support both simultaneously by checking the file
364 named. If it is a directory, rather than a regular file,
365 we assume -rpath was meant. */
369 if (stat (optarg, &s) >= 0
370 && ! S_ISDIR (s.st_mode))
372 lang_add_input_file (optarg,
373 lang_input_file_is_symbols_only_enum,
380 if (command_line.rpath == NULL)
381 command_line.rpath = buystring (optarg);
386 buf = xmalloc (strlen (command_line.rpath)
389 sprintf (buf, "%s:%s", command_line.rpath, optarg);
390 free (command_line.rpath);
391 command_line.rpath = buf;
394 case OPTION_RPATH_LINK:
395 if (command_line.rpath_link == NULL)
396 command_line.rpath_link = buystring (optarg);
401 buf = xmalloc (strlen (command_line.rpath_link)
404 sprintf (buf, "%s:%s", command_line.rpath_link, optarg);
405 free (command_line.rpath_link);
406 command_line.rpath_link = buf;
410 command_line.relax = true;
412 case OPTION_RETAIN_SYMBOLS_FILE:
413 add_keepsyms_file (optarg);
416 link_info.strip = strip_debugger;
419 link_info.strip = strip_all;
422 link_info.shared = true;
424 case 'h': /* Used on Solaris. */
426 command_line.soname = optarg;
428 case OPTION_SORT_COMMON:
429 config.sort_common = true;
434 case OPTION_SYMBOLIC:
435 link_info.symbolic = true;
441 ldfile_open_command_file (optarg);
442 parser_input = input_script;
446 set_section_start (".bss", optarg);
449 set_section_start (".data", optarg);
452 set_section_start (".text", optarg);
454 case OPTION_TRADITIONAL_FORMAT:
455 config.traditional_format = true;
458 link_info.relocateable = true;
459 config.build_constructors = true;
460 config.magic_demand_paged = false;
461 config.text_read_only = false;
462 config.dynamic_link = false;
465 ldlang_add_undef (optarg);
469 version_printed = true;
470 trace_file_tries = true;
474 version_printed = true;
478 version_printed = true;
482 version_printed = true;
484 case OPTION_WARN_COMMON:
485 config.warn_common = true;
487 case OPTION_WARN_CONSTRUCTORS:
488 config.warn_constructors = true;
490 case OPTION_WARN_ONCE:
491 config.warn_once = true;
493 case OPTION_WHOLE_ARCHIVE:
494 whole_archive = true;
497 link_info.discard = discard_l;
500 link_info.discard = discard_all;
503 if (strncmp (optarg, "P,", 2) == 0)
505 default_dirlist = optarg;
511 /* We accept and ignore this option for Solaris
512 compatibility. Actually, on Solaris, optarg is not
513 ignored. Someday we should handle it correctly. FIXME. */
515 case OPTION_SPLIT_BY_RELOC:
516 config.split_by_reloc = atoi (optarg);
518 case OPTION_SPLIT_BY_FILE:
519 config.split_by_file = true;
525 "%s: may not nest groups (--help for usage)\n",
536 "%s: group ended before it began (--help for usage)\n",
550 if (default_dirlist != NULL)
551 set_default_dirlist (default_dirlist);
555 /* Add the (colon-separated) elements of DIRLIST_PTR to the
556 library search path. */
559 set_default_dirlist (dirlist_ptr)
566 p = strchr (dirlist_ptr, ':');
570 ldfile_add_library_path (dirlist_ptr, true);
579 set_section_start (sect, valstr)
583 unsigned long val = strtoul (valstr, &end, 16);
585 einfo ("%P%F: invalid hex number `%s'\n", valstr);
586 lang_section_start (sect, exp_intop (val));