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