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