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