Fix a typo.
[binutils-gdb.git] / gold / gold.cc
index 4b52b570abca32b9889d41f966f95012ad271931..8d86a1bbb474bea9c9d3bb0cdf6c1772b219bbea 100644 (file)
@@ -31,7 +31,6 @@
 
 #include "options.h"
 #include "debug.h"
-#include "target-select.h"
 #include "workqueue.h"
 #include "dirsearch.h"
 #include "readsyms.h"
@@ -41,6 +40,7 @@
 #include "layout.h"
 #include "reloc.h"
 #include "defstd.h"
+#include "plugin.h"
 
 namespace gold
 {
@@ -50,8 +50,12 @@ const char* program_name;
 void
 gold_exit(bool status)
 {
+  if (parameters != NULL
+      && parameters->options_valid()
+      && parameters->options().has_plugins())
+    parameters->options().plugins()->cleanup();
   if (!status && parameters != NULL && parameters->options_valid())
-    unlink_if_ordinary(parameters->output_file_name());
+    unlink_if_ordinary(parameters->options().output_file_name());
   exit(status ? EXIT_SUCCESS : EXIT_FAILURE);
 }
 
@@ -61,9 +65,18 @@ gold_nomem()
   // We are out of memory, so try hard to print a reasonable message.
   // Note that we don't try to translate this message, since the
   // translation process itself will require memory.
-  write(2, program_name, strlen(program_name));
-  const char* const s = ": out of memory\n";
-  write(2, s, strlen(s));
+
+  // LEN only exists to avoid a pointless warning when write is
+  // declared with warn_use_result, as when compiling with
+  // -D_USE_FORTIFY on GNU/Linux.  Casting to void does not appear to
+  // work, at least not with gcc 4.3.0.
+
+  ssize_t len = write(2, program_name, strlen(program_name));
+  if (len >= 0)
+    {
+      const char* const s = ": out of memory\n";
+      len = write(2, s, strlen(s));
+    }
   gold_exit(false);
 }
 
@@ -86,9 +99,9 @@ class Middle_runner : public Task_function_runner
   Middle_runner(const General_options& options,
                const Input_objects* input_objects,
                Symbol_table* symtab,
-               Layout* layout)
+               Layout* layout, Mapfile* mapfile)
     : options_(options), input_objects_(input_objects), symtab_(symtab),
-      layout_(layout)
+      layout_(layout), mapfile_(mapfile)
   { }
 
   void
@@ -99,13 +112,14 @@ class Middle_runner : public Task_function_runner
   const Input_objects* input_objects_;
   Symbol_table* symtab_;
   Layout* layout_;
+  Mapfile* mapfile_;
 };
 
 void
 Middle_runner::run(Workqueue* workqueue, const Task* task)
 {
   queue_middle_tasks(this->options_, task, this->input_objects_, this->symtab_,
-                    this->layout_, workqueue);
+                    this->layout_, workqueue, this->mapfile_);
 }
 
 // Queue up the initial set of tasks for this link job.
@@ -115,7 +129,7 @@ queue_initial_tasks(const General_options& options,
                    Dirsearch& search_path,
                    const Command_line& cmdline,
                    Workqueue* workqueue, Input_objects* input_objects,
-                   Symbol_table* symtab, Layout* layout)
+                   Symbol_table* symtab, Layout* layout, Mapfile* mapfile)
 {
   if (cmdline.begin() == cmdline.end())
     gold_fatal(_("no input files"));
@@ -137,15 +151,26 @@ queue_initial_tasks(const General_options& options,
       Task_token* next_blocker = new Task_token(true);
       next_blocker->add_blocker();
       workqueue->queue(new Read_symbols(options, input_objects, symtab, layout,
-                                       &search_path, &*p, NULL, this_blocker,
-                                       next_blocker));
+                                       &search_path, mapfile, &*p, NULL,
+                                       this_blocker, next_blocker));
+      this_blocker = next_blocker;
+    }
+
+  if (options.has_plugins())
+    {
+      Task_token* next_blocker = new Task_token(true);
+      next_blocker->add_blocker();
+      workqueue->queue(new Plugin_hook(options, input_objects, symtab, layout,
+                                      &search_path, mapfile, this_blocker,
+                                      next_blocker));
       this_blocker = next_blocker;
     }
 
   workqueue->queue(new Task_function(new Middle_runner(options,
                                                       input_objects,
                                                       symtab,
-                                                      layout),
+                                                      layout,
+                                                      mapfile),
                                     this_blocker,
                                     "Task_function Middle_runner"));
 }
@@ -160,22 +185,15 @@ queue_middle_tasks(const General_options& options,
                   const Input_objects* input_objects,
                   Symbol_table* symtab,
                   Layout* layout,
-                  Workqueue* workqueue)
+                  Workqueue* workqueue,
+                  Mapfile* mapfile)
 {
   // We have to support the case of not seeing any input objects, and
   // generate an empty file.  Existing builds depend on being able to
   // pass an empty archive to the linker and get an empty object file
   // out.  In order to do this we need to use a default target.
   if (input_objects->number_of_input_objects() == 0)
-    {
-      // The GOLD_xx macros are defined by the configure script.
-      Target* target = select_target(elfcpp::GOLD_DEFAULT_MACHINE,
-                                    GOLD_DEFAULT_SIZE,
-                                    GOLD_DEFAULT_BIG_ENDIAN,
-                                    0, 0);
-      gold_assert(target != NULL);
-      set_parameters_target(target);
-    }
+    set_parameters_target(&parameters->default_target());
 
   int thread_count = options.thread_count_middle();
   if (thread_count == 0)
@@ -184,17 +202,22 @@ queue_middle_tasks(const General_options& options,
 
   // Now we have seen all the input files.
   const bool doing_static_link = (!input_objects->any_dynamic()
-                                 && !parameters->output_is_shared());
+                                 && !parameters->options().shared());
   set_parameters_doing_static_link(doing_static_link);
   if (!doing_static_link && options.is_static())
     {
       // We print out just the first .so we see; there may be others.
+      gold_assert(input_objects->dynobj_begin() != input_objects->dynobj_end());
       gold_error(_("cannot mix -static with dynamic object %s"),
                 (*input_objects->dynobj_begin())->name().c_str());
     }
-  if (!doing_static_link && parameters->output_is_object())
+  if (!doing_static_link && parameters->options().relocatable())
     gold_error(_("cannot mix -r with dynamic object %s"),
               (*input_objects->dynobj_begin())->name().c_str());
+  if (!doing_static_link
+      && options.oformat_enum() != General_options::OBJECT_FORMAT_ELF)
+    gold_fatal(_("cannot use non-ELF output format with dynamic object %s"),
+              (*input_objects->dynobj_begin())->name().c_str());
 
   if (is_debugging_enabled(DEBUG_SCRIPT))
     layout->script_options()->print(stderr);
@@ -207,6 +230,9 @@ queue_middle_tasks(const General_options& options,
   // TODO: if this is too slow, do this as a task, rather than inline.
   symtab->detect_odr_violations(task, options.output_file_name());
 
+  // Create any output sections required by any linker script.
+  layout->create_script_sections();
+
   // Define some sections and symbols needed for a dynamic link.  This
   // handles some cases we want to see before we read the relocs.
   layout->create_initial_dynamic_sections(symtab);
@@ -214,7 +240,13 @@ queue_middle_tasks(const General_options& options,
   // Define symbols from any linker scripts.
   layout->define_script_symbols(symtab);
 
-  if (!parameters->output_is_object())
+  // Add any symbols named with -u options to the symbol table.
+  symtab->add_undefined_symbols_from_command_line();
+
+  // Attach sections to segments.
+  layout->attach_sections_to_segments();
+
+  if (!parameters->options().relocatable())
     {
       // Predefine standard symbols.
       define_standard_symbols(symtab, layout);
@@ -256,21 +288,23 @@ queue_middle_tasks(const General_options& options,
 
   // Allocate common symbols.  This requires write access to the
   // symbol table, but is independent of the relocation processing.
-  // FIXME: We should have an option to do this even for a relocatable
-  // link.
-  if (!parameters->output_is_object())
+  if (parameters->options().define_common())
     {
       blocker->add_blocker();
-      workqueue->queue(new Allocate_commons_task(options, symtab, layout,
+      workqueue->queue(new Allocate_commons_task(symtab, layout, mapfile,
                                                 symtab_lock, blocker));
     }
 
   // When all those tasks are complete, we can start laying out the
   // output file.
+  // TODO(csilvers): figure out a more principled way to get the target
+  Target* target = const_cast<Target*>(&parameters->target());
   workqueue->queue(new Task_function(new Layout_task_runner(options,
                                                            input_objects,
                                                            symtab,
-                                                           layout),
+                                                            target,
+                                                           layout,
+                                                           mapfile),
                                     blocker,
                                     "Task_function Layout_task_runner"));
 }
@@ -307,16 +341,14 @@ queue_final_tasks(const General_options& options,
   Task_token* final_blocker = new Task_token(true);
 
   // Queue a task to write out the symbol table.
-  if (!options.strip_all())
-    {
-      final_blocker->add_blocker();
-      workqueue->queue(new Write_symbols_task(symtab,
-                                             input_objects,
-                                             layout->sympool(),
-                                             layout->dynpool(),
-                                             of,
-                                             final_blocker));
-    }
+  final_blocker->add_blocker();
+  workqueue->queue(new Write_symbols_task(layout,
+                                         symtab,
+                                         input_objects,
+                                         layout->sympool(),
+                                         layout->dynpool(),
+                                         of,
+                                         final_blocker));
 
   // Queue a task to write out the output sections.
   output_sections_blocker->add_blocker();
@@ -368,7 +400,8 @@ queue_final_tasks(const General_options& options,
 
   // Queue a task to close the output file.  This will be blocked by
   // FINAL_BLOCKER.
-  workqueue->queue(new Task_function(new Close_task_runner(of),
+  workqueue->queue(new Task_function(new Close_task_runner(&options, layout,
+                                                          of),
                                     final_blocker,
                                     "Task_function Close_task_runner"));
 }