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