Warn about undefined references in shared libraries if we have seen
[deliverable/binutils-gdb.git] / gold / gold.cc
index c2372adf526bed18c35c3174542521157b13fa6d..d0c2095c3f0c521e3aad85d5d8ab3af9a3f466cc 100644 (file)
@@ -1,4 +1,24 @@
-// ld.c -- linker main function
+// gold.cc -- main linker functions
+
+// 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"
 
@@ -6,6 +26,7 @@
 #include <cstdio>
 #include <cstring>
 #include <unistd.h>
+#include "libiberty.h"
 
 #include "options.h"
 #include "workqueue.h"
@@ -26,20 +47,11 @@ const char* program_name;
 void
 gold_exit(bool status)
 {
+  if (!status && parameters != NULL)
+    unlink_if_ordinary(parameters->output_file_name());
   exit(status ? EXIT_SUCCESS : EXIT_FAILURE);
 }
 
-void
-gold_fatal(const char* msg, bool perrno)
-{
-  fprintf(stderr, "%s: ", program_name);
-  if (perrno)
-    perror(msg);
-  else
-    fprintf(stderr, "%s\n", msg);
-  gold_exit(false);
-}
-
 void
 gold_nomem()
 {
@@ -52,10 +64,14 @@ gold_nomem()
   gold_exit(false);
 }
 
+// Handle an unreachable case.
+
 void
-gold_unreachable()
+do_gold_unreachable(const char* filename, int lineno, const char* function)
 {
-  abort();
+  fprintf(stderr, _("%s: internal error in %s, at %s:%d\n"),
+         program_name, function, filename, lineno);
+  gold_exit(false);
 }
 
 // This class arranges to run the functions done in the middle of the
@@ -99,7 +115,12 @@ queue_initial_tasks(const General_options& options,
                    Symbol_table* symtab, Layout* layout)
 {
   if (cmdline.begin() == cmdline.end())
-    gold_fatal(_("no input files"), false);
+    gold_fatal(_("no input files"));
+
+  int thread_count = options.thread_count_initial();
+  if (thread_count == 0)
+    thread_count = cmdline.number_of_input_files();
+  workqueue->set_thread_count(thread_count);
 
   // Read the input files.  We have to add the symbols to the symbol
   // table in order.  We do this by creating a separate blocker for
@@ -113,7 +134,7 @@ queue_initial_tasks(const General_options& options,
       Task_token* next_blocker = new Task_token();
       next_blocker->add_blocker();
       workqueue->queue(new Read_symbols(options, input_objects, symtab, layout,
-                                       search_path, *p, NULL, this_blocker,
+                                       search_path, &*p, NULL, this_blocker,
                                        next_blocker));
       this_blocker = next_blocker;
     }
@@ -136,10 +157,49 @@ queue_middle_tasks(const General_options& options,
                   Layout* layout,
                   Workqueue* workqueue)
 {
+  if (input_objects->number_of_input_objects() == 0)
+    {
+      // We had some input files, but we weren't able to open any of
+      // them.
+      gold_fatal(_("no input files"));
+    }
+
+  int thread_count = options.thread_count_middle();
+  if (thread_count == 0)
+    thread_count = input_objects->number_of_input_objects();
+  workqueue->set_thread_count(thread_count);
+
+  // Now we have seen all the input files.
+  const bool doing_static_link = (!input_objects->any_dynamic()
+                                 && !parameters->output_is_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_error(_("cannot mix -static with dynamic object %s"),
+                (*input_objects->dynobj_begin())->name().c_str());
+    }
+
+  // For each dynamic object, record whether we've seen all the
+  // dynamic objects that it depends upon.
+  input_objects->check_dynamic_dependencies();
+
+  // See if any of the input definitions violate the One Definition Rule.
+  // TODO: if this is too slow, do this as a task, rather than inline.
+  symtab->detect_odr_violations();
+
+  // 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(input_objects, symtab);
+
   // Predefine standard symbols.  This should be fast, so we don't
   // bother to create a task for it.
   define_standard_symbols(symtab, layout, input_objects->target());
 
+  // Define __start and __stop symbols for output sections where
+  // appropriate.
+  layout->define_section_symbols(symtab, input_objects->target());
+
   // Read the relocations of the input files.  We do this to find
   // which symbols are used by relocations which require a GOT and/or
   // a PLT entry, or a COPY reloc.  When we implement garbage
@@ -193,6 +253,19 @@ queue_final_tasks(const General_options& options,
                  Workqueue* workqueue,
                  Output_file* of)
 {
+  int thread_count = options.thread_count_final();
+  if (thread_count == 0)
+    thread_count = input_objects->number_of_input_objects();
+  workqueue->set_thread_count(thread_count);
+
+  // Use a blocker to wait until all the input sections have been
+  // written out.
+  Task_token* input_sections_blocker = new Task_token();
+
+  // Use a blocker to block any objects which have to wait for the
+  // output sections to complete before they can apply relocations.
+  Task_token* output_sections_blocker = new Task_token();
+
   // Use a blocker to block the final cleanup task.
   Task_token* final_blocker = new Task_token();
 
@@ -202,20 +275,39 @@ queue_final_tasks(const General_options& options,
        p != input_objects->relobj_end();
        ++p)
     {
+      input_sections_blocker->add_blocker();
       final_blocker->add_blocker();
       workqueue->queue(new Relocate_task(options, symtab, layout, *p, of,
+                                        input_sections_blocker,
+                                        output_sections_blocker,
                                         final_blocker));
     }
 
   // Queue a task to write out the symbol table.
   final_blocker->add_blocker();
-  workqueue->queue(new Write_symbols_task(symtab, input_objects->target(),
-                                         layout->sympool(), of,
+  workqueue->queue(new Write_symbols_task(symtab,
+                                         input_objects->target(),
+                                         layout->sympool(),
+                                         layout->dynpool(),
+                                         of,
                                          final_blocker));
 
+  // Queue a task to write out the output sections.
+  output_sections_blocker->add_blocker();
+  final_blocker->add_blocker();
+  workqueue->queue(new Write_sections_task(layout, of, output_sections_blocker,
+                                          final_blocker));
+
   // Queue a task to write out everything else.
   final_blocker->add_blocker();
-  workqueue->queue(new Write_data_task(layout, of, final_blocker));
+  workqueue->queue(new Write_data_task(layout, symtab, of, final_blocker));
+
+  // Queue a task to write out the output sections which depend on
+  // input sections.
+  final_blocker->add_blocker();
+  workqueue->queue(new Write_after_input_sections_task(layout, of,
+                                                      input_sections_blocker,
+                                                      final_blocker));
 
   // Queue a task to close the output file.  This will be blocked by
   // FINAL_BLOCKER.
@@ -224,50 +316,3 @@ queue_final_tasks(const General_options& options,
 }
 
 } // End namespace gold.
-
-using namespace gold;
-
-int
-main(int argc, char** argv)
-{
-#if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
-  setlocale (LC_MESSAGES, "");
-#endif
-#if defined (HAVE_SETLOCALE)
-  setlocale (LC_CTYPE, "");
-#endif
-  bindtextdomain (PACKAGE, LOCALEDIR);
-  textdomain (PACKAGE);
-
-  gold::program_name = argv[0];
-
-  // Handle the command line options.
-  gold::Command_line command_line;
-  command_line.process(argc - 1, argv + 1);
-
-  // The work queue.
-  gold::Workqueue workqueue(command_line.options());
-
-  // The list of input objects.
-  Input_objects input_objects;
-
-  // The symbol table.
-  Symbol_table symtab;
-
-  // The layout object.
-  Layout layout(command_line.options());
-
-  // Get the search path from the -L options.
-  Dirsearch search_path;
-  search_path.add(&workqueue, command_line.options().search_path());
-
-  // Queue up the first set of tasks.
-  queue_initial_tasks(command_line.options(), search_path,
-                     command_line, &workqueue, &input_objects,
-                     &symtab, &layout);
-
-  // Run the main task processing loop.
-  workqueue.process();
-
-  gold::gold_exit(true);
-}
This page took 0.025769 seconds and 4 git commands to generate.