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