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