PR ld/10515
[binutils-gdb.git] / gold / options.cc
index 78e14dcd305545200af13d3ef4260d9f59ac3870..bf420c679fdef01dec75f757659984ed5316ffb7 100644 (file)
@@ -47,6 +47,11 @@ Position_dependent_options::default_options_;
 namespace options
 {
 
+// This flag is TRUE if we should register the command-line options as they
+// are constructed.  It is set after contruction of the options within
+// class Position_dependent_options.
+static bool ready_to_register = false;
+
 // This global variable is set up as General_options is constructed.
 static std::vector<const One_option*> registered_options;
 
@@ -60,6 +65,9 @@ static One_option* short_options[128];
 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
@@ -75,7 +83,10 @@ One_option::register_option()
   const int shortname_as_int = static_cast<int>(this->shortname);
   gold_assert(shortname_as_int >= 0 && shortname_as_int < 128);
   if (this->shortname != '\0')
-    short_options[shortname_as_int] = this;
+    {
+      gold_assert(short_options[shortname_as_int] == NULL);
+      short_options[shortname_as_int] = this;
+    }
 }
 
 void
@@ -273,6 +284,7 @@ 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<const char*> supported_names;
   gold::supported_target_names(&supported_names);
@@ -403,7 +415,7 @@ General_options::parse_end_group(const char*, const char*,
 }
 
 // The function add_excluded_libs() in ld/ldlang.c of GNU ld breaks up a list
-// of names seperated by commas or semi-colons and puts them in a linked list.
+// of names seperated 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.
 
@@ -444,18 +456,21 @@ General_options::check_excluded_libs (const std::string &name) const
   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(name);
+  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 = name.length();
+  size_t length = strlen(basename);
   if ((length >= 2)
-      && (name[length-2] == '.')
-      && (name[length-1] == 'a'))
+      && (basename[length - 2] == '.')
+      && (basename[length - 1] == 'a'))
     {
-      p = excluded_libs_.find(name.substr(0, length - 2));
+      p = excluded_libs_.find(std::string(basename, length - 2));
       if (p != excluded_libs_.end())
        return true;
     }
@@ -463,6 +478,29 @@ General_options::check_excluded_libs (const std::string &name) const
   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)
+    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;
+    }
+}
+
 } // End namespace gold.
 
 namespace
@@ -486,29 +524,6 @@ usage(const char* msg, const char *opt)
   usage();
 }
 
-// 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".
-
-gold::General_options::Object_format
-string_to_object_format(const char* arg)
-{
-  if (strncmp(arg, "elf", 3) == 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;
-    }
-}
-
 // If the default sysroot is relocatable, try relocating it based on
 // the prefix FROM.
 
@@ -705,22 +720,25 @@ namespace gold
 {
 
 General_options::General_options()
-  : execstack_status_(General_options::EXECSTACK_FROM_INPUT), static_(false),
+  : printed_version_(false),
+    execstack_status_(General_options::EXECSTACK_FROM_INPUT), static_(false),
     do_demangle_(false), plugins_(),
     incremental_disposition_(INCREMENTAL_CHECK), implicit_incremental_(false)
 {
+  // Turn off option registration once construction is complete.
+  gold::options::ready_to_register = false;
 }
 
 General_options::Object_format
 General_options::format_enum() const
 {
-  return string_to_object_format(this->format());
+  return General_options::string_to_object_format(this->format());
 }
 
 General_options::Object_format
 General_options::oformat_enum() const
 {
-  return string_to_object_format(this->oformat());
+  return General_options::string_to_object_format(this->oformat());
 }
 
 // Add the sysroot, if any, to the search paths.
@@ -1034,6 +1052,13 @@ Command_line::Command_line()
 {
 }
 
+// Pre_options is the hook that sets the ready_to_register flag.
+
+Command_line::Pre_options::Pre_options()
+{
+  gold::options::ready_to_register = true;
+}
+
 // Process the command line options.  For process_one_option, i is the
 // index of argv to process next, and must be an option (that is,
 // start with a dash).  The return value is the index of the next