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