* libaout.h: Fix comment.
[deliverable/binutils-gdb.git] / ld / lexsup.c
CommitLineData
d4e5e3c3
DM
1/* Parse options for the GNU linker.
2 Copyright (C) 1991, 92, 93, 94 Free Software Foundation, Inc.
3
4This file is part of GLD, the Gnu Linker.
5
6GLD is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GLD is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GLD; see the file COPYING. If not, write to
18the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20#include "bfd.h"
21#include "sysdep.h"
22#include <stdio.h>
23#include <string.h>
24#include "getopt.h"
25#include "bfdlink.h"
26#include "config.h"
27#include "ld.h"
28#include "ldmain.h"
29#include "ldmisc.h"
30#include "ldexp.h"
31#include "ldlang.h"
32#include "ldgram.h"
33#include "ldlex.h"
34#include "ldfile.h"
4725fb48 35#include "ldver.h"
d4e5e3c3 36
809ee7e0
ILT
37/* Somewhere above, sys/stat.h got included . . . . */
38#if !defined(S_ISDIR) && defined(S_IFDIR)
39#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
40#endif
41
d4e5e3c3
DM
42/* Omit args to avoid the possibility of clashing with a system header
43 that might disagree about consts. */
44unsigned long strtoul ();
45
46static void set_default_dirlist PARAMS ((char *dirlist_ptr));
47static void set_section_start PARAMS ((char *sect, char *valstr));
48
49void
50parse_args (argc, argv)
51 int argc;
52 char **argv;
53{
db770dfa
ILT
54 int ingroup = 0;
55
d4e5e3c3
DM
56 /* Starting the short option string with '-' is for programs that
57 expect options and other ARGV-elements in any order and that care about
58 the ordering of the two. We describe each non-option ARGV-element
59 as if it were the argument of an option with character code 1. */
60
db770dfa
ILT
61 const char *shortopts =
62 "-A:B::b:cde:F::G:giL:l:Mm:NnO:o:R:rSsT:tu:VvXxY:y:()";
d4e5e3c3 63
d5b79a89
DM
64 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
65
bec7a138
ILT
66#define OPTION_CALL_SHARED 150
67#define OPTION_DEFSYM (OPTION_CALL_SHARED + 1)
7fb9ca5f
ILT
68#define OPTION_DYNAMIC_LINKER (OPTION_DEFSYM + 1)
69#define OPTION_EB (OPTION_DYNAMIC_LINKER + 1)
bec7a138
ILT
70#define OPTION_EL (OPTION_EB + 1)
71#define OPTION_HELP (OPTION_EL + 1)
72#define OPTION_IGNORE (OPTION_HELP + 1)
73#define OPTION_MAP (OPTION_IGNORE + 1)
74#define OPTION_NO_KEEP_MEMORY (OPTION_MAP + 1)
75#define OPTION_NOINHIBIT_EXEC (OPTION_NO_KEEP_MEMORY + 1)
76#define OPTION_NON_SHARED (OPTION_NOINHIBIT_EXEC + 1)
77#define OPTION_OFORMAT (OPTION_NON_SHARED + 1)
78#define OPTION_RELAX (OPTION_OFORMAT + 1)
79#define OPTION_RETAIN_SYMBOLS_FILE (OPTION_RELAX + 1)
809ee7e0
ILT
80#define OPTION_RPATH (OPTION_RETAIN_SYMBOLS_FILE + 1)
81#define OPTION_SHARED (OPTION_RPATH + 1)
82#define OPTION_SONAME (OPTION_SHARED + 1)
83#define OPTION_SORT_COMMON (OPTION_SONAME + 1)
bec7a138
ILT
84#define OPTION_STATS (OPTION_SORT_COMMON + 1)
85#define OPTION_TBSS (OPTION_STATS + 1)
86#define OPTION_TDATA (OPTION_TBSS + 1)
87#define OPTION_TTEXT (OPTION_TDATA + 1)
88#define OPTION_TRADITIONAL_FORMAT (OPTION_TTEXT + 1)
89#define OPTION_UR (OPTION_TRADITIONAL_FORMAT + 1)
90#define OPTION_VERSION (OPTION_UR + 1)
91#define OPTION_WARN_COMMON (OPTION_VERSION + 1)
809ee7e0 92#define OPTION_WARN_ONCE (OPTION_WARN_COMMON + 1)
bec7a138 93
d5b79a89 94 static struct option longopts[] = {
db770dfa
ILT
95 {"Bdynamic", no_argument, NULL, OPTION_CALL_SHARED},
96 {"Bstatic", no_argument, NULL, OPTION_NON_SHARED},
d5b79a89 97 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
d4e5e3c3 98 {"dc", no_argument, NULL, 'd'},
d5b79a89 99 {"defsym", required_argument, NULL, OPTION_DEFSYM},
809ee7e0 100 {"dll-verbose", no_argument, NULL, OPTION_VERSION}, /* Linux. */
2a9fa50c 101 {"dn", no_argument, NULL, OPTION_NON_SHARED},
d4e5e3c3 102 {"dp", no_argument, NULL, 'd'},
2a9fa50c 103 {"dy", no_argument, NULL, OPTION_CALL_SHARED},
7fb9ca5f 104 {"dynamic-linker", required_argument, NULL, OPTION_DYNAMIC_LINKER},
d5b79a89 105 {"EB", no_argument, NULL, OPTION_EB},
d5b79a89 106 {"EL", no_argument, NULL, OPTION_EL},
db770dfa 107 {"end-group", no_argument, NULL, ')'},
d4e5e3c3 108 {"format", required_argument, NULL, 'b'},
d5b79a89 109 {"help", no_argument, NULL, OPTION_HELP},
d5b79a89 110 {"Map", required_argument, NULL, OPTION_MAP},
d5b79a89 111 {"no-keep-memory", no_argument, NULL, OPTION_NO_KEEP_MEMORY},
d5b79a89
DM
112 {"noinhibit-exec", no_argument, NULL, OPTION_NOINHIBIT_EXEC},
113 {"noinhibit_exec", no_argument, NULL, OPTION_NOINHIBIT_EXEC},
2a9fa50c 114 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
d5b79a89 115 {"oformat", required_argument, NULL, OPTION_OFORMAT},
2a9fa50c 116 {"Qy", no_argument, NULL, OPTION_IGNORE},
cbbf9608 117 {"qmagic", no_argument, NULL, OPTION_IGNORE}, /* Linux compatibility. */
d5b79a89 118 {"relax", no_argument, NULL, OPTION_RELAX},
db770dfa 119 {"retain-symbols-file", required_argument, NULL, OPTION_RETAIN_SYMBOLS_FILE},
809ee7e0 120 {"rpath", required_argument, NULL, OPTION_RPATH},
db770dfa 121 {"shared", no_argument, NULL, OPTION_SHARED},
809ee7e0 122 {"soname", required_argument, NULL, OPTION_SONAME},
d5b79a89
DM
123 {"sort-common", no_argument, NULL, OPTION_SORT_COMMON},
124 {"sort_common", no_argument, NULL, OPTION_SORT_COMMON},
db770dfa 125 {"start-group", no_argument, NULL, '('},
d5b79a89 126 {"stats", no_argument, NULL, OPTION_STATS},
42792411 127 {"static", no_argument, NULL, OPTION_NON_SHARED},
d5b79a89 128 {"Tbss", required_argument, NULL, OPTION_TBSS},
d5b79a89 129 {"Tdata", required_argument, NULL, OPTION_TDATA},
d5b79a89 130 {"Ttext", required_argument, NULL, OPTION_TTEXT},
c96386c4 131 {"traditional-format", no_argument, NULL, OPTION_TRADITIONAL_FORMAT},
d5b79a89 132 {"Ur", no_argument, NULL, OPTION_UR},
d5b79a89 133 {"version", no_argument, NULL, OPTION_VERSION},
d5b79a89 134 {"warn-common", no_argument, NULL, OPTION_WARN_COMMON},
809ee7e0 135 {"warn-once", no_argument, NULL, OPTION_WARN_ONCE},
d4e5e3c3
DM
136 {NULL, no_argument, NULL, 0}
137 };
138
139 while (1)
140 {
141 /* getopt_long_only is like getopt_long, but '-' as well as '--' can
142 indicate a long option. */
d5b79a89
DM
143 int longind;
144 int optc = getopt_long_only (argc, argv, shortopts, longopts, &longind);
145
d4e5e3c3
DM
146 if (optc == -1)
147 break;
148
149 switch (optc)
150 {
151 default:
152 xexit (1);
153 case 1: /* File name. */
154 lang_add_input_file (optarg, lang_input_file_is_file_enum,
155 (char *) NULL);
156 break;
157
2a9fa50c
ILT
158 case OPTION_IGNORE:
159 break;
d4e5e3c3
DM
160 case 'A':
161 ldfile_add_arch (optarg);
162 break;
d4e5e3c3
DM
163 case 'b':
164 lang_add_target (optarg);
165 break;
166 case 'c':
167 ldfile_open_command_file (optarg);
168 parser_input = input_mri_script;
169 yyparse ();
170 break;
d5b79a89 171 case OPTION_CALL_SHARED:
2a9fa50c
ILT
172 config.dynamic_link = true;
173 break;
174 case OPTION_NON_SHARED:
175 config.dynamic_link = false;
d4e5e3c3
DM
176 break;
177 case 'd':
178 command_line.force_common_definition = true;
179 break;
d5b79a89 180 case OPTION_DEFSYM:
d4e5e3c3
DM
181 lex_redirect (optarg);
182 parser_input = input_defsym;
183 yyparse ();
184 break;
7fb9ca5f
ILT
185 case OPTION_DYNAMIC_LINKER:
186 command_line.interpreter = optarg;
187 break;
d5b79a89 188 case OPTION_EB:
d4e5e3c3
DM
189 /* FIXME: This is currently ignored. It means
190 ``produce a big-endian object file''. It could
191 be used to select an output format. */
192 break;
d5b79a89 193 case OPTION_EL:
d4e5e3c3
DM
194 /* FIXME: This is currently ignored. It means
195 ``produce a little-endian object file''. It could
196 be used to select an output format. */
197 break;
198 case 'e':
60e8a534 199 lang_add_entry (optarg, 1);
d4e5e3c3
DM
200 break;
201 case 'F':
202 /* Ignore. */
203 break;
204 case 'G':
205 {
206 char *end;
207 g_switch_value = strtoul (optarg, &end, 0);
208 if (*end)
9d3898b2 209 einfo ("%P%F: invalid number `%s'\n", optarg);
d4e5e3c3
DM
210 }
211 break;
212 case 'g':
213 /* Ignore. */
214 break;
d5b79a89 215 case OPTION_HELP:
d4e5e3c3
DM
216 help ();
217 xexit (0);
218 break;
219 case 'L':
0cd82d00 220 ldfile_add_library_path (optarg, true);
d4e5e3c3
DM
221 break;
222 case 'l':
223 lang_add_input_file (optarg, lang_input_file_is_l_enum,
224 (char *) NULL);
225 break;
226 case 'M':
227 config.map_filename = "-";
228 break;
229 case 'm':
230 /* Ignore. Was handled in a pre-parse. */
231 break;
d5b79a89 232 case OPTION_MAP:
d4e5e3c3
DM
233 config.map_filename = optarg;
234 break;
235 case 'N':
236 config.text_read_only = false;
237 config.magic_demand_paged = false;
238 break;
239 case 'n':
240 config.magic_demand_paged = false;
241 break;
d5b79a89 242 case OPTION_NO_KEEP_MEMORY:
d4e5e3c3
DM
243 link_info.keep_memory = false;
244 break;
d5b79a89 245 case OPTION_NOINHIBIT_EXEC:
d4e5e3c3
DM
246 force_make_executable = true;
247 break;
248 case 'O':
249 /* FIXME "-O<non-digits> <value>" used to set the address of
250 section <non-digits>. Was this for compatibility with
251 something, or can we create a new option to do that
252 (with a syntax similar to -defsym)?
253 getopt can't handle two args to an option without kludges. */
254 set_default_dirlist (optarg);
255 break;
256 case 'o':
257 lang_add_output (optarg, 0);
258 break;
d5b79a89 259 case OPTION_OFORMAT:
d4e5e3c3
DM
260 lang_add_output_format (optarg, 0);
261 break;
3e1fd33d 262 case 'i':
d4e5e3c3
DM
263 case 'r':
264 link_info.relocateable = true;
265 config.build_constructors = false;
266 config.magic_demand_paged = false;
267 config.text_read_only = false;
2a9fa50c 268 config.dynamic_link = false;
d4e5e3c3
DM
269 break;
270 case 'R':
809ee7e0
ILT
271 /* The GNU linker traditionally uses -R to mean to include
272 only the symbols from a file. The Solaris linker uses -R
273 to set the path used by the runtime linker to find
274 libraries. This is the GNU linker -rpath argument. We
275 try to support both simultaneously by checking the file
276 named. If it is a directory, rather than a regular file,
277 we assume -rpath was meant. */
278 {
279 struct stat s;
280
281 if (stat (optarg, &s) >= 0
282 && ! S_ISDIR (s.st_mode))
283 {
284 lang_add_input_file (optarg,
285 lang_input_file_is_symbols_only_enum,
286 (char *) NULL);
287 break;
288 }
289 }
290 /* Fall through. */
291 case OPTION_RPATH:
292 if (command_line.rpath == NULL)
293 command_line.rpath = buystring (optarg);
294 else
295 {
296 char *buf;
297
298 buf = xmalloc (strlen (command_line.rpath)
299 + strlen (optarg)
300 + 2);
301 sprintf (buf, "%s:%s", command_line.rpath, optarg);
302 free (command_line.rpath);
303 command_line.rpath = buf;
304 }
d4e5e3c3 305 break;
d5b79a89 306 case OPTION_RELAX:
d4e5e3c3
DM
307 command_line.relax = true;
308 break;
d5b79a89 309 case OPTION_RETAIN_SYMBOLS_FILE:
d4e5e3c3
DM
310 add_keepsyms_file (optarg);
311 break;
312 case 'S':
313 link_info.strip = strip_debugger;
314 break;
315 case 's':
316 link_info.strip = strip_all;
317 break;
db770dfa
ILT
318 case OPTION_SHARED:
319 link_info.shared = true;
320 break;
809ee7e0
ILT
321 case OPTION_SONAME:
322 command_line.soname = optarg;
323 break;
d5b79a89 324 case OPTION_SORT_COMMON:
d4e5e3c3
DM
325 config.sort_common = true;
326 break;
d5b79a89 327 case OPTION_STATS:
d4e5e3c3
DM
328 config.stats = true;
329 break;
330 case 't':
331 trace_files = true;
332 break;
333 case 'T':
334 ldfile_open_command_file (optarg);
335 parser_input = input_script;
336 yyparse ();
337 break;
d5b79a89 338 case OPTION_TBSS:
d4e5e3c3
DM
339 set_section_start (".bss", optarg);
340 break;
d5b79a89 341 case OPTION_TDATA:
d4e5e3c3
DM
342 set_section_start (".data", optarg);
343 break;
d5b79a89 344 case OPTION_TTEXT:
d4e5e3c3
DM
345 set_section_start (".text", optarg);
346 break;
c96386c4
ILT
347 case OPTION_TRADITIONAL_FORMAT:
348 config.traditional_format = true;
349 break;
d5b79a89 350 case OPTION_UR:
d4e5e3c3
DM
351 link_info.relocateable = true;
352 config.build_constructors = true;
353 config.magic_demand_paged = false;
354 config.text_read_only = false;
2a9fa50c 355 config.dynamic_link = false;
d4e5e3c3
DM
356 break;
357 case 'u':
358 ldlang_add_undef (optarg);
359 break;
360 case 'V':
361 ldversion (1);
362 version_printed = true;
363 trace_file_tries = true;
364 break;
365 case 'v':
366 ldversion (0);
367 version_printed = true;
368 break;
d5b79a89 369 case OPTION_VERSION:
d4e5e3c3
DM
370 ldversion (0);
371 version_printed = true;
372 break;
d5b79a89 373 case OPTION_WARN_COMMON:
d4e5e3c3
DM
374 config.warn_common = true;
375 break;
809ee7e0
ILT
376 case OPTION_WARN_ONCE:
377 config.warn_once = true;
378 break;
d4e5e3c3
DM
379 case 'X':
380 link_info.discard = discard_l;
381 break;
382 case 'x':
383 link_info.discard = discard_all;
384 break;
d5b79a89
DM
385 case 'Y':
386 set_default_dirlist (optarg);
387 break;
d4e5e3c3
DM
388 case 'y':
389 add_ysym (optarg);
390 break;
db770dfa
ILT
391 case '(':
392 if (ingroup)
393 {
394 fprintf (stderr,
395 "%s: may not nest groups (--help for usage)\n",
396 program_name);
397 xexit (1);
398 }
399 lang_enter_group ();
400 ingroup = 1;
401 break;
402 case ')':
403 if (! ingroup)
404 {
405 fprintf (stderr,
406 "%s: group ended before it began (--help for usage)\n",
407 program_name);
408 xexit (1);
409 }
410 lang_leave_group ();
411 ingroup = 0;
412 break;
d4e5e3c3
DM
413 }
414 }
db770dfa
ILT
415
416 if (ingroup)
417 lang_leave_group ();
d4e5e3c3
DM
418}
419
420/* Add the (colon-separated) elements of DIRLIST_PTR to the
421 library search path. */
422
423static void
424set_default_dirlist (dirlist_ptr)
425 char *dirlist_ptr;
426{
427 char *p;
428
429 while (1)
430 {
431 p = strchr (dirlist_ptr, ':');
432 if (p != NULL)
433 *p = 0;
434 if (*dirlist_ptr)
0cd82d00 435 ldfile_add_library_path (dirlist_ptr, true);
d4e5e3c3
DM
436 if (p == NULL)
437 break;
438 *p = ':';
439 dirlist_ptr = p + 1;
440 }
441}
442
443static void
444set_section_start (sect, valstr)
445 char *sect, *valstr;
446{
447 char *end;
448 unsigned long val = strtoul (valstr, &end, 16);
449 if (*end)
d5b79a89 450 einfo ("%P%F: invalid hex number `%s'\n", valstr);
d4e5e3c3
DM
451 lang_section_start (sect, exp_intop (val));
452}
This page took 0.059396 seconds and 4 git commands to generate.