*** empty log message ***
[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
f0558624
ST
542 /* If plugins have specified a section order, re-arrange input sections
543 according to a specified section order. If --section-ordering-file is
544 also specified, do not do anything here. */
545 if (parameters->options().has_plugins()
546 && layout->is_section_ordering_specified()
547 && !parameters->options().section_ordering_file ())
548 {
549 for (Layout::Section_list::const_iterator p
550 = layout->section_list().begin();
551 p != layout->section_list().end();
552 ++p)
553 (*p)->update_section_layout(layout->get_section_order_map());
554 }
555
6d03d481
ST
556 // Layout deferred objects due to plugins.
557 if (parameters->options().has_plugins())
558 {
559 Plugin_manager* plugins = parameters->options().plugins();
560 gold_assert(plugins != NULL);
561 plugins->layout_deferred_objects();
562 }
ef15dade 563
032ce4e9
ST
564 if (parameters->options().gc_sections()
565 || parameters->options().icf_enabled())
6d03d481
ST
566 {
567 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
568 p != input_objects->relobj_end();
569 ++p)
570 {
571 // Update the value of output_section stored in rd.
ca09d69a 572 Read_relocs_data* rd = (*p)->get_relocs_data();
6d03d481
ST
573 for (Read_relocs_data::Relocs_list::iterator q = rd->relocs.begin();
574 q != rd->relocs.end();
575 ++q)
576 {
577 q->output_section = (*p)->output_section(q->data_shndx);
578 q->needs_special_offset_handling =
579 (*p)->is_output_section_offset_invalid(q->data_shndx);
580 }
581 }
582 }
583
fbfba508
ILT
584 // We have to support the case of not seeing any input objects, and
585 // generate an empty file. Existing builds depend on being able to
586 // pass an empty archive to the linker and get an empty object file
587 // out. In order to do this we need to use a default target.
cdc29364
CC
588 if (input_objects->number_of_input_objects() == 0
589 && layout->incremental_base() == NULL)
029ba973 590 parameters_force_valid_target();
2c0aeda4 591
fe9a4c12
ILT
592 int thread_count = options.thread_count_middle();
593 if (thread_count == 0)
fbfba508 594 thread_count = std::max(2, input_objects->number_of_input_objects());
fe9a4c12
ILT
595 workqueue->set_thread_count(thread_count);
596
b3b74ddc 597 // Now we have seen all the input files.
374ad285
ILT
598 const bool doing_static_link =
599 (!input_objects->any_dynamic()
600 && !parameters->options().output_is_position_independent());
4eff2974
ILT
601 set_parameters_doing_static_link(doing_static_link);
602 if (!doing_static_link && options.is_static())
603 {
604 // We print out just the first .so we see; there may be others.
4e1e25e0 605 gold_assert(input_objects->dynobj_begin() != input_objects->dynobj_end());
75f2446e
ILT
606 gold_error(_("cannot mix -static with dynamic object %s"),
607 (*input_objects->dynobj_begin())->name().c_str());
4eff2974 608 }
8851ecca 609 if (!doing_static_link && parameters->options().relocatable())
459c9f1c 610 gold_fatal(_("cannot mix -r with dynamic object %s"),
6a74a719 611 (*input_objects->dynobj_begin())->name().c_str());
516cb3d0 612 if (!doing_static_link
7cc619c3 613 && options.oformat_enum() != General_options::OBJECT_FORMAT_ELF)
516cb3d0
ILT
614 gold_fatal(_("cannot use non-ELF output format with dynamic object %s"),
615 (*input_objects->dynobj_begin())->name().c_str());
b3b74ddc 616
364c7fa5
ILT
617 if (parameters->options().relocatable())
618 {
619 Input_objects::Relobj_iterator p = input_objects->relobj_begin();
620 if (p != input_objects->relobj_end())
621 {
622 bool uses_split_stack = (*p)->uses_split_stack();
623 for (++p; p != input_objects->relobj_end(); ++p)
624 {
625 if ((*p)->uses_split_stack() != uses_split_stack)
626 gold_fatal(_("cannot mix split-stack '%s' and "
627 "non-split-stack '%s' when using -r"),
628 (*input_objects->relobj_begin())->name().c_str(),
629 (*p)->name().c_str());
630 }
631 }
632 }
633
26d3c67d
CC
634 // For incremental updates, record the existing GOT and PLT entries,
635 // and the COPY relocations.
4829d394
CC
636 if (parameters->incremental_update())
637 {
638 Incremental_binary* ibase = layout->incremental_base();
639 ibase->process_got_plt(symtab, layout);
26d3c67d 640 ibase->emit_copy_relocs(symtab);
4829d394
CC
641 }
642
494e05f4
ILT
643 if (is_debugging_enabled(DEBUG_SCRIPT))
644 layout->script_options()->print(stderr);
645
e2827e5f
ILT
646 // For each dynamic object, record whether we've seen all the
647 // dynamic objects that it depends upon.
648 input_objects->check_dynamic_dependencies();
649
70e654ba
ILT
650 // See if any of the input definitions violate the One Definition Rule.
651 // TODO: if this is too slow, do this as a task, rather than inline.
17a1d0a9 652 symtab->detect_odr_violations(task, options.output_file_name());
70e654ba 653
62dfdd4d
ILT
654 // Do the --no-undefined-version check.
655 if (!parameters->options().undefined_version())
656 {
657 Script_options* so = layout->script_options();
658 so->version_script_info()->check_unmatched_names(symtab);
659 }
660
9c547ec3
ILT
661 // Create any automatic note sections.
662 layout->create_notes();
663
919ed24c
ILT
664 // Create any output sections required by any linker script.
665 layout->create_script_sections();
666
a3ad94ed
ILT
667 // Define some sections and symbols needed for a dynamic link. This
668 // handles some cases we want to see before we read the relocs.
9b07f471 669 layout->create_initial_dynamic_sections(symtab);
a3ad94ed 670
a445fddf
ILT
671 // Define symbols from any linker scripts.
672 layout->define_script_symbols(symtab);
673
154e0e9a
ILT
674 // Attach sections to segments.
675 layout->attach_sections_to_segments();
676
8851ecca 677 if (!parameters->options().relocatable())
6a74a719
ILT
678 {
679 // Predefine standard symbols.
680 define_standard_symbols(symtab, layout);
ead1e424 681
6a74a719
ILT
682 // Define __start and __stop symbols for output sections where
683 // appropriate.
684 layout->define_section_symbols(symtab);
685 }
bfd58944 686
755ab8af
ILT
687 // Make sure we have symbols for any required group signatures.
688 layout->define_group_signatures(symtab);
689
93ceb764 690 Task_token* this_blocker = NULL;
fa17a3f4 691
93ceb764
ILT
692 // Allocate common symbols. We use a blocker to run this before the
693 // Scan_relocs tasks, because it writes to the symbol table just as
694 // they do.
695 if (parameters->options().define_common())
696 {
697 this_blocker = new Task_token(true);
698 this_blocker->add_blocker();
699 workqueue->queue(new Allocate_commons_task(symtab, layout, mapfile,
700 this_blocker));
701 }
6d03d481
ST
702
703 // If doing garbage collection, the relocations have already been read.
704 // Otherwise, read and scan the relocations.
032ce4e9
ST
705 if (parameters->options().gc_sections()
706 || parameters->options().icf_enabled())
92e059d8 707 {
6d03d481
ST
708 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
709 p != input_objects->relobj_end();
710 ++p)
93ceb764
ILT
711 {
712 Task_token* next_blocker = new Task_token(true);
713 next_blocker->add_blocker();
714 workqueue->queue(new Scan_relocs(symtab, layout, *p,
715 (*p)->get_relocs_data(),
716 this_blocker, next_blocker));
717 this_blocker = next_blocker;
718 }
6d03d481
ST
719 }
720 else
721 {
722 // Read the relocations of the input files. We do this to find
723 // which symbols are used by relocations which require a GOT and/or
724 // a PLT entry, or a COPY reloc. When we implement garbage
725 // collection we will do it here by reading the relocations in a
726 // breadth first search by references.
727 //
728 // We could also read the relocations during the first pass, and
729 // mark symbols at that time. That is how the old GNU linker works.
730 // Doing that is more complex, since we may later decide to discard
731 // some of the sections, and thus change our minds about the types
732 // of references made to the symbols.
733 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
734 p != input_objects->relobj_end();
735 ++p)
736 {
93ceb764
ILT
737 Task_token* next_blocker = new Task_token(true);
738 next_blocker->add_blocker();
739 workqueue->queue(new Read_relocs(symtab, layout, *p, this_blocker,
740 next_blocker));
741 this_blocker = next_blocker;
6d03d481 742 }
92e059d8
ILT
743 }
744
135b9c78
ILT
745 if (this_blocker == NULL)
746 {
7296d933
DK
747 if (input_objects->number_of_relobjs() == 0)
748 {
749 // If we are given only archives in input, we have no regular
750 // objects and THIS_BLOCKER is NULL here. Create a dummy
751 // blocker here so that we can run the layout task immediately.
752 this_blocker = new Task_token(true);
753 }
754 else
755 {
756 // If we failed to open any input files, it's possible for
757 // THIS_BLOCKER to be NULL here. There's no real point in
758 // continuing if that happens.
759 gold_assert(parameters->errors()->error_count() > 0);
e6455dfb 760 gold_exit(GOLD_ERR);
7296d933 761 }
135b9c78
ILT
762 }
763
92e059d8
ILT
764 // When all those tasks are complete, we can start laying out the
765 // output file.
8851ecca
ILT
766 // TODO(csilvers): figure out a more principled way to get the target
767 Target* target = const_cast<Target*>(&parameters->target());
92e059d8
ILT
768 workqueue->queue(new Task_function(new Layout_task_runner(options,
769 input_objects,
770 symtab,
8851ecca 771 target,
7d9e3d98
ILT
772 layout,
773 mapfile),
93ceb764 774 this_blocker,
c7912668 775 "Task_function Layout_task_runner"));
92e059d8 776}
61ba1cf9
ILT
777
778// Queue up the final set of tasks. This is called at the end of
779// Layout_task.
780
781void
782queue_final_tasks(const General_options& options,
783 const Input_objects* input_objects,
784 const Symbol_table* symtab,
27bc2bce 785 Layout* layout,
61ba1cf9
ILT
786 Workqueue* workqueue,
787 Output_file* of)
788{
fe9a4c12
ILT
789 int thread_count = options.thread_count_final();
790 if (thread_count == 0)
fbfba508 791 thread_count = std::max(2, input_objects->number_of_input_objects());
fe9a4c12
ILT
792 workqueue->set_thread_count(thread_count);
793
17a1d0a9
ILT
794 bool any_postprocessing_sections = layout->any_postprocessing_sections();
795
730cdc88
ILT
796 // Use a blocker to wait until all the input sections have been
797 // written out.
17a1d0a9
ILT
798 Task_token* input_sections_blocker = NULL;
799 if (!any_postprocessing_sections)
fa17a3f4
ILT
800 {
801 input_sections_blocker = new Task_token(true);
802 input_sections_blocker->add_blockers(input_objects->number_of_relobjs());
803 }
730cdc88
ILT
804
805 // Use a blocker to block any objects which have to wait for the
806 // output sections to complete before they can apply relocations.
17a1d0a9 807 Task_token* output_sections_blocker = new Task_token(true);
fa17a3f4 808 output_sections_blocker->add_blocker();
730cdc88 809
61ba1cf9 810 // Use a blocker to block the final cleanup task.
17a1d0a9 811 Task_token* final_blocker = new Task_token(true);
fa17a3f4
ILT
812 // Write_symbols_task, Write_sections_task, Write_data_task,
813 // Relocate_tasks.
814 final_blocker->add_blockers(3);
815 final_blocker->add_blockers(input_objects->number_of_relobjs());
816 if (!any_postprocessing_sections)
817 final_blocker->add_blocker();
61ba1cf9
ILT
818
819 // Queue a task to write out the symbol table.
d491d34e
ILT
820 workqueue->queue(new Write_symbols_task(layout,
821 symtab,
5fe2a0f5
ILT
822 input_objects,
823 layout->sympool(),
824 layout->dynpool(),
825 of,
826 final_blocker));
61ba1cf9 827
730cdc88 828 // Queue a task to write out the output sections.
730cdc88
ILT
829 workqueue->queue(new Write_sections_task(layout, of, output_sections_blocker,
830 final_blocker));
831
61ba1cf9 832 // Queue a task to write out everything else.
9025d29d 833 workqueue->queue(new Write_data_task(layout, symtab, of, final_blocker));
61ba1cf9 834
17a1d0a9
ILT
835 // Queue a task for each input object to relocate the sections and
836 // write out the local symbols.
837 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
838 p != input_objects->relobj_end();
839 ++p)
fa17a3f4
ILT
840 workqueue->queue(new Relocate_task(symtab, layout, *p, of,
841 input_sections_blocker,
842 output_sections_blocker,
843 final_blocker));
17a1d0a9 844
730cdc88 845 // Queue a task to write out the output sections which depend on
17a1d0a9
ILT
846 // input sections. If there are any sections which require
847 // postprocessing, then we need to do this last, since it may resize
848 // the output file.
849 if (!any_postprocessing_sections)
850 {
17a1d0a9
ILT
851 Task* t = new Write_after_input_sections_task(layout, of,
852 input_sections_blocker,
853 final_blocker);
854 workqueue->queue(t);
855 }
856 else
857 {
ca09d69a 858 Task_token* new_final_blocker = new Task_token(true);
17a1d0a9
ILT
859 new_final_blocker->add_blocker();
860 Task* t = new Write_after_input_sections_task(layout, of,
861 final_blocker,
862 new_final_blocker);
863 workqueue->queue(t);
864 final_blocker = new_final_blocker;
865 }
730cdc88 866
61ba1cf9
ILT
867 // Queue a task to close the output file. This will be blocked by
868 // FINAL_BLOCKER.
516cb3d0
ILT
869 workqueue->queue(new Task_function(new Close_task_runner(&options, layout,
870 of),
c7912668
ILT
871 final_blocker,
872 "Task_function Close_task_runner"));
61ba1cf9
ILT
873}
874
875} // End namespace gold.
This page took 0.295739 seconds and 4 git commands to generate.