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