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 /* PE format; declare additional functions */
51 static void set_pe_subsystem PARAMS ((void));
52 static void set_pe_stack_heap PARAMS ((bfd_link_pe_info_dval *reserve,
53 bfd_link_pe_info_dval *commit));
54 static void set_pe_value PARAMS ((bfd_link_pe_info_dval *val));
56 /* PE info; passed to bfd if any pe options set. */
57 static bfd_link_pe_info pe_info;
61 parse_args (argc, argv)
68 /* Starting the short option string with '-' is for programs that
69 expect options and other ARGV-elements in any order and that care about
70 the ordering of the two. We describe each non-option ARGV-element
71 as if it were the argument of an option with character code 1. */
73 const char *shortopts =
74 "-a:A:B::b:c:de:F::G:gh:iL:l:Mm:NnO:o:R:rSsT:tu:VvXxY:y:z:()";
76 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
78 #define OPTION_CALL_SHARED 150
79 #define OPTION_DEFSYM (OPTION_CALL_SHARED + 1)
80 #define OPTION_DYNAMIC_LINKER (OPTION_DEFSYM + 1)
81 #define OPTION_EB (OPTION_DYNAMIC_LINKER + 1)
82 #define OPTION_EL (OPTION_EB + 1)
83 #define OPTION_HEAP (OPTION_EL + 1)
84 #define OPTION_EMBEDDED_RELOCS (OPTION_HEAP + 1)
85 #define OPTION_EXPORT_DYNAMIC (OPTION_EMBEDDED_RELOCS + 1)
86 #define OPTION_HELP (OPTION_EXPORT_DYNAMIC + 1)
87 #define OPTION_IGNORE (OPTION_HELP + 1)
88 #define OPTION_MAP (OPTION_IGNORE + 1)
89 #define OPTION_NO_KEEP_MEMORY (OPTION_MAP + 1)
90 #define OPTION_NOINHIBIT_EXEC (OPTION_NO_KEEP_MEMORY + 1)
91 #define OPTION_NON_SHARED (OPTION_NOINHIBIT_EXEC + 1)
92 #define OPTION_OFORMAT (OPTION_NON_SHARED + 1)
93 #define OPTION_RELAX (OPTION_OFORMAT + 1)
94 #define OPTION_RETAIN_SYMBOLS_FILE (OPTION_RELAX + 1)
95 #define OPTION_RPATH (OPTION_RETAIN_SYMBOLS_FILE + 1)
96 #define OPTION_SHARED (OPTION_RPATH + 1)
97 #define OPTION_SONAME (OPTION_SHARED + 1)
98 #define OPTION_SORT_COMMON (OPTION_SONAME + 1)
99 #define OPTION_STATS (OPTION_SORT_COMMON + 1)
100 #define OPTION_SYMBOLIC (OPTION_STATS + 1)
101 #define OPTION_TBSS (OPTION_SYMBOLIC + 1)
102 #define OPTION_TDATA (OPTION_TBSS + 1)
103 #define OPTION_TTEXT (OPTION_TDATA + 1)
104 #define OPTION_TRADITIONAL_FORMAT (OPTION_TTEXT + 1)
105 #define OPTION_UR (OPTION_TRADITIONAL_FORMAT + 1)
106 #define OPTION_VERBOSE (OPTION_UR + 1)
107 #define OPTION_VERSION (OPTION_VERBOSE + 1)
108 #define OPTION_WARN_COMMON (OPTION_VERSION + 1)
109 #define OPTION_WARN_CONSTRUCTORS (OPTION_WARN_COMMON + 1)
110 #define OPTION_WARN_ONCE (OPTION_WARN_CONSTRUCTORS + 1)
111 #define OPTION_SPLIT_BY_RELOC (OPTION_WARN_ONCE + 1)
112 #define OPTION_SPLIT_BY_FILE (OPTION_SPLIT_BY_RELOC + 1)
113 #define OPTION_WHOLE_ARCHIVE (OPTION_SPLIT_BY_FILE + 1)
115 /* Used for setting flags in the PE header. */
116 #define OPTION_BASE_FILE (OPTION_WHOLE_ARCHIVE + 1)
117 #define OPTION_DLL (OPTION_BASE_FILE + 1)
118 #define OPTION_FILE_ALIGNMENT (OPTION_DLL + 1)
119 #define OPTION_IMAGE_BASE (OPTION_FILE_ALIGNMENT + 1)
120 #define OPTION_MAJOR_IMAGE_VERSION (OPTION_IMAGE_BASE + 1)
121 #define OPTION_MAJOR_OS_VERSION (OPTION_MAJOR_IMAGE_VERSION + 1)
122 #define OPTION_MAJOR_SUBSYSTEM_VERSION (OPTION_MAJOR_OS_VERSION + 1)
123 #define OPTION_MINOR_IMAGE_VERSION (OPTION_MAJOR_SUBSYSTEM_VERSION + 1)
124 #define OPTION_MINOR_OS_VERSION (OPTION_MINOR_IMAGE_VERSION + 1)
125 #define OPTION_MINOR_SUBSYSTEM_VERSION (OPTION_MINOR_OS_VERSION + 1)
126 #define OPTION_SECTION_ALIGNMENT (OPTION_MINOR_SUBSYSTEM_VERSION + 1)
127 #define OPTION_STACK (OPTION_SECTION_ALIGNMENT + 1)
128 #define OPTION_SUBSYSTEM (OPTION_STACK + 1)
130 static struct option longopts[] = {
131 /* Sorted alphabeticaly, except for the PE options grouped at the end. */
132 {"Bdynamic", no_argument, NULL, OPTION_CALL_SHARED},
133 {"Bstatic", no_argument, NULL, OPTION_NON_SHARED},
134 {"Bsymbolic", no_argument, NULL, OPTION_SYMBOLIC},
135 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
136 {"dc", no_argument, NULL, 'd'},
137 {"defsym", required_argument, NULL, OPTION_DEFSYM},
138 {"dll-verbose", no_argument, NULL, OPTION_VERSION}, /* Linux. */
139 {"dn", no_argument, NULL, OPTION_NON_SHARED},
140 {"dp", no_argument, NULL, 'd'},
141 {"dy", no_argument, NULL, OPTION_CALL_SHARED},
142 {"dynamic-linker", required_argument, NULL, OPTION_DYNAMIC_LINKER},
143 {"EB", no_argument, NULL, OPTION_EB},
144 {"EL", no_argument, NULL, OPTION_EL},
145 {"embedded-relocs", no_argument, NULL, OPTION_EMBEDDED_RELOCS},
146 {"end-group", no_argument, NULL, ')'},
147 {"export-dynamic", no_argument, NULL, OPTION_EXPORT_DYNAMIC},
148 {"format", required_argument, NULL, 'b'},
149 {"help", no_argument, NULL, OPTION_HELP},
150 {"Map", required_argument, NULL, OPTION_MAP},
151 {"no-keep-memory", no_argument, NULL, OPTION_NO_KEEP_MEMORY},
152 {"noinhibit-exec", no_argument, NULL, OPTION_NOINHIBIT_EXEC},
153 {"noinhibit_exec", no_argument, NULL, OPTION_NOINHIBIT_EXEC},
154 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
155 {"oformat", required_argument, NULL, OPTION_OFORMAT},
156 {"Qy", no_argument, NULL, OPTION_IGNORE},
157 {"qmagic", no_argument, NULL, OPTION_IGNORE}, /* Linux compatibility. */
158 {"relax", no_argument, NULL, OPTION_RELAX},
159 {"retain-symbols-file", required_argument, NULL, OPTION_RETAIN_SYMBOLS_FILE},
160 {"rpath", required_argument, NULL, OPTION_RPATH},
161 {"shared", no_argument, NULL, OPTION_SHARED},
162 {"soname", required_argument, NULL, OPTION_SONAME},
163 {"sort-common", no_argument, NULL, OPTION_SORT_COMMON},
164 {"sort_common", no_argument, NULL, OPTION_SORT_COMMON},
165 {"start-group", no_argument, NULL, '('},
166 {"stats", no_argument, NULL, OPTION_STATS},
167 {"static", no_argument, NULL, OPTION_NON_SHARED},
168 {"Tbss", required_argument, NULL, OPTION_TBSS},
169 {"Tdata", required_argument, NULL, OPTION_TDATA},
170 {"Ttext", required_argument, NULL, OPTION_TTEXT},
171 {"traditional-format", no_argument, NULL, OPTION_TRADITIONAL_FORMAT},
172 {"Ur", no_argument, NULL, OPTION_UR},
173 {"verbose", no_argument, NULL, OPTION_VERBOSE},
174 {"version", no_argument, NULL, OPTION_VERSION},
175 {"warn-common", no_argument, NULL, OPTION_WARN_COMMON},
176 {"warn-constructors", no_argument, NULL, OPTION_WARN_CONSTRUCTORS},
177 {"warn-once", no_argument, NULL, OPTION_WARN_ONCE},
178 {"split-by-reloc", required_argument, NULL, OPTION_SPLIT_BY_RELOC},
179 {"split-by-file", no_argument, NULL, OPTION_SPLIT_BY_FILE},
180 {"whole-archive", no_argument, NULL, OPTION_WHOLE_ARCHIVE},
183 {"base-file", required_argument, NULL, OPTION_BASE_FILE},
184 {"dll", no_argument, NULL, OPTION_DLL},
185 {"file-alignment", required_argument, NULL, OPTION_FILE_ALIGNMENT},
186 {"heap", required_argument, NULL, OPTION_HEAP},
187 {"image-base", required_argument, NULL, OPTION_IMAGE_BASE},
188 {"major-image-version", required_argument, NULL, OPTION_MAJOR_IMAGE_VERSION},
189 {"major-os-version", required_argument, NULL, OPTION_MAJOR_OS_VERSION},
190 {"major-subsystem-version", required_argument, NULL, OPTION_MAJOR_SUBSYSTEM_VERSION},
191 {"minor-image-version", required_argument, NULL, OPTION_MINOR_IMAGE_VERSION},
192 {"minor-os-version", required_argument, NULL, OPTION_MINOR_OS_VERSION},
193 {"minor-subsystem-version", required_argument, NULL, OPTION_MINOR_SUBSYSTEM_VERSION},
194 {"section-alignment", required_argument, NULL, OPTION_SECTION_ALIGNMENT},
195 {"stack", required_argument, NULL, OPTION_STACK},
196 {"subsystem", required_argument, NULL, OPTION_SUBSYSTEM},
198 {NULL, no_argument, NULL, 0}
201 /* The -G option is ambiguous on different platforms. Sometimes it
202 specifies the largest data size to put into the small data
203 section. Sometimes it is equivalent to --shared. Unfortunately,
204 the first form takes an argument, while the second does not.
206 We need to permit the --shared form because on some platforms,
207 such as Solaris, gcc -shared will pass -G to the linker.
209 To permit either usage, we look through the argument list. If we
210 find -G not followed by a number, we change it into --shared.
211 This will work for most normal cases. */
212 for (i = 1; i < argc; i++)
213 if (strcmp (argv[i], "-G") == 0
215 || ! isdigit (argv[i + 1][0])))
216 argv[i] = (char *) "--shared";
220 /* getopt_long_only is like getopt_long, but '-' as well as '--' can
221 indicate a long option. */
223 int optc = getopt_long_only (argc, argv, shortopts, longopts, &longind);
232 case 1: /* File name. */
233 lang_add_input_file (optarg, lang_input_file_is_file_enum,
240 /* For HP/UX compatibility. Actually -a shared should mean
241 ``use only shared libraries'' but, then, we don't
242 currently support shared libraries on HP/UX anyhow. */
243 if (strcmp (optarg, "archive") == 0)
244 config.dynamic_link = false;
245 else if (strcmp (optarg, "shared") == 0
246 || strcmp (optarg, "default") == 0)
247 config.dynamic_link = true;
249 einfo ("%P%F: unrecognized -a option `%s'\n", optarg);
252 ldfile_add_arch (optarg);
255 lang_add_target (optarg);
258 ldfile_open_command_file (optarg);
259 parser_input = input_mri_script;
262 case OPTION_CALL_SHARED:
263 config.dynamic_link = true;
265 case OPTION_NON_SHARED:
266 config.dynamic_link = false;
269 command_line.force_common_definition = true;
272 lex_redirect (optarg);
273 parser_input = input_defsym;
276 case OPTION_DYNAMIC_LINKER:
277 command_line.interpreter = optarg;
280 command_line.endian = ENDIAN_BIG;
283 command_line.endian = ENDIAN_LITTLE;
285 case OPTION_EMBEDDED_RELOCS:
286 command_line.embedded_relocs = true;
288 case OPTION_EXPORT_DYNAMIC:
289 command_line.export_dynamic = true;
292 lang_add_entry (optarg, 1);
300 g_switch_value = strtoul (optarg, &end, 0);
302 einfo ("%P%F: invalid number `%s'\n", optarg);
313 ldfile_add_library_path (optarg, true);
316 lang_add_input_file (optarg, lang_input_file_is_l_enum,
320 config.map_filename = "-";
323 /* Ignore. Was handled in a pre-parse. */
326 config.map_filename = optarg;
329 config.text_read_only = false;
330 config.magic_demand_paged = false;
333 config.magic_demand_paged = false;
335 case OPTION_NO_KEEP_MEMORY:
336 link_info.keep_memory = false;
338 case OPTION_NOINHIBIT_EXEC:
339 force_make_executable = true;
342 /* FIXME "-O<non-digits> <value>" used to set the address of
343 section <non-digits>. Was this for compatibility with
344 something, or can we create a new option to do that
345 (with a syntax similar to -defsym)?
346 getopt can't handle two args to an option without kludges. */
347 set_default_dirlist (optarg);
350 lang_add_output (optarg, 0);
353 lang_add_output_format (optarg, (char *) NULL, (char *) NULL, 0);
357 link_info.relocateable = true;
358 config.build_constructors = false;
359 config.magic_demand_paged = false;
360 config.text_read_only = false;
361 config.dynamic_link = false;
364 /* The GNU linker traditionally uses -R to mean to include
365 only the symbols from a file. The Solaris linker uses -R
366 to set the path used by the runtime linker to find
367 libraries. This is the GNU linker -rpath argument. We
368 try to support both simultaneously by checking the file
369 named. If it is a directory, rather than a regular file,
370 we assume -rpath was meant. */
374 if (stat (optarg, &s) >= 0
375 && ! S_ISDIR (s.st_mode))
377 lang_add_input_file (optarg,
378 lang_input_file_is_symbols_only_enum,
385 if (command_line.rpath == NULL)
386 command_line.rpath = buystring (optarg);
391 buf = xmalloc (strlen (command_line.rpath)
394 sprintf (buf, "%s:%s", command_line.rpath, optarg);
395 free (command_line.rpath);
396 command_line.rpath = buf;
400 command_line.relax = true;
402 case OPTION_RETAIN_SYMBOLS_FILE:
403 add_keepsyms_file (optarg);
406 link_info.strip = strip_debugger;
409 link_info.strip = strip_all;
412 link_info.shared = true;
414 case 'h': /* Used on Solaris. */
416 command_line.soname = optarg;
418 case OPTION_SORT_COMMON:
419 config.sort_common = true;
424 case OPTION_SYMBOLIC:
425 link_info.symbolic = true;
431 ldfile_open_command_file (optarg);
432 parser_input = input_script;
436 set_section_start (".bss", optarg);
439 set_section_start (".data", optarg);
442 set_section_start (".text", optarg);
444 case OPTION_TRADITIONAL_FORMAT:
445 config.traditional_format = true;
448 link_info.relocateable = true;
449 config.build_constructors = true;
450 config.magic_demand_paged = false;
451 config.text_read_only = false;
452 config.dynamic_link = false;
455 ldlang_add_undef (optarg);
459 version_printed = true;
460 trace_file_tries = true;
464 version_printed = true;
468 version_printed = true;
472 version_printed = true;
474 case OPTION_WARN_COMMON:
475 config.warn_common = true;
477 case OPTION_WARN_CONSTRUCTORS:
478 config.warn_constructors = true;
480 case OPTION_WARN_ONCE:
481 config.warn_once = true;
483 case OPTION_WHOLE_ARCHIVE:
484 whole_archive = true;
487 link_info.discard = discard_l;
490 link_info.discard = discard_all;
493 set_default_dirlist (optarg);
499 /* We accept and ignore this option for Solaris
500 compatibility. Actually, on Solaris, optarg is not
501 ignored. Someday we should handle it correctly. FIXME. */
503 case OPTION_SPLIT_BY_RELOC:
504 config.split_by_reloc = atoi (optarg);
506 case OPTION_SPLIT_BY_FILE:
507 config.split_by_file = true;
513 "%s: may not nest groups (--help for usage)\n",
524 "%s: group ended before it began (--help for usage)\n",
534 case OPTION_BASE_FILE:
535 link_info.base_file = (PTR) fopen (optarg,"w");
536 if (link_info.base_file == NULL)
538 fprintf (stderr, "%s: Can't open base file %s\n",
539 program_name, optarg);
546 set_pe_stack_heap (&pe_info.heap_reserve, &pe_info.heap_commit);
549 set_pe_stack_heap (&pe_info.stack_reserve, &pe_info.stack_commit);
551 case OPTION_SUBSYSTEM:
554 case OPTION_MAJOR_OS_VERSION:
555 set_pe_value (&pe_info.major_os_version);
557 case OPTION_MINOR_OS_VERSION:
558 set_pe_value (&pe_info.minor_os_version);
560 case OPTION_MAJOR_SUBSYSTEM_VERSION:
561 set_pe_value (&pe_info.major_subsystem_version);
563 case OPTION_MINOR_SUBSYSTEM_VERSION:
564 set_pe_value (&pe_info.minor_subsystem_version);
566 case OPTION_MAJOR_IMAGE_VERSION:
567 set_pe_value (&pe_info.major_image_version);
569 case OPTION_MINOR_IMAGE_VERSION:
570 set_pe_value (&pe_info.minor_image_version);
572 case OPTION_FILE_ALIGNMENT:
573 set_pe_value (&pe_info.file_alignment);
575 case OPTION_SECTION_ALIGNMENT:
576 set_pe_value (&pe_info.section_alignment);
579 pe_info.dll.defined = 1;
580 pe_info.dll.value = 1;
582 case OPTION_IMAGE_BASE:
583 set_pe_value (&pe_info.image_base);
592 /* Add the (colon-separated) elements of DIRLIST_PTR to the
593 library search path. */
596 set_default_dirlist (dirlist_ptr)
603 p = strchr (dirlist_ptr, ':');
607 ldfile_add_library_path (dirlist_ptr, true);
616 set_section_start (sect, valstr)
620 unsigned long val = strtoul (valstr, &end, 16);
622 einfo ("%P%F: invalid hex number `%s'\n", valstr);
623 lang_section_start (sect, exp_intop (val));
626 /* PE/WIN32; added routines to get the subsystem type, heap and/or stack
627 parameters which may be input from the command line */
640 {"native", BFD_PE_NATIVE},
641 {"windows",BFD_PE_WINDOWS},
642 {"console",BFD_PE_CONSOLE},
644 {"posix", BFD_PE_POSIX},
648 for (i = 0; v[i].name; i++)
650 if (!strcmp (optarg, v[i].name))
652 link_info.pe_info = &pe_info;
653 pe_info.subsystem.value = v[i].value;
654 pe_info.subsystem.defined = 1;
658 einfo ("%P%F: invalid subsystem type `%s'\n", optarg);
663 bfd_link_pe_info_dval *ptr;
666 ptr->value = strtoul (optarg, &end, 16);
669 einfo ("%P%F: invalid hex number for PE parameter '%s'\n", optarg);
674 link_info.pe_info = &pe_info;
678 set_pe_stack_heap (reserve, commit)
679 bfd_link_pe_info_dval *reserve;
680 bfd_link_pe_info_dval *commit;
685 set_pe_value (reserve);
689 set_pe_value (commit);
693 einfo ("%P%F: strange hex info for PE parameter '%s'\n", optarg);