X-Git-Url: http://git.efficios.com/?a=blobdiff_plain;f=gold%2Foptions.cc;h=d92394120a54f2cafdb40274c574688929b3f0c8;hb=260bcd09bfb98ebc5d8f0eb564edca21872e9f7f;hp=20ddc6ad386aadeb16adec2dabc2423358d45ddd;hpb=bc2c67ffde4dd6cf884aa5fcb93e1fc8b3cb16a8;p=deliverable%2Fbinutils-gdb.git diff --git a/gold/options.cc b/gold/options.cc index 20ddc6ad38..d92394120a 100644 --- a/gold/options.cc +++ b/gold/options.cc @@ -1,6 +1,6 @@ // options.c -- handle command line options for gold -// Copyright 2006, 2007 Free Software Foundation, Inc. +// Copyright (C) 2006-2019 Free Software Foundation, Inc. // Written by Ian Lance Taylor . // This file is part of gold. @@ -22,620 +22,1357 @@ #include "gold.h" +#include #include +#include +#include +#include #include #include #include "filenames.h" #include "libiberty.h" +#include "demangle.h" +#include "../bfd/bfdver.h" #include "debug.h" +#include "script.h" +#include "target-select.h" #include "options.h" +#include "plugin.h" namespace gold { -// The information we keep for a single command line option. +General_options +Position_dependent_options::default_options_; -struct options::One_option +namespace options { - // The single character option name, or '\0' if this is only a long - // option. - char short_option; - // The long option name, or NULL if this is only a short option. - const char* long_option; +// This flag is TRUE if we should register the command-line options as they +// are constructed. It is set after construction of the options within +// class Position_dependent_options. +static bool ready_to_register = false; - // Description of the option for --help output, or NULL if there is none. - const char* doc; +// This global variable is set up as General_options is constructed. +static std::vector registered_options; - // How to print the option name in --help output, or NULL to use the - // default. - const char* help_output; +// These are set up at the same time -- the variables that accept one +// dash, two, or require -z. A single variable may be in more than +// one of these data structures. +typedef Unordered_map Option_map; +static Option_map* long_options = NULL; +static One_option* short_options[128]; - // Long option dash control. This is ignored if long_option is - // NULL. - enum +void +One_option::register_option() +{ + if (!ready_to_register) + return; + + registered_options.push_back(this); + + // We can't make long_options a static Option_map because we can't + // guarantee that will be initialized before register_option() is + // first called. + if (long_options == NULL) + long_options = new Option_map; + + // TWO_DASHES means that two dashes are preferred, but one is ok too. + if (!this->longname.empty()) + (*long_options)[this->longname] = this; + + const int shortname_as_int = static_cast(this->shortname); + gold_assert(shortname_as_int >= 0 && shortname_as_int < 128); + if (this->shortname != '\0') + { + gold_assert(short_options[shortname_as_int] == NULL); + short_options[shortname_as_int] = this; + } +} + +void +One_option::print() const +{ + bool comma = false; + printf(" "); + int len = 2; + if (this->shortname != '\0') + { + len += printf("-%c", this->shortname); + if (this->helparg) + { + // -z takes long-names only. + gold_assert(this->dashes != DASH_Z); + len += printf(" %s", gettext(this->helparg)); + } + comma = true; + } + if (!this->longname.empty() + && !(this->longname[0] == this->shortname + && this->longname[1] == '\0')) + { + if (comma) + len += printf(", "); + switch (this->dashes) + { + case options::ONE_DASH: case options::EXACTLY_ONE_DASH: + len += printf("-"); + break; + case options::TWO_DASHES: case options::EXACTLY_TWO_DASHES: + len += printf("--"); + break; + case options::DASH_Z: + len += printf("-z "); + break; + default: + gold_unreachable(); + } + len += printf("%s", this->longname.c_str()); + if (this->helparg) + { + // For most options, we print "--frob FOO". But for -z + // we print "-z frob=FOO". + len += printf("%c%s", this->dashes == options::DASH_Z ? '=' : ' ', + gettext(this->helparg)); + } + } + + if (len >= 30) + { + printf("\n"); + len = 0; + } + for (; len < 30; ++len) + std::putchar(' '); + + printf("%s", gettext(this->helpstring)); + if (this->is_default) + printf(" (%s)", _("default")); + printf("\n"); +} + +void +help() +{ + printf(_("Usage: %s [options] file...\nOptions:\n"), gold::program_name); + + std::vector::const_iterator it; + for (it = registered_options.begin(); it != registered_options.end(); ++it) + (*it)->print(); + + // config.guess and libtool.m4 look in ld --help output for the + // string "supported targets". + printf(_("%s: supported targets:"), gold::program_name); + std::vector supported_names; + gold::supported_target_names(&supported_names); + for (std::vector::const_iterator p = supported_names.begin(); + p != supported_names.end(); + ++p) + printf(" %s", *p); + printf("\n"); + + printf(_("%s: supported emulations:"), gold::program_name); + supported_names.clear(); + gold::supported_emulation_names(&supported_names); + for (std::vector::const_iterator p = supported_names.begin(); + p != supported_names.end(); + ++p) + printf(" %s", *p); + printf("\n"); + + // REPORT_BUGS_TO is defined in bfd/bfdver.h. + const char* report = REPORT_BUGS_TO; + if (*report != '\0') + printf(_("Report bugs to %s\n"), report); +} + +// For bool, arg will be NULL (boolean options take no argument); +// we always just set to true. +void +parse_bool(const char*, const char*, bool* retval) +{ + *retval = true; +} + +void +parse_uint(const char* option_name, const char* arg, int* retval) +{ + char* endptr; + *retval = strtol(arg, &endptr, 0); + if (*endptr != '\0' || *retval < 0) + gold_fatal(_("%s: invalid option value (expected an integer): %s"), + option_name, arg); +} + +void +parse_int(const char* option_name, const char* arg, int* retval) +{ + char* endptr; + *retval = strtol(arg, &endptr, 0); + if (*endptr != '\0') + gold_fatal(_("%s: invalid option value (expected an integer): %s"), + option_name, arg); +} + +void +parse_uint64(const char* option_name, const char* arg, uint64_t* retval) +{ + char* endptr; + *retval = strtoull(arg, &endptr, 0); + if (*endptr != '\0') + gold_fatal(_("%s: invalid option value (expected an integer): %s"), + option_name, arg); +} + +void +parse_double(const char* option_name, const char* arg, double* retval) +{ + char* endptr; + *retval = strtod(arg, &endptr); + if (*endptr != '\0') + gold_fatal(_("%s: invalid option value " + "(expected a floating point number): %s"), + option_name, arg); +} + +void +parse_percent(const char* option_name, const char* arg, double* retval) +{ + char* endptr; + *retval = strtod(arg, &endptr) / 100.0; + if (*endptr != '\0') + gold_fatal(_("%s: invalid option value " + "(expected a floating point number): %s"), + option_name, arg); +} + +void +parse_string(const char* option_name, const char* arg, const char** retval) +{ + if (*arg == '\0') + gold_fatal(_("%s: must take a non-empty argument"), option_name); + *retval = arg; +} + +void +parse_optional_string(const char*, const char* arg, const char** retval) +{ + *retval = arg; +} + +void +parse_dirlist(const char*, const char* arg, Dir_list* retval) +{ + retval->push_back(Search_directory(arg, false)); +} + +void +parse_set(const char*, const char* arg, String_set* retval) +{ + retval->insert(std::string(arg)); +} + +void +parse_choices(const char* option_name, const char* arg, const char** retval, + const char* choices[], int num_choices) +{ + for (int i = 0; i < num_choices; i++) + if (strcmp(choices[i], arg) == 0) + { + *retval = arg; + return; + } + + // If we get here, the user did not enter a valid choice, so we die. + std::string choices_list; + for (int i = 0; i < num_choices; i++) { - // Long option normally takes one dash; two dashes are also - // accepted. - ONE_DASH, - // Long option normally takes two dashes; one dash is also - // accepted. - TWO_DASHES, - // Long option always takes two dashes. - EXACTLY_TWO_DASHES - } dash; - - // Function for special handling, or NULL. Returns the number of - // arguments to skip. This will normally be at least 1, but it may - // be 0 if this function changes *argv. ARG points to the location - // in *ARGV where the option starts, which may be helpful for a - // short option. - int (*special)(int argc, char** argv, char *arg, bool long_option, - Command_line*); - - // If this is a position independent option which does not take an - // argument, this is the member function to call to record it. - void (General_options::*general_noarg)(); - - // If this is a position independent function which takes an - // argument, this is the member function to call to record it. - void (General_options::*general_arg)(const char*); - - // If this is a position dependent option which does not take an - // argument, this is the member function to call to record it. - void (Position_dependent_options::*dependent_noarg)(); - - // If this is a position dependent option which takes an argument, - // this is the member function to record it. - void (Position_dependent_options::*dependent_arg)(const char*); - - // Return whether this option takes an argument. - bool - takes_argument() const - { return this->general_arg != NULL || this->dependent_arg != NULL; } -}; - -// We have a separate table for -z options. - -struct options::One_z_option -{ - // The name of the option. + choices_list += choices[i]; + if (i != num_choices - 1) + choices_list += ", "; + } + gold_fatal(_("%s: must take one of the following arguments: %s"), + option_name, choices_list.c_str()); +} + +} // End namespace options. + +// Define the handler for "special" options (set via DEFINE_special). + +void +General_options::parse_help(const char*, const char*, Command_line*) +{ + options::help(); + ::exit(EXIT_SUCCESS); +} + +void +General_options::parse_version(const char* opt, const char*, Command_line*) +{ + bool print_short = (opt[0] == '-' && opt[1] == 'v'); + gold::print_version(print_short); + this->printed_version_ = true; + if (!print_short) + ::exit(EXIT_SUCCESS); +} + +void +General_options::parse_V(const char*, const char*, Command_line*) +{ + gold::print_version(true); + this->printed_version_ = true; + + printf(_(" Supported targets:\n")); + std::vector supported_names; + gold::supported_target_names(&supported_names); + for (std::vector::const_iterator p = supported_names.begin(); + p != supported_names.end(); + ++p) + printf(" %s\n", *p); + + printf(_(" Supported emulations:\n")); + supported_names.clear(); + gold::supported_emulation_names(&supported_names); + for (std::vector::const_iterator p = supported_names.begin(); + p != supported_names.end(); + ++p) + printf(" %s\n", *p); +} + +void +General_options::parse_defsym(const char*, const char* arg, + Command_line* cmdline) +{ + cmdline->script_options().define_symbol(arg); +} + +void +General_options::parse_discard_all(const char*, const char*, + Command_line*) +{ + this->discard_locals_ = DISCARD_ALL; +} + +void +General_options::parse_discard_locals(const char*, const char*, + Command_line*) +{ + this->discard_locals_ = DISCARD_LOCALS; +} + +void +General_options::parse_discard_none(const char*, const char*, + Command_line*) +{ + this->discard_locals_ = DISCARD_NONE; +} + +void +General_options::parse_incremental(const char*, const char*, + Command_line*) +{ + this->incremental_mode_ = INCREMENTAL_AUTO; +} + +void +General_options::parse_no_incremental(const char*, const char*, + Command_line*) +{ + this->incremental_mode_ = INCREMENTAL_OFF; +} + +void +General_options::parse_incremental_full(const char*, const char*, + Command_line*) +{ + this->incremental_mode_ = INCREMENTAL_FULL; +} + +void +General_options::parse_incremental_update(const char*, const char*, + Command_line*) +{ + this->incremental_mode_ = INCREMENTAL_UPDATE; +} + +void +General_options::parse_incremental_changed(const char*, const char*, + Command_line*) +{ + this->implicit_incremental_ = true; + this->incremental_disposition_ = INCREMENTAL_CHANGED; +} + +void +General_options::parse_incremental_unchanged(const char*, const char*, + Command_line*) +{ + this->implicit_incremental_ = true; + this->incremental_disposition_ = INCREMENTAL_UNCHANGED; +} + +void +General_options::parse_incremental_unknown(const char*, const char*, + Command_line*) +{ + this->implicit_incremental_ = true; + this->incremental_disposition_ = INCREMENTAL_CHECK; +} + +void +General_options::parse_incremental_startup_unchanged(const char*, const char*, + Command_line*) +{ + this->implicit_incremental_ = true; + this->incremental_startup_disposition_ = INCREMENTAL_UNCHANGED; +} + +void +General_options::parse_library(const char*, const char* arg, + Command_line* cmdline) +{ + Input_file_argument::Input_file_type type; const char* name; + if (arg[0] == ':') + { + type = Input_file_argument::INPUT_FILE_TYPE_SEARCHED_FILE; + name = arg + 1; + } + else + { + type = Input_file_argument::INPUT_FILE_TYPE_LIBRARY; + name = arg; + } + Input_file_argument file(name, type, "", false, *this); + cmdline->inputs().add_file(file); +} + +void +General_options::parse_plugin(const char*, const char* arg, + Command_line*) +{ + this->add_plugin(arg); +} + +// Parse --plugin-opt. + +void +General_options::parse_plugin_opt(const char*, const char* arg, + Command_line*) +{ + this->add_plugin_option(arg); +} + +void +General_options::parse_R(const char* option, const char* arg, + Command_line* cmdline) +{ + struct stat s; + if (::stat(arg, &s) != 0 || S_ISDIR(s.st_mode)) + this->add_to_rpath(arg); + else + this->parse_just_symbols(option, arg, cmdline); +} + +void +General_options::parse_just_symbols(const char*, const char* arg, + Command_line* cmdline) +{ + Input_file_argument file(arg, Input_file_argument::INPUT_FILE_TYPE_FILE, + "", true, *this); + cmdline->inputs().add_file(file); +} + +// Handle --section-start. + +void +General_options::parse_section_start(const char*, const char* arg, + Command_line*) +{ + const char* eq = strchr(arg, '='); + if (eq == NULL) + { + gold_error(_("invalid argument to --section-start; " + "must be SECTION=ADDRESS")); + return; + } + + std::string section_name(arg, eq - arg); + + ++eq; + const char* val_start = eq; + if (eq[0] == '0' && (eq[1] == 'x' || eq[1] == 'X')) + eq += 2; + if (*eq == '\0') + { + gold_error(_("--section-start address missing")); + return; + } + uint64_t addr = 0; + hex_init(); + for (; *eq != '\0'; ++eq) + { + if (!hex_p(*eq)) + { + gold_error(_("--section-start argument %s is not a valid hex number"), + val_start); + return; + } + addr <<= 4; + addr += hex_value(*eq); + } + + this->section_starts_[section_name] = addr; +} + +// Look up a --section-start value. + +bool +General_options::section_start(const char* secname, uint64_t* paddr) const +{ + if (this->section_starts_.empty()) + return false; + std::map::const_iterator p = + this->section_starts_.find(secname); + if (p == this->section_starts_.end()) + return false; + *paddr = p->second; + return true; +} + +void +General_options::parse_static(const char*, const char*, Command_line*) +{ + this->set_static(true); +} + +void +General_options::parse_script(const char*, const char* arg, + Command_line* cmdline) +{ + if (!read_commandline_script(arg, cmdline)) + gold::gold_fatal(_("unable to parse script file %s"), arg); +} + +void +General_options::parse_version_script(const char*, const char* arg, + Command_line* cmdline) +{ + if (!read_version_script(arg, cmdline)) + gold::gold_fatal(_("unable to parse version script file %s"), arg); +} + +void +General_options::parse_dynamic_list(const char*, const char* arg, + Command_line* cmdline) +{ + if (!read_dynamic_list(arg, cmdline, &this->dynamic_list_)) + gold::gold_fatal(_("unable to parse dynamic-list script file %s"), arg); + this->have_dynamic_list_ = true; +} + +void +General_options::parse_start_group(const char*, const char*, + Command_line* cmdline) +{ + cmdline->inputs().start_group(); +} + +void +General_options::parse_end_group(const char*, const char*, + Command_line* cmdline) +{ + cmdline->inputs().end_group(); +} + +void +General_options::parse_start_lib(const char*, const char*, + Command_line* cmdline) +{ + cmdline->inputs().start_lib(cmdline->position_dependent_options()); +} + +void +General_options::parse_end_lib(const char*, const char*, + Command_line* cmdline) +{ + cmdline->inputs().end_lib(); +} + +// The function add_excluded_libs() in ld/ldlang.c of GNU ld breaks up a list +// of names separated by commas or colons and puts them in a linked list. +// We implement the same parsing of names here but store names in an unordered +// map to speed up searching of names. + +void +General_options::parse_exclude_libs(const char*, const char* arg, + Command_line*) +{ + const char* p = arg; + + while (*p != '\0') + { + size_t length = strcspn(p, ",:"); + this->excluded_libs_.insert(std::string(p, length)); + p += (p[length] ? length + 1 : length); + } +} + +// The checking logic is based on the function check_excluded_libs() in +// ld/ldlang.c of GNU ld but our implementation is different because we use +// an unordered map instead of a linked list, which is what GNU ld uses. GNU +// ld searches sequentially in the excluded libs list. For a given archive, +// a match is found if the archive's name matches exactly one of the list +// entry or if the archive's name is of the form FOO.a and FOO matches exactly +// one of the list entry. An entry "ALL" in the list is considered as a +// wild-card and matches any given name. + +bool +General_options::check_excluded_libs(const std::string &name) const +{ + Unordered_set::const_iterator p; + + // Exit early for the most common case. + if (excluded_libs_.empty()) + return false; + + // If we see "ALL", all archives are excluded from automatic export. + p = excluded_libs_.find(std::string("ALL")); + if (p != excluded_libs_.end()) + return true; + + // First strip off any directories in name. + const char* basename = lbasename(name.c_str()); + + // Try finding an exact match. + p = excluded_libs_.find(std::string(basename)); + if (p != excluded_libs_.end()) + return true; + + // Try matching NAME without ".a" at the end. + size_t length = strlen(basename); + if ((length >= 2) + && (basename[length - 2] == '.') + && (basename[length - 1] == 'a')) + { + p = excluded_libs_.find(std::string(basename, length - 2)); + if (p != excluded_libs_.end()) + return true; + } + + return false; +} + +// Recognize input and output target names. The GNU linker accepts +// these with --format and --oformat. This code is intended to be +// minimally compatible. In practice for an ELF target this would be +// the same target as the input files; that name always start with +// "elf". Non-ELF targets would be "srec", "symbolsrec", "tekhex", +// "binary", "ihex". + +General_options::Object_format +General_options::string_to_object_format(const char* arg) +{ + if (strncmp(arg, "elf", 3) == 0 || strcmp(arg, "default") == 0) + return gold::General_options::OBJECT_FORMAT_ELF; + else if (strcmp(arg, "binary") == 0) + return gold::General_options::OBJECT_FORMAT_BINARY; + else + { + gold::gold_error(_("format '%s' not supported; treating as elf " + "(supported formats: elf, binary)"), + arg); + return gold::General_options::OBJECT_FORMAT_ELF; + } +} + +const char* +General_options::object_format_to_string(General_options::Object_format fmt) +{ + switch (fmt) + { + case General_options::OBJECT_FORMAT_ELF: + return "elf"; + case General_options::OBJECT_FORMAT_BINARY: + return "binary"; + default: + gold_unreachable(); + } +} + +void +General_options::parse_fix_v4bx(const char*, const char*, + Command_line*) +{ + this->fix_v4bx_ = FIX_V4BX_REPLACE; +} + +void +General_options::parse_fix_v4bx_interworking(const char*, const char*, + Command_line*) +{ + this->fix_v4bx_ = FIX_V4BX_INTERWORKING; +} + +void +General_options::parse_EB(const char*, const char*, Command_line*) +{ + this->endianness_ = ENDIANNESS_BIG; +} + +void +General_options::parse_EL(const char*, const char*, Command_line*) +{ + this->endianness_ = ENDIANNESS_LITTLE; +} + +void +General_options::copy_from_posdep_options( + const Position_dependent_options& posdep) +{ + this->set_as_needed(posdep.as_needed()); + this->set_Bdynamic(posdep.Bdynamic()); + this->set_format( + General_options::object_format_to_string(posdep.format_enum())); + this->set_whole_archive(posdep.whole_archive()); + this->set_incremental_disposition(posdep.incremental_disposition()); +} + +void +General_options::parse_push_state(const char*, const char*, Command_line*) +{ + Position_dependent_options* posdep = new Position_dependent_options(*this); + this->options_stack_.push_back(posdep); +} + +void +General_options::parse_pop_state(const char*, const char*, Command_line*) +{ + if (this->options_stack_.empty()) + { + gold::gold_error(_("unbalanced --push-state/--pop-state")); + return; + } + Position_dependent_options* posdep = this->options_stack_.back(); + this->options_stack_.pop_back(); + this->copy_from_posdep_options(*posdep); + delete posdep; +} + +} // End namespace gold. + +namespace +{ + +void +usage() +{ + fprintf(stderr, + _("%s: use the --help option for usage information\n"), + gold::program_name); + ::exit(EXIT_FAILURE); +} + +void +usage(const char* msg, const char* opt) +{ + fprintf(stderr, + _("%s: %s: %s\n"), + gold::program_name, opt, msg); + usage(); +} + +// If the default sysroot is relocatable, try relocating it based on +// the prefix FROM. + +static char* +get_relative_sysroot(const char* from) +{ + char* path = make_relative_prefix(gold::program_name, from, + TARGET_SYSTEM_ROOT); + if (path != NULL) + { + struct stat s; + if (::stat(path, &s) == 0 && S_ISDIR(s.st_mode)) + return path; + free(path); + } + + return NULL; +} + +// Return the default sysroot. This is set by the --with-sysroot +// option to configure. Note we do not free the return value of +// get_relative_sysroot, which is a small memory leak, but is +// necessary since we store this pointer directly in General_options. + +static const char* +get_default_sysroot() +{ + const char* sysroot = TARGET_SYSTEM_ROOT; + if (*sysroot == '\0') + return NULL; + + if (TARGET_SYSTEM_ROOT_RELOCATABLE) + { + char* path = get_relative_sysroot(BINDIR); + if (path == NULL) + path = get_relative_sysroot(TOOLBINDIR); + if (path != NULL) + return path; + } + + return sysroot; +} + +// Parse a long option. Such options have the form +// <-|-->