1 // gold.cc -- main linker functions
3 // Copyright 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
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.
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.
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.
30 #include "libiberty.h"
34 #include "workqueue.h"
35 #include "dirsearch.h"
46 #include "incremental.h"
51 const char* program_name
;
54 gold_exit(bool status
)
56 if (parameters
!= NULL
57 && parameters
->options_valid()
58 && parameters
->options().has_plugins())
59 parameters
->options().plugins()->cleanup();
60 if (!status
&& parameters
!= NULL
&& parameters
->options_valid())
61 unlink_if_ordinary(parameters
->options().output_file_name());
62 exit(status
? EXIT_SUCCESS
: EXIT_FAILURE
);
68 // We are out of memory, so try hard to print a reasonable message.
69 // Note that we don't try to translate this message, since the
70 // translation process itself will require memory.
72 // LEN only exists to avoid a pointless warning when write is
73 // declared with warn_use_result, as when compiling with
74 // -D_USE_FORTIFY on GNU/Linux. Casting to void does not appear to
75 // work, at least not with gcc 4.3.0.
77 ssize_t len
= write(2, program_name
, strlen(program_name
));
80 const char* const s
= ": out of memory\n";
81 len
= write(2, s
, strlen(s
));
86 // Handle an unreachable case.
89 do_gold_unreachable(const char* filename
, int lineno
, const char* function
)
91 fprintf(stderr
, _("%s: internal error in %s, at %s:%d\n"),
92 program_name
, function
, filename
, lineno
);
96 // This class arranges to run the functions done in the middle of the
97 // link. It is just a closure.
99 class Middle_runner
: public Task_function_runner
102 Middle_runner(const General_options
& options
,
103 const Input_objects
* input_objects
,
104 Symbol_table
* symtab
,
105 Layout
* layout
, Mapfile
* mapfile
)
106 : options_(options
), input_objects_(input_objects
), symtab_(symtab
),
107 layout_(layout
), mapfile_(mapfile
)
111 run(Workqueue
*, const Task
*);
114 const General_options
& options_
;
115 const Input_objects
* input_objects_
;
116 Symbol_table
* symtab_
;
122 Middle_runner::run(Workqueue
* workqueue
, const Task
* task
)
124 queue_middle_tasks(this->options_
, task
, this->input_objects_
, this->symtab_
,
125 this->layout_
, workqueue
, this->mapfile_
);
128 // This class arranges the tasks to process the relocs for garbage collection.
130 class Gc_runner
: public Task_function_runner
133 Gc_runner(const General_options
& options
,
134 const Input_objects
* input_objects
,
135 Symbol_table
* symtab
,
136 Layout
* layout
, Mapfile
* mapfile
)
137 : options_(options
), input_objects_(input_objects
), symtab_(symtab
),
138 layout_(layout
), mapfile_(mapfile
)
142 run(Workqueue
*, const Task
*);
145 const General_options
& options_
;
146 const Input_objects
* input_objects_
;
147 Symbol_table
* symtab_
;
153 Gc_runner::run(Workqueue
* workqueue
, const Task
* task
)
155 queue_middle_gc_tasks(this->options_
, task
, this->input_objects_
,
156 this->symtab_
, this->layout_
, workqueue
,
160 // Queue up the initial set of tasks for this link job.
163 queue_initial_tasks(const General_options
& options
,
164 Dirsearch
& search_path
,
165 const Command_line
& cmdline
,
166 Workqueue
* workqueue
, Input_objects
* input_objects
,
167 Symbol_table
* symtab
, Layout
* layout
, Mapfile
* mapfile
)
169 if (cmdline
.begin() == cmdline
.end())
171 if (options
.printed_version())
173 gold_fatal(_("no input files"));
176 int thread_count
= options
.thread_count_initial();
177 if (thread_count
== 0)
178 thread_count
= cmdline
.number_of_input_files();
179 workqueue
->set_thread_count(thread_count
);
181 if (cmdline
.options().incremental())
183 Incremental_checker
incremental_checker(
184 parameters
->options().output_file_name(),
185 layout
->incremental_inputs());
186 if (incremental_checker
.can_incrementally_link_output_file())
188 // TODO: remove when incremental linking implemented.
189 printf("Incremental linking might be possible "
190 "(not implemented yet)\n");
192 // TODO: If we decide on an incremental build, fewer tasks
193 // should be scheduled.
196 // Read the input files. We have to add the symbols to the symbol
197 // table in order. We do this by creating a separate blocker for
198 // each input file. We associate the blocker with the following
199 // input file, to give us a convenient place to delete it.
200 Task_token
* this_blocker
= NULL
;
201 for (Command_line::const_iterator p
= cmdline
.begin();
205 Task_token
* next_blocker
= new Task_token(true);
206 next_blocker
->add_blocker();
207 workqueue
->queue(new Read_symbols(input_objects
, symtab
, layout
,
208 &search_path
, 0, mapfile
, &*p
, NULL
,
209 this_blocker
, next_blocker
));
210 this_blocker
= next_blocker
;
213 if (options
.has_plugins())
215 Task_token
* next_blocker
= new Task_token(true);
216 next_blocker
->add_blocker();
217 workqueue
->queue(new Plugin_hook(options
, input_objects
, symtab
, layout
,
218 &search_path
, mapfile
, this_blocker
,
220 this_blocker
= next_blocker
;
223 if (parameters
->options().relocatable()
224 && (parameters
->options().gc_sections() || parameters
->options().icf()))
225 gold_error(_("cannot mix -r with --gc-sections or --icf"));
227 if (parameters
->options().gc_sections() || parameters
->options().icf())
229 workqueue
->queue(new Task_function(new Gc_runner(options
,
235 "Task_function Gc_runner"));
239 workqueue
->queue(new Task_function(new Middle_runner(options
,
245 "Task_function Middle_runner"));
249 // Queue up a set of tasks to be done before queueing the middle set
250 // of tasks. This is only necessary when garbage collection
251 // (--gc-sections) of unused sections is desired. The relocs are read
252 // and processed here early to determine the garbage sections before the
253 // relocs can be scanned in later tasks.
256 queue_middle_gc_tasks(const General_options
& options
,
258 const Input_objects
* input_objects
,
259 Symbol_table
* symtab
,
261 Workqueue
* workqueue
,
264 // Read_relocs for all the objects must be done and processed to find
265 // unused sections before any scanning of the relocs can take place.
266 Task_token
* blocker
= new Task_token(true);
267 Task_token
* symtab_lock
= new Task_token(false);
268 for (Input_objects::Relobj_iterator p
= input_objects
->relobj_begin();
269 p
!= input_objects
->relobj_end();
272 // We can read and process the relocations in any order.
273 blocker
->add_blocker();
274 workqueue
->queue(new Read_relocs(options
, symtab
, layout
, *p
,
275 symtab_lock
, blocker
));
278 Task_token
* this_blocker
= new Task_token(true);
279 workqueue
->queue(new Task_function(new Middle_runner(options
,
285 "Task_function Middle_runner"));
288 // Queue up the middle set of tasks. These are the tasks which run
289 // after all the input objects have been found and all the symbols
290 // have been read, but before we lay out the output file.
293 queue_middle_tasks(const General_options
& options
,
295 const Input_objects
* input_objects
,
296 Symbol_table
* symtab
,
298 Workqueue
* workqueue
,
301 // Add any symbols named with -u options to the symbol table.
302 symtab
->add_undefined_symbols_from_command_line();
304 // If garbage collection was chosen, relocs have been read and processed
305 // at this point by pre_middle_tasks. Layout can then be done for all
307 if (parameters
->options().gc_sections())
309 // Find the start symbol if any.
311 if (parameters
->options().entry())
312 start_sym
= symtab
->lookup(parameters
->options().entry());
314 start_sym
= symtab
->lookup("_start");
315 if (start_sym
!=NULL
)
318 unsigned int shndx
= start_sym
->shndx(&is_ordinary
);
321 symtab
->gc()->worklist().push(
322 Section_id(start_sym
->object(), shndx
));
325 // Symbols named with -u should not be considered garbage.
326 symtab
->gc_mark_undef_symbols();
327 gold_assert(symtab
->gc() != NULL
);
328 // Do a transitive closure on all references to determine the worklist.
329 symtab
->gc()->do_transitive_closure();
332 // If identical code folding (--icf) is chosen it makes sense to do it
333 // only after garbage collection (--gc-sections) as we do not want to
334 // be folding sections that will be garbage.
335 if (parameters
->options().icf())
337 symtab
->icf()->find_identical_sections(input_objects
, symtab
);
340 // Call Object::layout for the second time to determine the
341 // output_sections for all referenced input sections. When
342 // --gc-sections or --icf is turned on, Object::layout is
343 // called twice. It is called the first time when the
344 // symbols are added.
345 if (parameters
->options().gc_sections() || parameters
->options().icf())
347 for (Input_objects::Relobj_iterator p
= input_objects
->relobj_begin();
348 p
!= input_objects
->relobj_end();
351 (*p
)->layout(symtab
, layout
, NULL
);
355 // Layout deferred objects due to plugins.
356 if (parameters
->options().has_plugins())
358 Plugin_manager
* plugins
= parameters
->options().plugins();
359 gold_assert(plugins
!= NULL
);
360 plugins
->layout_deferred_objects();
363 if (parameters
->options().gc_sections() || parameters
->options().icf())
365 for (Input_objects::Relobj_iterator p
= input_objects
->relobj_begin();
366 p
!= input_objects
->relobj_end();
369 // Update the value of output_section stored in rd.
370 Read_relocs_data
*rd
= (*p
)->get_relocs_data();
371 for (Read_relocs_data::Relocs_list::iterator q
= rd
->relocs
.begin();
372 q
!= rd
->relocs
.end();
375 q
->output_section
= (*p
)->output_section(q
->data_shndx
);
376 q
->needs_special_offset_handling
=
377 (*p
)->is_output_section_offset_invalid(q
->data_shndx
);
382 // We have to support the case of not seeing any input objects, and
383 // generate an empty file. Existing builds depend on being able to
384 // pass an empty archive to the linker and get an empty object file
385 // out. In order to do this we need to use a default target.
386 if (input_objects
->number_of_input_objects() == 0)
387 parameters_force_valid_target();
389 int thread_count
= options
.thread_count_middle();
390 if (thread_count
== 0)
391 thread_count
= std::max(2, input_objects
->number_of_input_objects());
392 workqueue
->set_thread_count(thread_count
);
394 // Now we have seen all the input files.
395 const bool doing_static_link
= (!input_objects
->any_dynamic()
396 && !parameters
->options().shared());
397 set_parameters_doing_static_link(doing_static_link
);
398 if (!doing_static_link
&& options
.is_static())
400 // We print out just the first .so we see; there may be others.
401 gold_assert(input_objects
->dynobj_begin() != input_objects
->dynobj_end());
402 gold_error(_("cannot mix -static with dynamic object %s"),
403 (*input_objects
->dynobj_begin())->name().c_str());
405 if (!doing_static_link
&& parameters
->options().relocatable())
406 gold_fatal(_("cannot mix -r with dynamic object %s"),
407 (*input_objects
->dynobj_begin())->name().c_str());
408 if (!doing_static_link
409 && options
.oformat_enum() != General_options::OBJECT_FORMAT_ELF
)
410 gold_fatal(_("cannot use non-ELF output format with dynamic object %s"),
411 (*input_objects
->dynobj_begin())->name().c_str());
413 if (parameters
->options().relocatable())
415 Input_objects::Relobj_iterator p
= input_objects
->relobj_begin();
416 if (p
!= input_objects
->relobj_end())
418 bool uses_split_stack
= (*p
)->uses_split_stack();
419 for (++p
; p
!= input_objects
->relobj_end(); ++p
)
421 if ((*p
)->uses_split_stack() != uses_split_stack
)
422 gold_fatal(_("cannot mix split-stack '%s' and "
423 "non-split-stack '%s' when using -r"),
424 (*input_objects
->relobj_begin())->name().c_str(),
425 (*p
)->name().c_str());
430 if (is_debugging_enabled(DEBUG_SCRIPT
))
431 layout
->script_options()->print(stderr
);
433 // For each dynamic object, record whether we've seen all the
434 // dynamic objects that it depends upon.
435 input_objects
->check_dynamic_dependencies();
437 // See if any of the input definitions violate the One Definition Rule.
438 // TODO: if this is too slow, do this as a task, rather than inline.
439 symtab
->detect_odr_violations(task
, options
.output_file_name());
441 // Create any automatic note sections.
442 layout
->create_notes();
444 // Create any output sections required by any linker script.
445 layout
->create_script_sections();
447 // Define some sections and symbols needed for a dynamic link. This
448 // handles some cases we want to see before we read the relocs.
449 layout
->create_initial_dynamic_sections(symtab
);
451 // Define symbols from any linker scripts.
452 layout
->define_script_symbols(symtab
);
454 // Attach sections to segments.
455 layout
->attach_sections_to_segments();
457 if (!parameters
->options().relocatable())
459 // Predefine standard symbols.
460 define_standard_symbols(symtab
, layout
);
462 // Define __start and __stop symbols for output sections where
464 layout
->define_section_symbols(symtab
);
467 // Make sure we have symbols for any required group signatures.
468 layout
->define_group_signatures(symtab
);
470 Task_token
* blocker
= new Task_token(true);
471 Task_token
* symtab_lock
= new Task_token(false);
473 // If doing garbage collection, the relocations have already been read.
474 // Otherwise, read and scan the relocations.
475 if (parameters
->options().gc_sections() || parameters
->options().icf())
477 for (Input_objects::Relobj_iterator p
= input_objects
->relobj_begin();
478 p
!= input_objects
->relobj_end();
481 blocker
->add_blocker();
482 workqueue
->queue(new Scan_relocs(options
, symtab
, layout
, *p
,
483 (*p
)->get_relocs_data(),symtab_lock
, blocker
));
488 // Read the relocations of the input files. We do this to find
489 // which symbols are used by relocations which require a GOT and/or
490 // a PLT entry, or a COPY reloc. When we implement garbage
491 // collection we will do it here by reading the relocations in a
492 // breadth first search by references.
494 // We could also read the relocations during the first pass, and
495 // mark symbols at that time. That is how the old GNU linker works.
496 // Doing that is more complex, since we may later decide to discard
497 // some of the sections, and thus change our minds about the types
498 // of references made to the symbols.
499 for (Input_objects::Relobj_iterator p
= input_objects
->relobj_begin();
500 p
!= input_objects
->relobj_end();
503 // We can read and process the relocations in any order. But we
504 // only want one task to write to the symbol table at a time.
505 // So we queue up a task for each object to read the
506 // relocations. That task will in turn queue a task to wait
507 // until it can write to the symbol table.
508 blocker
->add_blocker();
509 workqueue
->queue(new Read_relocs(options
, symtab
, layout
, *p
,
510 symtab_lock
, blocker
));
514 // Allocate common symbols. This requires write access to the
515 // symbol table, but is independent of the relocation processing.
516 if (parameters
->options().define_common())
518 blocker
->add_blocker();
519 workqueue
->queue(new Allocate_commons_task(symtab
, layout
, mapfile
,
520 symtab_lock
, blocker
));
523 // When all those tasks are complete, we can start laying out the
525 // TODO(csilvers): figure out a more principled way to get the target
526 Target
* target
= const_cast<Target
*>(¶meters
->target());
527 workqueue
->queue(new Task_function(new Layout_task_runner(options
,
534 "Task_function Layout_task_runner"));
537 // Queue up the final set of tasks. This is called at the end of
541 queue_final_tasks(const General_options
& options
,
542 const Input_objects
* input_objects
,
543 const Symbol_table
* symtab
,
545 Workqueue
* workqueue
,
548 int thread_count
= options
.thread_count_final();
549 if (thread_count
== 0)
550 thread_count
= std::max(2, input_objects
->number_of_input_objects());
551 workqueue
->set_thread_count(thread_count
);
553 bool any_postprocessing_sections
= layout
->any_postprocessing_sections();
555 // Use a blocker to wait until all the input sections have been
557 Task_token
* input_sections_blocker
= NULL
;
558 if (!any_postprocessing_sections
)
559 input_sections_blocker
= new Task_token(true);
561 // Use a blocker to block any objects which have to wait for the
562 // output sections to complete before they can apply relocations.
563 Task_token
* output_sections_blocker
= new Task_token(true);
565 // Use a blocker to block the final cleanup task.
566 Task_token
* final_blocker
= new Task_token(true);
568 // Queue a task to write out the symbol table.
569 final_blocker
->add_blocker();
570 workqueue
->queue(new Write_symbols_task(layout
,
578 // Queue a task to write out the output sections.
579 output_sections_blocker
->add_blocker();
580 final_blocker
->add_blocker();
581 workqueue
->queue(new Write_sections_task(layout
, of
, output_sections_blocker
,
584 // Queue a task to write out everything else.
585 final_blocker
->add_blocker();
586 workqueue
->queue(new Write_data_task(layout
, symtab
, of
, final_blocker
));
588 // Queue a task for each input object to relocate the sections and
589 // write out the local symbols.
590 for (Input_objects::Relobj_iterator p
= input_objects
->relobj_begin();
591 p
!= input_objects
->relobj_end();
594 if (input_sections_blocker
!= NULL
)
595 input_sections_blocker
->add_blocker();
596 final_blocker
->add_blocker();
597 workqueue
->queue(new Relocate_task(options
, symtab
, layout
, *p
, of
,
598 input_sections_blocker
,
599 output_sections_blocker
,
603 // Queue a task to write out the output sections which depend on
604 // input sections. If there are any sections which require
605 // postprocessing, then we need to do this last, since it may resize
607 if (!any_postprocessing_sections
)
609 final_blocker
->add_blocker();
610 Task
* t
= new Write_after_input_sections_task(layout
, of
,
611 input_sections_blocker
,
617 Task_token
*new_final_blocker
= new Task_token(true);
618 new_final_blocker
->add_blocker();
619 Task
* t
= new Write_after_input_sections_task(layout
, of
,
623 final_blocker
= new_final_blocker
;
626 // Queue a task to close the output file. This will be blocked by
628 workqueue
->queue(new Task_function(new Close_task_runner(&options
, layout
,
631 "Task_function Close_task_runner"));
634 } // End namespace gold.