ARI fix: OP eol rule.
[deliverable/binutils-gdb.git] / gold / gold.cc
CommitLineData
5a6f7e2d 1// gold.cc -- main linker functions
bae7f79e 2
6d03d481 3// Copyright 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
6cb15b7f
ILT
4// Written by Ian Lance Taylor <iant@google.com>.
5
6// This file is part of gold.
7
8// This program is free software; you can redistribute it and/or modify
9// it under the terms of the GNU General Public License as published by
10// the Free Software Foundation; either version 3 of the License, or
11// (at your option) any later version.
12
13// This program is distributed in the hope that it will be useful,
14// but WITHOUT ANY WARRANTY; without even the implied warranty of
15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16// GNU General Public License for more details.
17
18// You should have received a copy of the GNU General Public License
19// along with this program; if not, write to the Free Software
20// Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21// MA 02110-1301, USA.
22
bae7f79e
ILT
23#include "gold.h"
24
25#include <cstdlib>
26#include <cstdio>
27#include <cstring>
28#include <unistd.h>
fbfba508 29#include <algorithm>
75f2446e 30#include "libiberty.h"
bae7f79e
ILT
31
32#include "options.h"
494e05f4 33#include "debug.h"
bae7f79e
ILT
34#include "workqueue.h"
35#include "dirsearch.h"
36#include "readsyms.h"
14bfc3f5 37#include "symtab.h"
ead1e424 38#include "common.h"
54dc6425 39#include "object.h"
a2fb1b05 40#include "layout.h"
61ba1cf9 41#include "reloc.h"
ead1e424 42#include "defstd.h"
89fc3421 43#include "plugin.h"
ef15dade 44#include "icf.h"
44453f85 45#include "incremental.h"
bae7f79e
ILT
46
47namespace gold
48{
49
50const char* program_name;
51
52void
53gold_exit(bool status)
54{
483620e8
CC
55 if (parameters != NULL
56 && parameters->options_valid()
57 && parameters->options().has_plugins())
58 parameters->options().plugins()->cleanup();
2d684091 59 if (!status && parameters != NULL && parameters->options_valid())
8851ecca 60 unlink_if_ordinary(parameters->options().output_file_name());
bae7f79e
ILT
61 exit(status ? EXIT_SUCCESS : EXIT_FAILURE);
62}
63
bae7f79e
ILT
64void
65gold_nomem()
66{
67 // We are out of memory, so try hard to print a reasonable message.
68 // Note that we don't try to translate this message, since the
69 // translation process itself will require memory.
55ba0940
ILT
70
71 // LEN only exists to avoid a pointless warning when write is
72 // declared with warn_use_result, as when compiling with
73 // -D_USE_FORTIFY on GNU/Linux. Casting to void does not appear to
74 // work, at least not with gcc 4.3.0.
75
76 ssize_t len = write(2, program_name, strlen(program_name));
77 if (len >= 0)
78 {
79 const char* const s = ": out of memory\n";
80 len = write(2, s, strlen(s));
81 }
bae7f79e
ILT
82 gold_exit(false);
83}
84
a3ad94ed
ILT
85// Handle an unreachable case.
86
bae7f79e 87void
a3ad94ed 88do_gold_unreachable(const char* filename, int lineno, const char* function)
bae7f79e 89{
27b7985a 90 fprintf(stderr, _("%s: internal error in %s, at %s:%d\n"),
a3ad94ed
ILT
91 program_name, function, filename, lineno);
92 gold_exit(false);
bae7f79e
ILT
93}
94
92e059d8
ILT
95// This class arranges to run the functions done in the middle of the
96// link. It is just a closure.
bae7f79e 97
92e059d8 98class Middle_runner : public Task_function_runner
bae7f79e 99{
92e059d8
ILT
100 public:
101 Middle_runner(const General_options& options,
102 const Input_objects* input_objects,
103 Symbol_table* symtab,
7d9e3d98 104 Layout* layout, Mapfile* mapfile)
92e059d8 105 : options_(options), input_objects_(input_objects), symtab_(symtab),
7d9e3d98 106 layout_(layout), mapfile_(mapfile)
92e059d8
ILT
107 { }
108
109 void
17a1d0a9 110 run(Workqueue*, const Task*);
92e059d8
ILT
111
112 private:
113 const General_options& options_;
114 const Input_objects* input_objects_;
115 Symbol_table* symtab_;
116 Layout* layout_;
7d9e3d98 117 Mapfile* mapfile_;
92e059d8 118};
bae7f79e 119
92e059d8 120void
17a1d0a9 121Middle_runner::run(Workqueue* workqueue, const Task* task)
92e059d8 122{
17a1d0a9 123 queue_middle_tasks(this->options_, task, this->input_objects_, this->symtab_,
7d9e3d98 124 this->layout_, workqueue, this->mapfile_);
92e059d8 125}
bae7f79e 126
6d03d481
ST
127// This class arranges the tasks to process the relocs for garbage collection.
128
129class Gc_runner : public Task_function_runner
130{
131 public:
132 Gc_runner(const General_options& options,
133 const Input_objects* input_objects,
134 Symbol_table* symtab,
135 Layout* layout, Mapfile* mapfile)
136 : options_(options), input_objects_(input_objects), symtab_(symtab),
137 layout_(layout), mapfile_(mapfile)
138 { }
139
140 void
141 run(Workqueue*, const Task*);
142
143 private:
144 const General_options& options_;
145 const Input_objects* input_objects_;
146 Symbol_table* symtab_;
147 Layout* layout_;
148 Mapfile* mapfile_;
149};
150
151void
152Gc_runner::run(Workqueue* workqueue, const Task* task)
153{
154 queue_middle_gc_tasks(this->options_, task, this->input_objects_,
155 this->symtab_, this->layout_, workqueue,
156 this->mapfile_);
157}
158
bae7f79e
ILT
159// Queue up the initial set of tasks for this link job.
160
161void
162queue_initial_tasks(const General_options& options,
17a1d0a9 163 Dirsearch& search_path,
ead1e424 164 const Command_line& cmdline,
54dc6425 165 Workqueue* workqueue, Input_objects* input_objects,
7d9e3d98 166 Symbol_table* symtab, Layout* layout, Mapfile* mapfile)
bae7f79e 167{
e5366891 168 if (cmdline.begin() == cmdline.end())
459c9f1c
ILT
169 {
170 if (options.printed_version())
171 gold_exit(true);
172 gold_fatal(_("no input files"));
173 }
e5366891 174
fe9a4c12
ILT
175 int thread_count = options.thread_count_initial();
176 if (thread_count == 0)
e5366891 177 thread_count = cmdline.number_of_input_files();
fe9a4c12
ILT
178 workqueue->set_thread_count(thread_count);
179
44453f85
ILT
180 if (cmdline.options().incremental())
181 {
182 Incremental_checker incremental_checker(
183 parameters->options().output_file_name());
184 if (incremental_checker.can_incrementally_link_output_file())
185 {
186 // TODO: remove when incremental linking implemented.
187 printf("Incremental linking might be possible "
188 "(not implemented yet)\n");
189 }
190 // TODO: If we decide on an incremental build, fewer tasks
191 // should be scheduled.
192 }
193
bae7f79e
ILT
194 // Read the input files. We have to add the symbols to the symbol
195 // table in order. We do this by creating a separate blocker for
196 // each input file. We associate the blocker with the following
197 // input file, to give us a convenient place to delete it.
198 Task_token* this_blocker = NULL;
ead1e424
ILT
199 for (Command_line::const_iterator p = cmdline.begin();
200 p != cmdline.end();
bae7f79e
ILT
201 ++p)
202 {
17a1d0a9 203 Task_token* next_blocker = new Task_token(true);
bae7f79e 204 next_blocker->add_blocker();
f1ed28fb 205 workqueue->queue(new Read_symbols(input_objects, symtab, layout,
15f8229b 206 &search_path, 0, mapfile, &*p, NULL,
7d9e3d98 207 this_blocker, next_blocker));
bae7f79e
ILT
208 this_blocker = next_blocker;
209 }
210
89fc3421
CC
211 if (options.has_plugins())
212 {
213 Task_token* next_blocker = new Task_token(true);
214 next_blocker->add_blocker();
215 workqueue->queue(new Plugin_hook(options, input_objects, symtab, layout,
216 &search_path, mapfile, this_blocker,
217 next_blocker));
218 this_blocker = next_blocker;
219 }
220
6d03d481 221 if (parameters->options().relocatable()
ef15dade
ST
222 && (parameters->options().gc_sections() || parameters->options().icf()))
223 gold_error(_("cannot mix -r with --gc-sections or --icf"));
6d03d481 224
ef15dade 225 if (parameters->options().gc_sections() || parameters->options().icf())
6d03d481
ST
226 {
227 workqueue->queue(new Task_function(new Gc_runner(options,
228 input_objects,
229 symtab,
230 layout,
231 mapfile),
232 this_blocker,
233 "Task_function Gc_runner"));
234 }
235 else
236 {
237 workqueue->queue(new Task_function(new Middle_runner(options,
238 input_objects,
239 symtab,
240 layout,
241 mapfile),
242 this_blocker,
243 "Task_function Middle_runner"));
244 }
245}
246
44453f85
ILT
247// Queue up a set of tasks to be done before queueing the middle set
248// of tasks. This is only necessary when garbage collection
6d03d481
ST
249// (--gc-sections) of unused sections is desired. The relocs are read
250// and processed here early to determine the garbage sections before the
251// relocs can be scanned in later tasks.
252
253void
254queue_middle_gc_tasks(const General_options& options,
255 const Task* ,
256 const Input_objects* input_objects,
257 Symbol_table* symtab,
258 Layout* layout,
259 Workqueue* workqueue,
260 Mapfile* mapfile)
261{
262 // Read_relocs for all the objects must be done and processed to find
263 // unused sections before any scanning of the relocs can take place.
264 Task_token* blocker = new Task_token(true);
265 Task_token* symtab_lock = new Task_token(false);
266 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
267 p != input_objects->relobj_end();
268 ++p)
269 {
270 // We can read and process the relocations in any order.
271 blocker->add_blocker();
272 workqueue->queue(new Read_relocs(options, symtab, layout, *p,
273 symtab_lock, blocker));
274 }
275
276 Task_token* this_blocker = new Task_token(true);
92e059d8 277 workqueue->queue(new Task_function(new Middle_runner(options,
6d03d481
ST
278 input_objects,
279 symtab,
280 layout,
281 mapfile),
282 this_blocker,
283 "Task_function Middle_runner"));
bae7f79e
ILT
284}
285
92e059d8
ILT
286// Queue up the middle set of tasks. These are the tasks which run
287// after all the input objects have been found and all the symbols
288// have been read, but before we lay out the output file.
bae7f79e 289
92e059d8
ILT
290void
291queue_middle_tasks(const General_options& options,
17a1d0a9 292 const Task* task,
92e059d8
ILT
293 const Input_objects* input_objects,
294 Symbol_table* symtab,
295 Layout* layout,
7d9e3d98
ILT
296 Workqueue* workqueue,
297 Mapfile* mapfile)
61ba1cf9 298{
6d03d481
ST
299 // Add any symbols named with -u options to the symbol table.
300 symtab->add_undefined_symbols_from_command_line();
301
302 // If garbage collection was chosen, relocs have been read and processed
303 // at this point by pre_middle_tasks. Layout can then be done for all
304 // objects.
305 if (parameters->options().gc_sections())
306 {
307 // Find the start symbol if any.
308 Symbol* start_sym;
309 if (parameters->options().entry())
310 start_sym = symtab->lookup(parameters->options().entry());
311 else
312 start_sym = symtab->lookup("_start");
313 if (start_sym !=NULL)
314 {
315 bool is_ordinary;
316 unsigned int shndx = start_sym->shndx(&is_ordinary);
317 if (is_ordinary)
318 {
319 symtab->gc()->worklist().push(
320 Section_id(start_sym->object(), shndx));
321 }
322 }
323 // Symbols named with -u should not be considered garbage.
324 symtab->gc_mark_undef_symbols();
325 gold_assert(symtab->gc() != NULL);
326 // Do a transitive closure on all references to determine the worklist.
327 symtab->gc()->do_transitive_closure();
ef15dade
ST
328 }
329
330 // If identical code folding (--icf) is chosen it makes sense to do it
331 // only after garbage collection (--gc-sections) as we do not want to
332 // be folding sections that will be garbage.
333 if (parameters->options().icf())
334 {
335 symtab->icf()->find_identical_sections(input_objects, symtab);
336 }
337
338 // Call Object::layout for the second time to determine the
339 // output_sections for all referenced input sections. When
340 // --gc-sections or --icf is turned on, Object::layout is
341 // called twice. It is called the first time when the
342 // symbols are added.
343 if (parameters->options().gc_sections() || parameters->options().icf())
344 {
6d03d481
ST
345 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
346 p != input_objects->relobj_end();
347 ++p)
348 {
349 (*p)->layout(symtab, layout, NULL);
350 }
351 }
ef15dade 352
6d03d481
ST
353 // Layout deferred objects due to plugins.
354 if (parameters->options().has_plugins())
355 {
356 Plugin_manager* plugins = parameters->options().plugins();
357 gold_assert(plugins != NULL);
358 plugins->layout_deferred_objects();
359 }
ef15dade
ST
360
361 if (parameters->options().gc_sections() || parameters->options().icf())
6d03d481
ST
362 {
363 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
364 p != input_objects->relobj_end();
365 ++p)
366 {
367 // Update the value of output_section stored in rd.
368 Read_relocs_data *rd = (*p)->get_relocs_data();
369 for (Read_relocs_data::Relocs_list::iterator q = rd->relocs.begin();
370 q != rd->relocs.end();
371 ++q)
372 {
373 q->output_section = (*p)->output_section(q->data_shndx);
374 q->needs_special_offset_handling =
375 (*p)->is_output_section_offset_invalid(q->data_shndx);
376 }
377 }
378 }
379
fbfba508
ILT
380 // We have to support the case of not seeing any input objects, and
381 // generate an empty file. Existing builds depend on being able to
382 // pass an empty archive to the linker and get an empty object file
383 // out. In order to do this we need to use a default target.
2c0aeda4 384 if (input_objects->number_of_input_objects() == 0)
029ba973 385 parameters_force_valid_target();
2c0aeda4 386
fe9a4c12
ILT
387 int thread_count = options.thread_count_middle();
388 if (thread_count == 0)
fbfba508 389 thread_count = std::max(2, input_objects->number_of_input_objects());
fe9a4c12
ILT
390 workqueue->set_thread_count(thread_count);
391
b3b74ddc 392 // Now we have seen all the input files.
436ca963 393 const bool doing_static_link = (!input_objects->any_dynamic()
8851ecca 394 && !parameters->options().shared());
4eff2974
ILT
395 set_parameters_doing_static_link(doing_static_link);
396 if (!doing_static_link && options.is_static())
397 {
398 // We print out just the first .so we see; there may be others.
4e1e25e0 399 gold_assert(input_objects->dynobj_begin() != input_objects->dynobj_end());
75f2446e
ILT
400 gold_error(_("cannot mix -static with dynamic object %s"),
401 (*input_objects->dynobj_begin())->name().c_str());
4eff2974 402 }
8851ecca 403 if (!doing_static_link && parameters->options().relocatable())
459c9f1c 404 gold_fatal(_("cannot mix -r with dynamic object %s"),
6a74a719 405 (*input_objects->dynobj_begin())->name().c_str());
516cb3d0 406 if (!doing_static_link
7cc619c3 407 && options.oformat_enum() != General_options::OBJECT_FORMAT_ELF)
516cb3d0
ILT
408 gold_fatal(_("cannot use non-ELF output format with dynamic object %s"),
409 (*input_objects->dynobj_begin())->name().c_str());
b3b74ddc 410
494e05f4
ILT
411 if (is_debugging_enabled(DEBUG_SCRIPT))
412 layout->script_options()->print(stderr);
413
e2827e5f
ILT
414 // For each dynamic object, record whether we've seen all the
415 // dynamic objects that it depends upon.
416 input_objects->check_dynamic_dependencies();
417
70e654ba
ILT
418 // See if any of the input definitions violate the One Definition Rule.
419 // TODO: if this is too slow, do this as a task, rather than inline.
17a1d0a9 420 symtab->detect_odr_violations(task, options.output_file_name());
70e654ba 421
9c547ec3
ILT
422 // Create any automatic note sections.
423 layout->create_notes();
424
919ed24c
ILT
425 // Create any output sections required by any linker script.
426 layout->create_script_sections();
427
a3ad94ed
ILT
428 // Define some sections and symbols needed for a dynamic link. This
429 // handles some cases we want to see before we read the relocs.
9b07f471 430 layout->create_initial_dynamic_sections(symtab);
a3ad94ed 431
a445fddf
ILT
432 // Define symbols from any linker scripts.
433 layout->define_script_symbols(symtab);
434
154e0e9a
ILT
435 // Attach sections to segments.
436 layout->attach_sections_to_segments();
437
8851ecca 438 if (!parameters->options().relocatable())
6a74a719
ILT
439 {
440 // Predefine standard symbols.
441 define_standard_symbols(symtab, layout);
ead1e424 442
6a74a719
ILT
443 // Define __start and __stop symbols for output sections where
444 // appropriate.
445 layout->define_section_symbols(symtab);
446 }
bfd58944 447
755ab8af
ILT
448 // Make sure we have symbols for any required group signatures.
449 layout->define_group_signatures(symtab);
450
17a1d0a9
ILT
451 Task_token* blocker = new Task_token(true);
452 Task_token* symtab_lock = new Task_token(false);
6d03d481
ST
453
454 // If doing garbage collection, the relocations have already been read.
455 // Otherwise, read and scan the relocations.
ef15dade 456 if (parameters->options().gc_sections() || parameters->options().icf())
92e059d8 457 {
6d03d481
ST
458 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
459 p != input_objects->relobj_end();
460 ++p)
461 {
462 blocker->add_blocker();
463 workqueue->queue(new Scan_relocs(options, symtab, layout, *p,
464 (*p)->get_relocs_data(),symtab_lock, blocker));
465 }
466 }
467 else
468 {
469 // Read the relocations of the input files. We do this to find
470 // which symbols are used by relocations which require a GOT and/or
471 // a PLT entry, or a COPY reloc. When we implement garbage
472 // collection we will do it here by reading the relocations in a
473 // breadth first search by references.
474 //
475 // We could also read the relocations during the first pass, and
476 // mark symbols at that time. That is how the old GNU linker works.
477 // Doing that is more complex, since we may later decide to discard
478 // some of the sections, and thus change our minds about the types
479 // of references made to the symbols.
480 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
481 p != input_objects->relobj_end();
482 ++p)
483 {
484 // We can read and process the relocations in any order. But we
485 // only want one task to write to the symbol table at a time.
486 // So we queue up a task for each object to read the
487 // relocations. That task will in turn queue a task to wait
488 // until it can write to the symbol table.
489 blocker->add_blocker();
490 workqueue->queue(new Read_relocs(options, symtab, layout, *p,
491 symtab_lock, blocker));
492 }
92e059d8
ILT
493 }
494
495 // Allocate common symbols. This requires write access to the
496 // symbol table, but is independent of the relocation processing.
0dfbdef4 497 if (parameters->options().define_common())
6a74a719
ILT
498 {
499 blocker->add_blocker();
7d9e3d98
ILT
500 workqueue->queue(new Allocate_commons_task(symtab, layout, mapfile,
501 symtab_lock, blocker));
6a74a719 502 }
92e059d8
ILT
503
504 // When all those tasks are complete, we can start laying out the
505 // output file.
8851ecca
ILT
506 // TODO(csilvers): figure out a more principled way to get the target
507 Target* target = const_cast<Target*>(&parameters->target());
92e059d8
ILT
508 workqueue->queue(new Task_function(new Layout_task_runner(options,
509 input_objects,
510 symtab,
8851ecca 511 target,
7d9e3d98
ILT
512 layout,
513 mapfile),
c7912668
ILT
514 blocker,
515 "Task_function Layout_task_runner"));
92e059d8 516}
61ba1cf9
ILT
517
518// Queue up the final set of tasks. This is called at the end of
519// Layout_task.
520
521void
522queue_final_tasks(const General_options& options,
523 const Input_objects* input_objects,
524 const Symbol_table* symtab,
27bc2bce 525 Layout* layout,
61ba1cf9
ILT
526 Workqueue* workqueue,
527 Output_file* of)
528{
fe9a4c12
ILT
529 int thread_count = options.thread_count_final();
530 if (thread_count == 0)
fbfba508 531 thread_count = std::max(2, input_objects->number_of_input_objects());
fe9a4c12
ILT
532 workqueue->set_thread_count(thread_count);
533
17a1d0a9
ILT
534 bool any_postprocessing_sections = layout->any_postprocessing_sections();
535
730cdc88
ILT
536 // Use a blocker to wait until all the input sections have been
537 // written out.
17a1d0a9
ILT
538 Task_token* input_sections_blocker = NULL;
539 if (!any_postprocessing_sections)
540 input_sections_blocker = new Task_token(true);
730cdc88
ILT
541
542 // Use a blocker to block any objects which have to wait for the
543 // output sections to complete before they can apply relocations.
17a1d0a9 544 Task_token* output_sections_blocker = new Task_token(true);
730cdc88 545
61ba1cf9 546 // Use a blocker to block the final cleanup task.
17a1d0a9 547 Task_token* final_blocker = new Task_token(true);
61ba1cf9
ILT
548
549 // Queue a task to write out the symbol table.
5fe2a0f5 550 final_blocker->add_blocker();
d491d34e
ILT
551 workqueue->queue(new Write_symbols_task(layout,
552 symtab,
5fe2a0f5
ILT
553 input_objects,
554 layout->sympool(),
555 layout->dynpool(),
556 of,
557 final_blocker));
61ba1cf9 558
730cdc88
ILT
559 // Queue a task to write out the output sections.
560 output_sections_blocker->add_blocker();
561 final_blocker->add_blocker();
562 workqueue->queue(new Write_sections_task(layout, of, output_sections_blocker,
563 final_blocker));
564
61ba1cf9
ILT
565 // Queue a task to write out everything else.
566 final_blocker->add_blocker();
9025d29d 567 workqueue->queue(new Write_data_task(layout, symtab, of, final_blocker));
61ba1cf9 568
17a1d0a9
ILT
569 // Queue a task for each input object to relocate the sections and
570 // write out the local symbols.
571 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
572 p != input_objects->relobj_end();
573 ++p)
574 {
575 if (input_sections_blocker != NULL)
576 input_sections_blocker->add_blocker();
577 final_blocker->add_blocker();
578 workqueue->queue(new Relocate_task(options, symtab, layout, *p, of,
579 input_sections_blocker,
580 output_sections_blocker,
581 final_blocker));
582 }
583
730cdc88 584 // Queue a task to write out the output sections which depend on
17a1d0a9
ILT
585 // input sections. If there are any sections which require
586 // postprocessing, then we need to do this last, since it may resize
587 // the output file.
588 if (!any_postprocessing_sections)
589 {
590 final_blocker->add_blocker();
591 Task* t = new Write_after_input_sections_task(layout, of,
592 input_sections_blocker,
593 final_blocker);
594 workqueue->queue(t);
595 }
596 else
597 {
598 Task_token *new_final_blocker = new Task_token(true);
599 new_final_blocker->add_blocker();
600 Task* t = new Write_after_input_sections_task(layout, of,
601 final_blocker,
602 new_final_blocker);
603 workqueue->queue(t);
604 final_blocker = new_final_blocker;
605 }
730cdc88 606
61ba1cf9
ILT
607 // Queue a task to close the output file. This will be blocked by
608 // FINAL_BLOCKER.
516cb3d0
ILT
609 workqueue->queue(new Task_function(new Close_task_runner(&options, layout,
610 of),
c7912668
ILT
611 final_blocker,
612 "Task_function Close_task_runner"));
61ba1cf9
ILT
613}
614
615} // End namespace gold.
This page took 0.165421 seconds and 4 git commands to generate.