Commit | Line | Data |
---|---|---|
bae7f79e ILT |
1 | // options.c -- handle command line options for gold |
2 | ||
e5756efb | 3 | // Copyright 2006, 2007, 2008 Free Software Foundation, Inc. |
6cb15b7f ILT |
4 | // Written by Ian Lance Taylor <iant@google.com>. |
5 | ||
6 | // This file is part of gold. | |
7 | ||
8 | // This program is free software; you can redistribute it and/or modify | |
9 | // it under the terms of the GNU General Public License as published by | |
10 | // the Free Software Foundation; either version 3 of the License, or | |
11 | // (at your option) any later version. | |
12 | ||
13 | // This program is distributed in the hope that it will be useful, | |
14 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | // GNU General Public License for more details. | |
17 | ||
18 | // You should have received a copy of the GNU General Public License | |
19 | // along with this program; if not, write to the Free Software | |
20 | // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, | |
21 | // MA 02110-1301, USA. | |
22 | ||
ad2d6943 ILT |
23 | #include "gold.h" |
24 | ||
a2b1aa12 | 25 | #include <cstdlib> |
ee1fe73e | 26 | #include <vector> |
bae7f79e | 27 | #include <iostream> |
ad2d6943 ILT |
28 | #include <sys/stat.h> |
29 | #include "filenames.h" | |
30 | #include "libiberty.h" | |
086a1841 | 31 | #include "demangle.h" |
bae7f79e | 32 | |
c7912668 | 33 | #include "debug.h" |
e5756efb | 34 | #include "script.h" |
ee1fe73e | 35 | #include "target-select.h" |
bae7f79e ILT |
36 | #include "options.h" |
37 | ||
61ba1cf9 ILT |
38 | namespace gold |
39 | { | |
40 | ||
ee1fe73e ILT |
41 | General_options |
42 | Position_dependent_options::default_options_; | |
bae7f79e | 43 | |
ee1fe73e | 44 | namespace options |
bae7f79e | 45 | { |
bae7f79e | 46 | |
ee1fe73e ILT |
47 | // This global variable is set up as General_options is constructed. |
48 | static std::vector<const One_option*> registered_options; | |
bae7f79e | 49 | |
ee1fe73e ILT |
50 | // These are set up at the same time -- the variables that accept one |
51 | // dash, two, or require -z. A single variable may be in more than | |
52 | // one of thes data structures. | |
53 | typedef Unordered_map<std::string, One_option*> Option_map; | |
54 | static Option_map* long_options = NULL; | |
55 | static One_option* short_options[128]; | |
bae7f79e | 56 | |
ee1fe73e ILT |
57 | void |
58 | One_option::register_option() | |
35cdfc9a | 59 | { |
ee1fe73e | 60 | registered_options.push_back(this); |
35cdfc9a | 61 | |
ee1fe73e ILT |
62 | // We can't make long_options a static Option_map because we can't |
63 | // guarantee that will be initialized before register_option() is | |
64 | // first called. | |
65 | if (long_options == NULL) | |
66 | long_options = new Option_map; | |
cd72c291 | 67 | |
ee1fe73e ILT |
68 | // TWO_DASHES means that two dashes are preferred, but one is ok too. |
69 | if (!this->longname.empty()) | |
70 | (*long_options)[this->longname] = this; | |
35cdfc9a | 71 | |
ee1fe73e ILT |
72 | const int shortname_as_int = static_cast<int>(this->shortname); |
73 | gold_assert(shortname_as_int >= 0 && shortname_as_int < 128); | |
74 | if (this->shortname != '\0') | |
75 | short_options[shortname_as_int] = this; | |
76 | } | |
c7912668 | 77 | |
ee1fe73e ILT |
78 | void |
79 | One_option::print() const | |
c7912668 | 80 | { |
ee1fe73e ILT |
81 | bool comma = false; |
82 | printf(" "); | |
83 | int len = 2; | |
84 | if (this->shortname != '\0') | |
85 | { | |
86 | len += printf("-%c", this->shortname); | |
87 | if (this->helparg) | |
88 | { | |
89 | // -z takes long-names only. | |
90 | gold_assert(this->dashes != DASH_Z); | |
a4d4b13f | 91 | len += printf(" %s", gettext(this->helparg)); |
ee1fe73e ILT |
92 | } |
93 | comma = true; | |
94 | } | |
95 | if (!this->longname.empty() | |
96 | && !(this->longname[0] == this->shortname | |
97 | && this->longname[1] == '\0')) | |
98 | { | |
99 | if (comma) | |
100 | len += printf(", "); | |
101 | switch (this->dashes) | |
102 | { | |
103 | case options::ONE_DASH: case options::EXACTLY_ONE_DASH: | |
104 | len += printf("-"); | |
105 | break; | |
106 | case options::TWO_DASHES: case options::EXACTLY_TWO_DASHES: | |
107 | len += printf("--"); | |
108 | break; | |
109 | case options::DASH_Z: | |
110 | len += printf("-z "); | |
111 | break; | |
112 | default: | |
113 | gold_unreachable(); | |
114 | } | |
115 | len += printf("%s", this->longname.c_str()); | |
116 | if (this->helparg) | |
117 | { | |
118 | // For most options, we print "--frob FOO". But for -z | |
119 | // we print "-z frob=FOO". | |
120 | len += printf("%c%s", this->dashes == options::DASH_Z ? '=' : ' ', | |
a4d4b13f | 121 | gettext(this->helparg)); |
ee1fe73e ILT |
122 | } |
123 | } | |
124 | ||
125 | if (len >= 30) | |
126 | { | |
127 | printf("\n"); | |
128 | len = 0; | |
129 | } | |
130 | for (; len < 30; ++len) | |
131 | std::putchar(' '); | |
c7912668 | 132 | |
ee1fe73e | 133 | // TODO: if we're boolean, add " (default)" when appropriate. |
a4d4b13f | 134 | printf("%s\n", gettext(this->helpstring)); |
ee1fe73e | 135 | } |
c7912668 | 136 | |
ee1fe73e ILT |
137 | void |
138 | help() | |
bae7f79e | 139 | { |
ee1fe73e | 140 | printf(_("Usage: %s [options] file...\nOptions:\n"), gold::program_name); |
bae7f79e | 141 | |
ee1fe73e ILT |
142 | std::vector<const One_option*>::const_iterator it; |
143 | for (it = registered_options.begin(); it != registered_options.end(); ++it) | |
144 | (*it)->print(); | |
e96caa79 ILT |
145 | |
146 | // config.guess and libtool.m4 look in ld --help output for the | |
147 | // string "supported targets". | |
148 | printf(_("%s: supported targets:"), gold::program_name); | |
149 | std::vector<const char*> supported_names; | |
150 | gold::supported_target_names(&supported_names); | |
151 | for (std::vector<const char*>::const_iterator p = supported_names.begin(); | |
152 | p != supported_names.end(); | |
153 | ++p) | |
154 | printf(" %s", *p); | |
155 | printf("\n"); | |
ee1fe73e | 156 | } |
61ba1cf9 | 157 | |
ee1fe73e ILT |
158 | // For bool, arg will be NULL (boolean options take no argument); |
159 | // we always just set to true. | |
160 | void | |
161 | parse_bool(const char*, const char*, bool* retval) | |
bae7f79e | 162 | { |
ee1fe73e ILT |
163 | *retval = true; |
164 | } | |
bae7f79e | 165 | |
ee1fe73e ILT |
166 | void |
167 | parse_uint(const char* option_name, const char* arg, int* retval) | |
168 | { | |
169 | char* endptr; | |
170 | *retval = strtol(arg, &endptr, 0); | |
171 | if (*endptr != '\0' || retval < 0) | |
172 | gold_fatal(_("%s: invalid option value (expected an integer): %s"), | |
173 | option_name, arg); | |
174 | } | |
bc644c6c | 175 | |
ee1fe73e ILT |
176 | void |
177 | parse_uint64(const char* option_name, const char* arg, uint64_t *retval) | |
bc644c6c | 178 | { |
ee1fe73e ILT |
179 | char* endptr; |
180 | *retval = strtoull(arg, &endptr, 0); | |
181 | if (*endptr != '\0') | |
182 | gold_fatal(_("%s: invalid option value (expected an integer): %s"), | |
183 | option_name, arg); | |
184 | } | |
185 | ||
c18476e7 ILT |
186 | void |
187 | parse_double(const char* option_name, const char* arg, double* retval) | |
188 | { | |
189 | char* endptr; | |
190 | *retval = strtod(arg, &endptr); | |
191 | if (*endptr != '\0') | |
192 | gold_fatal(_("%s: invalid option value " | |
193 | "(expected a floating point number): %s"), | |
194 | option_name, arg); | |
195 | } | |
196 | ||
ee1fe73e ILT |
197 | void |
198 | parse_string(const char* option_name, const char* arg, const char** retval) | |
199 | { | |
200 | if (*arg == '\0') | |
201 | gold_fatal(_("%s: must take a non-empty argument"), option_name); | |
202 | *retval = arg; | |
203 | } | |
204 | ||
086a1841 ILT |
205 | void |
206 | parse_optional_string(const char*, const char* arg, const char** retval) | |
207 | { | |
208 | *retval = arg; | |
209 | } | |
210 | ||
ee1fe73e ILT |
211 | void |
212 | parse_dirlist(const char*, const char* arg, Dir_list* retval) | |
213 | { | |
214 | retval->push_back(Search_directory(arg, false)); | |
215 | } | |
216 | ||
217 | void | |
218 | parse_choices(const char* option_name, const char* arg, const char** retval, | |
219 | const char* choices[], int num_choices) | |
220 | { | |
221 | for (int i = 0; i < num_choices; i++) | |
222 | if (strcmp(choices[i], arg) == 0) | |
223 | { | |
224 | *retval = arg; | |
225 | return; | |
226 | } | |
227 | ||
228 | // If we get here, the user did not enter a valid choice, so we die. | |
229 | std::string choices_list; | |
230 | for (int i = 0; i < num_choices; i++) | |
bc644c6c | 231 | { |
ee1fe73e ILT |
232 | choices_list += choices[i]; |
233 | if (i != num_choices - 1) | |
234 | choices_list += ", "; | |
bc644c6c | 235 | } |
ee1fe73e ILT |
236 | gold_fatal(_("%s: must take one of the following arguments: %s"), |
237 | option_name, choices_list.c_str()); | |
bc644c6c ILT |
238 | } |
239 | ||
ee1fe73e | 240 | } // End namespace options. |
a5dc0706 | 241 | |
ee1fe73e ILT |
242 | // Define the handler for "special" options (set via DEFINE_special). |
243 | ||
244 | void | |
245 | General_options::parse_help(const char*, const char*, Command_line*) | |
a5dc0706 | 246 | { |
ee1fe73e ILT |
247 | options::help(); |
248 | ::exit(EXIT_SUCCESS); | |
a5dc0706 ILT |
249 | } |
250 | ||
ee1fe73e ILT |
251 | void |
252 | General_options::parse_version(const char* opt, const char*, Command_line*) | |
253 | { | |
254 | gold::print_version(opt[0] == '-' && opt[1] == 'v'); | |
255 | ::exit(EXIT_SUCCESS); | |
256 | } | |
61ba1cf9 | 257 | |
ee1fe73e ILT |
258 | void |
259 | General_options::parse_Bstatic(const char*, const char*, Command_line*) | |
61ba1cf9 | 260 | { |
ee1fe73e | 261 | this->set_Bdynamic(false); |
3c2fafa5 ILT |
262 | } |
263 | ||
ee1fe73e ILT |
264 | void |
265 | General_options::parse_defsym(const char*, const char* arg, | |
266 | Command_line* cmdline) | |
267 | { | |
268 | cmdline->script_options().define_symbol(arg); | |
269 | } | |
88dd47ac | 270 | |
ee1fe73e ILT |
271 | void |
272 | General_options::parse_library(const char*, const char* arg, | |
273 | Command_line* cmdline) | |
274 | { | |
275 | Input_file_argument file(arg, true, "", false, *this); | |
276 | cmdline->inputs().add_file(file); | |
277 | } | |
278 | ||
279 | void | |
280 | General_options::parse_R(const char* option, const char* arg, | |
281 | Command_line* cmdline) | |
88dd47ac | 282 | { |
88dd47ac | 283 | struct stat s; |
ee1fe73e ILT |
284 | if (::stat(arg, &s) != 0 || S_ISDIR(s.st_mode)) |
285 | this->add_to_rpath(arg); | |
88dd47ac | 286 | else |
ee1fe73e | 287 | this->parse_just_symbols(option, arg, cmdline); |
88dd47ac ILT |
288 | } |
289 | ||
ee1fe73e ILT |
290 | void |
291 | General_options::parse_just_symbols(const char*, const char* arg, | |
292 | Command_line* cmdline) | |
88dd47ac | 293 | { |
ee1fe73e ILT |
294 | Input_file_argument file(arg, false, "", true, *this); |
295 | cmdline->inputs().add_file(file); | |
88dd47ac ILT |
296 | } |
297 | ||
ee1fe73e ILT |
298 | void |
299 | General_options::parse_static(const char*, const char*, Command_line*) | |
3c2fafa5 | 300 | { |
ee1fe73e | 301 | this->set_static(true); |
09124467 ILT |
302 | } |
303 | ||
ee1fe73e ILT |
304 | void |
305 | General_options::parse_script(const char*, const char* arg, | |
306 | Command_line* cmdline) | |
09124467 | 307 | { |
ee1fe73e ILT |
308 | if (!read_commandline_script(arg, cmdline)) |
309 | gold::gold_fatal(_("unable to parse script file %s"), arg); | |
61ba1cf9 ILT |
310 | } |
311 | ||
ee1fe73e ILT |
312 | void |
313 | General_options::parse_version_script(const char*, const char* arg, | |
314 | Command_line* cmdline) | |
ead1e424 | 315 | { |
ee1fe73e ILT |
316 | if (!read_version_script(arg, cmdline)) |
317 | gold::gold_fatal(_("unable to parse version script file %s"), arg); | |
ead1e424 ILT |
318 | } |
319 | ||
ee1fe73e ILT |
320 | void |
321 | General_options::parse_start_group(const char*, const char*, | |
322 | Command_line* cmdline) | |
323 | { | |
324 | cmdline->inputs().start_group(); | |
325 | } | |
ead1e424 | 326 | |
ee1fe73e ILT |
327 | void |
328 | General_options::parse_end_group(const char*, const char*, | |
329 | Command_line* cmdline) | |
ead1e424 | 330 | { |
ee1fe73e | 331 | cmdline->inputs().end_group(); |
ead1e424 ILT |
332 | } |
333 | ||
ee1fe73e | 334 | } // End namespace gold. |
bae7f79e | 335 | |
ee1fe73e | 336 | namespace |
bae7f79e | 337 | { |
bae7f79e | 338 | |
ee1fe73e ILT |
339 | void |
340 | usage() | |
341 | { | |
342 | fprintf(stderr, | |
343 | _("%s: use the --help option for usage information\n"), | |
344 | gold::program_name); | |
345 | ::exit(EXIT_FAILURE); | |
346 | } | |
bae7f79e | 347 | |
ee1fe73e ILT |
348 | void |
349 | usage(const char* msg, const char *opt) | |
350 | { | |
351 | fprintf(stderr, | |
352 | _("%s: %s: %s\n"), | |
353 | gold::program_name, opt, msg); | |
354 | usage(); | |
bae7f79e ILT |
355 | } |
356 | ||
ee1fe73e ILT |
357 | // Recognize input and output target names. The GNU linker accepts |
358 | // these with --format and --oformat. This code is intended to be | |
359 | // minimally compatible. In practice for an ELF target this would be | |
360 | // the same target as the input files; that name always start with | |
361 | // "elf". Non-ELF targets would be "srec", "symbolsrec", "tekhex", | |
362 | // "binary", "ihex". | |
8486ee48 | 363 | |
ee1fe73e ILT |
364 | gold::General_options::Object_format |
365 | string_to_object_format(const char* arg) | |
8486ee48 | 366 | { |
ee1fe73e ILT |
367 | if (strncmp(arg, "elf", 3) == 0) |
368 | return gold::General_options::OBJECT_FORMAT_ELF; | |
369 | else if (strcmp(arg, "binary") == 0) | |
370 | return gold::General_options::OBJECT_FORMAT_BINARY; | |
371 | else | |
372 | { | |
09ffbbe0 | 373 | gold::gold_error(_("format '%s' not supported; treating as elf " |
ee1fe73e ILT |
374 | "(supported formats: elf, binary)"), |
375 | arg); | |
376 | return gold::General_options::OBJECT_FORMAT_ELF; | |
377 | } | |
8486ee48 ILT |
378 | } |
379 | ||
ad2d6943 ILT |
380 | // If the default sysroot is relocatable, try relocating it based on |
381 | // the prefix FROM. | |
382 | ||
383 | char* | |
384 | get_relative_sysroot(const char* from) | |
385 | { | |
386 | char* path = make_relative_prefix(gold::program_name, from, | |
ee1fe73e | 387 | TARGET_SYSTEM_ROOT); |
ad2d6943 ILT |
388 | if (path != NULL) |
389 | { | |
390 | struct stat s; | |
391 | if (::stat(path, &s) == 0 && S_ISDIR(s.st_mode)) | |
ee1fe73e | 392 | return path; |
ad2d6943 ILT |
393 | free(path); |
394 | } | |
395 | ||
396 | return NULL; | |
397 | } | |
398 | ||
399 | // Return the default sysroot. This is set by the --with-sysroot | |
ee1fe73e ILT |
400 | // option to configure. Note we do not free the return value of |
401 | // get_relative_sysroot, which is a small memory leak, but is | |
402 | // necessary since we store this pointer directly in General_options. | |
ad2d6943 | 403 | |
ee1fe73e | 404 | const char* |
ad2d6943 ILT |
405 | get_default_sysroot() |
406 | { | |
407 | const char* sysroot = TARGET_SYSTEM_ROOT; | |
408 | if (*sysroot == '\0') | |
ee1fe73e | 409 | return NULL; |
ad2d6943 ILT |
410 | |
411 | if (TARGET_SYSTEM_ROOT_RELOCATABLE) | |
412 | { | |
ee1fe73e | 413 | char* path = get_relative_sysroot(BINDIR); |
ad2d6943 | 414 | if (path == NULL) |
ee1fe73e | 415 | path = get_relative_sysroot(TOOLBINDIR); |
ad2d6943 | 416 | if (path != NULL) |
ee1fe73e | 417 | return path; |
ad2d6943 ILT |
418 | } |
419 | ||
420 | return sysroot; | |
421 | } | |
422 | ||
ee1fe73e ILT |
423 | // Parse a long option. Such options have the form |
424 | // <-|--><option>[=arg]. If "=arg" is not present but the option | |
425 | // takes an argument, the next word is taken to the be the argument. | |
426 | // If equals_only is set, then only the <option>=<arg> form is | |
427 | // accepted, not the <option><space><arg> form. Returns a One_option | |
428 | // struct or NULL if argv[i] cannot be parsed as a long option. In | |
429 | // the not-NULL case, *arg is set to the option's argument (NULL if | |
430 | // the option takes no argument), and *i is advanced past this option. | |
431 | // NOTE: it is safe for argv and arg to point to the same place. | |
432 | gold::options::One_option* | |
433 | parse_long_option(int argc, const char** argv, bool equals_only, | |
434 | const char** arg, int* i) | |
61ba1cf9 | 435 | { |
ee1fe73e | 436 | const char* const this_argv = argv[*i]; |
bae7f79e | 437 | |
ee1fe73e ILT |
438 | const char* equals = strchr(this_argv, '='); |
439 | const char* option_start = this_argv + strspn(this_argv, "-"); | |
440 | std::string option(option_start, | |
441 | equals ? equals - option_start : strlen(option_start)); | |
35cdfc9a | 442 | |
ee1fe73e ILT |
443 | gold::options::Option_map::iterator it |
444 | = gold::options::long_options->find(option); | |
445 | if (it == gold::options::long_options->end()) | |
446 | return NULL; | |
35cdfc9a | 447 | |
ee1fe73e | 448 | gold::options::One_option* retval = it->second; |
c7912668 | 449 | |
ee1fe73e ILT |
450 | // If the dash-count doesn't match, we fail. |
451 | if (this_argv[0] != '-') // no dashes at all: had better be "-z <longopt>" | |
452 | { | |
453 | if (retval->dashes != gold::options::DASH_Z) | |
454 | return NULL; | |
455 | } | |
456 | else if (this_argv[1] != '-') // one dash | |
457 | { | |
458 | if (retval->dashes != gold::options::ONE_DASH | |
459 | && retval->dashes != gold::options::EXACTLY_ONE_DASH | |
460 | && retval->dashes != gold::options::TWO_DASHES) | |
461 | return NULL; | |
462 | } | |
463 | else // two dashes (or more!) | |
464 | { | |
465 | if (retval->dashes != gold::options::TWO_DASHES | |
466 | && retval->dashes != gold::options::EXACTLY_TWO_DASHES | |
467 | && retval->dashes != gold::options::ONE_DASH) | |
468 | return NULL; | |
469 | } | |
bae7f79e | 470 | |
ee1fe73e ILT |
471 | // Now that we know the option is good (or else bad in a way that |
472 | // will cause us to die), increment i to point past this argv. | |
473 | ++(*i); | |
bae7f79e | 474 | |
ee1fe73e ILT |
475 | // Figure out the option's argument, if any. |
476 | if (!retval->takes_argument()) | |
477 | { | |
478 | if (equals) | |
479 | usage(_("unexpected argument"), this_argv); | |
480 | else | |
481 | *arg = NULL; | |
482 | } | |
483 | else | |
484 | { | |
485 | if (equals) | |
486 | *arg = equals + 1; | |
086a1841 ILT |
487 | else if (retval->takes_optional_argument()) |
488 | *arg = retval->default_value; | |
ee1fe73e ILT |
489 | else if (*i < argc && !equals_only) |
490 | *arg = argv[(*i)++]; | |
491 | else | |
492 | usage(_("missing argument"), this_argv); | |
493 | } | |
516cb3d0 | 494 | |
ee1fe73e ILT |
495 | return retval; |
496 | } | |
497 | ||
498 | // Parse a short option. Such options have the form -<option>[arg]. | |
499 | // If "arg" is not present but the option takes an argument, the next | |
500 | // word is taken to the be the argument. If the option does not take | |
501 | // an argument, it may be followed by another short option. Returns a | |
502 | // One_option struct or NULL if argv[i] cannot be parsed as a short | |
503 | // option. In the not-NULL case, *arg is set to the option's argument | |
504 | // (NULL if the option takes no argument), and *i is advanced past | |
505 | // this option. This function keeps *i the same if we parsed a short | |
506 | // option that does not take an argument, that looks to be followed by | |
507 | // another short option in the same word. | |
508 | gold::options::One_option* | |
509 | parse_short_option(int argc, const char** argv, int pos_in_argv_i, | |
510 | const char** arg, int* i) | |
511 | { | |
512 | const char* const this_argv = argv[*i]; | |
513 | ||
514 | if (this_argv[0] != '-') | |
515 | return NULL; | |
516 | ||
517 | // We handle -z as a special case. | |
518 | static gold::options::One_option dash_z("", gold::options::DASH_Z, | |
086a1841 ILT |
519 | 'z', "", "-z", "Z-OPTION", false, |
520 | NULL); | |
ee1fe73e ILT |
521 | gold::options::One_option* retval = NULL; |
522 | if (this_argv[pos_in_argv_i] == 'z') | |
523 | retval = &dash_z; | |
524 | else | |
525 | { | |
526 | const int char_as_int = static_cast<int>(this_argv[pos_in_argv_i]); | |
527 | if (char_as_int > 0 && char_as_int < 128) | |
528 | retval = gold::options::short_options[char_as_int]; | |
529 | } | |
516cb3d0 | 530 | |
ee1fe73e ILT |
531 | if (retval == NULL) |
532 | return NULL; | |
35cdfc9a | 533 | |
ee1fe73e ILT |
534 | // Figure out the option's argument, if any. |
535 | if (!retval->takes_argument()) | |
cd72c291 | 536 | { |
ee1fe73e ILT |
537 | *arg = NULL; |
538 | // We only advance past this argument if it's the only one in argv. | |
539 | if (this_argv[pos_in_argv_i + 1] == '\0') | |
540 | ++(*i); | |
cd72c291 ILT |
541 | } |
542 | else | |
ee1fe73e ILT |
543 | { |
544 | // If we take an argument, we'll eat up this entire argv entry. | |
545 | ++(*i); | |
546 | if (this_argv[pos_in_argv_i + 1] != '\0') | |
547 | *arg = this_argv + pos_in_argv_i + 1; | |
086a1841 ILT |
548 | else if (retval->takes_optional_argument()) |
549 | *arg = retval->default_value; | |
ee1fe73e ILT |
550 | else if (*i < argc) |
551 | *arg = argv[(*i)++]; | |
552 | else | |
553 | usage(_("missing argument"), this_argv); | |
554 | } | |
cd72c291 | 555 | |
ee1fe73e ILT |
556 | // If we're a -z option, we need to parse our argument as a |
557 | // long-option, e.g. "-z stacksize=8192". | |
558 | if (retval == &dash_z) | |
35cdfc9a | 559 | { |
ee1fe73e ILT |
560 | int dummy_i = 0; |
561 | const char* dash_z_arg = *arg; | |
562 | retval = parse_long_option(1, arg, true, arg, &dummy_i); | |
563 | if (retval == NULL) | |
564 | usage(_("unknown -z option"), dash_z_arg); | |
35cdfc9a ILT |
565 | } |
566 | ||
ee1fe73e | 567 | return retval; |
c7912668 ILT |
568 | } |
569 | ||
ee1fe73e | 570 | } // End anonymous namespace. |
c7912668 | 571 | |
ee1fe73e | 572 | namespace gold |
c7912668 | 573 | { |
c7912668 | 574 | |
ee1fe73e | 575 | General_options::General_options() |
086a1841 ILT |
576 | : execstack_status_(General_options::EXECSTACK_FROM_INPUT), static_(false), |
577 | do_demangle_(false) | |
ee1fe73e ILT |
578 | { |
579 | } | |
580 | ||
581 | General_options::Object_format | |
582 | General_options::format_enum() const | |
583 | { | |
584 | return string_to_object_format(this->format()); | |
585 | } | |
586 | ||
587 | General_options::Object_format | |
588 | General_options::oformat_enum() const | |
589 | { | |
590 | return string_to_object_format(this->oformat()); | |
35cdfc9a ILT |
591 | } |
592 | ||
ad2d6943 ILT |
593 | // Add the sysroot, if any, to the search paths. |
594 | ||
595 | void | |
596 | General_options::add_sysroot() | |
597 | { | |
ee1fe73e | 598 | if (this->sysroot() == NULL || this->sysroot()[0] == '\0') |
ad2d6943 | 599 | { |
ee1fe73e ILT |
600 | this->set_sysroot(get_default_sysroot()); |
601 | if (this->sysroot() == NULL || this->sysroot()[0] == '\0') | |
602 | return; | |
ad2d6943 ILT |
603 | } |
604 | ||
ee1fe73e | 605 | char* canonical_sysroot = lrealpath(this->sysroot()); |
ad2d6943 | 606 | |
ee1fe73e ILT |
607 | for (Dir_list::iterator p = this->library_path_.value.begin(); |
608 | p != this->library_path_.value.end(); | |
ad2d6943 | 609 | ++p) |
ee1fe73e | 610 | p->add_sysroot(this->sysroot(), canonical_sysroot); |
ad2d6943 ILT |
611 | |
612 | free(canonical_sysroot); | |
613 | } | |
614 | ||
ee1fe73e ILT |
615 | // Set up variables and other state that isn't set up automatically by |
616 | // the parse routine, and ensure options don't contradict each other | |
617 | // and are otherwise kosher. | |
e5756efb | 618 | |
ee1fe73e ILT |
619 | void |
620 | General_options::finalize() | |
bc644c6c | 621 | { |
ee1fe73e ILT |
622 | // Normalize the strip modifiers. They have a total order: |
623 | // strip_all > strip_debug > strip_debug_gdb. If one is true, set | |
624 | // all beneath it to true as well. | |
625 | if (this->strip_all()) | |
626 | this->set_strip_debug(true); | |
627 | if (this->strip_debug()) | |
628 | this->set_strip_debug_gdb(true); | |
bc644c6c | 629 | |
ee1fe73e ILT |
630 | // If the user specifies both -s and -r, convert the -s to -S. |
631 | // -r requires us to keep externally visible symbols! | |
632 | if (this->strip_all() && this->relocatable()) | |
633 | { | |
634 | this->set_strip_all(false); | |
635 | gold_assert(this->strip_debug()); | |
636 | } | |
bc644c6c | 637 | |
ee1fe73e ILT |
638 | // For us, -dc and -dp are synonyms for --define-common. |
639 | if (this->dc()) | |
640 | this->set_define_common(true); | |
641 | if (this->dp()) | |
642 | this->set_define_common(true); | |
643 | ||
644 | // We also set --define-common if we're not relocatable, as long as | |
645 | // the user didn't explicitly ask for something different. | |
646 | if (!this->user_set_define_common()) | |
647 | this->set_define_common(!this->relocatable()); | |
648 | ||
649 | // execstack_status_ is a three-state variable; update it based on | |
650 | // -z [no]execstack. | |
651 | if (this->execstack()) | |
652 | this->set_execstack_status(EXECSTACK_YES); | |
653 | else if (this->noexecstack()) | |
654 | this->set_execstack_status(EXECSTACK_NO); | |
655 | ||
086a1841 ILT |
656 | // Handle the optional argument for --demangle. |
657 | if (this->user_set_demangle()) | |
658 | { | |
659 | this->set_do_demangle(true); | |
660 | const char* style = this->demangle(); | |
661 | if (*style != '\0') | |
662 | { | |
663 | enum demangling_styles style_code; | |
664 | ||
665 | style_code = cplus_demangle_name_to_style(style); | |
666 | if (style_code == unknown_demangling) | |
667 | gold_fatal("unknown demangling style '%s'", style); | |
668 | cplus_demangle_set_style(style_code); | |
669 | } | |
670 | } | |
671 | else if (this->user_set_no_demangle()) | |
672 | this->set_do_demangle(false); | |
673 | else | |
674 | { | |
675 | // Testing COLLECT_NO_DEMANGLE makes our default demangling | |
676 | // behaviour identical to that of gcc's linker wrapper. | |
677 | this->set_do_demangle(getenv("COLLECT_NO_DEMANGLE") == NULL); | |
678 | } | |
679 | ||
ee1fe73e ILT |
680 | // If --thread_count is specified, it applies to |
681 | // --thread-count-{initial,middle,final}, though it doesn't override | |
682 | // them. | |
683 | if (this->thread_count() > 0 && this->thread_count_initial() == 0) | |
684 | this->set_thread_count_initial(this->thread_count()); | |
685 | if (this->thread_count() > 0 && this->thread_count_middle() == 0) | |
686 | this->set_thread_count_middle(this->thread_count()); | |
687 | if (this->thread_count() > 0 && this->thread_count_final() == 0) | |
688 | this->set_thread_count_final(this->thread_count()); | |
689 | ||
09ffbbe0 ILT |
690 | // Let's warn if you set the thread-count but we're going to ignore it. |
691 | #ifndef ENABLE_THREADS | |
692 | if (this->threads()) | |
693 | { | |
694 | gold_warning(_("ignoring --threads: " | |
695 | "%s was compiled without thread support"), | |
696 | program_name); | |
697 | this->set_threads(false); | |
698 | } | |
699 | if (this->thread_count() > 0 || this->thread_count_initial() > 0 | |
700 | || this->thread_count_middle() > 0 || this->thread_count_final() > 0) | |
701 | gold_warning(_("ignoring --thread-count: " | |
702 | "%s was compiled without thread support"), | |
703 | program_name); | |
704 | #endif | |
705 | ||
ee1fe73e ILT |
706 | // Even if they don't specify it, we add -L /lib and -L /usr/lib. |
707 | // FIXME: We should only do this when configured in native mode. | |
708 | this->add_to_library_path_with_sysroot("/lib"); | |
709 | this->add_to_library_path_with_sysroot("/usr/lib"); | |
710 | ||
711 | // Normalize library_path() by adding the sysroot to all directories | |
712 | // in the path, as appropriate. | |
713 | this->add_sysroot(); | |
714 | ||
715 | // Now that we've normalized the options, check for contradictory ones. | |
716 | if (this->shared() && this->relocatable()) | |
717 | gold_fatal(_("-shared and -r are incompatible")); | |
718 | ||
719 | if (this->oformat_enum() != General_options::OBJECT_FORMAT_ELF | |
720 | && (this->shared() || this->relocatable())) | |
721 | gold_fatal(_("binary output format not compatible with -shared or -r")); | |
722 | ||
c18476e7 ILT |
723 | if (this->user_set_hash_bucket_empty_fraction() |
724 | && (this->hash_bucket_empty_fraction() < 0.0 | |
725 | || this->hash_bucket_empty_fraction() >= 1.0)) | |
726 | gold_fatal(_("--hash-bucket-empty-fraction value %g out of range " | |
727 | "[0.0, 1.0)"), | |
728 | this->hash_bucket_empty_fraction()); | |
729 | ||
ee1fe73e | 730 | // FIXME: we can/should be doing a lot more sanity checking here. |
e5756efb ILT |
731 | } |
732 | ||
ad2d6943 ILT |
733 | // Search_directory methods. |
734 | ||
735 | // This is called if we have a sysroot. Apply the sysroot if | |
736 | // appropriate. Record whether the directory is in the sysroot. | |
737 | ||
738 | void | |
739 | Search_directory::add_sysroot(const char* sysroot, | |
ee1fe73e | 740 | const char* canonical_sysroot) |
ad2d6943 ILT |
741 | { |
742 | gold_assert(*sysroot != '\0'); | |
743 | if (this->put_in_sysroot_) | |
744 | { | |
745 | if (!IS_DIR_SEPARATOR(this->name_[0]) | |
ee1fe73e ILT |
746 | && !IS_DIR_SEPARATOR(sysroot[strlen(sysroot) - 1])) |
747 | this->name_ = '/' + this->name_; | |
ad2d6943 ILT |
748 | this->name_ = sysroot + this->name_; |
749 | this->is_in_sysroot_ = true; | |
750 | } | |
751 | else | |
752 | { | |
753 | // Check whether this entry is in the sysroot. To do this | |
754 | // correctly, we need to use canonical names. Otherwise we will | |
755 | // get confused by the ../../.. paths that gcc tends to use. | |
756 | char* canonical_name = lrealpath(this->name_.c_str()); | |
757 | int canonical_name_len = strlen(canonical_name); | |
758 | int canonical_sysroot_len = strlen(canonical_sysroot); | |
759 | if (canonical_name_len > canonical_sysroot_len | |
ee1fe73e ILT |
760 | && IS_DIR_SEPARATOR(canonical_name[canonical_sysroot_len])) |
761 | { | |
762 | canonical_name[canonical_sysroot_len] = '\0'; | |
763 | if (FILENAME_CMP(canonical_name, canonical_sysroot) == 0) | |
764 | this->is_in_sysroot_ = true; | |
765 | } | |
ad2d6943 ILT |
766 | free(canonical_name); |
767 | } | |
768 | } | |
769 | ||
dbe717ef ILT |
770 | // Input_arguments methods. |
771 | ||
772 | // Add a file to the list. | |
773 | ||
774 | void | |
775 | Input_arguments::add_file(const Input_file_argument& file) | |
776 | { | |
777 | if (!this->in_group_) | |
778 | this->input_argument_list_.push_back(Input_argument(file)); | |
779 | else | |
780 | { | |
a3ad94ed ILT |
781 | gold_assert(!this->input_argument_list_.empty()); |
782 | gold_assert(this->input_argument_list_.back().is_group()); | |
dbe717ef ILT |
783 | this->input_argument_list_.back().group()->add_file(file); |
784 | } | |
785 | } | |
786 | ||
787 | // Start a group. | |
788 | ||
789 | void | |
790 | Input_arguments::start_group() | |
791 | { | |
ee1fe73e ILT |
792 | if (this->in_group_) |
793 | gold_fatal(_("May not nest groups")); | |
dbe717ef ILT |
794 | Input_file_group* group = new Input_file_group(); |
795 | this->input_argument_list_.push_back(Input_argument(group)); | |
796 | this->in_group_ = true; | |
797 | } | |
798 | ||
799 | // End a group. | |
800 | ||
801 | void | |
802 | Input_arguments::end_group() | |
803 | { | |
ee1fe73e ILT |
804 | if (!this->in_group_) |
805 | gold_fatal(_("Group end without group start")); | |
dbe717ef ILT |
806 | this->in_group_ = false; |
807 | } | |
808 | ||
ead1e424 | 809 | // Command_line options. |
bae7f79e | 810 | |
a5dc0706 | 811 | Command_line::Command_line() |
bae7f79e ILT |
812 | { |
813 | } | |
814 | ||
ee1fe73e ILT |
815 | // Process the command line options. For process_one_option, i is the |
816 | // index of argv to process next, and must be an option (that is, | |
817 | // start with a dash). The return value is the index of the next | |
818 | // option to process (i+1 or i+2, or argc to indicate processing is | |
819 | // done). no_more_options is set to true if (and when) "--" is seen | |
820 | // as an option. | |
bae7f79e | 821 | |
a0451b38 | 822 | int |
ee1fe73e | 823 | Command_line::process_one_option(int argc, const char** argv, int i, |
a0451b38 | 824 | bool* no_more_options) |
bae7f79e | 825 | { |
ee1fe73e | 826 | gold_assert(argv[i][0] == '-' && !(*no_more_options)); |
a0451b38 | 827 | |
ee1fe73e ILT |
828 | // If we are reading "--", then just set no_more_options and return. |
829 | if (argv[i][1] == '-' && argv[i][2] == '\0') | |
bae7f79e | 830 | { |
ee1fe73e | 831 | *no_more_options = true; |
a0451b38 ILT |
832 | return i + 1; |
833 | } | |
bae7f79e | 834 | |
ee1fe73e ILT |
835 | int new_i = i; |
836 | options::One_option* option = NULL; | |
837 | const char* arg = NULL; | |
bae7f79e | 838 | |
ee1fe73e ILT |
839 | // First, try to process argv as a long option. |
840 | option = parse_long_option(argc, argv, false, &arg, &new_i); | |
841 | if (option) | |
a0451b38 | 842 | { |
ee1fe73e ILT |
843 | option->reader->parse_to_value(argv[i], arg, this, &this->options_); |
844 | return new_i; | |
a0451b38 | 845 | } |
bae7f79e | 846 | |
ee1fe73e ILT |
847 | // Now, try to process argv as a short option. Since several short |
848 | // options can be combined in one argv, we may have to parse a lot | |
849 | // until we're done reading this argv. | |
850 | int pos_in_argv_i = 1; | |
851 | while (new_i == i) | |
a0451b38 | 852 | { |
ee1fe73e ILT |
853 | option = parse_short_option(argc, argv, pos_in_argv_i, &arg, &new_i); |
854 | if (!option) | |
855 | break; | |
856 | option->reader->parse_to_value(argv[i], arg, this, &this->options_); | |
857 | ++pos_in_argv_i; | |
a0451b38 | 858 | } |
ee1fe73e ILT |
859 | if (option) |
860 | return new_i; | |
a0451b38 | 861 | |
ee1fe73e ILT |
862 | // I guess it's neither a long option nor a short option. |
863 | usage(_("unknown option"), argv[i]); | |
864 | return argc; | |
a0451b38 | 865 | } |
bae7f79e | 866 | |
bae7f79e | 867 | |
a0451b38 | 868 | void |
ee1fe73e | 869 | Command_line::process(int argc, const char** argv) |
a0451b38 ILT |
870 | { |
871 | bool no_more_options = false; | |
872 | int i = 0; | |
873 | while (i < argc) | |
ead1e424 | 874 | { |
ee1fe73e ILT |
875 | this->position_options_.copy_from_options(this->options()); |
876 | if (no_more_options || argv[i][0] != '-') | |
877 | { | |
878 | Input_file_argument file(argv[i], false, "", false, | |
879 | this->position_options_); | |
880 | this->inputs_.add_file(file); | |
881 | ++i; | |
882 | } | |
bae7f79e | 883 | else |
ee1fe73e | 884 | i = process_one_option(argc, argv, i, &no_more_options); |
bae7f79e | 885 | } |
bae7f79e | 886 | |
dbe717ef | 887 | if (this->inputs_.in_group()) |
ee1fe73e ILT |
888 | { |
889 | fprintf(stderr, _("%s: missing group end\n"), program_name); | |
890 | usage(); | |
891 | } | |
bae7f79e | 892 | |
ee1fe73e ILT |
893 | // Normalize the options and ensure they don't contradict each other. |
894 | this->options_.finalize(); | |
bae7f79e | 895 | } |
61ba1cf9 ILT |
896 | |
897 | } // End namespace gold. |