]>
Commit | Line | Data |
---|---|---|
5a6f7e2d | 1 | // gold.cc -- main linker functions |
bae7f79e | 2 | |
f1ddb600 | 3 | // Copyright 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc. |
6cb15b7f ILT |
4 | // Written by Ian Lance Taylor <[email protected]>. |
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" | |
f1ddb600 | 33 | #include "target-select.h" |
494e05f4 | 34 | #include "debug.h" |
bae7f79e ILT |
35 | #include "workqueue.h" |
36 | #include "dirsearch.h" | |
37 | #include "readsyms.h" | |
14bfc3f5 | 38 | #include "symtab.h" |
ead1e424 | 39 | #include "common.h" |
54dc6425 | 40 | #include "object.h" |
a2fb1b05 | 41 | #include "layout.h" |
61ba1cf9 | 42 | #include "reloc.h" |
ead1e424 | 43 | #include "defstd.h" |
89fc3421 | 44 | #include "plugin.h" |
f345227a | 45 | #include "gc.h" |
ef15dade | 46 | #include "icf.h" |
44453f85 | 47 | #include "incremental.h" |
bae7f79e ILT |
48 | |
49 | namespace gold | |
50 | { | |
51 | ||
cdc29364 CC |
52 | class Object; |
53 | ||
bae7f79e ILT |
54 | const char* program_name; |
55 | ||
cdc29364 CC |
56 | static Task* |
57 | process_incremental_input(Incremental_binary*, unsigned int, Input_objects*, | |
58 | Symbol_table*, Layout*, Dirsearch*, Mapfile*, | |
59 | Task_token*, Task_token*); | |
60 | ||
bae7f79e | 61 | void |
e6455dfb | 62 | gold_exit(Exit_status status) |
bae7f79e | 63 | { |
483620e8 CC |
64 | if (parameters != NULL |
65 | && parameters->options_valid() | |
66 | && parameters->options().has_plugins()) | |
67 | parameters->options().plugins()->cleanup(); | |
e6455dfb | 68 | if (status != GOLD_OK && parameters != NULL && parameters->options_valid()) |
8851ecca | 69 | unlink_if_ordinary(parameters->options().output_file_name()); |
e6455dfb | 70 | exit(status); |
bae7f79e ILT |
71 | } |
72 | ||
bae7f79e ILT |
73 | void |
74 | gold_nomem() | |
75 | { | |
76 | // We are out of memory, so try hard to print a reasonable message. | |
77 | // Note that we don't try to translate this message, since the | |
78 | // translation process itself will require memory. | |
55ba0940 ILT |
79 | |
80 | // LEN only exists to avoid a pointless warning when write is | |
81 | // declared with warn_use_result, as when compiling with | |
82 | // -D_USE_FORTIFY on GNU/Linux. Casting to void does not appear to | |
83 | // work, at least not with gcc 4.3.0. | |
84 | ||
85 | ssize_t len = write(2, program_name, strlen(program_name)); | |
86 | if (len >= 0) | |
87 | { | |
88 | const char* const s = ": out of memory\n"; | |
89 | len = write(2, s, strlen(s)); | |
90 | } | |
e6455dfb | 91 | gold_exit(GOLD_ERR); |
bae7f79e ILT |
92 | } |
93 | ||
a3ad94ed ILT |
94 | // Handle an unreachable case. |
95 | ||
bae7f79e | 96 | void |
a3ad94ed | 97 | do_gold_unreachable(const char* filename, int lineno, const char* function) |
bae7f79e | 98 | { |
27b7985a | 99 | fprintf(stderr, _("%s: internal error in %s, at %s:%d\n"), |
a3ad94ed | 100 | program_name, function, filename, lineno); |
e6455dfb | 101 | gold_exit(GOLD_ERR); |
bae7f79e ILT |
102 | } |
103 | ||
92e059d8 ILT |
104 | // This class arranges to run the functions done in the middle of the |
105 | // link. It is just a closure. | |
bae7f79e | 106 | |
92e059d8 | 107 | class Middle_runner : public Task_function_runner |
bae7f79e | 108 | { |
92e059d8 ILT |
109 | public: |
110 | Middle_runner(const General_options& options, | |
111 | const Input_objects* input_objects, | |
112 | Symbol_table* symtab, | |
7d9e3d98 | 113 | Layout* layout, Mapfile* mapfile) |
92e059d8 | 114 | : options_(options), input_objects_(input_objects), symtab_(symtab), |
7d9e3d98 | 115 | layout_(layout), mapfile_(mapfile) |
92e059d8 ILT |
116 | { } |
117 | ||
118 | void | |
17a1d0a9 | 119 | run(Workqueue*, const Task*); |
92e059d8 ILT |
120 | |
121 | private: | |
122 | const General_options& options_; | |
123 | const Input_objects* input_objects_; | |
124 | Symbol_table* symtab_; | |
125 | Layout* layout_; | |
7d9e3d98 | 126 | Mapfile* mapfile_; |
92e059d8 | 127 | }; |
bae7f79e | 128 | |
92e059d8 | 129 | void |
17a1d0a9 | 130 | Middle_runner::run(Workqueue* workqueue, const Task* task) |
92e059d8 | 131 | { |
17a1d0a9 | 132 | queue_middle_tasks(this->options_, task, this->input_objects_, this->symtab_, |
7d9e3d98 | 133 | this->layout_, workqueue, this->mapfile_); |
92e059d8 | 134 | } |
bae7f79e | 135 | |
6d03d481 ST |
136 | // This class arranges the tasks to process the relocs for garbage collection. |
137 | ||
138 | class Gc_runner : public Task_function_runner | |
139 | { | |
140 | public: | |
141 | Gc_runner(const General_options& options, | |
142 | const Input_objects* input_objects, | |
143 | Symbol_table* symtab, | |
144 | Layout* layout, Mapfile* mapfile) | |
145 | : options_(options), input_objects_(input_objects), symtab_(symtab), | |
146 | layout_(layout), mapfile_(mapfile) | |
147 | { } | |
148 | ||
149 | void | |
150 | run(Workqueue*, const Task*); | |
151 | ||
152 | private: | |
153 | const General_options& options_; | |
154 | const Input_objects* input_objects_; | |
155 | Symbol_table* symtab_; | |
156 | Layout* layout_; | |
157 | Mapfile* mapfile_; | |
158 | }; | |
159 | ||
160 | void | |
161 | Gc_runner::run(Workqueue* workqueue, const Task* task) | |
162 | { | |
163 | queue_middle_gc_tasks(this->options_, task, this->input_objects_, | |
164 | this->symtab_, this->layout_, workqueue, | |
165 | this->mapfile_); | |
166 | } | |
167 | ||
bae7f79e ILT |
168 | // Queue up the initial set of tasks for this link job. |
169 | ||
170 | void | |
171 | queue_initial_tasks(const General_options& options, | |
17a1d0a9 | 172 | Dirsearch& search_path, |
ead1e424 | 173 | const Command_line& cmdline, |
54dc6425 | 174 | Workqueue* workqueue, Input_objects* input_objects, |
7d9e3d98 | 175 | Symbol_table* symtab, Layout* layout, Mapfile* mapfile) |
bae7f79e | 176 | { |
e5366891 | 177 | if (cmdline.begin() == cmdline.end()) |
459c9f1c | 178 | { |
f1ddb600 | 179 | bool is_ok = false; |
459c9f1c | 180 | if (options.printed_version()) |
f1ddb600 ILT |
181 | is_ok = true; |
182 | if (options.print_output_format()) | |
183 | { | |
184 | print_output_format(); | |
185 | is_ok = true; | |
186 | } | |
187 | if (is_ok) | |
e6455dfb | 188 | gold_exit(GOLD_OK); |
459c9f1c ILT |
189 | gold_fatal(_("no input files")); |
190 | } | |
e5366891 | 191 | |
fe9a4c12 ILT |
192 | int thread_count = options.thread_count_initial(); |
193 | if (thread_count == 0) | |
e5366891 | 194 | thread_count = cmdline.number_of_input_files(); |
fe9a4c12 ILT |
195 | workqueue->set_thread_count(thread_count); |
196 | ||
cdc29364 CC |
197 | // For incremental links, the base output file. |
198 | Incremental_binary* ibase = NULL; | |
199 | ||
8c21d9d3 | 200 | if (parameters->incremental()) |
44453f85 | 201 | { |
cdc29364 CC |
202 | if (options.relocatable()) |
203 | gold_error(_("incremental linking is incompatible with -r")); | |
204 | if (options.emit_relocs()) | |
205 | gold_error(_("incremental linking is incompatible with --emit-relocs")); | |
206 | if (options.gc_sections()) | |
207 | gold_error(_("incremental linking is incompatible with --gc-sections")); | |
208 | if (options.icf_enabled()) | |
209 | gold_error(_("incremental linking is incompatible with --icf")); | |
210 | if (options.has_plugins()) | |
211 | gold_error(_("incremental linking is incompatible with --plugin")); | |
4fb3a1c3 CC |
212 | if (strcmp(options.compress_debug_sections(), "none") != 0) |
213 | gold_error(_("incremental linking is incompatible with " | |
214 | "--compress-debug-sections")); | |
cdc29364 CC |
215 | |
216 | if (parameters->incremental_update()) | |
217 | { | |
218 | Output_file* of = new Output_file(options.output_file_name()); | |
aa92d6ed | 219 | if (of->open_base_file(options.incremental_base(), true)) |
cdc29364 CC |
220 | { |
221 | ibase = open_incremental_binary(of); | |
222 | if (ibase != NULL | |
223 | && ibase->check_inputs(cmdline, layout->incremental_inputs())) | |
224 | ibase->init_layout(layout); | |
225 | else | |
226 | { | |
227 | delete ibase; | |
228 | ibase = NULL; | |
229 | of->close(); | |
230 | } | |
231 | } | |
232 | if (ibase == NULL) | |
233 | { | |
234 | if (set_parameters_incremental_full()) | |
235 | gold_info(_("linking with --incremental-full")); | |
236 | else | |
e6455dfb | 237 | gold_fallback(_("restart link with --incremental-full")); |
cdc29364 CC |
238 | } |
239 | } | |
44453f85 ILT |
240 | } |
241 | ||
bae7f79e ILT |
242 | // Read the input files. We have to add the symbols to the symbol |
243 | // table in order. We do this by creating a separate blocker for | |
244 | // each input file. We associate the blocker with the following | |
245 | // input file, to give us a convenient place to delete it. | |
246 | Task_token* this_blocker = NULL; | |
cdc29364 | 247 | if (ibase == NULL) |
bae7f79e | 248 | { |
cdc29364 CC |
249 | // Normal link. Queue a Read_symbols task for each input file |
250 | // on the command line. | |
251 | for (Command_line::const_iterator p = cmdline.begin(); | |
252 | p != cmdline.end(); | |
253 | ++p) | |
254 | { | |
255 | Task_token* next_blocker = new Task_token(true); | |
256 | next_blocker->add_blocker(); | |
257 | workqueue->queue(new Read_symbols(input_objects, symtab, layout, | |
258 | &search_path, 0, mapfile, &*p, NULL, | |
259 | NULL, this_blocker, next_blocker)); | |
260 | this_blocker = next_blocker; | |
261 | } | |
262 | } | |
263 | else | |
264 | { | |
265 | // Incremental update link. Process the list of input files | |
266 | // stored in the base file, and queue a task for each file: | |
267 | // a Read_symbols task for a changed file, and an Add_symbols task | |
268 | // for an unchanged file. We need to mark all the space used by | |
269 | // unchanged files before we can start any tasks running. | |
270 | unsigned int input_file_count = ibase->input_file_count(); | |
271 | std::vector<Task*> tasks; | |
272 | tasks.reserve(input_file_count); | |
273 | for (unsigned int i = 0; i < input_file_count; ++i) | |
274 | { | |
275 | Task_token* next_blocker = new Task_token(true); | |
276 | next_blocker->add_blocker(); | |
277 | Task* t = process_incremental_input(ibase, i, input_objects, symtab, | |
278 | layout, &search_path, mapfile, | |
279 | this_blocker, next_blocker); | |
280 | tasks.push_back(t); | |
281 | this_blocker = next_blocker; | |
282 | } | |
283 | // Now we can queue the tasks. | |
284 | for (unsigned int i = 0; i < tasks.size(); i++) | |
285 | workqueue->queue(tasks[i]); | |
bae7f79e ILT |
286 | } |
287 | ||
89fc3421 CC |
288 | if (options.has_plugins()) |
289 | { | |
290 | Task_token* next_blocker = new Task_token(true); | |
291 | next_blocker->add_blocker(); | |
292 | workqueue->queue(new Plugin_hook(options, input_objects, symtab, layout, | |
293 | &search_path, mapfile, this_blocker, | |
294 | next_blocker)); | |
295 | this_blocker = next_blocker; | |
296 | } | |
297 | ||
cdc29364 CC |
298 | if (options.relocatable() |
299 | && (options.gc_sections() || options.icf_enabled())) | |
ef15dade | 300 | gold_error(_("cannot mix -r with --gc-sections or --icf")); |
6d03d481 | 301 | |
cdc29364 | 302 | if (options.gc_sections() || options.icf_enabled()) |
6d03d481 ST |
303 | { |
304 | workqueue->queue(new Task_function(new Gc_runner(options, | |
ad0f2072 | 305 | input_objects, |
6d03d481 ST |
306 | symtab, |
307 | layout, | |
308 | mapfile), | |
309 | this_blocker, | |
310 | "Task_function Gc_runner")); | |
311 | } | |
312 | else | |
313 | { | |
314 | workqueue->queue(new Task_function(new Middle_runner(options, | |
315 | input_objects, | |
316 | symtab, | |
317 | layout, | |
318 | mapfile), | |
319 | this_blocker, | |
320 | "Task_function Middle_runner")); | |
321 | } | |
322 | } | |
323 | ||
cdc29364 CC |
324 | // Process an incremental input file: if it is unchanged from the previous |
325 | // link, return a task to add its symbols from the base file's incremental | |
326 | // info; if it has changed, return a normal Read_symbols task. We create a | |
327 | // task for every input file, if only to report the file for rebuilding the | |
328 | // incremental info. | |
329 | ||
330 | static Task* | |
331 | process_incremental_input(Incremental_binary* ibase, | |
332 | unsigned int input_file_index, | |
333 | Input_objects* input_objects, | |
334 | Symbol_table* symtab, | |
335 | Layout* layout, | |
336 | Dirsearch* search_path, | |
337 | Mapfile* mapfile, | |
338 | Task_token* this_blocker, | |
339 | Task_token* next_blocker) | |
340 | { | |
341 | const Incremental_binary::Input_reader* input_reader = | |
342 | ibase->get_input_reader(input_file_index); | |
343 | Incremental_input_type input_type = input_reader->type(); | |
344 | ||
345 | // Get the input argument corresponding to this input file, matching on | |
346 | // the argument serial number. If the input file cannot be matched | |
347 | // to an existing input argument, synthesize a new one. | |
348 | const Input_argument* input_argument = | |
349 | ibase->get_input_argument(input_file_index); | |
350 | if (input_argument == NULL) | |
351 | { | |
352 | Input_file_argument file(input_reader->filename(), | |
353 | Input_file_argument::INPUT_FILE_TYPE_FILE, | |
354 | "", false, parameters->options()); | |
355 | Input_argument* arg = new Input_argument(file); | |
356 | arg->set_script_info(ibase->get_script_info(input_file_index)); | |
357 | input_argument = arg; | |
358 | } | |
359 | ||
360 | gold_debug(DEBUG_INCREMENTAL, "Incremental object: %s, type %d", | |
361 | input_reader->filename(), input_type); | |
362 | ||
363 | if (input_type == INCREMENTAL_INPUT_SCRIPT) | |
364 | { | |
365 | // Incremental_binary::check_inputs should have cancelled the | |
366 | // incremental update if the script has changed. | |
367 | gold_assert(!ibase->file_has_changed(input_file_index)); | |
368 | return new Check_script(layout, ibase, input_file_index, input_reader, | |
369 | this_blocker, next_blocker); | |
370 | } | |
371 | ||
372 | if (input_type == INCREMENTAL_INPUT_ARCHIVE) | |
373 | { | |
374 | Incremental_library* lib = ibase->get_library(input_file_index); | |
375 | gold_assert(lib != NULL); | |
376 | if (lib->filename() == "/group/" | |
377 | || !ibase->file_has_changed(input_file_index)) | |
378 | { | |
379 | // Queue a task to check that no references have been added to any | |
380 | // of the library's unused symbols. | |
381 | return new Check_library(symtab, layout, ibase, input_file_index, | |
382 | input_reader, this_blocker, next_blocker); | |
383 | } | |
384 | else | |
385 | { | |
386 | // Queue a Read_symbols task to process the archive normally. | |
387 | return new Read_symbols(input_objects, symtab, layout, search_path, | |
388 | 0, mapfile, input_argument, NULL, NULL, | |
389 | this_blocker, next_blocker); | |
390 | } | |
391 | } | |
392 | ||
393 | if (input_type == INCREMENTAL_INPUT_ARCHIVE_MEMBER) | |
394 | { | |
395 | // For archive members, check the timestamp of the containing archive. | |
396 | Incremental_library* lib = ibase->get_library(input_file_index); | |
397 | gold_assert(lib != NULL); | |
398 | // Process members of a --start-lib/--end-lib group as normal objects. | |
399 | if (lib->filename() != "/group/") | |
400 | { | |
401 | if (ibase->file_has_changed(lib->input_file_index())) | |
402 | { | |
403 | return new Read_member(input_objects, symtab, layout, mapfile, | |
404 | input_reader, this_blocker, next_blocker); | |
405 | } | |
406 | else | |
407 | { | |
408 | // The previous contributions from this file will be kept. | |
409 | // Mark the pieces of output sections contributed by this | |
410 | // object. | |
411 | ibase->reserve_layout(input_file_index); | |
412 | Object* obj = make_sized_incremental_object(ibase, | |
413 | input_file_index, | |
414 | input_type, | |
415 | input_reader); | |
416 | return new Add_symbols(input_objects, symtab, layout, | |
417 | search_path, 0, mapfile, input_argument, | |
418 | obj, lib, NULL, this_blocker, | |
419 | next_blocker); | |
420 | } | |
421 | } | |
422 | } | |
423 | ||
424 | // Normal object file or shared library. Check if the file has changed | |
425 | // since the last incremental link. | |
426 | if (ibase->file_has_changed(input_file_index)) | |
427 | { | |
428 | return new Read_symbols(input_objects, symtab, layout, search_path, 0, | |
429 | mapfile, input_argument, NULL, NULL, | |
430 | this_blocker, next_blocker); | |
431 | } | |
432 | else | |
433 | { | |
434 | // The previous contributions from this file will be kept. | |
435 | // Mark the pieces of output sections contributed by this object. | |
436 | ibase->reserve_layout(input_file_index); | |
437 | Object* obj = make_sized_incremental_object(ibase, | |
438 | input_file_index, | |
439 | input_type, | |
440 | input_reader); | |
441 | return new Add_symbols(input_objects, symtab, layout, search_path, 0, | |
442 | mapfile, input_argument, obj, NULL, NULL, | |
443 | this_blocker, next_blocker); | |
444 | } | |
445 | } | |
446 | ||
44453f85 ILT |
447 | // Queue up a set of tasks to be done before queueing the middle set |
448 | // of tasks. This is only necessary when garbage collection | |
6d03d481 ST |
449 | // (--gc-sections) of unused sections is desired. The relocs are read |
450 | // and processed here early to determine the garbage sections before the | |
451 | // relocs can be scanned in later tasks. | |
452 | ||
453 | void | |
454 | queue_middle_gc_tasks(const General_options& options, | |
455 | const Task* , | |
456 | const Input_objects* input_objects, | |
457 | Symbol_table* symtab, | |
458 | Layout* layout, | |
459 | Workqueue* workqueue, | |
460 | Mapfile* mapfile) | |
461 | { | |
462 | // Read_relocs for all the objects must be done and processed to find | |
463 | // unused sections before any scanning of the relocs can take place. | |
93ceb764 | 464 | Task_token* this_blocker = NULL; |
6d03d481 ST |
465 | for (Input_objects::Relobj_iterator p = input_objects->relobj_begin(); |
466 | p != input_objects->relobj_end(); | |
467 | ++p) | |
93ceb764 ILT |
468 | { |
469 | Task_token* next_blocker = new Task_token(true); | |
470 | next_blocker->add_blocker(); | |
471 | workqueue->queue(new Read_relocs(symtab, layout, *p, this_blocker, | |
472 | next_blocker)); | |
473 | this_blocker = next_blocker; | |
474 | } | |
04ceb17c DK |
475 | |
476 | // If we are given only archives in input, we have no regular | |
477 | // objects and THIS_BLOCKER is NULL here. Create a dummy | |
478 | // blocker here so that we can run the middle tasks immediately. | |
479 | if (this_blocker == NULL) | |
480 | { | |
481 | gold_assert(input_objects->number_of_relobjs() == 0); | |
482 | this_blocker = new Task_token(true); | |
483 | } | |
484 | ||
92e059d8 | 485 | workqueue->queue(new Task_function(new Middle_runner(options, |
6d03d481 ST |
486 | input_objects, |
487 | symtab, | |
488 | layout, | |
489 | mapfile), | |
93ceb764 | 490 | this_blocker, |
6d03d481 | 491 | "Task_function Middle_runner")); |
bae7f79e ILT |
492 | } |
493 | ||
92e059d8 ILT |
494 | // Queue up the middle set of tasks. These are the tasks which run |
495 | // after all the input objects have been found and all the symbols | |
496 | // have been read, but before we lay out the output file. | |
bae7f79e | 497 | |
92e059d8 ILT |
498 | void |
499 | queue_middle_tasks(const General_options& options, | |
17a1d0a9 | 500 | const Task* task, |
92e059d8 ILT |
501 | const Input_objects* input_objects, |
502 | Symbol_table* symtab, | |
503 | Layout* layout, | |
7d9e3d98 ILT |
504 | Workqueue* workqueue, |
505 | Mapfile* mapfile) | |
61ba1cf9 | 506 | { |
6d03d481 | 507 | // Add any symbols named with -u options to the symbol table. |
88a4108b | 508 | symtab->add_undefined_symbols_from_command_line(layout); |
6d03d481 ST |
509 | |
510 | // If garbage collection was chosen, relocs have been read and processed | |
511 | // at this point by pre_middle_tasks. Layout can then be done for all | |
512 | // objects. | |
513 | if (parameters->options().gc_sections()) | |
514 | { | |
515 | // Find the start symbol if any. | |
a10ae760 | 516 | Symbol* start_sym = symtab->lookup(parameters->entry()); |
556bd683 | 517 | if (start_sym != NULL) |
6d03d481 ST |
518 | { |
519 | bool is_ordinary; | |
520 | unsigned int shndx = start_sym->shndx(&is_ordinary); | |
521 | if (is_ordinary) | |
522 | { | |
523 | symtab->gc()->worklist().push( | |
524 | Section_id(start_sym->object(), shndx)); | |
525 | } | |
526 | } | |
527 | // Symbols named with -u should not be considered garbage. | |
88a4108b | 528 | symtab->gc_mark_undef_symbols(layout); |
6d03d481 ST |
529 | gold_assert(symtab->gc() != NULL); |
530 | // Do a transitive closure on all references to determine the worklist. | |
531 | symtab->gc()->do_transitive_closure(); | |
ef15dade ST |
532 | } |
533 | ||
534 | // If identical code folding (--icf) is chosen it makes sense to do it | |
535 | // only after garbage collection (--gc-sections) as we do not want to | |
536 | // be folding sections that will be garbage. | |
032ce4e9 | 537 | if (parameters->options().icf_enabled()) |
ef15dade ST |
538 | { |
539 | symtab->icf()->find_identical_sections(input_objects, symtab); | |
540 | } | |
541 | ||
542 | // Call Object::layout for the second time to determine the | |
543 | // output_sections for all referenced input sections. When | |
544 | // --gc-sections or --icf is turned on, Object::layout is | |
545 | // called twice. It is called the first time when the | |
546 | // symbols are added. | |
032ce4e9 ST |
547 | if (parameters->options().gc_sections() |
548 | || parameters->options().icf_enabled()) | |
ef15dade | 549 | { |
6d03d481 ST |
550 | for (Input_objects::Relobj_iterator p = input_objects->relobj_begin(); |
551 | p != input_objects->relobj_end(); | |
552 | ++p) | |
553 | { | |
5f9bcf58 | 554 | Task_lock_obj<Object> tlo(task, *p); |
6d03d481 ST |
555 | (*p)->layout(symtab, layout, NULL); |
556 | } | |
557 | } | |
ef15dade | 558 | |
6d03d481 ST |
559 | // Layout deferred objects due to plugins. |
560 | if (parameters->options().has_plugins()) | |
561 | { | |
562 | Plugin_manager* plugins = parameters->options().plugins(); | |
563 | gold_assert(plugins != NULL); | |
564 | plugins->layout_deferred_objects(); | |
565 | } | |
ef15dade | 566 | |
032ce4e9 ST |
567 | if (parameters->options().gc_sections() |
568 | || parameters->options().icf_enabled()) | |
6d03d481 ST |
569 | { |
570 | for (Input_objects::Relobj_iterator p = input_objects->relobj_begin(); | |
571 | p != input_objects->relobj_end(); | |
572 | ++p) | |
573 | { | |
574 | // Update the value of output_section stored in rd. | |
ca09d69a | 575 | Read_relocs_data* rd = (*p)->get_relocs_data(); |
6d03d481 ST |
576 | for (Read_relocs_data::Relocs_list::iterator q = rd->relocs.begin(); |
577 | q != rd->relocs.end(); | |
578 | ++q) | |
579 | { | |
580 | q->output_section = (*p)->output_section(q->data_shndx); | |
581 | q->needs_special_offset_handling = | |
582 | (*p)->is_output_section_offset_invalid(q->data_shndx); | |
583 | } | |
584 | } | |
585 | } | |
586 | ||
fbfba508 ILT |
587 | // We have to support the case of not seeing any input objects, and |
588 | // generate an empty file. Existing builds depend on being able to | |
589 | // pass an empty archive to the linker and get an empty object file | |
590 | // out. In order to do this we need to use a default target. | |
cdc29364 CC |
591 | if (input_objects->number_of_input_objects() == 0 |
592 | && layout->incremental_base() == NULL) | |
029ba973 | 593 | parameters_force_valid_target(); |
2c0aeda4 | 594 | |
fe9a4c12 ILT |
595 | int thread_count = options.thread_count_middle(); |
596 | if (thread_count == 0) | |
fbfba508 | 597 | thread_count = std::max(2, input_objects->number_of_input_objects()); |
fe9a4c12 ILT |
598 | workqueue->set_thread_count(thread_count); |
599 | ||
b3b74ddc | 600 | // Now we have seen all the input files. |
374ad285 ILT |
601 | const bool doing_static_link = |
602 | (!input_objects->any_dynamic() | |
603 | && !parameters->options().output_is_position_independent()); | |
4eff2974 ILT |
604 | set_parameters_doing_static_link(doing_static_link); |
605 | if (!doing_static_link && options.is_static()) | |
606 | { | |
607 | // We print out just the first .so we see; there may be others. | |
4e1e25e0 | 608 | gold_assert(input_objects->dynobj_begin() != input_objects->dynobj_end()); |
75f2446e ILT |
609 | gold_error(_("cannot mix -static with dynamic object %s"), |
610 | (*input_objects->dynobj_begin())->name().c_str()); | |
4eff2974 | 611 | } |
8851ecca | 612 | if (!doing_static_link && parameters->options().relocatable()) |
459c9f1c | 613 | gold_fatal(_("cannot mix -r with dynamic object %s"), |
6a74a719 | 614 | (*input_objects->dynobj_begin())->name().c_str()); |
516cb3d0 | 615 | if (!doing_static_link |
7cc619c3 | 616 | && options.oformat_enum() != General_options::OBJECT_FORMAT_ELF) |
516cb3d0 ILT |
617 | gold_fatal(_("cannot use non-ELF output format with dynamic object %s"), |
618 | (*input_objects->dynobj_begin())->name().c_str()); | |
b3b74ddc | 619 | |
364c7fa5 ILT |
620 | if (parameters->options().relocatable()) |
621 | { | |
622 | Input_objects::Relobj_iterator p = input_objects->relobj_begin(); | |
623 | if (p != input_objects->relobj_end()) | |
624 | { | |
625 | bool uses_split_stack = (*p)->uses_split_stack(); | |
626 | for (++p; p != input_objects->relobj_end(); ++p) | |
627 | { | |
628 | if ((*p)->uses_split_stack() != uses_split_stack) | |
629 | gold_fatal(_("cannot mix split-stack '%s' and " | |
630 | "non-split-stack '%s' when using -r"), | |
631 | (*input_objects->relobj_begin())->name().c_str(), | |
632 | (*p)->name().c_str()); | |
633 | } | |
634 | } | |
635 | } | |
636 | ||
26d3c67d CC |
637 | // For incremental updates, record the existing GOT and PLT entries, |
638 | // and the COPY relocations. | |
4829d394 CC |
639 | if (parameters->incremental_update()) |
640 | { | |
641 | Incremental_binary* ibase = layout->incremental_base(); | |
642 | ibase->process_got_plt(symtab, layout); | |
26d3c67d | 643 | ibase->emit_copy_relocs(symtab); |
4829d394 CC |
644 | } |
645 | ||
494e05f4 ILT |
646 | if (is_debugging_enabled(DEBUG_SCRIPT)) |
647 | layout->script_options()->print(stderr); | |
648 | ||
e2827e5f ILT |
649 | // For each dynamic object, record whether we've seen all the |
650 | // dynamic objects that it depends upon. | |
651 | input_objects->check_dynamic_dependencies(); | |
652 | ||
70e654ba ILT |
653 | // See if any of the input definitions violate the One Definition Rule. |
654 | // TODO: if this is too slow, do this as a task, rather than inline. | |
17a1d0a9 | 655 | symtab->detect_odr_violations(task, options.output_file_name()); |
70e654ba | 656 | |
62dfdd4d ILT |
657 | // Do the --no-undefined-version check. |
658 | if (!parameters->options().undefined_version()) | |
659 | { | |
660 | Script_options* so = layout->script_options(); | |
661 | so->version_script_info()->check_unmatched_names(symtab); | |
662 | } | |
663 | ||
9c547ec3 ILT |
664 | // Create any automatic note sections. |
665 | layout->create_notes(); | |
666 | ||
919ed24c ILT |
667 | // Create any output sections required by any linker script. |
668 | layout->create_script_sections(); | |
669 | ||
a3ad94ed ILT |
670 | // Define some sections and symbols needed for a dynamic link. This |
671 | // handles some cases we want to see before we read the relocs. | |
9b07f471 | 672 | layout->create_initial_dynamic_sections(symtab); |
a3ad94ed | 673 | |
a445fddf ILT |
674 | // Define symbols from any linker scripts. |
675 | layout->define_script_symbols(symtab); | |
676 | ||
154e0e9a ILT |
677 | // Attach sections to segments. |
678 | layout->attach_sections_to_segments(); | |
679 | ||
8851ecca | 680 | if (!parameters->options().relocatable()) |
6a74a719 ILT |
681 | { |
682 | // Predefine standard symbols. | |
683 | define_standard_symbols(symtab, layout); | |
ead1e424 | 684 | |
6a74a719 ILT |
685 | // Define __start and __stop symbols for output sections where |
686 | // appropriate. | |
687 | layout->define_section_symbols(symtab); | |
688 | } | |
bfd58944 | 689 | |
755ab8af ILT |
690 | // Make sure we have symbols for any required group signatures. |
691 | layout->define_group_signatures(symtab); | |
692 | ||
93ceb764 | 693 | Task_token* this_blocker = NULL; |
fa17a3f4 | 694 | |
93ceb764 ILT |
695 | // Allocate common symbols. We use a blocker to run this before the |
696 | // Scan_relocs tasks, because it writes to the symbol table just as | |
697 | // they do. | |
698 | if (parameters->options().define_common()) | |
699 | { | |
700 | this_blocker = new Task_token(true); | |
701 | this_blocker->add_blocker(); | |
702 | workqueue->queue(new Allocate_commons_task(symtab, layout, mapfile, | |
703 | this_blocker)); | |
704 | } | |
6d03d481 ST |
705 | |
706 | // If doing garbage collection, the relocations have already been read. | |
707 | // Otherwise, read and scan the relocations. | |
032ce4e9 ST |
708 | if (parameters->options().gc_sections() |
709 | || parameters->options().icf_enabled()) | |
92e059d8 | 710 | { |
6d03d481 ST |
711 | for (Input_objects::Relobj_iterator p = input_objects->relobj_begin(); |
712 | p != input_objects->relobj_end(); | |
713 | ++p) | |
93ceb764 ILT |
714 | { |
715 | Task_token* next_blocker = new Task_token(true); | |
716 | next_blocker->add_blocker(); | |
717 | workqueue->queue(new Scan_relocs(symtab, layout, *p, | |
718 | (*p)->get_relocs_data(), | |
719 | this_blocker, next_blocker)); | |
720 | this_blocker = next_blocker; | |
721 | } | |
6d03d481 ST |
722 | } |
723 | else | |
724 | { | |
725 | // Read the relocations of the input files. We do this to find | |
726 | // which symbols are used by relocations which require a GOT and/or | |
727 | // a PLT entry, or a COPY reloc. When we implement garbage | |
728 | // collection we will do it here by reading the relocations in a | |
729 | // breadth first search by references. | |
730 | // | |
731 | // We could also read the relocations during the first pass, and | |
732 | // mark symbols at that time. That is how the old GNU linker works. | |
733 | // Doing that is more complex, since we may later decide to discard | |
734 | // some of the sections, and thus change our minds about the types | |
735 | // of references made to the symbols. | |
736 | for (Input_objects::Relobj_iterator p = input_objects->relobj_begin(); | |
737 | p != input_objects->relobj_end(); | |
738 | ++p) | |
739 | { | |
93ceb764 ILT |
740 | Task_token* next_blocker = new Task_token(true); |
741 | next_blocker->add_blocker(); | |
742 | workqueue->queue(new Read_relocs(symtab, layout, *p, this_blocker, | |
743 | next_blocker)); | |
744 | this_blocker = next_blocker; | |
6d03d481 | 745 | } |
92e059d8 ILT |
746 | } |
747 | ||
135b9c78 ILT |
748 | if (this_blocker == NULL) |
749 | { | |
7296d933 DK |
750 | if (input_objects->number_of_relobjs() == 0) |
751 | { | |
752 | // If we are given only archives in input, we have no regular | |
753 | // objects and THIS_BLOCKER is NULL here. Create a dummy | |
754 | // blocker here so that we can run the layout task immediately. | |
755 | this_blocker = new Task_token(true); | |
756 | } | |
757 | else | |
758 | { | |
759 | // If we failed to open any input files, it's possible for | |
760 | // THIS_BLOCKER to be NULL here. There's no real point in | |
761 | // continuing if that happens. | |
762 | gold_assert(parameters->errors()->error_count() > 0); | |
e6455dfb | 763 | gold_exit(GOLD_ERR); |
7296d933 | 764 | } |
135b9c78 ILT |
765 | } |
766 | ||
92e059d8 ILT |
767 | // When all those tasks are complete, we can start laying out the |
768 | // output file. | |
8851ecca ILT |
769 | // TODO(csilvers): figure out a more principled way to get the target |
770 | Target* target = const_cast<Target*>(¶meters->target()); | |
92e059d8 ILT |
771 | workqueue->queue(new Task_function(new Layout_task_runner(options, |
772 | input_objects, | |
773 | symtab, | |
8851ecca | 774 | target, |
7d9e3d98 ILT |
775 | layout, |
776 | mapfile), | |
93ceb764 | 777 | this_blocker, |
c7912668 | 778 | "Task_function Layout_task_runner")); |
92e059d8 | 779 | } |
61ba1cf9 ILT |
780 | |
781 | // Queue up the final set of tasks. This is called at the end of | |
782 | // Layout_task. | |
783 | ||
784 | void | |
785 | queue_final_tasks(const General_options& options, | |
786 | const Input_objects* input_objects, | |
787 | const Symbol_table* symtab, | |
27bc2bce | 788 | Layout* layout, |
61ba1cf9 ILT |
789 | Workqueue* workqueue, |
790 | Output_file* of) | |
791 | { | |
fe9a4c12 ILT |
792 | int thread_count = options.thread_count_final(); |
793 | if (thread_count == 0) | |
fbfba508 | 794 | thread_count = std::max(2, input_objects->number_of_input_objects()); |
fe9a4c12 ILT |
795 | workqueue->set_thread_count(thread_count); |
796 | ||
17a1d0a9 ILT |
797 | bool any_postprocessing_sections = layout->any_postprocessing_sections(); |
798 | ||
730cdc88 ILT |
799 | // Use a blocker to wait until all the input sections have been |
800 | // written out. | |
17a1d0a9 ILT |
801 | Task_token* input_sections_blocker = NULL; |
802 | if (!any_postprocessing_sections) | |
fa17a3f4 ILT |
803 | { |
804 | input_sections_blocker = new Task_token(true); | |
805 | input_sections_blocker->add_blockers(input_objects->number_of_relobjs()); | |
806 | } | |
730cdc88 ILT |
807 | |
808 | // Use a blocker to block any objects which have to wait for the | |
809 | // output sections to complete before they can apply relocations. | |
17a1d0a9 | 810 | Task_token* output_sections_blocker = new Task_token(true); |
fa17a3f4 | 811 | output_sections_blocker->add_blocker(); |
730cdc88 | 812 | |
61ba1cf9 | 813 | // Use a blocker to block the final cleanup task. |
17a1d0a9 | 814 | Task_token* final_blocker = new Task_token(true); |
fa17a3f4 ILT |
815 | // Write_symbols_task, Write_sections_task, Write_data_task, |
816 | // Relocate_tasks. | |
817 | final_blocker->add_blockers(3); | |
818 | final_blocker->add_blockers(input_objects->number_of_relobjs()); | |
819 | if (!any_postprocessing_sections) | |
820 | final_blocker->add_blocker(); | |
61ba1cf9 ILT |
821 | |
822 | // Queue a task to write out the symbol table. | |
d491d34e ILT |
823 | workqueue->queue(new Write_symbols_task(layout, |
824 | symtab, | |
5fe2a0f5 ILT |
825 | input_objects, |
826 | layout->sympool(), | |
827 | layout->dynpool(), | |
828 | of, | |
829 | final_blocker)); | |
61ba1cf9 | 830 | |
730cdc88 | 831 | // Queue a task to write out the output sections. |
730cdc88 ILT |
832 | workqueue->queue(new Write_sections_task(layout, of, output_sections_blocker, |
833 | final_blocker)); | |
834 | ||
61ba1cf9 | 835 | // Queue a task to write out everything else. |
9025d29d | 836 | workqueue->queue(new Write_data_task(layout, symtab, of, final_blocker)); |
61ba1cf9 | 837 | |
17a1d0a9 ILT |
838 | // Queue a task for each input object to relocate the sections and |
839 | // write out the local symbols. | |
840 | for (Input_objects::Relobj_iterator p = input_objects->relobj_begin(); | |
841 | p != input_objects->relobj_end(); | |
842 | ++p) | |
fa17a3f4 ILT |
843 | workqueue->queue(new Relocate_task(symtab, layout, *p, of, |
844 | input_sections_blocker, | |
845 | output_sections_blocker, | |
846 | final_blocker)); | |
17a1d0a9 | 847 | |
730cdc88 | 848 | // Queue a task to write out the output sections which depend on |
17a1d0a9 ILT |
849 | // input sections. If there are any sections which require |
850 | // postprocessing, then we need to do this last, since it may resize | |
851 | // the output file. | |
852 | if (!any_postprocessing_sections) | |
853 | { | |
17a1d0a9 ILT |
854 | Task* t = new Write_after_input_sections_task(layout, of, |
855 | input_sections_blocker, | |
856 | final_blocker); | |
857 | workqueue->queue(t); | |
858 | } | |
859 | else | |
860 | { | |
ca09d69a | 861 | Task_token* new_final_blocker = new Task_token(true); |
17a1d0a9 ILT |
862 | new_final_blocker->add_blocker(); |
863 | Task* t = new Write_after_input_sections_task(layout, of, | |
864 | final_blocker, | |
865 | new_final_blocker); | |
866 | workqueue->queue(t); | |
867 | final_blocker = new_final_blocker; | |
868 | } | |
730cdc88 | 869 | |
61ba1cf9 ILT |
870 | // Queue a task to close the output file. This will be blocked by |
871 | // FINAL_BLOCKER. | |
516cb3d0 ILT |
872 | workqueue->queue(new Task_function(new Close_task_runner(&options, layout, |
873 | of), | |
c7912668 ILT |
874 | final_blocker, |
875 | "Task_function Close_task_runner")); | |
61ba1cf9 ILT |
876 | } |
877 | ||
878 | } // End namespace gold. |