1 /* Parse options for the GNU linker.
2 Copyright (C) 1991, 92, 93, 94, 95, 1996 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:EF::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_MULTIPLE_GP (OPTION_WARN_CONSTRUCTORS + 1)
107 #define OPTION_WARN_ONCE (OPTION_WARN_MULTIPLE_GP + 1)
108 #define OPTION_SPLIT_BY_RELOC (OPTION_WARN_ONCE + 1)
109 #define OPTION_SPLIT_BY_FILE (OPTION_SPLIT_BY_RELOC + 1)
110 #define OPTION_WHOLE_ARCHIVE (OPTION_SPLIT_BY_FILE + 1)
111 #define OPTION_WRAP (OPTION_WHOLE_ARCHIVE + 1)
113 static struct option longopts
[] = {
114 /* Sorted alphabeticaly, except for the PE options grouped at the end. */
115 {"assert", required_argument
, NULL
, OPTION_ASSERT
},
116 {"Bdynamic", no_argument
, NULL
, OPTION_CALL_SHARED
},
117 {"Bshareable", no_argument
, NULL
, OPTION_SHARED
}, /* FreeBSD. */
118 {"Bstatic", no_argument
, NULL
, OPTION_NON_SHARED
},
119 {"Bsymbolic", no_argument
, NULL
, OPTION_SYMBOLIC
},
120 {"call_shared", no_argument
, NULL
, OPTION_CALL_SHARED
},
121 {"dc", no_argument
, NULL
, 'd'},
122 {"defsym", required_argument
, NULL
, OPTION_DEFSYM
},
123 {"dll-verbose", no_argument
, NULL
, OPTION_VERSION
}, /* Linux. */
124 {"dn", no_argument
, NULL
, OPTION_NON_SHARED
},
125 {"dp", no_argument
, NULL
, 'd'},
126 {"dy", no_argument
, NULL
, OPTION_CALL_SHARED
},
127 {"dynamic-linker", required_argument
, NULL
, OPTION_DYNAMIC_LINKER
},
128 {"EB", no_argument
, NULL
, OPTION_EB
},
129 {"EL", no_argument
, NULL
, OPTION_EL
},
130 {"embedded-relocs", no_argument
, NULL
, OPTION_EMBEDDED_RELOCS
},
131 {"end-group", no_argument
, NULL
, ')'},
132 {"export-dynamic", no_argument
, NULL
, OPTION_EXPORT_DYNAMIC
},
133 {"format", required_argument
, NULL
, 'b'},
134 {"help", no_argument
, NULL
, OPTION_HELP
},
135 {"Map", required_argument
, NULL
, OPTION_MAP
},
136 {"no-keep-memory", no_argument
, NULL
, OPTION_NO_KEEP_MEMORY
},
137 {"no-whole-archive", no_argument
, NULL
, OPTION_NO_WHOLE_ARCHIVE
},
138 {"noinhibit-exec", no_argument
, NULL
, OPTION_NOINHIBIT_EXEC
},
139 {"noinhibit_exec", no_argument
, NULL
, OPTION_NOINHIBIT_EXEC
},
140 {"non_shared", no_argument
, NULL
, OPTION_NON_SHARED
},
141 {"oformat", required_argument
, NULL
, OPTION_OFORMAT
},
142 {"Qy", no_argument
, NULL
, OPTION_IGNORE
},
143 {"qmagic", no_argument
, NULL
, OPTION_IGNORE
}, /* Linux compatibility. */
144 {"relax", no_argument
, NULL
, OPTION_RELAX
},
145 {"retain-symbols-file", required_argument
, NULL
, OPTION_RETAIN_SYMBOLS_FILE
},
146 {"rpath", required_argument
, NULL
, OPTION_RPATH
},
147 {"rpath-link", required_argument
, NULL
, OPTION_RPATH_LINK
},
148 {"shared", no_argument
, NULL
, OPTION_SHARED
},
149 {"soname", required_argument
, NULL
, OPTION_SONAME
},
150 {"sort-common", no_argument
, NULL
, OPTION_SORT_COMMON
},
151 {"sort_common", no_argument
, NULL
, OPTION_SORT_COMMON
},
152 {"start-group", no_argument
, NULL
, '('},
153 {"stats", no_argument
, NULL
, OPTION_STATS
},
154 {"static", no_argument
, NULL
, OPTION_NON_SHARED
},
155 {"Tbss", required_argument
, NULL
, OPTION_TBSS
},
156 {"Tdata", required_argument
, NULL
, OPTION_TDATA
},
157 {"Ttext", required_argument
, NULL
, OPTION_TTEXT
},
158 {"traditional-format", no_argument
, NULL
, OPTION_TRADITIONAL_FORMAT
},
159 {"Ur", no_argument
, NULL
, OPTION_UR
},
160 {"verbose", no_argument
, NULL
, OPTION_VERBOSE
},
161 {"version", no_argument
, NULL
, OPTION_VERSION
},
162 {"warn-common", no_argument
, NULL
, OPTION_WARN_COMMON
},
163 {"warn-constructors", no_argument
, NULL
, OPTION_WARN_CONSTRUCTORS
},
164 {"warn-multiple-gp", no_argument
, NULL
, OPTION_WARN_MULTIPLE_GP
},
165 {"warn-once", no_argument
, NULL
, OPTION_WARN_ONCE
},
166 {"split-by-reloc", required_argument
, NULL
, OPTION_SPLIT_BY_RELOC
},
167 {"split-by-file", no_argument
, NULL
, OPTION_SPLIT_BY_FILE
},
168 {"whole-archive", no_argument
, NULL
, OPTION_WHOLE_ARCHIVE
},
169 {"wrap", required_argument
, NULL
, OPTION_WRAP
},
171 {NULL
, no_argument
, NULL
, 0}
174 /* The -G option is ambiguous on different platforms. Sometimes it
175 specifies the largest data size to put into the small data
176 section. Sometimes it is equivalent to --shared. Unfortunately,
177 the first form takes an argument, while the second does not.
179 We need to permit the --shared form because on some platforms,
180 such as Solaris, gcc -shared will pass -G to the linker.
182 To permit either usage, we look through the argument list. If we
183 find -G not followed by a number, we change it into --shared.
184 This will work for most normal cases. */
185 for (i
= 1; i
< argc
; i
++)
186 if (strcmp (argv
[i
], "-G") == 0
188 || ! isdigit (argv
[i
+ 1][0])))
189 argv
[i
] = (char *) "--shared";
193 /* getopt_long_only is like getopt_long, but '-' as well as '--' can
194 indicate a long option. */
198 if (ldemul_parse_args (argc
, argv
))
201 optc
= getopt_long_only (argc
, argv
, shortopts
, longopts
, &longind
);
209 case 1: /* File name. */
210 lang_add_input_file (optarg
, lang_input_file_is_file_enum
,
217 /* For HP/UX compatibility. Actually -a shared should mean
218 ``use only shared libraries'' but, then, we don't
219 currently support shared libraries on HP/UX anyhow. */
220 if (strcmp (optarg
, "archive") == 0)
221 config
.dynamic_link
= false;
222 else if (strcmp (optarg
, "shared") == 0
223 || strcmp (optarg
, "default") == 0)
224 config
.dynamic_link
= true;
226 einfo ("%P%F: unrecognized -a option `%s'\n", optarg
);
229 /* FIXME: We just ignore these, but we should handle them. */
230 if (strcmp (optarg
, "definitions") == 0)
232 else if (strcmp (optarg
, "nodefinitions") == 0)
234 else if (strcmp (optarg
, "nosymbolic") == 0)
236 else if (strcmp (optarg
, "pure-text") == 0)
239 einfo ("%P%F: unrecognized -assert option `%s'\n", optarg
);
242 ldfile_add_arch (optarg
);
245 lang_add_target (optarg
);
248 ldfile_open_command_file (optarg
);
249 parser_input
= input_mri_script
;
252 case OPTION_CALL_SHARED
:
253 config
.dynamic_link
= true;
255 case OPTION_NON_SHARED
:
256 config
.dynamic_link
= false;
259 command_line
.force_common_definition
= true;
263 lex_redirect (optarg
);
264 parser_input
= input_defsym
;
270 case OPTION_DYNAMIC_LINKER
:
271 command_line
.interpreter
= optarg
;
274 command_line
.endian
= ENDIAN_BIG
;
277 command_line
.endian
= ENDIAN_LITTLE
;
279 case OPTION_EMBEDDED_RELOCS
:
280 command_line
.embedded_relocs
= true;
282 case OPTION_EXPORT_DYNAMIC
:
283 case 'E': /* HP/UX compatibility. */
284 command_line
.export_dynamic
= true;
287 lang_add_entry (optarg
, true);
295 g_switch_value
= strtoul (optarg
, &end
, 0);
297 einfo ("%P%F: invalid number `%s'\n", optarg
);
308 ldfile_add_library_path (optarg
, true);
311 lang_add_input_file (optarg
, lang_input_file_is_l_enum
,
315 config
.map_filename
= "-";
318 /* Ignore. Was handled in a pre-parse. */
321 config
.map_filename
= optarg
;
324 config
.text_read_only
= false;
325 config
.magic_demand_paged
= false;
326 config
.dynamic_link
= false;
329 config
.magic_demand_paged
= false;
330 config
.dynamic_link
= false;
332 case OPTION_NO_KEEP_MEMORY
:
333 link_info
.keep_memory
= false;
335 case OPTION_NOINHIBIT_EXEC
:
336 force_make_executable
= true;
338 case OPTION_NO_WHOLE_ARCHIVE
:
339 whole_archive
= false;
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. */
349 lang_add_output (optarg
, 0);
352 lang_add_output_format (optarg
, (char *) NULL
, (char *) NULL
, 0);
356 link_info
.relocateable
= true;
357 config
.build_constructors
= false;
358 config
.magic_demand_paged
= false;
359 config
.text_read_only
= false;
360 config
.dynamic_link
= false;
363 /* The GNU linker traditionally uses -R to mean to include
364 only the symbols from a file. The Solaris linker uses -R
365 to set the path used by the runtime linker to find
366 libraries. This is the GNU linker -rpath argument. We
367 try to support both simultaneously by checking the file
368 named. If it is a directory, rather than a regular file,
369 we assume -rpath was meant. */
373 if (stat (optarg
, &s
) >= 0
374 && ! S_ISDIR (s
.st_mode
))
376 lang_add_input_file (optarg
,
377 lang_input_file_is_symbols_only_enum
,
384 if (command_line
.rpath
== NULL
)
385 command_line
.rpath
= buystring (optarg
);
390 buf
= xmalloc (strlen (command_line
.rpath
)
393 sprintf (buf
, "%s:%s", command_line
.rpath
, optarg
);
394 free (command_line
.rpath
);
395 command_line
.rpath
= buf
;
398 case OPTION_RPATH_LINK
:
399 if (command_line
.rpath_link
== NULL
)
400 command_line
.rpath_link
= buystring (optarg
);
405 buf
= xmalloc (strlen (command_line
.rpath_link
)
408 sprintf (buf
, "%s:%s", command_line
.rpath_link
, optarg
);
409 free (command_line
.rpath_link
);
410 command_line
.rpath_link
= buf
;
414 command_line
.relax
= true;
416 case OPTION_RETAIN_SYMBOLS_FILE
:
417 add_keepsyms_file (optarg
);
420 link_info
.strip
= strip_debugger
;
423 link_info
.strip
= strip_all
;
426 link_info
.shared
= true;
428 case 'h': /* Used on Solaris. */
430 command_line
.soname
= optarg
;
432 case OPTION_SORT_COMMON
:
433 config
.sort_common
= true;
438 case OPTION_SYMBOLIC
:
439 link_info
.symbolic
= true;
445 ldfile_open_command_file (optarg
);
446 parser_input
= input_script
;
450 set_section_start (".bss", optarg
);
453 set_section_start (".data", optarg
);
456 set_section_start (".text", optarg
);
458 case OPTION_TRADITIONAL_FORMAT
:
459 config
.traditional_format
= true;
462 link_info
.relocateable
= true;
463 config
.build_constructors
= true;
464 config
.magic_demand_paged
= false;
465 config
.text_read_only
= false;
466 config
.dynamic_link
= false;
469 ldlang_add_undef (optarg
);
473 version_printed
= true;
474 trace_file_tries
= true;
478 version_printed
= true;
482 version_printed
= true;
486 version_printed
= true;
488 case OPTION_WARN_COMMON
:
489 config
.warn_common
= true;
491 case OPTION_WARN_CONSTRUCTORS
:
492 config
.warn_constructors
= true;
494 case OPTION_WARN_MULTIPLE_GP
:
495 config
.warn_multiple_gp
= true;
497 case OPTION_WARN_ONCE
:
498 config
.warn_once
= true;
500 case OPTION_WHOLE_ARCHIVE
:
501 whole_archive
= true;
507 link_info
.discard
= discard_l
;
510 link_info
.discard
= discard_all
;
513 if (strncmp (optarg
, "P,", 2) == 0)
515 default_dirlist
= optarg
;
521 /* We accept and ignore this option for Solaris
522 compatibility. Actually, on Solaris, optarg is not
523 ignored. Someday we should handle it correctly. FIXME. */
525 case OPTION_SPLIT_BY_RELOC
:
526 config
.split_by_reloc
= atoi (optarg
);
528 case OPTION_SPLIT_BY_FILE
:
529 config
.split_by_file
= true;
535 "%s: may not nest groups (--help for usage)\n",
546 "%s: group ended before it began (--help for usage)\n",
560 if (default_dirlist
!= NULL
)
561 set_default_dirlist (default_dirlist
);
565 /* Add the (colon-separated) elements of DIRLIST_PTR to the
566 library search path. */
569 set_default_dirlist (dirlist_ptr
)
576 p
= strchr (dirlist_ptr
, ':');
580 ldfile_add_library_path (dirlist_ptr
, true);
589 set_section_start (sect
, valstr
)
593 unsigned long val
= strtoul (valstr
, &end
, 16);
595 einfo ("%P%F: invalid hex number `%s'\n", valstr
);
596 lang_section_start (sect
, exp_intop (val
));