* gold.cc (queue_middle_tasks): Process existing GOT/PLT entries.
[external/binutils.git] / gold / gold.cc
1 // gold.cc -- main linker functions
2
3 // Copyright 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
5
6 // This file is part of gold.
7
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
12
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
22
23 #include "gold.h"
24
25 #include <cstdlib>
26 #include <cstdio>
27 #include <cstring>
28 #include <unistd.h>
29 #include <algorithm>
30 #include "libiberty.h"
31
32 #include "options.h"
33 #include "debug.h"
34 #include "workqueue.h"
35 #include "dirsearch.h"
36 #include "readsyms.h"
37 #include "symtab.h"
38 #include "common.h"
39 #include "object.h"
40 #include "layout.h"
41 #include "reloc.h"
42 #include "defstd.h"
43 #include "plugin.h"
44 #include "gc.h"
45 #include "icf.h"
46 #include "incremental.h"
47
48 namespace gold
49 {
50
51 class Object;
52
53 const char* program_name;
54
55 static Task*
56 process_incremental_input(Incremental_binary*, unsigned int, Input_objects*,
57                           Symbol_table*, Layout*, Dirsearch*, Mapfile*,
58                           Task_token*, Task_token*);
59
60 void
61 gold_exit(bool status)
62 {
63   if (parameters != NULL
64       && parameters->options_valid()
65       && parameters->options().has_plugins())
66     parameters->options().plugins()->cleanup();
67   if (!status && parameters != NULL && parameters->options_valid())
68     unlink_if_ordinary(parameters->options().output_file_name());
69   exit(status ? EXIT_SUCCESS : EXIT_FAILURE);
70 }
71
72 void
73 gold_nomem()
74 {
75   // We are out of memory, so try hard to print a reasonable message.
76   // Note that we don't try to translate this message, since the
77   // translation process itself will require memory.
78
79   // LEN only exists to avoid a pointless warning when write is
80   // declared with warn_use_result, as when compiling with
81   // -D_USE_FORTIFY on GNU/Linux.  Casting to void does not appear to
82   // work, at least not with gcc 4.3.0.
83
84   ssize_t len = write(2, program_name, strlen(program_name));
85   if (len >= 0)
86     {
87       const char* const s = ": out of memory\n";
88       len = write(2, s, strlen(s));
89     }
90   gold_exit(false);
91 }
92
93 // Handle an unreachable case.
94
95 void
96 do_gold_unreachable(const char* filename, int lineno, const char* function)
97 {
98   fprintf(stderr, _("%s: internal error in %s, at %s:%d\n"),
99           program_name, function, filename, lineno);
100   gold_exit(false);
101 }
102
103 // This class arranges to run the functions done in the middle of the
104 // link.  It is just a closure.
105
106 class Middle_runner : public Task_function_runner
107 {
108  public:
109   Middle_runner(const General_options& options,
110                 const Input_objects* input_objects,
111                 Symbol_table* symtab,
112                 Layout* layout, Mapfile* mapfile)
113     : options_(options), input_objects_(input_objects), symtab_(symtab),
114       layout_(layout), mapfile_(mapfile)
115   { }
116
117   void
118   run(Workqueue*, const Task*);
119
120  private:
121   const General_options& options_;
122   const Input_objects* input_objects_;
123   Symbol_table* symtab_;
124   Layout* layout_;
125   Mapfile* mapfile_;
126 };
127
128 void
129 Middle_runner::run(Workqueue* workqueue, const Task* task)
130 {
131   queue_middle_tasks(this->options_, task, this->input_objects_, this->symtab_,
132                      this->layout_, workqueue, this->mapfile_);
133 }
134
135 // This class arranges the tasks to process the relocs for garbage collection.
136
137 class Gc_runner : public Task_function_runner 
138 {
139   public:
140    Gc_runner(const General_options& options,
141              const Input_objects* input_objects,
142              Symbol_table* symtab,
143              Layout* layout, Mapfile* mapfile)
144     : options_(options), input_objects_(input_objects), symtab_(symtab),
145       layout_(layout), mapfile_(mapfile)
146    { }
147
148   void
149   run(Workqueue*, const Task*);
150
151  private:
152   const General_options& options_;
153   const Input_objects* input_objects_;
154   Symbol_table* symtab_;
155   Layout* layout_;
156   Mapfile* mapfile_;
157 };
158
159 void
160 Gc_runner::run(Workqueue* workqueue, const Task* task)
161 {
162   queue_middle_gc_tasks(this->options_, task, this->input_objects_, 
163                         this->symtab_, this->layout_, workqueue, 
164                         this->mapfile_);
165 }
166
167 // Queue up the initial set of tasks for this link job.
168
169 void
170 queue_initial_tasks(const General_options& options,
171                     Dirsearch& search_path,
172                     const Command_line& cmdline,
173                     Workqueue* workqueue, Input_objects* input_objects,
174                     Symbol_table* symtab, Layout* layout, Mapfile* mapfile)
175 {
176   if (cmdline.begin() == cmdline.end())
177     {
178       if (options.printed_version())
179         gold_exit(true);
180       gold_fatal(_("no input files"));
181     }
182
183   int thread_count = options.thread_count_initial();
184   if (thread_count == 0)
185     thread_count = cmdline.number_of_input_files();
186   workqueue->set_thread_count(thread_count);
187
188   // For incremental links, the base output file.
189   Incremental_binary* ibase = NULL;
190
191   if (parameters->incremental())
192     {
193       if (options.relocatable())
194         gold_error(_("incremental linking is incompatible with -r"));
195       if (options.emit_relocs())
196         gold_error(_("incremental linking is incompatible with --emit-relocs"));
197       if (options.gc_sections())
198         gold_error(_("incremental linking is incompatible with --gc-sections"));
199       if (options.icf_enabled())
200         gold_error(_("incremental linking is incompatible with --icf"));
201       if (options.has_plugins())
202         gold_error(_("incremental linking is incompatible with --plugin"));
203
204       if (parameters->incremental_update())
205         {
206           Output_file* of = new Output_file(options.output_file_name());
207           if (!of->open_for_modification())
208             gold_info(_("incremental update not possible: "
209                         "cannot open %s"),
210                       options.output_file_name());
211           else
212             {
213               ibase = open_incremental_binary(of);
214               if (ibase != NULL
215                   && ibase->check_inputs(cmdline, layout->incremental_inputs()))
216                 ibase->init_layout(layout);
217               else
218                 {
219                   delete ibase;
220                   ibase = NULL;
221                   of->close();
222                 }
223             }
224           if (ibase == NULL)
225             {
226               if (set_parameters_incremental_full())
227                 gold_info(_("linking with --incremental-full"));
228               else
229                 gold_fatal(_("restart link with --incremental-full"));
230             }
231         }
232     }
233
234   // Read the input files.  We have to add the symbols to the symbol
235   // table in order.  We do this by creating a separate blocker for
236   // each input file.  We associate the blocker with the following
237   // input file, to give us a convenient place to delete it.
238   Task_token* this_blocker = NULL;
239   if (ibase == NULL)
240     {
241       // Normal link.  Queue a Read_symbols task for each input file
242       // on the command line.
243       for (Command_line::const_iterator p = cmdline.begin();
244            p != cmdline.end();
245            ++p)
246         {
247           Task_token* next_blocker = new Task_token(true);
248           next_blocker->add_blocker();
249           workqueue->queue(new Read_symbols(input_objects, symtab, layout,
250                                             &search_path, 0, mapfile, &*p, NULL,
251                                             NULL, this_blocker, next_blocker));
252           this_blocker = next_blocker;
253         }
254     }
255   else
256     {
257       // Incremental update link.  Process the list of input files
258       // stored in the base file, and queue a task for each file:
259       // a Read_symbols task for a changed file, and an Add_symbols task
260       // for an unchanged file.  We need to mark all the space used by
261       // unchanged files before we can start any tasks running.
262       unsigned int input_file_count = ibase->input_file_count();
263       std::vector<Task*> tasks;
264       tasks.reserve(input_file_count);
265       for (unsigned int i = 0; i < input_file_count; ++i)
266         {
267           Task_token* next_blocker = new Task_token(true);
268           next_blocker->add_blocker();
269           Task* t = process_incremental_input(ibase, i, input_objects, symtab,
270                                               layout, &search_path, mapfile,
271                                               this_blocker, next_blocker);
272           tasks.push_back(t);
273           this_blocker = next_blocker;
274         }
275       // Now we can queue the tasks.
276       for (unsigned int i = 0; i < tasks.size(); i++)
277         workqueue->queue(tasks[i]);
278     }
279
280   if (options.has_plugins())
281     {
282       Task_token* next_blocker = new Task_token(true);
283       next_blocker->add_blocker();
284       workqueue->queue(new Plugin_hook(options, input_objects, symtab, layout,
285                                        &search_path, mapfile, this_blocker,
286                                        next_blocker));
287       this_blocker = next_blocker;
288     }
289
290   if (options.relocatable()
291       && (options.gc_sections() || options.icf_enabled()))
292     gold_error(_("cannot mix -r with --gc-sections or --icf"));
293
294   if (options.gc_sections() || options.icf_enabled())
295     {
296       workqueue->queue(new Task_function(new Gc_runner(options,
297                                                        input_objects,
298                                                        symtab,
299                                                        layout,
300                                                        mapfile),
301                                          this_blocker,
302                                          "Task_function Gc_runner"));
303     }
304   else
305     {
306       workqueue->queue(new Task_function(new Middle_runner(options,
307                                                            input_objects,
308                                                            symtab,
309                                                            layout,
310                                                            mapfile),
311                                          this_blocker,
312                                          "Task_function Middle_runner"));
313     }
314 }
315
316 // Process an incremental input file: if it is unchanged from the previous
317 // link, return a task to add its symbols from the base file's incremental
318 // info; if it has changed, return a normal Read_symbols task.  We create a
319 // task for every input file, if only to report the file for rebuilding the
320 // incremental info.
321
322 static Task*
323 process_incremental_input(Incremental_binary* ibase,
324                           unsigned int input_file_index,
325                           Input_objects* input_objects,
326                           Symbol_table* symtab,
327                           Layout* layout,
328                           Dirsearch* search_path,
329                           Mapfile* mapfile,
330                           Task_token* this_blocker,
331                           Task_token* next_blocker)
332 {
333   const Incremental_binary::Input_reader* input_reader =
334       ibase->get_input_reader(input_file_index);
335   Incremental_input_type input_type = input_reader->type();
336
337   // Get the input argument corresponding to this input file, matching on
338   // the argument serial number.  If the input file cannot be matched
339   // to an existing input argument, synthesize a new one.
340   const Input_argument* input_argument =
341       ibase->get_input_argument(input_file_index);
342   if (input_argument == NULL)
343     {
344       Input_file_argument file(input_reader->filename(),
345                                Input_file_argument::INPUT_FILE_TYPE_FILE,
346                                "", false, parameters->options());
347       Input_argument* arg = new Input_argument(file);
348       arg->set_script_info(ibase->get_script_info(input_file_index));
349       input_argument = arg;
350     }
351
352   gold_debug(DEBUG_INCREMENTAL, "Incremental object: %s, type %d",
353              input_reader->filename(), input_type);
354
355   if (input_type == INCREMENTAL_INPUT_SCRIPT)
356     {
357       // Incremental_binary::check_inputs should have cancelled the
358       // incremental update if the script has changed.
359       gold_assert(!ibase->file_has_changed(input_file_index));
360       return new Check_script(layout, ibase, input_file_index, input_reader,
361                               this_blocker, next_blocker);
362     }
363
364   if (input_type == INCREMENTAL_INPUT_ARCHIVE)
365     {
366       Incremental_library* lib = ibase->get_library(input_file_index);
367       gold_assert(lib != NULL);
368       if (lib->filename() == "/group/"
369           || !ibase->file_has_changed(input_file_index))
370         {
371           // Queue a task to check that no references have been added to any
372           // of the library's unused symbols.
373           return new Check_library(symtab, layout, ibase, input_file_index,
374                                    input_reader, this_blocker, next_blocker);
375         }
376       else
377         {
378           // Queue a Read_symbols task to process the archive normally.
379           return new Read_symbols(input_objects, symtab, layout, search_path,
380                                   0, mapfile, input_argument, NULL, NULL,
381                                   this_blocker, next_blocker);
382         }
383     }
384
385   if (input_type == INCREMENTAL_INPUT_ARCHIVE_MEMBER)
386     {
387       // For archive members, check the timestamp of the containing archive.
388       Incremental_library* lib = ibase->get_library(input_file_index);
389       gold_assert(lib != NULL);
390       // Process members of a --start-lib/--end-lib group as normal objects.
391       if (lib->filename() != "/group/")
392         {
393           if (ibase->file_has_changed(lib->input_file_index()))
394             {
395               return new Read_member(input_objects, symtab, layout, mapfile,
396                                      input_reader, this_blocker, next_blocker);
397             }
398           else
399             {
400               // The previous contributions from this file will be kept.
401               // Mark the pieces of output sections contributed by this
402               // object.
403               ibase->reserve_layout(input_file_index);
404               Object* obj = make_sized_incremental_object(ibase,
405                                                           input_file_index,
406                                                           input_type,
407                                                           input_reader);
408               return new Add_symbols(input_objects, symtab, layout,
409                                      search_path, 0, mapfile, input_argument,
410                                      obj, lib, NULL, this_blocker,
411                                      next_blocker);
412             }
413         }
414     }
415
416   // Normal object file or shared library.  Check if the file has changed
417   // since the last incremental link.
418   if (ibase->file_has_changed(input_file_index))
419     {
420       return new Read_symbols(input_objects, symtab, layout, search_path, 0,
421                               mapfile, input_argument, NULL, NULL,
422                               this_blocker, next_blocker);
423     }
424   else
425     {
426       // The previous contributions from this file will be kept.
427       // Mark the pieces of output sections contributed by this object.
428       ibase->reserve_layout(input_file_index);
429       Object* obj = make_sized_incremental_object(ibase,
430                                                   input_file_index,
431                                                   input_type,
432                                                   input_reader);
433       return new Add_symbols(input_objects, symtab, layout, search_path, 0,
434                              mapfile, input_argument, obj, NULL, NULL,
435                              this_blocker, next_blocker);
436     }
437 }
438
439 // Queue up a set of tasks to be done before queueing the middle set
440 // of tasks.  This is only necessary when garbage collection
441 // (--gc-sections) of unused sections is desired.  The relocs are read
442 // and processed here early to determine the garbage sections before the
443 // relocs can be scanned in later tasks.
444
445 void
446 queue_middle_gc_tasks(const General_options& options,
447                       const Task* ,
448                       const Input_objects* input_objects,
449                       Symbol_table* symtab,
450                       Layout* layout,
451                       Workqueue* workqueue,
452                       Mapfile* mapfile)
453 {
454   // Read_relocs for all the objects must be done and processed to find
455   // unused sections before any scanning of the relocs can take place.
456   Task_token* this_blocker = NULL;
457   for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
458        p != input_objects->relobj_end();
459        ++p)
460     {
461       Task_token* next_blocker = new Task_token(true);
462       next_blocker->add_blocker();
463       workqueue->queue(new Read_relocs(symtab, layout, *p, this_blocker,
464                                        next_blocker));
465       this_blocker = next_blocker;
466     }
467
468   // If we are given only archives in input, we have no regular
469   // objects and THIS_BLOCKER is NULL here.  Create a dummy
470   // blocker here so that we can run the middle tasks immediately.
471   if (this_blocker == NULL)
472     {
473       gold_assert(input_objects->number_of_relobjs() == 0);
474       this_blocker = new Task_token(true);
475     }
476
477   workqueue->queue(new Task_function(new Middle_runner(options,
478                                                        input_objects,
479                                                        symtab,
480                                                        layout,
481                                                        mapfile),
482                                      this_blocker,
483                                      "Task_function Middle_runner"));
484 }
485
486 // Queue up the middle set of tasks.  These are the tasks which run
487 // after all the input objects have been found and all the symbols
488 // have been read, but before we lay out the output file.
489
490 void
491 queue_middle_tasks(const General_options& options,
492                    const Task* task,
493                    const Input_objects* input_objects,
494                    Symbol_table* symtab,
495                    Layout* layout,
496                    Workqueue* workqueue,
497                    Mapfile* mapfile)
498 {
499   // Add any symbols named with -u options to the symbol table.
500   symtab->add_undefined_symbols_from_command_line(layout);
501
502   // If garbage collection was chosen, relocs have been read and processed
503   // at this point by pre_middle_tasks.  Layout can then be done for all 
504   // objects.
505   if (parameters->options().gc_sections())
506     {
507       // Find the start symbol if any.
508       Symbol* start_sym;
509       if (parameters->options().entry())
510         start_sym = symtab->lookup(parameters->options().entry());
511       else
512         start_sym = symtab->lookup("_start");
513       if (start_sym != NULL)
514         {
515           bool is_ordinary;
516           unsigned int shndx = start_sym->shndx(&is_ordinary);
517           if (is_ordinary) 
518             {
519               symtab->gc()->worklist().push(
520                 Section_id(start_sym->object(), shndx));
521             }
522         }
523       // Symbols named with -u should not be considered garbage.
524       symtab->gc_mark_undef_symbols(layout);
525       gold_assert(symtab->gc() != NULL);
526       // Do a transitive closure on all references to determine the worklist.
527       symtab->gc()->do_transitive_closure();
528     }
529
530   // If identical code folding (--icf) is chosen it makes sense to do it 
531   // only after garbage collection (--gc-sections) as we do not want to 
532   // be folding sections that will be garbage.
533   if (parameters->options().icf_enabled())
534     {
535       symtab->icf()->find_identical_sections(input_objects, symtab);
536     }
537
538   // Call Object::layout for the second time to determine the 
539   // output_sections for all referenced input sections.  When 
540   // --gc-sections or --icf is turned on, Object::layout is 
541   // called twice.  It is called the first time when the 
542   // symbols are added.
543   if (parameters->options().gc_sections()
544       || parameters->options().icf_enabled())
545     {
546       for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
547            p != input_objects->relobj_end();
548            ++p)
549         {
550           Task_lock_obj<Object> tlo(task, *p);
551           (*p)->layout(symtab, layout, NULL);
552         }
553     }
554
555   // Layout deferred objects due to plugins.
556   if (parameters->options().has_plugins())
557     {
558       Plugin_manager* plugins = parameters->options().plugins();
559       gold_assert(plugins != NULL);
560       plugins->layout_deferred_objects();
561     }     
562
563   if (parameters->options().gc_sections()
564       || parameters->options().icf_enabled())
565     {
566       for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
567            p != input_objects->relobj_end();
568            ++p)
569         {
570           // Update the value of output_section stored in rd.
571           Read_relocs_data* rd = (*p)->get_relocs_data();
572           for (Read_relocs_data::Relocs_list::iterator q = rd->relocs.begin();
573                q != rd->relocs.end();
574                ++q)
575             {
576               q->output_section = (*p)->output_section(q->data_shndx);
577               q->needs_special_offset_handling = 
578                       (*p)->is_output_section_offset_invalid(q->data_shndx);
579             }
580         }
581     }
582
583   // We have to support the case of not seeing any input objects, and
584   // generate an empty file.  Existing builds depend on being able to
585   // pass an empty archive to the linker and get an empty object file
586   // out.  In order to do this we need to use a default target.
587   if (input_objects->number_of_input_objects() == 0
588       && layout->incremental_base() == NULL)
589     parameters_force_valid_target();
590
591   int thread_count = options.thread_count_middle();
592   if (thread_count == 0)
593     thread_count = std::max(2, input_objects->number_of_input_objects());
594   workqueue->set_thread_count(thread_count);
595
596   // Now we have seen all the input files.
597   const bool doing_static_link =
598     (!input_objects->any_dynamic()
599      && !parameters->options().output_is_position_independent());
600   set_parameters_doing_static_link(doing_static_link);
601   if (!doing_static_link && options.is_static())
602     {
603       // We print out just the first .so we see; there may be others.
604       gold_assert(input_objects->dynobj_begin() != input_objects->dynobj_end());
605       gold_error(_("cannot mix -static with dynamic object %s"),
606                  (*input_objects->dynobj_begin())->name().c_str());
607     }
608   if (!doing_static_link && parameters->options().relocatable())
609     gold_fatal(_("cannot mix -r with dynamic object %s"),
610                (*input_objects->dynobj_begin())->name().c_str());
611   if (!doing_static_link
612       && options.oformat_enum() != General_options::OBJECT_FORMAT_ELF)
613     gold_fatal(_("cannot use non-ELF output format with dynamic object %s"),
614                (*input_objects->dynobj_begin())->name().c_str());
615
616   if (parameters->options().relocatable())
617     {
618       Input_objects::Relobj_iterator p = input_objects->relobj_begin();
619       if (p != input_objects->relobj_end())
620         {
621           bool uses_split_stack = (*p)->uses_split_stack();
622           for (++p; p != input_objects->relobj_end(); ++p)
623             {
624               if ((*p)->uses_split_stack() != uses_split_stack)
625                 gold_fatal(_("cannot mix split-stack '%s' and "
626                              "non-split-stack '%s' when using -r"),
627                            (*input_objects->relobj_begin())->name().c_str(),
628                            (*p)->name().c_str());
629             }
630         }
631     }
632
633   // For incremental updates, record the existing GOT and PLT entries.
634   if (parameters->incremental_update())
635     {
636       Incremental_binary* ibase = layout->incremental_base();
637       ibase->process_got_plt(symtab, layout);
638     }
639
640   if (is_debugging_enabled(DEBUG_SCRIPT))
641     layout->script_options()->print(stderr);
642
643   // For each dynamic object, record whether we've seen all the
644   // dynamic objects that it depends upon.
645   input_objects->check_dynamic_dependencies();
646
647   // See if any of the input definitions violate the One Definition Rule.
648   // TODO: if this is too slow, do this as a task, rather than inline.
649   symtab->detect_odr_violations(task, options.output_file_name());
650
651   // Do the --no-undefined-version check.
652   if (!parameters->options().undefined_version())
653     {
654       Script_options* so = layout->script_options();
655       so->version_script_info()->check_unmatched_names(symtab);
656     }
657
658   // Create any automatic note sections.
659   layout->create_notes();
660
661   // Create any output sections required by any linker script.
662   layout->create_script_sections();
663
664   // Define some sections and symbols needed for a dynamic link.  This
665   // handles some cases we want to see before we read the relocs.
666   layout->create_initial_dynamic_sections(symtab);
667
668   // Define symbols from any linker scripts.
669   layout->define_script_symbols(symtab);
670
671   // Attach sections to segments.
672   layout->attach_sections_to_segments();
673
674   if (!parameters->options().relocatable())
675     {
676       // Predefine standard symbols.
677       define_standard_symbols(symtab, layout);
678
679       // Define __start and __stop symbols for output sections where
680       // appropriate.
681       layout->define_section_symbols(symtab);
682     }
683
684   // Make sure we have symbols for any required group signatures.
685   layout->define_group_signatures(symtab);
686
687   Task_token* this_blocker = NULL;
688
689   // Allocate common symbols.  We use a blocker to run this before the
690   // Scan_relocs tasks, because it writes to the symbol table just as
691   // they do.
692   if (parameters->options().define_common())
693     {
694       this_blocker = new Task_token(true);
695       this_blocker->add_blocker();
696       workqueue->queue(new Allocate_commons_task(symtab, layout, mapfile,
697                                                  this_blocker));
698     }
699
700   // If doing garbage collection, the relocations have already been read.
701   // Otherwise, read and scan the relocations.
702   if (parameters->options().gc_sections()
703       || parameters->options().icf_enabled())
704     {
705       for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
706            p != input_objects->relobj_end();
707            ++p)
708         {
709           Task_token* next_blocker = new Task_token(true);
710           next_blocker->add_blocker();
711           workqueue->queue(new Scan_relocs(symtab, layout, *p, 
712                                            (*p)->get_relocs_data(),
713                                            this_blocker, next_blocker));
714           this_blocker = next_blocker;
715         }
716     }
717   else
718     {
719       // Read the relocations of the input files.  We do this to find
720       // which symbols are used by relocations which require a GOT and/or
721       // a PLT entry, or a COPY reloc.  When we implement garbage
722       // collection we will do it here by reading the relocations in a
723       // breadth first search by references.
724       //
725       // We could also read the relocations during the first pass, and
726       // mark symbols at that time.  That is how the old GNU linker works.
727       // Doing that is more complex, since we may later decide to discard
728       // some of the sections, and thus change our minds about the types
729       // of references made to the symbols.
730       for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
731            p != input_objects->relobj_end();
732            ++p)
733         {
734           Task_token* next_blocker = new Task_token(true);
735           next_blocker->add_blocker();
736           workqueue->queue(new Read_relocs(symtab, layout, *p, this_blocker,
737                                            next_blocker));
738           this_blocker = next_blocker;
739         }
740     }
741
742   if (this_blocker == NULL)
743     {
744       if (input_objects->number_of_relobjs() == 0)
745         {
746           // If we are given only archives in input, we have no regular
747           // objects and THIS_BLOCKER is NULL here.  Create a dummy
748           // blocker here so that we can run the layout task immediately.
749           this_blocker = new Task_token(true);
750         }
751       else 
752         {
753           // If we failed to open any input files, it's possible for
754           // THIS_BLOCKER to be NULL here.  There's no real point in
755           // continuing if that happens.
756           gold_assert(parameters->errors()->error_count() > 0);
757           gold_exit(false);
758         }
759     }
760
761   // When all those tasks are complete, we can start laying out the
762   // output file.
763   // TODO(csilvers): figure out a more principled way to get the target
764   Target* target = const_cast<Target*>(&parameters->target());
765   workqueue->queue(new Task_function(new Layout_task_runner(options,
766                                                             input_objects,
767                                                             symtab,
768                                                             target,
769                                                             layout,
770                                                             mapfile),
771                                      this_blocker,
772                                      "Task_function Layout_task_runner"));
773 }
774
775 // Queue up the final set of tasks.  This is called at the end of
776 // Layout_task.
777
778 void
779 queue_final_tasks(const General_options& options,
780                   const Input_objects* input_objects,
781                   const Symbol_table* symtab,
782                   Layout* layout,
783                   Workqueue* workqueue,
784                   Output_file* of)
785 {
786   int thread_count = options.thread_count_final();
787   if (thread_count == 0)
788     thread_count = std::max(2, input_objects->number_of_input_objects());
789   workqueue->set_thread_count(thread_count);
790
791   bool any_postprocessing_sections = layout->any_postprocessing_sections();
792
793   // Use a blocker to wait until all the input sections have been
794   // written out.
795   Task_token* input_sections_blocker = NULL;
796   if (!any_postprocessing_sections)
797     {
798       input_sections_blocker = new Task_token(true);
799       input_sections_blocker->add_blockers(input_objects->number_of_relobjs());
800     }
801
802   // Use a blocker to block any objects which have to wait for the
803   // output sections to complete before they can apply relocations.
804   Task_token* output_sections_blocker = new Task_token(true);
805   output_sections_blocker->add_blocker();
806
807   // Use a blocker to block the final cleanup task.
808   Task_token* final_blocker = new Task_token(true);
809   // Write_symbols_task, Write_sections_task, Write_data_task,
810   // Relocate_tasks.
811   final_blocker->add_blockers(3);
812   final_blocker->add_blockers(input_objects->number_of_relobjs());
813   if (!any_postprocessing_sections)
814     final_blocker->add_blocker();
815
816   // Queue a task to write out the symbol table.
817   workqueue->queue(new Write_symbols_task(layout,
818                                           symtab,
819                                           input_objects,
820                                           layout->sympool(),
821                                           layout->dynpool(),
822                                           of,
823                                           final_blocker));
824
825   // Queue a task to write out the output sections.
826   workqueue->queue(new Write_sections_task(layout, of, output_sections_blocker,
827                                            final_blocker));
828
829   // Queue a task to write out everything else.
830   workqueue->queue(new Write_data_task(layout, symtab, of, final_blocker));
831
832   // Queue a task for each input object to relocate the sections and
833   // write out the local symbols.
834   for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
835        p != input_objects->relobj_end();
836        ++p)
837     workqueue->queue(new Relocate_task(symtab, layout, *p, of,
838                                        input_sections_blocker,
839                                        output_sections_blocker,
840                                        final_blocker));
841
842   // Queue a task to write out the output sections which depend on
843   // input sections.  If there are any sections which require
844   // postprocessing, then we need to do this last, since it may resize
845   // the output file.
846   if (!any_postprocessing_sections)
847     {
848       Task* t = new Write_after_input_sections_task(layout, of,
849                                                     input_sections_blocker,
850                                                     final_blocker);
851       workqueue->queue(t);
852     }
853   else
854     {
855       Task_token* new_final_blocker = new Task_token(true);
856       new_final_blocker->add_blocker();
857       Task* t = new Write_after_input_sections_task(layout, of,
858                                                     final_blocker,
859                                                     new_final_blocker);
860       workqueue->queue(t);
861       final_blocker = new_final_blocker;
862     }
863
864   // Queue a task to close the output file.  This will be blocked by
865   // FINAL_BLOCKER.
866   workqueue->queue(new Task_function(new Close_task_runner(&options, layout,
867                                                            of),
868                                      final_blocker,
869                                      "Task_function Close_task_runner"));
870 }
871
872 } // End namespace gold.