Add --stats option to print runtime and memory usage statistics.
[deliverable/binutils-gdb.git] / gold / options.cc
index 8e0465f2af919cc7eb16a70492e91c7ab1796876..89eecf66bb8635b01e732535ed2c8808f8bda6b4 100644 (file)
@@ -1,8 +1,32 @@
 // options.c -- handle command line options for gold
 
-#include <iostream>
+// Copyright 2006, 2007 Free Software Foundation, Inc.
+// Written by Ian Lance Taylor <iant@google.com>.
+
+// This file is part of gold.
+
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 3 of the License, or
+// (at your option) any later version.
+
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
+// MA 02110-1301, USA.
 
 #include "gold.h"
+
+#include <iostream>
+#include <sys/stat.h>
+#include "filenames.h"
+#include "libiberty.h"
+
 #include "options.h"
 
 namespace gold
@@ -89,6 +113,24 @@ library(int argc, char** argv, char* arg, gold::Command_line* cmdline)
   return cmdline->process_l_option(argc, argv, arg);
 }
 
+// Handle the special --start-group option.
+
+int
+start_group(int, char**, char* arg, gold::Command_line* cmdline)
+{
+  cmdline->start_group(arg);
+  return 1;
+}
+
+// Handle the special --end-group option.
+
+int
+end_group(int, char**, char* arg, gold::Command_line* cmdline)
+{
+  cmdline->end_group(arg);
+  return 1;
+}
+
 // Report usage information for ld --help, and exit.
 
 int
@@ -120,6 +162,7 @@ help(int, char**, char*, gold::Command_line*)
                }
              printf(options[j].help_output);
              len += std::strlen(options[i].help_output);
+              comma = true;
            }
          else
            {
@@ -132,6 +175,7 @@ help(int, char**, char*, gold::Command_line*)
                    }
                  printf("-%c", options[j].short_option);
                  len += 2;
+                  comma = true;
                }
 
              if (options[j].long_option != NULL)
@@ -153,13 +197,14 @@ help(int, char**, char*, gold::Command_line*)
                    }
                  printf("%s", options[j].long_option);
                  len += std::strlen(options[j].long_option);
+                  comma = true;
                }
            }
          ++j;
        }
       while (j < options_size && options[j].doc == NULL);
 
-      if (len > 30)
+      if (len >= 30)
        {
          printf("\n");
          len = 0;
@@ -175,6 +220,61 @@ help(int, char**, char*, gold::Command_line*)
   return 0;
 }
 
+// Report version information.
+
+int
+version(int, char**, char* opt, gold::Command_line*)
+{
+  gold::print_version(opt[0] == 'v' && opt[1] == '\0');
+  gold::gold_exit(true);
+  return 0;
+}
+
+// If the default sysroot is relocatable, try relocating it based on
+// the prefix FROM.
+
+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.
+
+std::string
+get_default_sysroot()
+{
+  const char* sysroot = TARGET_SYSTEM_ROOT;
+  if (*sysroot == '\0')
+    return "";
+
+  if (TARGET_SYSTEM_ROOT_RELOCATABLE)
+    {
+      char* path = get_relative_sysroot (BINDIR);
+      if (path == NULL)
+       path = get_relative_sysroot (TOOLBINDIR);
+      if (path != NULL)
+       {
+         std::string ret = path;
+         free(path);
+         return ret;
+       }
+    }
+
+  return sysroot;
+}
+
 } // End anonymous namespace.
 
 namespace gold
@@ -207,22 +307,69 @@ const options::One_option
 options::Command_line_options::options[] =
 {
   SPECIAL('l', "library", N_("Search for library LIBNAME"),
-         N_("-lLIBNAME --library LIBNAME"), TWO_DASHES,
+         N_("-lLIBNAME, --library LIBNAME"), TWO_DASHES,
          &library),
+  SPECIAL('(', "start-group", N_("Start a library search group"), NULL,
+         TWO_DASHES, &start_group),
+  SPECIAL(')', "end-group", N_("End a library search group"), NULL,
+         TWO_DASHES, &end_group),
+  GENERAL_NOARG('E', "export-dynamic", N_("Export all dynamic symbols"),
+                NULL, TWO_DASHES, &General_options::set_export_dynamic),
+  GENERAL_ARG('I', "dynamic-linker", N_("Set dynamic linker path"),
+             N_("-I PROGRAM, --dynamic-linker PROGRAM"), TWO_DASHES,
+             &General_options::set_dynamic_linker),
   GENERAL_ARG('L', "library-path", N_("Add directory to search path"),
              N_("-L DIR, --library-path DIR"), TWO_DASHES,
              &General_options::add_to_search_path),
+  GENERAL_ARG('m', NULL, N_("Ignored for compatibility"), NULL, ONE_DASH,
+             &General_options::ignore),
+  GENERAL_ARG('O', NULL, N_("Optimize output file size"),
+             N_("-O level"), ONE_DASH,
+             &General_options::set_optimization_level),
   GENERAL_ARG('o', "output", N_("Set output file name"),
              N_("-o FILE, --output FILE"), TWO_DASHES,
              &General_options::set_output_file_name),
   GENERAL_NOARG('r', NULL, N_("Generate relocatable output"), NULL,
                ONE_DASH, &General_options::set_relocatable),
+  GENERAL_ARG('R', "rpath", N_("Add DIR to runtime search path"),
+              N_("-R DIR, -rpath DIR"), ONE_DASH,
+              &General_options::add_to_rpath),
+  GENERAL_NOARG('s', "strip-all", N_("Strip all symbols"), NULL,
+               TWO_DASHES, &General_options::set_strip_all),
+  GENERAL_NOARG('S', "strip-debug", N_("Strip debugging information"), NULL,
+               TWO_DASHES, &General_options::set_strip_debug),
+  GENERAL_NOARG('\0', "eh-frame-hdr", N_("Create exception frame header"),
+                NULL, TWO_DASHES, &General_options::set_create_eh_frame_hdr),
+  GENERAL_ARG('\0', "rpath-link",
+              N_("Add DIR to link time shared library search path"),
+              N_("--rpath-link DIR"), TWO_DASHES,
+              &General_options::add_to_rpath_link),
   GENERAL_NOARG('\0', "shared", N_("Generate shared library"),
                NULL, ONE_DASH, &General_options::set_shared),
   GENERAL_NOARG('\0', "static", N_("Do not link against shared libraries"),
                NULL, ONE_DASH, &General_options::set_static),
+  GENERAL_NOARG('\0', "stats", N_("Print resource usage statistics"),
+               NULL, TWO_DASHES, &General_options::set_stats),
+  GENERAL_ARG('\0', "sysroot", N_("Set target system root directory"),
+             N_("--sysroot DIR"), TWO_DASHES, &General_options::set_sysroot),
+  POSDEP_NOARG('\0', "as-needed",
+              N_("Only set DT_NEEDED for dynamic libs if used"),
+              NULL, TWO_DASHES, &Position_dependent_options::set_as_needed),
+  POSDEP_NOARG('\0', "no-as-needed",
+              N_("Always DT_NEEDED for dynamic libs (default)"),
+              NULL, TWO_DASHES, &Position_dependent_options::clear_as_needed),
+  POSDEP_NOARG('\0', "whole-archive",
+               N_("Include all archive contents"),
+               NULL, TWO_DASHES,
+               &Position_dependent_options::set_whole_archive),
+  POSDEP_NOARG('\0', "no-whole-archive",
+               N_("Include only needed archive contents"),
+               NULL, TWO_DASHES,
+               &Position_dependent_options::clear_whole_archive),
   SPECIAL('\0', "help", N_("Report usage information"), NULL,
-         TWO_DASHES, &help)
+         TWO_DASHES, &help),
+  SPECIAL('v', "version", N_("Report version information"), NULL,
+         TWO_DASHES, &version)
 };
 
 const int options::Command_line_options::options_size =
@@ -231,24 +378,133 @@ const int options::Command_line_options::options_size =
 // The default values for the general options.
 
 General_options::General_options()
-  : search_path_(),
+  : export_dynamic_(false),
+    dynamic_linker_(NULL),
+    search_path_(),
+    optimization_level_(0),
     output_file_name_("a.out"),
     is_relocatable_(false),
+    strip_(STRIP_NONE),
+    create_eh_frame_hdr_(false),
+    rpath_(),
+    rpath_link_(),
     is_shared_(false),
-    is_static_(false)
+    is_static_(false),
+    print_stats_(false),
+    sysroot_()
 {
 }
 
 // The default values for the position dependent options.
 
 Position_dependent_options::Position_dependent_options()
-  : do_static_search_(false)
+  : do_static_search_(false),
+    as_needed_(false),
+    include_whole_archive_(false)
 {
 }
 
-// Construct a Command_line.
+// Add the sysroot, if any, to the search paths.
+
+void
+General_options::add_sysroot()
+{
+  if (this->sysroot_.empty())
+    {
+      this->sysroot_ = get_default_sysroot();
+      if (this->sysroot_.empty())
+       return;
+    }
+
+  const char* sysroot = this->sysroot_.c_str();
+  char* canonical_sysroot = lrealpath(sysroot);
+
+  for (Dir_list::iterator p = this->search_path_.begin();
+       p != this->search_path_.end();
+       ++p)
+    p->add_sysroot(sysroot, canonical_sysroot);
+
+  free(canonical_sysroot);
+}
+
+// Search_directory methods.
+
+// This is called if we have a sysroot.  Apply the sysroot if
+// appropriate.  Record whether the directory is in the sysroot.
+
+void
+Search_directory::add_sysroot(const char* sysroot,
+                             const char* canonical_sysroot)
+{
+  gold_assert(*sysroot != '\0');
+  if (this->put_in_sysroot_)
+    {
+      if (!IS_DIR_SEPARATOR(this->name_[0])
+         && !IS_DIR_SEPARATOR(sysroot[strlen(sysroot) - 1]))
+       this->name_ = '/' + this->name_;
+      this->name_ = sysroot + this->name_;
+      this->is_in_sysroot_ = true;
+    }
+  else
+    {
+      // Check whether this entry is in the sysroot.  To do this
+      // correctly, we need to use canonical names.  Otherwise we will
+      // get confused by the ../../.. paths that gcc tends to use.
+      char* canonical_name = lrealpath(this->name_.c_str());
+      int canonical_name_len = strlen(canonical_name);
+      int canonical_sysroot_len = strlen(canonical_sysroot);
+      if (canonical_name_len > canonical_sysroot_len
+         && IS_DIR_SEPARATOR(canonical_name[canonical_sysroot_len]))
+       {
+         canonical_name[canonical_sysroot_len] = '\0';
+         if (FILENAME_CMP(canonical_name, canonical_sysroot) == 0)
+           this->is_in_sysroot_ = true;
+       }
+      free(canonical_name);
+    }
+}
+
+// Input_arguments methods.
+
+// Add a file to the list.
+
+void
+Input_arguments::add_file(const Input_file_argument& file)
+{
+  if (!this->in_group_)
+    this->input_argument_list_.push_back(Input_argument(file));
+  else
+    {
+      gold_assert(!this->input_argument_list_.empty());
+      gold_assert(this->input_argument_list_.back().is_group());
+      this->input_argument_list_.back().group()->add_file(file);
+    }
+}
+
+// Start a group.
+
+void
+Input_arguments::start_group()
+{
+  gold_assert(!this->in_group_);
+  Input_file_group* group = new Input_file_group();
+  this->input_argument_list_.push_back(Input_argument(group));
+  this->in_group_ = true;
+}
+
+// End a group.
+
+void
+Input_arguments::end_group()
+{
+  gold_assert(this->in_group_);
+  this->in_group_ = false;
+}
+
+// Command_line options.
 
 Command_line::Command_line()
+  : options_(), position_options_(), inputs_()
 {
 }
 
@@ -266,8 +522,7 @@ Command_line::process(int argc, char** argv)
     {
       if (argv[i][0] != '-' || no_more_options)
        {
-         this->inputs_.push_back(Input_argument(argv[i], false,
-                                                this->position_options_));
+         this->add_file(argv[i], false);
          ++i;
          continue;
        }
@@ -289,8 +544,12 @@ Command_line::process(int argc, char** argv)
       char first = opt[0];
       int skiparg = 0;
       char* arg = strchr(opt, '=');
+      bool argument_with_equals = arg != NULL;
       if (arg != NULL)
-       *arg = '\0';
+       {
+         *arg = '\0';
+         ++arg;
+       }
       else if (i + 1 < argc)
        {
          arg = argv[i + 1];
@@ -313,6 +572,8 @@ Command_line::process(int argc, char** argv)
                {
                  if (!options[j].takes_argument())
                    {
+                     if (argument_with_equals)
+                       this->usage(_("unexpected argument"), argv[i]);
                      arg = NULL;
                      skiparg = 0;
                    }
@@ -385,11 +646,39 @@ Command_line::process(int argc, char** argv)
        }
     }
 
+  if (this->inputs_.in_group())
+    {
+      fprintf(stderr, _("%s: missing group end"), program_name);
+      this->usage();
+    }
+
   // FIXME: We should only do this when configured in native mode.
-  this->options_.add_to_search_path("/lib");
-  this->options_.add_to_search_path("/usr/lib");
+  this->options_.add_to_search_path_with_sysroot("/lib");
+  this->options_.add_to_search_path_with_sysroot("/usr/lib");
+
+  this->options_.add_sysroot();
+
+  // Ensure options don't contradict each other and are otherwise kosher.
+  this->normalize_options();
+}
+
+// Ensure options don't contradict each other and are otherwise kosher.
+
+void
+Command_line::normalize_options()
+{
+  // If the user specifies both -s and -r, convert the -s as -S.
+  // -r requires us to keep externally visible symbols!
+  if (this->options_.strip_all() && this->options_.is_relocatable())
+    {
+      // Clears the strip_all() status, replacing it with strip_debug().
+      this->options_.set_strip_debug();
+    }
+
+  // FIXME: we can/should be doing a lot more sanity checking here.
 }
 
+
 // Apply a command line option.
 
 void
@@ -416,6 +705,15 @@ Command_line::apply_option(const options::One_option& opt,
     }
 }
 
+// Add an input file or library.
+
+void
+Command_line::add_file(const char* name, bool is_lib)
+{
+  Input_file_argument file(name, is_lib, "", this->position_options_);
+  this->inputs_.add_file(file);
+}
+
 // Handle the -l option, which requires special treatment.
 
 int
@@ -436,12 +734,31 @@ Command_line::process_l_option(int argc, char** argv, char* arg)
   else
     this->usage(_("missing argument"), arg);
 
-  this->inputs_.push_back(Input_argument(libname, true,
-                                        this->position_options_));
+  this->add_file(libname, true);
 
   return ret;
 }
 
+// Handle the --start-group option.
+
+void
+Command_line::start_group(const char* arg)
+{
+  if (this->inputs_.in_group())
+    this->usage(_("may not nest groups"), arg);
+  this->inputs_.start_group();
+}
+
+// Handle the --end-group option.
+
+void
+Command_line::end_group(const char* arg)
+{
+  if (!this->inputs_.in_group())
+    this->usage(_("group end without group start"), arg);
+  this->inputs_.end_group();
+}
+
 // Report a usage error.  */
 
 void
This page took 0.029227 seconds and 4 git commands to generate.