Implement -Bstatic/-Bdynamic.
[deliverable/binutils-gdb.git] / gold / options.cc
index b8339e8a625390ba35f89b31bcc9fd8104876d2d..4e74dcefa8a33e661a1913e44c117b21648e7baf 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
@@ -138,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
            {
@@ -150,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)
@@ -171,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;
@@ -188,11 +215,66 @@ help(int, char**, char*, gold::Command_line*)
       std::puts(options[i].doc);
     }
 
-  gold::gold_exit(true);
+  ::exit(0);
+
+  return 0;
+}
+
+// Report version information.
 
+int
+version(int, char**, char* opt, gold::Command_line*)
+{
+  gold::print_version(opt[0] == 'v' && opt[1] == '\0');
+  ::exit(0);
   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
@@ -224,16 +306,32 @@ namespace gold
 const options::One_option
 options::Command_line_options::options[] =
 {
-  SPECIAL('l', "library", N_("Search for library LIBNAME"),
-         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),
+  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', "Bdynamic",
+              N_("-l searches for shared libraries"),
+              NULL, ONE_DASH,
+              &Position_dependent_options::set_dynamic_search),
+  POSDEP_NOARG('\0', "Bstatic",
+              N_("-l does not search for shared libraries"),
+              NULL, ONE_DASH,
+              &Position_dependent_options::set_static_search),
+  GENERAL_NOARG('\0', "Bsymbolic", N_("Bind defined symbols locally"),
+               NULL, ONE_DASH, &General_options::set_symbolic),
+  GENERAL_NOARG('E', "export-dynamic", N_("Export all dynamic symbols"),
+                NULL, TWO_DASHES, &General_options::set_export_dynamic),
+  GENERAL_NOARG('\0', "eh-frame-hdr", N_("Create exception frame header"),
+                NULL, TWO_DASHES, &General_options::set_create_eh_frame_hdr),
   GENERAL_ARG('I', "dynamic-linker", N_("Set dynamic linker path"),
              N_("-I PROGRAM, --dynamic-linker PROGRAM"), TWO_DASHES,
              &General_options::set_dynamic_linker),
+  SPECIAL('l', "library", N_("Search for library LIBNAME"),
+         N_("-lLIBNAME, --library LIBNAME"), TWO_DASHES,
+         &library),
   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),
@@ -242,20 +340,68 @@ options::Command_line_options::options[] =
   GENERAL_ARG('o', "output", N_("Set output file name"),
              N_("-o FILE, --output FILE"), TWO_DASHES,
              &General_options::set_output_file_name),
+  GENERAL_ARG('O', NULL, N_("Optimize output file size"),
+             N_("-O level"), ONE_DASH,
+             &General_options::set_optimization_level),
   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_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('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', "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),
-  POSDEP_NOARG('\0', "as-needed",
-              N_("Only set DT_NEEDED for following dynamic libs if used"),
-              NULL, TWO_DASHES, &Position_dependent_options::set_as_needed),
-  POSDEP_NOARG('\0', "no-as-needed",
-              N_("Always DT_NEEDED for following dynamic libs (default)"),
-              NULL, TWO_DASHES, &Position_dependent_options::clear_as_needed),
+  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),
+  GENERAL_ARG('\0', "Ttext", N_("Set the address of the .text section"),
+              N_("-Ttext ADDRESS"), ONE_DASH,
+              &General_options::set_text_segment_address),
+  GENERAL_NOARG('\0', "threads", N_("Run the linker multi-threaded"),
+               NULL, TWO_DASHES, &General_options::set_threads),
+  GENERAL_NOARG('\0', "no-threads", N_("Do not run the linker multi-threaded"),
+               NULL, TWO_DASHES, &General_options::clear_threads),
+  GENERAL_ARG('\0', "thread-count", N_("Number of threads to use"),
+             N_("--thread-count COUNT"), TWO_DASHES,
+             &General_options::set_thread_count),
+  GENERAL_ARG('\0', "thread-count-initial",
+             N_("Number of threads to use in initial pass"),
+             N_("--thread-count-initial COUNT"), TWO_DASHES,
+             &General_options::set_thread_count_initial),
+  GENERAL_ARG('\0', "thread-count-middle",
+             N_("Number of threads to use in middle pass"),
+             N_("--thread-count-middle COUNT"), TWO_DASHES,
+             &General_options::set_thread_count_middle),
+  GENERAL_ARG('\0', "thread-count-final",
+             N_("Number of threads to use in final pass"),
+             N_("--thread-count-final COUNT"), TWO_DASHES,
+             &General_options::set_thread_count_final),
+  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('(', "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),
   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 =
@@ -264,20 +410,96 @@ const int options::Command_line_options::options_size =
 // The default values for the general options.
 
 General_options::General_options()
-  : dynamic_linker_(NULL),
+  : export_dynamic_(false),
+    dynamic_linker_(NULL),
     search_path_(),
+    optimization_level_(0),
     output_file_name_("a.out"),
     is_relocatable_(false),
+    strip_(STRIP_NONE),
+    symbolic_(false),
+    create_eh_frame_hdr_(false),
+    rpath_(),
+    rpath_link_(),
     is_shared_(false),
-    is_static_(false)
+    is_static_(false),
+    print_stats_(false),
+    sysroot_(),
+    text_segment_address_(-1U),   // -1 indicates value not set by user
+    threads_(false),
+    thread_count_initial_(0),
+    thread_count_middle_(0),
+    thread_count_final_(0)
 {
 }
 
 // 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)
+{
+}
+
+// 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.
@@ -360,8 +582,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];
@@ -384,6 +610,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;
                    }
@@ -463,10 +691,32 @@ Command_line::process(int argc, char** argv)
     }
 
   // 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
@@ -498,7 +748,7 @@ Command_line::apply_option(const options::One_option& opt,
 void
 Command_line::add_file(const char* name, bool is_lib)
 {
-  Input_file_argument file(name, is_lib, this->position_options_);
+  Input_file_argument file(name, is_lib, "", this->position_options_);
   this->inputs_.add_file(file);
 }
 
@@ -555,7 +805,7 @@ Command_line::usage()
   fprintf(stderr,
          _("%s: use the --help option for usage information\n"),
          program_name);
-  gold_exit(false);
+  ::exit(1);
 }
 
 void
This page took 0.028693 seconds and 4 git commands to generate.