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