daily update
[external/binutils.git] / gold / main.cc
index 49b50b2..a6c9872 100644 (file)
@@ -1,6 +1,6 @@
 // main.cc -- gold main function.
 
-// Copyright 2006, 2007 Free Software Foundation, Inc.
+// Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
 // Written by Ian Lance Taylor <iant@google.com>.
 
 // This file is part of gold.
 #endif
 #include "libiberty.h"
 
+#include "script.h"
 #include "options.h"
 #include "parameters.h"
+#include "errors.h"
 #include "dirsearch.h"
 #include "workqueue.h"
 #include "object.h"
 
 using namespace gold;
 
+// This function emits the commandline to a hard-coded file in temp.
+// This is useful for debugging since ld is typically invoked by gcc,
+// so its commandline is not always easy to extract.  You should be
+// able to run 'gcc -B... foo.o -o foo' to invoke this linker the
+// first time, and then /tmp/ld-run-foo.sh to invoke it on subsequent
+// runes.  "/tmp/ld-run-foo.sh debug" will run the linker inside gdb
+// (or whatever value the environment variable GDB is set to), for
+// even easier debugging.  Since this is a debugging-only tool, and
+// creates files, it is only turned on when the user explicitly asks
+// for it, by compiling with -DDEBUG.  Do not do this for release
+// versions of the linker!
+
+#ifdef DEBUG
+#include <stdio.h>
+#include <sys/stat.h>    // for chmod()
+
+static std::string
+collect_argv(int argc, char** argv)
+{
+  // This is used by write_debug_script(), which wants the unedited argv.
+  std::string args;
+  for (int i = 0; i < argc; ++i)
+    {
+      args.append(" '");
+      // Now append argv[i], but with all single-quotes escaped
+      const char* argpos = argv[i];
+      while (1)
+        {
+          const int len = strcspn(argpos, "'");
+          args.append(argpos, len);
+          if (argpos[len] == '\0')
+            break;
+          args.append("'\"'\"'");
+          argpos += len + 1;
+        }
+      args.append("'");
+    }
+  return args;
+}
+
+static void
+write_debug_script(std::string filename_str,
+                  const char* argv_0, const char* args)
+{
+  size_t slash = filename_str.rfind('/');
+  if (slash != std::string::npos)
+    filename_str = filename_str.c_str() + slash + 1;
+  filename_str = std::string("/tmp/ld-run-") + filename_str + ".sh";
+  const char* filename = filename_str.c_str();
+  FILE* fp = fopen(filename, "w");
+  if (fp)
+    {
+      fprintf(fp, "[ \"$1\" = debug ] && PREFIX=\"${GDB-/home/build/static/projects/tools/gdb} --annotate=3 --fullname %s --args\" && shift\n", argv_0);
+      fprintf(fp, "$PREFIX%s $*\n", args);
+      fclose(fp);
+      chmod(filename, 0755);
+    }
+  else
+    filename = "[none]";
+  fprintf(stderr, "Welcome to gold!  Commandline written to %s.\n", filename);
+  fflush(stderr);
+}
+
+#else // !defined(DEBUG)
+
+static inline std::string
+collect_argv(int, char**)
+{
+  return "";
+}
+
+static inline void
+write_debug_script(std::string, const char*, const char*)
+{
+}
+
+#endif // !defined(DEBUG)
+
+
 int
 main(int argc, char** argv)
 {
@@ -51,15 +132,33 @@ main(int argc, char** argv)
 
   program_name = argv[0];
 
+  // This is used by write_debug_script(), which wants the unedited argv.
+  std::string args = collect_argv(argc, argv);
+
+  Errors errors(program_name);
+
+  // Initialize the global parameters, to let random code get to the
+  // errors object.
+  initialize_parameters(&errors);
+
+  // Options which may be set by the command line or by linker
+  // scripts.
+  Script_options script_options;
+
   // Handle the command line options.
-  Command_line command_line;
+  Command_line command_line(&script_options);
   command_line.process(argc - 1, argv + 1);
 
   long start_time = 0;
   if (command_line.options().print_stats())
     start_time = get_run_time();
 
-  initialize_parameters(&command_line.options());
+  // Store some options in the globally accessible parameters.
+  set_parameters_from_options(&command_line.options());
+
+  // Do this as early as possible (since it prints a welcome message).
+  write_debug_script(command_line.options().output_file_name(),
+                     program_name, args.c_str());
 
   // The work queue.
   Workqueue workqueue(command_line.options());
@@ -67,11 +166,15 @@ main(int argc, char** argv)
   // The list of input objects.
   Input_objects input_objects;
 
-  // The symbol table.
-  Symbol_table symtab;
+  // The symbol table.  We're going to guess here how many symbols
+  // we're going to see based on the number of input files.  Even when
+  // this is off, it means at worst we don't quite optimize hashtable
+  // resizing as well as we could have (perhap using more memory).
+  Symbol_table symtab(command_line.number_of_input_files() * 1024,
+                      command_line.options().version_script());
 
   // The layout object.
-  Layout layout(command_line.options());
+  Layout layout(command_line.options(), &script_options);
 
   // Get the search path from the -L options.
   Dirsearch search_path;
@@ -83,7 +186,7 @@ main(int argc, char** argv)
                      &symtab, &layout);
 
   // Run the main task processing loop.
-  workqueue.process();
+  workqueue.process(0);
 
   if (command_line.options().print_stats())
     {
@@ -98,7 +201,9 @@ main(int argc, char** argv)
       File_read::print_stats();
       fprintf(stderr, _("%s: output file size: %lld bytes\n"),
              program_name, static_cast<long long>(layout.output_file_size()));
+      symtab.print_stats();
+      layout.print_stats();
     }
 
-  gold_exit(true);
+  gold_exit(errors.error_count() == 0);
 }