Add support for ELF shared libraries. Loosely based on work by
[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
DM
36
37/* Omit args to avoid the possibility of clashing with a system header
38 that might disagree about consts. */
39unsigned long strtoul ();
40
41static void set_default_dirlist PARAMS ((char *dirlist_ptr));
42static void set_section_start PARAMS ((char *sect, char *valstr));
43
44void
45parse_args (argc, argv)
46 int argc;
47 char **argv;
48{
49 /* Starting the short option string with '-' is for programs that
50 expect options and other ARGV-elements in any order and that care about
51 the ordering of the two. We describe each non-option ARGV-element
52 as if it were the argument of an option with character code 1. */
53
d5b79a89 54 const char *shortopts = "-A:B::b:cde:F::G:giL:l:Mm:NnO:o:R:rSsT:tu:VvXxY:y:";
d4e5e3c3 55
d5b79a89
DM
56 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
57
58 static struct option longopts[] = {
59#define OPTION_CALL_SHARED 150
60 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
d4e5e3c3 61 {"dc", no_argument, NULL, 'd'},
d5b79a89
DM
62#define OPTION_DEFSYM 151
63 {"defsym", required_argument, NULL, OPTION_DEFSYM},
64 {"dn", no_argument, NULL, OPTION_CALL_SHARED},
d4e5e3c3 65 {"dp", no_argument, NULL, 'd'},
d5b79a89
DM
66#define OPTION_EB 152
67 {"EB", no_argument, NULL, OPTION_EB},
68#define OPTION_EL 153
69 {"EL", no_argument, NULL, OPTION_EL},
d4e5e3c3 70 {"format", required_argument, NULL, 'b'},
d5b79a89
DM
71#define OPTION_HELP 154
72 {"help", no_argument, NULL, OPTION_HELP},
73#define OPTION_MAP 155
74 {"Map", required_argument, NULL, OPTION_MAP},
75#define OPTION_NO_KEEP_MEMORY 156
76 {"no-keep-memory", no_argument, NULL, OPTION_NO_KEEP_MEMORY},
77#define OPTION_NOINHIBIT_EXEC 157
78 {"noinhibit-exec", no_argument, NULL, OPTION_NOINHIBIT_EXEC},
79 {"noinhibit_exec", no_argument, NULL, OPTION_NOINHIBIT_EXEC},
80 {"non_shared", no_argument, NULL, OPTION_CALL_SHARED},
81#define OPTION_OFORMAT 158
82 {"oformat", required_argument, NULL, OPTION_OFORMAT},
83 {"Qy", no_argument, NULL, OPTION_CALL_SHARED},
84#define OPTION_RELAX 159
85 {"relax", no_argument, NULL, OPTION_RELAX},
86#define OPTION_RETAIN_SYMBOLS_FILE 160
87 {"retain-symbols-file", no_argument, NULL, OPTION_RETAIN_SYMBOLS_FILE},
88#define OPTION_SORT_COMMON 161
89 {"sort-common", no_argument, NULL, OPTION_SORT_COMMON},
90 {"sort_common", no_argument, NULL, OPTION_SORT_COMMON},
91#define OPTION_STATS 162
92 {"stats", no_argument, NULL, OPTION_STATS},
93#define OPTION_TBSS 163
94 {"Tbss", required_argument, NULL, OPTION_TBSS},
95#define OPTION_TDATA 164
96 {"Tdata", required_argument, NULL, OPTION_TDATA},
97#define OPTION_TTEXT 165
98 {"Ttext", required_argument, NULL, OPTION_TTEXT},
99#define OPTION_UR 166
100 {"Ur", no_argument, NULL, OPTION_UR},
101#define OPTION_VERSION 167
102 {"version", no_argument, NULL, OPTION_VERSION},
103#define OPTION_WARN_COMMON 168
104 {"warn-common", no_argument, NULL, OPTION_WARN_COMMON},
d4e5e3c3
DM
105 {NULL, no_argument, NULL, 0}
106 };
107
108 while (1)
109 {
110 /* getopt_long_only is like getopt_long, but '-' as well as '--' can
111 indicate a long option. */
d5b79a89
DM
112 int longind;
113 int optc = getopt_long_only (argc, argv, shortopts, longopts, &longind);
114
d4e5e3c3
DM
115 if (optc == -1)
116 break;
117
118 switch (optc)
119 {
120 default:
121 xexit (1);
122 case 1: /* File name. */
123 lang_add_input_file (optarg, lang_input_file_is_file_enum,
124 (char *) NULL);
125 break;
126
127 case 'A':
128 ldfile_add_arch (optarg);
129 break;
130 case 'B':
131 /* Ignore. */
132 break;
133 case 'b':
134 lang_add_target (optarg);
135 break;
136 case 'c':
137 ldfile_open_command_file (optarg);
138 parser_input = input_mri_script;
139 yyparse ();
140 break;
d5b79a89
DM
141 case OPTION_CALL_SHARED:
142 set_default_dirlist ((char *) longopts[longind].name);
d4e5e3c3
DM
143 break;
144 case 'd':
145 command_line.force_common_definition = true;
146 break;
d5b79a89 147 case OPTION_DEFSYM:
d4e5e3c3
DM
148 lex_redirect (optarg);
149 parser_input = input_defsym;
150 yyparse ();
151 break;
d5b79a89 152 case OPTION_EB:
d4e5e3c3
DM
153 /* FIXME: This is currently ignored. It means
154 ``produce a big-endian object file''. It could
155 be used to select an output format. */
156 break;
d5b79a89 157 case OPTION_EL:
d4e5e3c3
DM
158 /* FIXME: This is currently ignored. It means
159 ``produce a little-endian object file''. It could
160 be used to select an output format. */
161 break;
162 case 'e':
163 lang_add_entry (optarg);
164 break;
165 case 'F':
166 /* Ignore. */
167 break;
168 case 'G':
169 {
170 char *end;
171 g_switch_value = strtoul (optarg, &end, 0);
172 if (*end)
9d3898b2 173 einfo ("%P%F: invalid number `%s'\n", optarg);
d4e5e3c3
DM
174 }
175 break;
176 case 'g':
177 /* Ignore. */
178 break;
d5b79a89 179 case OPTION_HELP:
d4e5e3c3
DM
180 help ();
181 xexit (0);
182 break;
183 case 'L':
184 ldfile_add_library_path (optarg);
185 break;
186 case 'l':
187 lang_add_input_file (optarg, lang_input_file_is_l_enum,
188 (char *) NULL);
189 break;
190 case 'M':
191 config.map_filename = "-";
192 break;
193 case 'm':
194 /* Ignore. Was handled in a pre-parse. */
195 break;
d5b79a89 196 case OPTION_MAP:
d4e5e3c3
DM
197 write_map = true;
198 config.map_filename = optarg;
199 break;
200 case 'N':
201 config.text_read_only = false;
202 config.magic_demand_paged = false;
203 break;
204 case 'n':
205 config.magic_demand_paged = false;
206 break;
d5b79a89 207 case OPTION_NO_KEEP_MEMORY:
d4e5e3c3
DM
208 link_info.keep_memory = false;
209 break;
d5b79a89 210 case OPTION_NOINHIBIT_EXEC:
d4e5e3c3
DM
211 force_make_executable = true;
212 break;
213 case 'O':
214 /* FIXME "-O<non-digits> <value>" used to set the address of
215 section <non-digits>. Was this for compatibility with
216 something, or can we create a new option to do that
217 (with a syntax similar to -defsym)?
218 getopt can't handle two args to an option without kludges. */
219 set_default_dirlist (optarg);
220 break;
221 case 'o':
222 lang_add_output (optarg, 0);
223 break;
d5b79a89 224 case OPTION_OFORMAT:
d4e5e3c3
DM
225 lang_add_output_format (optarg, 0);
226 break;
227 case 'r':
228 link_info.relocateable = true;
229 config.build_constructors = false;
230 config.magic_demand_paged = false;
231 config.text_read_only = false;
232 break;
233 case 'R':
234 lang_add_input_file (optarg,
235 lang_input_file_is_symbols_only_enum,
236 (char *) NULL);
237 break;
d5b79a89 238 case OPTION_RELAX:
d4e5e3c3
DM
239 command_line.relax = true;
240 break;
d5b79a89 241 case OPTION_RETAIN_SYMBOLS_FILE:
d4e5e3c3
DM
242 add_keepsyms_file (optarg);
243 break;
244 case 'S':
245 link_info.strip = strip_debugger;
246 break;
247 case 's':
248 link_info.strip = strip_all;
249 break;
d5b79a89 250 case OPTION_SORT_COMMON:
d4e5e3c3
DM
251 config.sort_common = true;
252 break;
d5b79a89 253 case OPTION_STATS:
d4e5e3c3
DM
254 config.stats = true;
255 break;
256 case 't':
257 trace_files = true;
258 break;
259 case 'T':
260 ldfile_open_command_file (optarg);
261 parser_input = input_script;
262 yyparse ();
263 break;
d5b79a89 264 case OPTION_TBSS:
d4e5e3c3
DM
265 set_section_start (".bss", optarg);
266 break;
d5b79a89 267 case OPTION_TDATA:
d4e5e3c3
DM
268 set_section_start (".data", optarg);
269 break;
d5b79a89 270 case OPTION_TTEXT:
d4e5e3c3
DM
271 set_section_start (".text", optarg);
272 break;
d5b79a89 273 case OPTION_UR:
d4e5e3c3
DM
274 link_info.relocateable = true;
275 config.build_constructors = true;
276 config.magic_demand_paged = false;
277 config.text_read_only = false;
278 break;
279 case 'u':
280 ldlang_add_undef (optarg);
281 break;
282 case 'V':
283 ldversion (1);
284 version_printed = true;
285 trace_file_tries = true;
286 break;
287 case 'v':
288 ldversion (0);
289 version_printed = true;
290 break;
d5b79a89 291 case OPTION_VERSION:
d4e5e3c3
DM
292 ldversion (0);
293 version_printed = true;
294 break;
d5b79a89 295 case OPTION_WARN_COMMON:
d4e5e3c3
DM
296 config.warn_common = true;
297 break;
298 case 'X':
299 link_info.discard = discard_l;
300 break;
301 case 'x':
302 link_info.discard = discard_all;
303 break;
d5b79a89
DM
304 case 'Y':
305 set_default_dirlist (optarg);
306 break;
d4e5e3c3
DM
307 case 'y':
308 add_ysym (optarg);
309 break;
310 }
311 }
312}
313
314/* Add the (colon-separated) elements of DIRLIST_PTR to the
315 library search path. */
316
317static void
318set_default_dirlist (dirlist_ptr)
319 char *dirlist_ptr;
320{
321 char *p;
322
323 while (1)
324 {
325 p = strchr (dirlist_ptr, ':');
326 if (p != NULL)
327 *p = 0;
328 if (*dirlist_ptr)
329 ldfile_add_library_path (dirlist_ptr);
330 if (p == NULL)
331 break;
332 *p = ':';
333 dirlist_ptr = p + 1;
334 }
335}
336
337static void
338set_section_start (sect, valstr)
339 char *sect, *valstr;
340{
341 char *end;
342 unsigned long val = strtoul (valstr, &end, 16);
343 if (*end)
d5b79a89 344 einfo ("%P%F: invalid hex number `%s'\n", valstr);
d4e5e3c3
DM
345 lang_section_start (sect, exp_intop (val));
346}
This page took 0.078072 seconds and 4 git commands to generate.