]>
Commit | Line | Data |
---|---|---|
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 | |
47 | namespace gold | |
48 | { | |
49 | ||
50 | const char* program_name; | |
51 | ||
52 | void | |
53 | gold_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 |
64 | void |
65 | gold_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 | 87 | void |
a3ad94ed | 88 | do_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 | 98 | class 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 | 120 | void |
17a1d0a9 | 121 | Middle_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 | ||
129 | class 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 | ||
151 | void | |
152 | Gc_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 | ||
161 | void | |
162 | queue_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( | |
c4aa1e2d ILT |
183 | parameters->options().output_file_name(), |
184 | layout->incremental_inputs()); | |
44453f85 ILT |
185 | if (incremental_checker.can_incrementally_link_output_file()) |
186 | { | |
187 | // TODO: remove when incremental linking implemented. | |
188 | printf("Incremental linking might be possible " | |
189 | "(not implemented yet)\n"); | |
190 | } | |
191 | // TODO: If we decide on an incremental build, fewer tasks | |
192 | // should be scheduled. | |
193 | } | |
194 | ||
bae7f79e ILT |
195 | // Read the input files. We have to add the symbols to the symbol |
196 | // table in order. We do this by creating a separate blocker for | |
197 | // each input file. We associate the blocker with the following | |
198 | // input file, to give us a convenient place to delete it. | |
199 | Task_token* this_blocker = NULL; | |
ead1e424 ILT |
200 | for (Command_line::const_iterator p = cmdline.begin(); |
201 | p != cmdline.end(); | |
bae7f79e ILT |
202 | ++p) |
203 | { | |
17a1d0a9 | 204 | Task_token* next_blocker = new Task_token(true); |
bae7f79e | 205 | next_blocker->add_blocker(); |
f1ed28fb | 206 | workqueue->queue(new Read_symbols(input_objects, symtab, layout, |
15f8229b | 207 | &search_path, 0, mapfile, &*p, NULL, |
7d9e3d98 | 208 | this_blocker, next_blocker)); |
bae7f79e ILT |
209 | this_blocker = next_blocker; |
210 | } | |
211 | ||
89fc3421 CC |
212 | if (options.has_plugins()) |
213 | { | |
214 | Task_token* next_blocker = new Task_token(true); | |
215 | next_blocker->add_blocker(); | |
216 | workqueue->queue(new Plugin_hook(options, input_objects, symtab, layout, | |
217 | &search_path, mapfile, this_blocker, | |
218 | next_blocker)); | |
219 | this_blocker = next_blocker; | |
220 | } | |
221 | ||
6d03d481 | 222 | if (parameters->options().relocatable() |
ef15dade ST |
223 | && (parameters->options().gc_sections() || parameters->options().icf())) |
224 | gold_error(_("cannot mix -r with --gc-sections or --icf")); | |
6d03d481 | 225 | |
ef15dade | 226 | if (parameters->options().gc_sections() || parameters->options().icf()) |
6d03d481 ST |
227 | { |
228 | workqueue->queue(new Task_function(new Gc_runner(options, | |
229 | input_objects, | |
230 | symtab, | |
231 | layout, | |
232 | mapfile), | |
233 | this_blocker, | |
234 | "Task_function Gc_runner")); | |
235 | } | |
236 | else | |
237 | { | |
238 | workqueue->queue(new Task_function(new Middle_runner(options, | |
239 | input_objects, | |
240 | symtab, | |
241 | layout, | |
242 | mapfile), | |
243 | this_blocker, | |
244 | "Task_function Middle_runner")); | |
245 | } | |
246 | } | |
247 | ||
44453f85 ILT |
248 | // Queue up a set of tasks to be done before queueing the middle set |
249 | // of tasks. This is only necessary when garbage collection | |
6d03d481 ST |
250 | // (--gc-sections) of unused sections is desired. The relocs are read |
251 | // and processed here early to determine the garbage sections before the | |
252 | // relocs can be scanned in later tasks. | |
253 | ||
254 | void | |
255 | queue_middle_gc_tasks(const General_options& options, | |
256 | const Task* , | |
257 | const Input_objects* input_objects, | |
258 | Symbol_table* symtab, | |
259 | Layout* layout, | |
260 | Workqueue* workqueue, | |
261 | Mapfile* mapfile) | |
262 | { | |
263 | // Read_relocs for all the objects must be done and processed to find | |
264 | // unused sections before any scanning of the relocs can take place. | |
265 | Task_token* blocker = new Task_token(true); | |
266 | Task_token* symtab_lock = new Task_token(false); | |
267 | for (Input_objects::Relobj_iterator p = input_objects->relobj_begin(); | |
268 | p != input_objects->relobj_end(); | |
269 | ++p) | |
270 | { | |
271 | // We can read and process the relocations in any order. | |
272 | blocker->add_blocker(); | |
273 | workqueue->queue(new Read_relocs(options, symtab, layout, *p, | |
274 | symtab_lock, blocker)); | |
275 | } | |
276 | ||
277 | Task_token* this_blocker = new Task_token(true); | |
92e059d8 | 278 | workqueue->queue(new Task_function(new Middle_runner(options, |
6d03d481 ST |
279 | input_objects, |
280 | symtab, | |
281 | layout, | |
282 | mapfile), | |
283 | this_blocker, | |
284 | "Task_function Middle_runner")); | |
bae7f79e ILT |
285 | } |
286 | ||
92e059d8 ILT |
287 | // Queue up the middle set of tasks. These are the tasks which run |
288 | // after all the input objects have been found and all the symbols | |
289 | // have been read, but before we lay out the output file. | |
bae7f79e | 290 | |
92e059d8 ILT |
291 | void |
292 | queue_middle_tasks(const General_options& options, | |
17a1d0a9 | 293 | const Task* task, |
92e059d8 ILT |
294 | const Input_objects* input_objects, |
295 | Symbol_table* symtab, | |
296 | Layout* layout, | |
7d9e3d98 ILT |
297 | Workqueue* workqueue, |
298 | Mapfile* mapfile) | |
61ba1cf9 | 299 | { |
6d03d481 ST |
300 | // Add any symbols named with -u options to the symbol table. |
301 | symtab->add_undefined_symbols_from_command_line(); | |
302 | ||
303 | // If garbage collection was chosen, relocs have been read and processed | |
304 | // at this point by pre_middle_tasks. Layout can then be done for all | |
305 | // objects. | |
306 | if (parameters->options().gc_sections()) | |
307 | { | |
308 | // Find the start symbol if any. | |
309 | Symbol* start_sym; | |
310 | if (parameters->options().entry()) | |
311 | start_sym = symtab->lookup(parameters->options().entry()); | |
312 | else | |
313 | start_sym = symtab->lookup("_start"); | |
314 | if (start_sym !=NULL) | |
315 | { | |
316 | bool is_ordinary; | |
317 | unsigned int shndx = start_sym->shndx(&is_ordinary); | |
318 | if (is_ordinary) | |
319 | { | |
320 | symtab->gc()->worklist().push( | |
321 | Section_id(start_sym->object(), shndx)); | |
322 | } | |
323 | } | |
324 | // Symbols named with -u should not be considered garbage. | |
325 | symtab->gc_mark_undef_symbols(); | |
326 | gold_assert(symtab->gc() != NULL); | |
327 | // Do a transitive closure on all references to determine the worklist. | |
328 | symtab->gc()->do_transitive_closure(); | |
ef15dade ST |
329 | } |
330 | ||
331 | // If identical code folding (--icf) is chosen it makes sense to do it | |
332 | // only after garbage collection (--gc-sections) as we do not want to | |
333 | // be folding sections that will be garbage. | |
334 | if (parameters->options().icf()) | |
335 | { | |
336 | symtab->icf()->find_identical_sections(input_objects, symtab); | |
337 | } | |
338 | ||
339 | // Call Object::layout for the second time to determine the | |
340 | // output_sections for all referenced input sections. When | |
341 | // --gc-sections or --icf is turned on, Object::layout is | |
342 | // called twice. It is called the first time when the | |
343 | // symbols are added. | |
344 | if (parameters->options().gc_sections() || parameters->options().icf()) | |
345 | { | |
6d03d481 ST |
346 | for (Input_objects::Relobj_iterator p = input_objects->relobj_begin(); |
347 | p != input_objects->relobj_end(); | |
348 | ++p) | |
349 | { | |
350 | (*p)->layout(symtab, layout, NULL); | |
351 | } | |
352 | } | |
ef15dade | 353 | |
6d03d481 ST |
354 | // Layout deferred objects due to plugins. |
355 | if (parameters->options().has_plugins()) | |
356 | { | |
357 | Plugin_manager* plugins = parameters->options().plugins(); | |
358 | gold_assert(plugins != NULL); | |
359 | plugins->layout_deferred_objects(); | |
360 | } | |
ef15dade ST |
361 | |
362 | if (parameters->options().gc_sections() || parameters->options().icf()) | |
6d03d481 ST |
363 | { |
364 | for (Input_objects::Relobj_iterator p = input_objects->relobj_begin(); | |
365 | p != input_objects->relobj_end(); | |
366 | ++p) | |
367 | { | |
368 | // Update the value of output_section stored in rd. | |
369 | Read_relocs_data *rd = (*p)->get_relocs_data(); | |
370 | for (Read_relocs_data::Relocs_list::iterator q = rd->relocs.begin(); | |
371 | q != rd->relocs.end(); | |
372 | ++q) | |
373 | { | |
374 | q->output_section = (*p)->output_section(q->data_shndx); | |
375 | q->needs_special_offset_handling = | |
376 | (*p)->is_output_section_offset_invalid(q->data_shndx); | |
377 | } | |
378 | } | |
379 | } | |
380 | ||
fbfba508 ILT |
381 | // We have to support the case of not seeing any input objects, and |
382 | // generate an empty file. Existing builds depend on being able to | |
383 | // pass an empty archive to the linker and get an empty object file | |
384 | // out. In order to do this we need to use a default target. | |
2c0aeda4 | 385 | if (input_objects->number_of_input_objects() == 0) |
029ba973 | 386 | parameters_force_valid_target(); |
2c0aeda4 | 387 | |
fe9a4c12 ILT |
388 | int thread_count = options.thread_count_middle(); |
389 | if (thread_count == 0) | |
fbfba508 | 390 | thread_count = std::max(2, input_objects->number_of_input_objects()); |
fe9a4c12 ILT |
391 | workqueue->set_thread_count(thread_count); |
392 | ||
b3b74ddc | 393 | // Now we have seen all the input files. |
436ca963 | 394 | const bool doing_static_link = (!input_objects->any_dynamic() |
8851ecca | 395 | && !parameters->options().shared()); |
4eff2974 ILT |
396 | set_parameters_doing_static_link(doing_static_link); |
397 | if (!doing_static_link && options.is_static()) | |
398 | { | |
399 | // We print out just the first .so we see; there may be others. | |
4e1e25e0 | 400 | gold_assert(input_objects->dynobj_begin() != input_objects->dynobj_end()); |
75f2446e ILT |
401 | gold_error(_("cannot mix -static with dynamic object %s"), |
402 | (*input_objects->dynobj_begin())->name().c_str()); | |
4eff2974 | 403 | } |
8851ecca | 404 | if (!doing_static_link && parameters->options().relocatable()) |
459c9f1c | 405 | gold_fatal(_("cannot mix -r with dynamic object %s"), |
6a74a719 | 406 | (*input_objects->dynobj_begin())->name().c_str()); |
516cb3d0 | 407 | if (!doing_static_link |
7cc619c3 | 408 | && options.oformat_enum() != General_options::OBJECT_FORMAT_ELF) |
516cb3d0 ILT |
409 | gold_fatal(_("cannot use non-ELF output format with dynamic object %s"), |
410 | (*input_objects->dynobj_begin())->name().c_str()); | |
b3b74ddc | 411 | |
364c7fa5 ILT |
412 | if (parameters->options().relocatable()) |
413 | { | |
414 | Input_objects::Relobj_iterator p = input_objects->relobj_begin(); | |
415 | if (p != input_objects->relobj_end()) | |
416 | { | |
417 | bool uses_split_stack = (*p)->uses_split_stack(); | |
418 | for (++p; p != input_objects->relobj_end(); ++p) | |
419 | { | |
420 | if ((*p)->uses_split_stack() != uses_split_stack) | |
421 | gold_fatal(_("cannot mix split-stack '%s' and " | |
422 | "non-split-stack '%s' when using -r"), | |
423 | (*input_objects->relobj_begin())->name().c_str(), | |
424 | (*p)->name().c_str()); | |
425 | } | |
426 | } | |
427 | } | |
428 | ||
494e05f4 ILT |
429 | if (is_debugging_enabled(DEBUG_SCRIPT)) |
430 | layout->script_options()->print(stderr); | |
431 | ||
e2827e5f ILT |
432 | // For each dynamic object, record whether we've seen all the |
433 | // dynamic objects that it depends upon. | |
434 | input_objects->check_dynamic_dependencies(); | |
435 | ||
70e654ba ILT |
436 | // See if any of the input definitions violate the One Definition Rule. |
437 | // TODO: if this is too slow, do this as a task, rather than inline. | |
17a1d0a9 | 438 | symtab->detect_odr_violations(task, options.output_file_name()); |
70e654ba | 439 | |
9c547ec3 ILT |
440 | // Create any automatic note sections. |
441 | layout->create_notes(); | |
442 | ||
919ed24c ILT |
443 | // Create any output sections required by any linker script. |
444 | layout->create_script_sections(); | |
445 | ||
a3ad94ed ILT |
446 | // Define some sections and symbols needed for a dynamic link. This |
447 | // handles some cases we want to see before we read the relocs. | |
9b07f471 | 448 | layout->create_initial_dynamic_sections(symtab); |
a3ad94ed | 449 | |
a445fddf ILT |
450 | // Define symbols from any linker scripts. |
451 | layout->define_script_symbols(symtab); | |
452 | ||
154e0e9a ILT |
453 | // Attach sections to segments. |
454 | layout->attach_sections_to_segments(); | |
455 | ||
8851ecca | 456 | if (!parameters->options().relocatable()) |
6a74a719 ILT |
457 | { |
458 | // Predefine standard symbols. | |
459 | define_standard_symbols(symtab, layout); | |
ead1e424 | 460 | |
6a74a719 ILT |
461 | // Define __start and __stop symbols for output sections where |
462 | // appropriate. | |
463 | layout->define_section_symbols(symtab); | |
464 | } | |
bfd58944 | 465 | |
755ab8af ILT |
466 | // Make sure we have symbols for any required group signatures. |
467 | layout->define_group_signatures(symtab); | |
468 | ||
17a1d0a9 ILT |
469 | Task_token* blocker = new Task_token(true); |
470 | Task_token* symtab_lock = new Task_token(false); | |
6d03d481 ST |
471 | |
472 | // If doing garbage collection, the relocations have already been read. | |
473 | // Otherwise, read and scan the relocations. | |
ef15dade | 474 | if (parameters->options().gc_sections() || parameters->options().icf()) |
92e059d8 | 475 | { |
6d03d481 ST |
476 | for (Input_objects::Relobj_iterator p = input_objects->relobj_begin(); |
477 | p != input_objects->relobj_end(); | |
478 | ++p) | |
479 | { | |
480 | blocker->add_blocker(); | |
481 | workqueue->queue(new Scan_relocs(options, symtab, layout, *p, | |
482 | (*p)->get_relocs_data(),symtab_lock, blocker)); | |
483 | } | |
484 | } | |
485 | else | |
486 | { | |
487 | // Read the relocations of the input files. We do this to find | |
488 | // which symbols are used by relocations which require a GOT and/or | |
489 | // a PLT entry, or a COPY reloc. When we implement garbage | |
490 | // collection we will do it here by reading the relocations in a | |
491 | // breadth first search by references. | |
492 | // | |
493 | // We could also read the relocations during the first pass, and | |
494 | // mark symbols at that time. That is how the old GNU linker works. | |
495 | // Doing that is more complex, since we may later decide to discard | |
496 | // some of the sections, and thus change our minds about the types | |
497 | // of references made to the symbols. | |
498 | for (Input_objects::Relobj_iterator p = input_objects->relobj_begin(); | |
499 | p != input_objects->relobj_end(); | |
500 | ++p) | |
501 | { | |
502 | // We can read and process the relocations in any order. But we | |
503 | // only want one task to write to the symbol table at a time. | |
504 | // So we queue up a task for each object to read the | |
505 | // relocations. That task will in turn queue a task to wait | |
506 | // until it can write to the symbol table. | |
507 | blocker->add_blocker(); | |
508 | workqueue->queue(new Read_relocs(options, symtab, layout, *p, | |
509 | symtab_lock, blocker)); | |
510 | } | |
92e059d8 ILT |
511 | } |
512 | ||
513 | // Allocate common symbols. This requires write access to the | |
514 | // symbol table, but is independent of the relocation processing. | |
0dfbdef4 | 515 | if (parameters->options().define_common()) |
6a74a719 ILT |
516 | { |
517 | blocker->add_blocker(); | |
7d9e3d98 ILT |
518 | workqueue->queue(new Allocate_commons_task(symtab, layout, mapfile, |
519 | symtab_lock, blocker)); | |
6a74a719 | 520 | } |
92e059d8 ILT |
521 | |
522 | // When all those tasks are complete, we can start laying out the | |
523 | // output file. | |
8851ecca ILT |
524 | // TODO(csilvers): figure out a more principled way to get the target |
525 | Target* target = const_cast<Target*>(¶meters->target()); | |
92e059d8 ILT |
526 | workqueue->queue(new Task_function(new Layout_task_runner(options, |
527 | input_objects, | |
528 | symtab, | |
8851ecca | 529 | target, |
7d9e3d98 ILT |
530 | layout, |
531 | mapfile), | |
c7912668 ILT |
532 | blocker, |
533 | "Task_function Layout_task_runner")); | |
92e059d8 | 534 | } |
61ba1cf9 ILT |
535 | |
536 | // Queue up the final set of tasks. This is called at the end of | |
537 | // Layout_task. | |
538 | ||
539 | void | |
540 | queue_final_tasks(const General_options& options, | |
541 | const Input_objects* input_objects, | |
542 | const Symbol_table* symtab, | |
27bc2bce | 543 | Layout* layout, |
61ba1cf9 ILT |
544 | Workqueue* workqueue, |
545 | Output_file* of) | |
546 | { | |
fe9a4c12 ILT |
547 | int thread_count = options.thread_count_final(); |
548 | if (thread_count == 0) | |
fbfba508 | 549 | thread_count = std::max(2, input_objects->number_of_input_objects()); |
fe9a4c12 ILT |
550 | workqueue->set_thread_count(thread_count); |
551 | ||
17a1d0a9 ILT |
552 | bool any_postprocessing_sections = layout->any_postprocessing_sections(); |
553 | ||
730cdc88 ILT |
554 | // Use a blocker to wait until all the input sections have been |
555 | // written out. | |
17a1d0a9 ILT |
556 | Task_token* input_sections_blocker = NULL; |
557 | if (!any_postprocessing_sections) | |
558 | input_sections_blocker = new Task_token(true); | |
730cdc88 ILT |
559 | |
560 | // Use a blocker to block any objects which have to wait for the | |
561 | // output sections to complete before they can apply relocations. | |
17a1d0a9 | 562 | Task_token* output_sections_blocker = new Task_token(true); |
730cdc88 | 563 | |
61ba1cf9 | 564 | // Use a blocker to block the final cleanup task. |
17a1d0a9 | 565 | Task_token* final_blocker = new Task_token(true); |
61ba1cf9 ILT |
566 | |
567 | // Queue a task to write out the symbol table. | |
5fe2a0f5 | 568 | final_blocker->add_blocker(); |
d491d34e ILT |
569 | workqueue->queue(new Write_symbols_task(layout, |
570 | symtab, | |
5fe2a0f5 ILT |
571 | input_objects, |
572 | layout->sympool(), | |
573 | layout->dynpool(), | |
574 | of, | |
575 | final_blocker)); | |
61ba1cf9 | 576 | |
730cdc88 ILT |
577 | // Queue a task to write out the output sections. |
578 | output_sections_blocker->add_blocker(); | |
579 | final_blocker->add_blocker(); | |
580 | workqueue->queue(new Write_sections_task(layout, of, output_sections_blocker, | |
581 | final_blocker)); | |
582 | ||
61ba1cf9 ILT |
583 | // Queue a task to write out everything else. |
584 | final_blocker->add_blocker(); | |
9025d29d | 585 | workqueue->queue(new Write_data_task(layout, symtab, of, final_blocker)); |
61ba1cf9 | 586 | |
17a1d0a9 ILT |
587 | // Queue a task for each input object to relocate the sections and |
588 | // write out the local symbols. | |
589 | for (Input_objects::Relobj_iterator p = input_objects->relobj_begin(); | |
590 | p != input_objects->relobj_end(); | |
591 | ++p) | |
592 | { | |
593 | if (input_sections_blocker != NULL) | |
594 | input_sections_blocker->add_blocker(); | |
595 | final_blocker->add_blocker(); | |
596 | workqueue->queue(new Relocate_task(options, symtab, layout, *p, of, | |
597 | input_sections_blocker, | |
598 | output_sections_blocker, | |
599 | final_blocker)); | |
600 | } | |
601 | ||
730cdc88 | 602 | // Queue a task to write out the output sections which depend on |
17a1d0a9 ILT |
603 | // input sections. If there are any sections which require |
604 | // postprocessing, then we need to do this last, since it may resize | |
605 | // the output file. | |
606 | if (!any_postprocessing_sections) | |
607 | { | |
608 | final_blocker->add_blocker(); | |
609 | Task* t = new Write_after_input_sections_task(layout, of, | |
610 | input_sections_blocker, | |
611 | final_blocker); | |
612 | workqueue->queue(t); | |
613 | } | |
614 | else | |
615 | { | |
616 | Task_token *new_final_blocker = new Task_token(true); | |
617 | new_final_blocker->add_blocker(); | |
618 | Task* t = new Write_after_input_sections_task(layout, of, | |
619 | final_blocker, | |
620 | new_final_blocker); | |
621 | workqueue->queue(t); | |
622 | final_blocker = new_final_blocker; | |
623 | } | |
730cdc88 | 624 | |
61ba1cf9 ILT |
625 | // Queue a task to close the output file. This will be blocked by |
626 | // FINAL_BLOCKER. | |
516cb3d0 ILT |
627 | workqueue->queue(new Task_function(new Close_task_runner(&options, layout, |
628 | of), | |
c7912668 ILT |
629 | final_blocker, |
630 | "Task_function Close_task_runner")); | |
61ba1cf9 ILT |
631 | } |
632 | ||
633 | } // End namespace gold. |