1 // options.c -- handle command line options for gold
3 // Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
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.
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.
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.
29 #include "filenames.h"
30 #include "libiberty.h"
35 #include "target-select.h"
42 Position_dependent_options::default_options_
;
47 // This global variable is set up as General_options is constructed.
48 static std::vector
<const One_option
*> registered_options
;
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];
58 One_option::register_option()
60 registered_options
.push_back(this);
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
65 if (long_options
== NULL
)
66 long_options
= new Option_map
;
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;
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;
79 One_option::print() const
84 if (this->shortname
!= '\0')
86 len
+= printf("-%c", this->shortname
);
89 // -z takes long-names only.
90 gold_assert(this->dashes
!= DASH_Z
);
91 len
+= printf(" %s", gettext(this->helparg
));
95 if (!this->longname
.empty()
96 && !(this->longname
[0] == this->shortname
97 && this->longname
[1] == '\0'))
101 switch (this->dashes
)
103 case options::ONE_DASH
: case options::EXACTLY_ONE_DASH
:
106 case options::TWO_DASHES
: case options::EXACTLY_TWO_DASHES
:
109 case options::DASH_Z
:
110 len
+= printf("-z ");
115 len
+= printf("%s", this->longname
.c_str());
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
? '=' : ' ',
121 gettext(this->helparg
));
130 for (; len
< 30; ++len
)
133 // TODO: if we're boolean, add " (default)" when appropriate.
134 printf("%s\n", gettext(this->helpstring
));
140 printf(_("Usage: %s [options] file...\nOptions:\n"), gold::program_name
);
142 std::vector
<const One_option
*>::const_iterator it
;
143 for (it
= registered_options
.begin(); it
!= registered_options
.end(); ++it
)
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();
158 // For bool, arg will be NULL (boolean options take no argument);
159 // we always just set to true.
161 parse_bool(const char*, const char*, bool* retval
)
167 parse_uint(const char* option_name
, const char* arg
, int* retval
)
170 *retval
= strtol(arg
, &endptr
, 0);
171 if (*endptr
!= '\0' || retval
< 0)
172 gold_fatal(_("%s: invalid option value (expected an integer): %s"),
177 parse_uint64(const char* option_name
, const char* arg
, uint64_t *retval
)
180 *retval
= strtoull(arg
, &endptr
, 0);
182 gold_fatal(_("%s: invalid option value (expected an integer): %s"),
187 parse_double(const char* option_name
, const char* arg
, double* retval
)
190 *retval
= strtod(arg
, &endptr
);
192 gold_fatal(_("%s: invalid option value "
193 "(expected a floating point number): %s"),
198 parse_string(const char* option_name
, const char* arg
, const char** retval
)
201 gold_fatal(_("%s: must take a non-empty argument"), option_name
);
206 parse_optional_string(const char*, const char* arg
, const char** retval
)
212 parse_dirlist(const char*, const char* arg
, Dir_list
* retval
)
214 retval
->push_back(Search_directory(arg
, false));
218 parse_choices(const char* option_name
, const char* arg
, const char** retval
,
219 const char* choices
[], int num_choices
)
221 for (int i
= 0; i
< num_choices
; i
++)
222 if (strcmp(choices
[i
], arg
) == 0)
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
++)
232 choices_list
+= choices
[i
];
233 if (i
!= num_choices
- 1)
234 choices_list
+= ", ";
236 gold_fatal(_("%s: must take one of the following arguments: %s"),
237 option_name
, choices_list
.c_str());
240 } // End namespace options.
242 // Define the handler for "special" options (set via DEFINE_special).
245 General_options::parse_help(const char*, const char*, Command_line
*)
248 ::exit(EXIT_SUCCESS
);
252 General_options::parse_version(const char* opt
, const char*, Command_line
*)
254 gold::print_version(opt
[0] == '-' && opt
[1] == 'v');
255 ::exit(EXIT_SUCCESS
);
259 General_options::parse_Bstatic(const char*, const char*, Command_line
*)
261 this->set_Bdynamic(false);
265 General_options::parse_defsym(const char*, const char* arg
,
266 Command_line
* cmdline
)
268 cmdline
->script_options().define_symbol(arg
);
272 General_options::parse_library(const char*, const char* arg
,
273 Command_line
* cmdline
)
275 Input_file_argument
file(arg
, true, "", false, *this);
276 cmdline
->inputs().add_file(file
);
280 General_options::parse_R(const char* option
, const char* arg
,
281 Command_line
* cmdline
)
284 if (::stat(arg
, &s
) != 0 || S_ISDIR(s
.st_mode
))
285 this->add_to_rpath(arg
);
287 this->parse_just_symbols(option
, arg
, cmdline
);
291 General_options::parse_just_symbols(const char*, const char* arg
,
292 Command_line
* cmdline
)
294 Input_file_argument
file(arg
, false, "", true, *this);
295 cmdline
->inputs().add_file(file
);
299 General_options::parse_static(const char*, const char*, Command_line
*)
301 this->set_static(true);
305 General_options::parse_script(const char*, const char* arg
,
306 Command_line
* cmdline
)
308 if (!read_commandline_script(arg
, cmdline
))
309 gold::gold_fatal(_("unable to parse script file %s"), arg
);
313 General_options::parse_version_script(const char*, const char* arg
,
314 Command_line
* cmdline
)
316 if (!read_version_script(arg
, cmdline
))
317 gold::gold_fatal(_("unable to parse version script file %s"), arg
);
321 General_options::parse_start_group(const char*, const char*,
322 Command_line
* cmdline
)
324 cmdline
->inputs().start_group();
328 General_options::parse_end_group(const char*, const char*,
329 Command_line
* cmdline
)
331 cmdline
->inputs().end_group();
334 } // End namespace gold.
343 _("%s: use the --help option for usage information\n"),
345 ::exit(EXIT_FAILURE
);
349 usage(const char* msg
, const char *opt
)
353 gold::program_name
, opt
, msg
);
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",
364 gold::General_options::Object_format
365 string_to_object_format(const char* arg
)
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
;
373 gold::gold_error(_("format '%s' not supported; treating as elf "
374 "(supported formats: elf, binary)"),
376 return gold::General_options::OBJECT_FORMAT_ELF
;
380 // If the default sysroot is relocatable, try relocating it based on
384 get_relative_sysroot(const char* from
)
386 char* path
= make_relative_prefix(gold::program_name
, from
,
391 if (::stat(path
, &s
) == 0 && S_ISDIR(s
.st_mode
))
399 // Return the default sysroot. This is set by the --with-sysroot
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.
405 get_default_sysroot()
407 const char* sysroot
= TARGET_SYSTEM_ROOT
;
408 if (*sysroot
== '\0')
411 if (TARGET_SYSTEM_ROOT_RELOCATABLE
)
413 char* path
= get_relative_sysroot(BINDIR
);
415 path
= get_relative_sysroot(TOOLBINDIR
);
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
)
436 const char* const this_argv
= argv
[*i
];
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
));
443 gold::options::Option_map::iterator it
444 = gold::options::long_options
->find(option
);
445 if (it
== gold::options::long_options
->end())
448 gold::options::One_option
* retval
= it
->second
;
450 // If the dash-count doesn't match, we fail.
451 if (this_argv
[0] != '-') // no dashes at all: had better be "-z <longopt>"
453 if (retval
->dashes
!= gold::options::DASH_Z
)
456 else if (this_argv
[1] != '-') // one dash
458 if (retval
->dashes
!= gold::options::ONE_DASH
459 && retval
->dashes
!= gold::options::EXACTLY_ONE_DASH
460 && retval
->dashes
!= gold::options::TWO_DASHES
)
463 else // two dashes (or more!)
465 if (retval
->dashes
!= gold::options::TWO_DASHES
466 && retval
->dashes
!= gold::options::EXACTLY_TWO_DASHES
467 && retval
->dashes
!= gold::options::ONE_DASH
)
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.
475 // Figure out the option's argument, if any.
476 if (!retval
->takes_argument())
479 usage(_("unexpected argument"), this_argv
);
487 else if (retval
->takes_optional_argument())
488 *arg
= retval
->default_value
;
489 else if (*i
< argc
&& !equals_only
)
492 usage(_("missing argument"), this_argv
);
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
)
512 const char* const this_argv
= argv
[*i
];
514 if (this_argv
[0] != '-')
517 // We handle -z as a special case.
518 static gold::options::One_option
dash_z("", gold::options::DASH_Z
,
519 'z', "", "-z", "Z-OPTION", false,
521 gold::options::One_option
* retval
= NULL
;
522 if (this_argv
[pos_in_argv_i
] == 'z')
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
];
534 // Figure out the option's argument, if any.
535 if (!retval
->takes_argument())
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')
544 // If we take an argument, we'll eat up this entire argv entry.
546 if (this_argv
[pos_in_argv_i
+ 1] != '\0')
547 *arg
= this_argv
+ pos_in_argv_i
+ 1;
548 else if (retval
->takes_optional_argument())
549 *arg
= retval
->default_value
;
553 usage(_("missing argument"), this_argv
);
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
)
561 const char* dash_z_arg
= *arg
;
562 retval
= parse_long_option(1, arg
, true, arg
, &dummy_i
);
564 usage(_("unknown -z option"), dash_z_arg
);
570 } // End anonymous namespace.
575 General_options::General_options()
576 : execstack_status_(General_options::EXECSTACK_FROM_INPUT
), static_(false),
581 General_options::Object_format
582 General_options::format_enum() const
584 return string_to_object_format(this->format());
587 General_options::Object_format
588 General_options::oformat_enum() const
590 return string_to_object_format(this->oformat());
593 // Add the sysroot, if any, to the search paths.
596 General_options::add_sysroot()
598 if (this->sysroot() == NULL
|| this->sysroot()[0] == '\0')
600 this->set_sysroot(get_default_sysroot());
601 if (this->sysroot() == NULL
|| this->sysroot()[0] == '\0')
605 char* canonical_sysroot
= lrealpath(this->sysroot());
607 for (Dir_list::iterator p
= this->library_path_
.value
.begin();
608 p
!= this->library_path_
.value
.end();
610 p
->add_sysroot(this->sysroot(), canonical_sysroot
);
612 free(canonical_sysroot
);
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.
620 General_options::finalize()
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);
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())
634 this->set_strip_all(false);
635 gold_assert(this->strip_debug());
638 // For us, -dc and -dp are synonyms for --define-common.
640 this->set_define_common(true);
642 this->set_define_common(true);
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());
649 // execstack_status_ is a three-state variable; update it based on
651 if (this->execstack())
652 this->set_execstack_status(EXECSTACK_YES
);
653 else if (this->noexecstack())
654 this->set_execstack_status(EXECSTACK_NO
);
656 // Handle the optional argument for --demangle.
657 if (this->user_set_demangle())
659 this->set_do_demangle(true);
660 const char* style
= this->demangle();
663 enum demangling_styles style_code
;
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
);
671 else if (this->user_set_no_demangle())
672 this->set_do_demangle(false);
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
);
680 // If --thread_count is specified, it applies to
681 // --thread-count-{initial,middle,final}, though it doesn't override
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());
690 // Let's warn if you set the thread-count but we're going to ignore it.
691 #ifndef ENABLE_THREADS
694 gold_warning(_("ignoring --threads: "
695 "%s was compiled without thread support"),
697 this->set_threads(false);
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"),
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");
711 // Normalize library_path() by adding the sysroot to all directories
712 // in the path, as appropriate.
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"));
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"));
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 "
728 this->hash_bucket_empty_fraction());
730 // FIXME: we can/should be doing a lot more sanity checking here.
733 // Search_directory methods.
735 // This is called if we have a sysroot. Apply the sysroot if
736 // appropriate. Record whether the directory is in the sysroot.
739 Search_directory::add_sysroot(const char* sysroot
,
740 const char* canonical_sysroot
)
742 gold_assert(*sysroot
!= '\0');
743 if (this->put_in_sysroot_
)
745 if (!IS_DIR_SEPARATOR(this->name_
[0])
746 && !IS_DIR_SEPARATOR(sysroot
[strlen(sysroot
) - 1]))
747 this->name_
= '/' + this->name_
;
748 this->name_
= sysroot
+ this->name_
;
749 this->is_in_sysroot_
= true;
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
760 && IS_DIR_SEPARATOR(canonical_name
[canonical_sysroot_len
]))
762 canonical_name
[canonical_sysroot_len
] = '\0';
763 if (FILENAME_CMP(canonical_name
, canonical_sysroot
) == 0)
764 this->is_in_sysroot_
= true;
766 free(canonical_name
);
770 // Input_arguments methods.
772 // Add a file to the list.
775 Input_arguments::add_file(const Input_file_argument
& file
)
777 if (!this->in_group_
)
778 this->input_argument_list_
.push_back(Input_argument(file
));
781 gold_assert(!this->input_argument_list_
.empty());
782 gold_assert(this->input_argument_list_
.back().is_group());
783 this->input_argument_list_
.back().group()->add_file(file
);
790 Input_arguments::start_group()
793 gold_fatal(_("May not nest groups"));
794 Input_file_group
* group
= new Input_file_group();
795 this->input_argument_list_
.push_back(Input_argument(group
));
796 this->in_group_
= true;
802 Input_arguments::end_group()
804 if (!this->in_group_
)
805 gold_fatal(_("Group end without group start"));
806 this->in_group_
= false;
809 // Command_line options.
811 Command_line::Command_line()
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
823 Command_line::process_one_option(int argc
, const char** argv
, int i
,
824 bool* no_more_options
)
826 gold_assert(argv
[i
][0] == '-' && !(*no_more_options
));
828 // If we are reading "--", then just set no_more_options and return.
829 if (argv
[i
][1] == '-' && argv
[i
][2] == '\0')
831 *no_more_options
= true;
836 options::One_option
* option
= NULL
;
837 const char* arg
= NULL
;
839 // First, try to process argv as a long option.
840 option
= parse_long_option(argc
, argv
, false, &arg
, &new_i
);
843 option
->reader
->parse_to_value(argv
[i
], arg
, this, &this->options_
);
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;
853 option
= parse_short_option(argc
, argv
, pos_in_argv_i
, &arg
, &new_i
);
856 option
->reader
->parse_to_value(argv
[i
], arg
, this, &this->options_
);
862 // I guess it's neither a long option nor a short option.
863 usage(_("unknown option"), argv
[i
]);
869 Command_line::process(int argc
, const char** argv
)
871 bool no_more_options
= false;
875 this->position_options_
.copy_from_options(this->options());
876 if (no_more_options
|| argv
[i
][0] != '-')
878 Input_file_argument
file(argv
[i
], false, "", false,
879 this->position_options_
);
880 this->inputs_
.add_file(file
);
884 i
= process_one_option(argc
, argv
, i
, &no_more_options
);
887 if (this->inputs_
.in_group())
889 fprintf(stderr
, _("%s: missing group end\n"), program_name
);
893 // Normalize the options and ensure they don't contradict each other.
894 this->options_
.finalize();
897 } // End namespace gold.