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