* gold/incremental.cc (Sized_incremental_binary::do_process_got_plt):
[deliverable/binutils-gdb.git] / gold / gold.cc
CommitLineData
5a6f7e2d 1// gold.cc -- main linker functions
bae7f79e 2
f1ddb600 3// Copyright 2006, 2007, 2008, 2009, 2010, 2011 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"
f1ddb600 33#include "target-select.h"
494e05f4 34#include "debug.h"
bae7f79e
ILT
35#include "workqueue.h"
36#include "dirsearch.h"
37#include "readsyms.h"
14bfc3f5 38#include "symtab.h"
ead1e424 39#include "common.h"
54dc6425 40#include "object.h"
a2fb1b05 41#include "layout.h"
61ba1cf9 42#include "reloc.h"
ead1e424 43#include "defstd.h"
89fc3421 44#include "plugin.h"
f345227a 45#include "gc.h"
ef15dade 46#include "icf.h"
44453f85 47#include "incremental.h"
bae7f79e
ILT
48
49namespace gold
50{
51
cdc29364
CC
52class Object;
53
bae7f79e
ILT
54const char* program_name;
55
cdc29364
CC
56static Task*
57process_incremental_input(Incremental_binary*, unsigned int, Input_objects*,
58 Symbol_table*, Layout*, Dirsearch*, Mapfile*,
59 Task_token*, Task_token*);
60
bae7f79e 61void
e6455dfb 62gold_exit(Exit_status status)
bae7f79e 63{
483620e8
CC
64 if (parameters != NULL
65 && parameters->options_valid()
66 && parameters->options().has_plugins())
67 parameters->options().plugins()->cleanup();
e6455dfb 68 if (status != GOLD_OK && parameters != NULL && parameters->options_valid())
8851ecca 69 unlink_if_ordinary(parameters->options().output_file_name());
e6455dfb 70 exit(status);
bae7f79e
ILT
71}
72
bae7f79e
ILT
73void
74gold_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.
55ba0940
ILT
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 }
e6455dfb 91 gold_exit(GOLD_ERR);
bae7f79e
ILT
92}
93
a3ad94ed
ILT
94// Handle an unreachable case.
95
bae7f79e 96void
a3ad94ed 97do_gold_unreachable(const char* filename, int lineno, const char* function)
bae7f79e 98{
27b7985a 99 fprintf(stderr, _("%s: internal error in %s, at %s:%d\n"),
a3ad94ed 100 program_name, function, filename, lineno);
e6455dfb 101 gold_exit(GOLD_ERR);
bae7f79e
ILT
102}
103
92e059d8
ILT
104// This class arranges to run the functions done in the middle of the
105// link. It is just a closure.
bae7f79e 106
92e059d8 107class Middle_runner : public Task_function_runner
bae7f79e 108{
92e059d8
ILT
109 public:
110 Middle_runner(const General_options& options,
111 const Input_objects* input_objects,
112 Symbol_table* symtab,
7d9e3d98 113 Layout* layout, Mapfile* mapfile)
92e059d8 114 : options_(options), input_objects_(input_objects), symtab_(symtab),
7d9e3d98 115 layout_(layout), mapfile_(mapfile)
92e059d8
ILT
116 { }
117
118 void
17a1d0a9 119 run(Workqueue*, const Task*);
92e059d8
ILT
120
121 private:
122 const General_options& options_;
123 const Input_objects* input_objects_;
124 Symbol_table* symtab_;
125 Layout* layout_;
7d9e3d98 126 Mapfile* mapfile_;
92e059d8 127};
bae7f79e 128
92e059d8 129void
17a1d0a9 130Middle_runner::run(Workqueue* workqueue, const Task* task)
92e059d8 131{
17a1d0a9 132 queue_middle_tasks(this->options_, task, this->input_objects_, this->symtab_,
7d9e3d98 133 this->layout_, workqueue, this->mapfile_);
92e059d8 134}
bae7f79e 135
6d03d481
ST
136// This class arranges the tasks to process the relocs for garbage collection.
137
138class 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
160void
161Gc_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
bae7f79e
ILT
168// Queue up the initial set of tasks for this link job.
169
170void
171queue_initial_tasks(const General_options& options,
17a1d0a9 172 Dirsearch& search_path,
ead1e424 173 const Command_line& cmdline,
54dc6425 174 Workqueue* workqueue, Input_objects* input_objects,
7d9e3d98 175 Symbol_table* symtab, Layout* layout, Mapfile* mapfile)
bae7f79e 176{
e5366891 177 if (cmdline.begin() == cmdline.end())
459c9f1c 178 {
f1ddb600 179 bool is_ok = false;
459c9f1c 180 if (options.printed_version())
f1ddb600
ILT
181 is_ok = true;
182 if (options.print_output_format())
183 {
184 print_output_format();
185 is_ok = true;
186 }
187 if (is_ok)
e6455dfb 188 gold_exit(GOLD_OK);
459c9f1c
ILT
189 gold_fatal(_("no input files"));
190 }
e5366891 191
fe9a4c12
ILT
192 int thread_count = options.thread_count_initial();
193 if (thread_count == 0)
e5366891 194 thread_count = cmdline.number_of_input_files();
fe9a4c12
ILT
195 workqueue->set_thread_count(thread_count);
196
cdc29364
CC
197 // For incremental links, the base output file.
198 Incremental_binary* ibase = NULL;
199
403a3331 200 if (parameters->incremental_update())
44453f85 201 {
403a3331
CC
202 Output_file* of = new Output_file(options.output_file_name());
203 if (of->open_base_file(options.incremental_base(), true))
cdc29364 204 {
403a3331
CC
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
cdc29364 210 {
403a3331
CC
211 delete ibase;
212 ibase = NULL;
213 of->close();
cdc29364
CC
214 }
215 }
403a3331
CC
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 }
44453f85
ILT
223 }
224
bae7f79e
ILT
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;
cdc29364 230 if (ibase == NULL)
bae7f79e 231 {
cdc29364
CC
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]);
bae7f79e
ILT
269 }
270
89fc3421
CC
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
cdc29364
CC
281 if (options.relocatable()
282 && (options.gc_sections() || options.icf_enabled()))
ef15dade 283 gold_error(_("cannot mix -r with --gc-sections or --icf"));
6d03d481 284
cdc29364 285 if (options.gc_sections() || options.icf_enabled())
6d03d481
ST
286 {
287 workqueue->queue(new Task_function(new Gc_runner(options,
ad0f2072 288 input_objects,
6d03d481
ST
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
cdc29364
CC
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
313static Task*
314process_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
44453f85
ILT
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
6d03d481
ST
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
436void
437queue_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.
93ceb764 447 Task_token* this_blocker = NULL;
6d03d481
ST
448 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
449 p != input_objects->relobj_end();
450 ++p)
93ceb764
ILT
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 }
04ceb17c
DK
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
92e059d8 468 workqueue->queue(new Task_function(new Middle_runner(options,
6d03d481
ST
469 input_objects,
470 symtab,
471 layout,
472 mapfile),
93ceb764 473 this_blocker,
6d03d481 474 "Task_function Middle_runner"));
bae7f79e
ILT
475}
476
92e059d8
ILT
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.
bae7f79e 480
92e059d8
ILT
481void
482queue_middle_tasks(const General_options& options,
17a1d0a9 483 const Task* task,
92e059d8
ILT
484 const Input_objects* input_objects,
485 Symbol_table* symtab,
486 Layout* layout,
7d9e3d98
ILT
487 Workqueue* workqueue,
488 Mapfile* mapfile)
61ba1cf9 489{
6d03d481 490 // Add any symbols named with -u options to the symbol table.
88a4108b 491 symtab->add_undefined_symbols_from_command_line(layout);
6d03d481
ST
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.
a10ae760 499 Symbol* start_sym = symtab->lookup(parameters->entry());
556bd683 500 if (start_sym != NULL)
6d03d481
ST
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.
88a4108b 511 symtab->gc_mark_undef_symbols(layout);
6d03d481
ST
512 gold_assert(symtab->gc() != NULL);
513 // Do a transitive closure on all references to determine the worklist.
514 symtab->gc()->do_transitive_closure();
ef15dade
ST
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.
032ce4e9 520 if (parameters->options().icf_enabled())
ef15dade
ST
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.
032ce4e9
ST
530 if (parameters->options().gc_sections()
531 || parameters->options().icf_enabled())
ef15dade 532 {
6d03d481
ST
533 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
534 p != input_objects->relobj_end();
535 ++p)
536 {
5f9bcf58 537 Task_lock_obj<Object> tlo(task, *p);
6d03d481
ST
538 (*p)->layout(symtab, layout, NULL);
539 }
540 }
ef15dade 541
6d03d481
ST
542 // Layout deferred objects due to plugins.
543 if (parameters->options().has_plugins())
544 {
545 Plugin_manager* plugins = parameters->options().plugins();
546 gold_assert(plugins != NULL);
547 plugins->layout_deferred_objects();
548 }
ef15dade 549
032ce4e9
ST
550 if (parameters->options().gc_sections()
551 || parameters->options().icf_enabled())
6d03d481
ST
552 {
553 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
554 p != input_objects->relobj_end();
555 ++p)
556 {
557 // Update the value of output_section stored in rd.
ca09d69a 558 Read_relocs_data* rd = (*p)->get_relocs_data();
6d03d481
ST
559 for (Read_relocs_data::Relocs_list::iterator q = rd->relocs.begin();
560 q != rd->relocs.end();
561 ++q)
562 {
563 q->output_section = (*p)->output_section(q->data_shndx);
564 q->needs_special_offset_handling =
565 (*p)->is_output_section_offset_invalid(q->data_shndx);
566 }
567 }
568 }
569
fbfba508
ILT
570 // We have to support the case of not seeing any input objects, and
571 // generate an empty file. Existing builds depend on being able to
572 // pass an empty archive to the linker and get an empty object file
573 // out. In order to do this we need to use a default target.
cdc29364
CC
574 if (input_objects->number_of_input_objects() == 0
575 && layout->incremental_base() == NULL)
029ba973 576 parameters_force_valid_target();
2c0aeda4 577
fe9a4c12
ILT
578 int thread_count = options.thread_count_middle();
579 if (thread_count == 0)
fbfba508 580 thread_count = std::max(2, input_objects->number_of_input_objects());
fe9a4c12
ILT
581 workqueue->set_thread_count(thread_count);
582
b3b74ddc 583 // Now we have seen all the input files.
374ad285
ILT
584 const bool doing_static_link =
585 (!input_objects->any_dynamic()
586 && !parameters->options().output_is_position_independent());
4eff2974
ILT
587 set_parameters_doing_static_link(doing_static_link);
588 if (!doing_static_link && options.is_static())
589 {
590 // We print out just the first .so we see; there may be others.
4e1e25e0 591 gold_assert(input_objects->dynobj_begin() != input_objects->dynobj_end());
75f2446e
ILT
592 gold_error(_("cannot mix -static with dynamic object %s"),
593 (*input_objects->dynobj_begin())->name().c_str());
4eff2974 594 }
8851ecca 595 if (!doing_static_link && parameters->options().relocatable())
459c9f1c 596 gold_fatal(_("cannot mix -r with dynamic object %s"),
6a74a719 597 (*input_objects->dynobj_begin())->name().c_str());
516cb3d0 598 if (!doing_static_link
7cc619c3 599 && options.oformat_enum() != General_options::OBJECT_FORMAT_ELF)
516cb3d0
ILT
600 gold_fatal(_("cannot use non-ELF output format with dynamic object %s"),
601 (*input_objects->dynobj_begin())->name().c_str());
b3b74ddc 602
364c7fa5
ILT
603 if (parameters->options().relocatable())
604 {
605 Input_objects::Relobj_iterator p = input_objects->relobj_begin();
606 if (p != input_objects->relobj_end())
607 {
608 bool uses_split_stack = (*p)->uses_split_stack();
609 for (++p; p != input_objects->relobj_end(); ++p)
610 {
611 if ((*p)->uses_split_stack() != uses_split_stack)
612 gold_fatal(_("cannot mix split-stack '%s' and "
613 "non-split-stack '%s' when using -r"),
614 (*input_objects->relobj_begin())->name().c_str(),
615 (*p)->name().c_str());
616 }
617 }
618 }
619
26d3c67d
CC
620 // For incremental updates, record the existing GOT and PLT entries,
621 // and the COPY relocations.
4829d394
CC
622 if (parameters->incremental_update())
623 {
624 Incremental_binary* ibase = layout->incremental_base();
625 ibase->process_got_plt(symtab, layout);
26d3c67d 626 ibase->emit_copy_relocs(symtab);
4829d394
CC
627 }
628
494e05f4
ILT
629 if (is_debugging_enabled(DEBUG_SCRIPT))
630 layout->script_options()->print(stderr);
631
e2827e5f
ILT
632 // For each dynamic object, record whether we've seen all the
633 // dynamic objects that it depends upon.
634 input_objects->check_dynamic_dependencies();
635
70e654ba
ILT
636 // See if any of the input definitions violate the One Definition Rule.
637 // TODO: if this is too slow, do this as a task, rather than inline.
17a1d0a9 638 symtab->detect_odr_violations(task, options.output_file_name());
70e654ba 639
62dfdd4d
ILT
640 // Do the --no-undefined-version check.
641 if (!parameters->options().undefined_version())
642 {
643 Script_options* so = layout->script_options();
644 so->version_script_info()->check_unmatched_names(symtab);
645 }
646
9c547ec3
ILT
647 // Create any automatic note sections.
648 layout->create_notes();
649
919ed24c
ILT
650 // Create any output sections required by any linker script.
651 layout->create_script_sections();
652
a3ad94ed
ILT
653 // Define some sections and symbols needed for a dynamic link. This
654 // handles some cases we want to see before we read the relocs.
9b07f471 655 layout->create_initial_dynamic_sections(symtab);
a3ad94ed 656
a445fddf
ILT
657 // Define symbols from any linker scripts.
658 layout->define_script_symbols(symtab);
659
154e0e9a
ILT
660 // Attach sections to segments.
661 layout->attach_sections_to_segments();
662
8851ecca 663 if (!parameters->options().relocatable())
6a74a719
ILT
664 {
665 // Predefine standard symbols.
666 define_standard_symbols(symtab, layout);
ead1e424 667
6a74a719
ILT
668 // Define __start and __stop symbols for output sections where
669 // appropriate.
670 layout->define_section_symbols(symtab);
671 }
bfd58944 672
755ab8af
ILT
673 // Make sure we have symbols for any required group signatures.
674 layout->define_group_signatures(symtab);
675
93ceb764 676 Task_token* this_blocker = NULL;
fa17a3f4 677
93ceb764
ILT
678 // Allocate common symbols. We use a blocker to run this before the
679 // Scan_relocs tasks, because it writes to the symbol table just as
680 // they do.
681 if (parameters->options().define_common())
682 {
683 this_blocker = new Task_token(true);
684 this_blocker->add_blocker();
685 workqueue->queue(new Allocate_commons_task(symtab, layout, mapfile,
686 this_blocker));
687 }
6d03d481
ST
688
689 // If doing garbage collection, the relocations have already been read.
690 // Otherwise, read and scan the relocations.
032ce4e9
ST
691 if (parameters->options().gc_sections()
692 || parameters->options().icf_enabled())
92e059d8 693 {
6d03d481
ST
694 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
695 p != input_objects->relobj_end();
696 ++p)
93ceb764
ILT
697 {
698 Task_token* next_blocker = new Task_token(true);
699 next_blocker->add_blocker();
700 workqueue->queue(new Scan_relocs(symtab, layout, *p,
701 (*p)->get_relocs_data(),
702 this_blocker, next_blocker));
703 this_blocker = next_blocker;
704 }
6d03d481
ST
705 }
706 else
707 {
708 // Read the relocations of the input files. We do this to find
709 // which symbols are used by relocations which require a GOT and/or
710 // a PLT entry, or a COPY reloc. When we implement garbage
711 // collection we will do it here by reading the relocations in a
712 // breadth first search by references.
713 //
714 // We could also read the relocations during the first pass, and
715 // mark symbols at that time. That is how the old GNU linker works.
716 // Doing that is more complex, since we may later decide to discard
717 // some of the sections, and thus change our minds about the types
718 // of references made to the symbols.
719 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
720 p != input_objects->relobj_end();
721 ++p)
722 {
93ceb764
ILT
723 Task_token* next_blocker = new Task_token(true);
724 next_blocker->add_blocker();
725 workqueue->queue(new Read_relocs(symtab, layout, *p, this_blocker,
726 next_blocker));
727 this_blocker = next_blocker;
6d03d481 728 }
92e059d8
ILT
729 }
730
135b9c78
ILT
731 if (this_blocker == NULL)
732 {
7296d933
DK
733 if (input_objects->number_of_relobjs() == 0)
734 {
735 // If we are given only archives in input, we have no regular
736 // objects and THIS_BLOCKER is NULL here. Create a dummy
737 // blocker here so that we can run the layout task immediately.
738 this_blocker = new Task_token(true);
739 }
740 else
741 {
742 // If we failed to open any input files, it's possible for
743 // THIS_BLOCKER to be NULL here. There's no real point in
744 // continuing if that happens.
745 gold_assert(parameters->errors()->error_count() > 0);
e6455dfb 746 gold_exit(GOLD_ERR);
7296d933 747 }
135b9c78
ILT
748 }
749
92e059d8
ILT
750 // When all those tasks are complete, we can start laying out the
751 // output file.
8851ecca
ILT
752 // TODO(csilvers): figure out a more principled way to get the target
753 Target* target = const_cast<Target*>(&parameters->target());
92e059d8
ILT
754 workqueue->queue(new Task_function(new Layout_task_runner(options,
755 input_objects,
756 symtab,
8851ecca 757 target,
7d9e3d98
ILT
758 layout,
759 mapfile),
93ceb764 760 this_blocker,
c7912668 761 "Task_function Layout_task_runner"));
92e059d8 762}
61ba1cf9
ILT
763
764// Queue up the final set of tasks. This is called at the end of
765// Layout_task.
766
767void
768queue_final_tasks(const General_options& options,
769 const Input_objects* input_objects,
770 const Symbol_table* symtab,
27bc2bce 771 Layout* layout,
61ba1cf9
ILT
772 Workqueue* workqueue,
773 Output_file* of)
774{
fe9a4c12
ILT
775 int thread_count = options.thread_count_final();
776 if (thread_count == 0)
fbfba508 777 thread_count = std::max(2, input_objects->number_of_input_objects());
fe9a4c12
ILT
778 workqueue->set_thread_count(thread_count);
779
17a1d0a9
ILT
780 bool any_postprocessing_sections = layout->any_postprocessing_sections();
781
730cdc88
ILT
782 // Use a blocker to wait until all the input sections have been
783 // written out.
17a1d0a9
ILT
784 Task_token* input_sections_blocker = NULL;
785 if (!any_postprocessing_sections)
fa17a3f4
ILT
786 {
787 input_sections_blocker = new Task_token(true);
788 input_sections_blocker->add_blockers(input_objects->number_of_relobjs());
789 }
730cdc88
ILT
790
791 // Use a blocker to block any objects which have to wait for the
792 // output sections to complete before they can apply relocations.
17a1d0a9 793 Task_token* output_sections_blocker = new Task_token(true);
fa17a3f4 794 output_sections_blocker->add_blocker();
730cdc88 795
61ba1cf9 796 // Use a blocker to block the final cleanup task.
17a1d0a9 797 Task_token* final_blocker = new Task_token(true);
fa17a3f4
ILT
798 // Write_symbols_task, Write_sections_task, Write_data_task,
799 // Relocate_tasks.
800 final_blocker->add_blockers(3);
801 final_blocker->add_blockers(input_objects->number_of_relobjs());
802 if (!any_postprocessing_sections)
803 final_blocker->add_blocker();
61ba1cf9
ILT
804
805 // Queue a task to write out the symbol table.
d491d34e
ILT
806 workqueue->queue(new Write_symbols_task(layout,
807 symtab,
5fe2a0f5
ILT
808 input_objects,
809 layout->sympool(),
810 layout->dynpool(),
811 of,
812 final_blocker));
61ba1cf9 813
730cdc88 814 // Queue a task to write out the output sections.
730cdc88
ILT
815 workqueue->queue(new Write_sections_task(layout, of, output_sections_blocker,
816 final_blocker));
817
61ba1cf9 818 // Queue a task to write out everything else.
9025d29d 819 workqueue->queue(new Write_data_task(layout, symtab, of, final_blocker));
61ba1cf9 820
17a1d0a9
ILT
821 // Queue a task for each input object to relocate the sections and
822 // write out the local symbols.
823 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
824 p != input_objects->relobj_end();
825 ++p)
fa17a3f4
ILT
826 workqueue->queue(new Relocate_task(symtab, layout, *p, of,
827 input_sections_blocker,
828 output_sections_blocker,
829 final_blocker));
17a1d0a9 830
730cdc88 831 // Queue a task to write out the output sections which depend on
17a1d0a9
ILT
832 // input sections. If there are any sections which require
833 // postprocessing, then we need to do this last, since it may resize
834 // the output file.
835 if (!any_postprocessing_sections)
836 {
17a1d0a9
ILT
837 Task* t = new Write_after_input_sections_task(layout, of,
838 input_sections_blocker,
839 final_blocker);
840 workqueue->queue(t);
841 }
842 else
843 {
ca09d69a 844 Task_token* new_final_blocker = new Task_token(true);
17a1d0a9
ILT
845 new_final_blocker->add_blocker();
846 Task* t = new Write_after_input_sections_task(layout, of,
847 final_blocker,
848 new_final_blocker);
849 workqueue->queue(t);
850 final_blocker = new_final_blocker;
851 }
730cdc88 852
61ba1cf9
ILT
853 // Queue a task to close the output file. This will be blocked by
854 // FINAL_BLOCKER.
516cb3d0
ILT
855 workqueue->queue(new Task_function(new Close_task_runner(&options, layout,
856 of),
c7912668
ILT
857 final_blocker,
858 "Task_function Close_task_runner"));
61ba1cf9
ILT
859}
860
861} // End namespace gold.
This page took 0.290022 seconds and 4 git commands to generate.