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