2010-08-12 Daniel Jacobowitz <dan@codesourcery.com>
[deliverable/binutils-gdb.git] / gold / incremental.cc
CommitLineData
0e879927
ILT
1// inremental.cc -- incremental linking support for gold
2
3// Copyright 2009 Free Software Foundation, Inc.
4// Written by Mikolaj Zalewski <mikolajz@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"
c549a694
ILT
24
25#include <cstdarg>
a81ee015 26#include "libiberty.h"
c549a694 27
0e879927 28#include "elfcpp.h"
3ce2c28e
ILT
29#include "output.h"
30#include "incremental.h"
98fa85cb 31#include "archive.h"
44453f85 32#include "output.h"
c549a694 33#include "target-select.h"
0e879927 34
0e879927
ILT
35namespace gold {
36
37// Version information. Will change frequently during the development, later
38// we could think about backward (and forward?) compatibility.
c4aa1e2d 39const unsigned int INCREMENTAL_LINK_VERSION = 1;
0e879927 40
c549a694
ILT
41// Inform the user why we don't do an incremental link. Not called in
42// the obvious case of missing output file. TODO: Is this helpful?
43
44void
45vexplain_no_incremental(const char* format, va_list args)
46{
47 char* buf = NULL;
48 if (vasprintf(&buf, format, args) < 0)
49 gold_nomem();
50 gold_info(_("the link might take longer: "
51 "cannot perform incremental link: %s"), buf);
52 free(buf);
53}
54
55void
56explain_no_incremental(const char* format, ...)
57{
58 va_list args;
59 va_start(args, format);
60 vexplain_no_incremental(format, args);
61 va_end(args);
62}
63
64// Report an error.
65
66void
67Incremental_binary::error(const char* format, ...) const
68{
69 va_list args;
70 va_start(args, format);
71 // Current code only checks if the file can be used for incremental linking,
72 // so errors shouldn't fail the build, but only result in a fallback to a
73 // full build.
74 // TODO: when we implement incremental editing of the file, we may need a
75 // flag that will cause errors to be treated seriously.
76 vexplain_no_incremental(format, args);
77 va_end(args);
78}
79
80template<int size, bool big_endian>
81bool
82Sized_incremental_binary<size, big_endian>::do_find_incremental_inputs_section(
c4aa1e2d
ILT
83 Location* location,
84 unsigned int* strtab_shndx)
c549a694
ILT
85{
86 unsigned int shndx = this->elf_file_.find_section_by_type(
87 elfcpp::SHT_GNU_INCREMENTAL_INPUTS);
88 if (shndx == elfcpp::SHN_UNDEF) // Not found.
89 return false;
c4aa1e2d 90 *strtab_shndx = this->elf_file_.section_link(shndx);
c549a694
ILT
91 *location = this->elf_file_.section_contents(shndx);
92 return true;
93}
94
c4aa1e2d
ILT
95template<int size, bool big_endian>
96bool
97Sized_incremental_binary<size, big_endian>::do_check_inputs(
98 Incremental_inputs* incremental_inputs)
99{
100 const int entry_size =
101 Incremental_inputs_entry_write<size, big_endian>::data_size;
102 const int header_size =
103 Incremental_inputs_header_write<size, big_endian>::data_size;
104
105 unsigned int strtab_shndx;
106 Location location;
107
108 if (!do_find_incremental_inputs_section(&location, &strtab_shndx))
109 {
110 explain_no_incremental(_("no incremental data from previous build"));
111 return false;
112 }
113 if (location.data_size < header_size
114 || strtab_shndx >= this->elf_file_.shnum()
115 || this->elf_file_.section_type(strtab_shndx) != elfcpp::SHT_STRTAB)
116 {
117 explain_no_incremental(_("invalid incremental build data"));
118 return false;
119 }
120
121 Location strtab_location(this->elf_file_.section_contents(strtab_shndx));
122 View data_view(view(location));
123 View strtab_view(view(strtab_location));
124 elfcpp::Elf_strtab strtab(strtab_view.data(), strtab_location.data_size);
125 Incremental_inputs_header<size, big_endian> header(data_view.data());
126
127 if (header.get_version() != INCREMENTAL_LINK_VERSION)
128 {
129 explain_no_incremental(_("different version of incremental build data"));
130 return false;
131 }
132
133 const char* command_line;
134 // We divide instead of multiplying to make sure there is no integer
135 // overflow.
136 size_t max_input_entries = (location.data_size - header_size) / entry_size;
137 if (header.get_input_file_count() > max_input_entries
138 || !strtab.get_c_string(header.get_command_line_offset(), &command_line))
139 {
140 explain_no_incremental(_("invalid incremental build data"));
141 return false;
142 }
143
144 if (incremental_inputs->command_line() != command_line)
145 {
146 explain_no_incremental(_("command line changed"));
147 return false;
148 }
149
150 // TODO: compare incremental_inputs->inputs() with entries in data_view.
151 return true;
152}
153
c549a694
ILT
154namespace
155{
156
157// Create a Sized_incremental_binary object of the specified size and
158// endianness. Fails if the target architecture is not supported.
159
160template<int size, bool big_endian>
161Incremental_binary*
162make_sized_incremental_binary(Output_file* file,
163 const elfcpp::Ehdr<size, big_endian>& ehdr)
164{
165 Target* target = select_target(ehdr.get_e_machine(), size, big_endian,
166 ehdr.get_e_ident()[elfcpp::EI_OSABI],
167 ehdr.get_e_ident()[elfcpp::EI_ABIVERSION]);
168 if (target == NULL)
169 {
170 explain_no_incremental(_("unsupported ELF machine number %d"),
171 ehdr.get_e_machine());
172 return NULL;
173 }
174
3aec4f9c
RÁE
175 if (!parameters->target_valid())
176 set_parameters_target(target);
177 else if (target != &parameters->target())
178 gold_error(_("%s: incompatible target"), file->filename());
179
c549a694
ILT
180 return new Sized_incremental_binary<size, big_endian>(file, ehdr, target);
181}
182
183} // End of anonymous namespace.
184
185// Create an Incremental_binary object for FILE. Returns NULL is this is not
186// possible, e.g. FILE is not an ELF file or has an unsupported target. FILE
187// should be opened.
188
189Incremental_binary*
190open_incremental_binary(Output_file* file)
191{
192 off_t filesize = file->filesize();
193 int want = elfcpp::Elf_recognizer::max_header_size;
194 if (filesize < want)
195 want = filesize;
196
197 const unsigned char* p = file->get_input_view(0, want);
198 if (!elfcpp::Elf_recognizer::is_elf_file(p, want))
199 {
200 explain_no_incremental(_("output is not an ELF file."));
201 return NULL;
202 }
203
ac33a407
DK
204 int size = 0;
205 bool big_endian = false;
c549a694
ILT
206 std::string error;
207 if (!elfcpp::Elf_recognizer::is_valid_header(p, want, &size, &big_endian,
208 &error))
209 {
210 explain_no_incremental(error.c_str());
211 return NULL;
212 }
213
214 Incremental_binary* result = NULL;
215 if (size == 32)
216 {
217 if (big_endian)
218 {
219#ifdef HAVE_TARGET_32_BIG
220 result = make_sized_incremental_binary<32, true>(
221 file, elfcpp::Ehdr<32, true>(p));
222#else
223 explain_no_incremental(_("unsupported file: 32-bit, big-endian"));
224#endif
225 }
226 else
227 {
228#ifdef HAVE_TARGET_32_LITTLE
229 result = make_sized_incremental_binary<32, false>(
230 file, elfcpp::Ehdr<32, false>(p));
231#else
232 explain_no_incremental(_("unsupported file: 32-bit, little-endian"));
233#endif
234 }
235 }
236 else if (size == 64)
237 {
238 if (big_endian)
239 {
240#ifdef HAVE_TARGET_64_BIG
241 result = make_sized_incremental_binary<64, true>(
242 file, elfcpp::Ehdr<64, true>(p));
243#else
244 explain_no_incremental(_("unsupported file: 64-bit, big-endian"));
245#endif
246 }
247 else
248 {
249#ifdef HAVE_TARGET_64_LITTLE
250 result = make_sized_incremental_binary<64, false>(
251 file, elfcpp::Ehdr<64, false>(p));
252#else
253 explain_no_incremental(_("unsupported file: 64-bit, little-endian"));
254#endif
255 }
256 }
257 else
258 gold_unreachable();
259
260 return result;
261}
262
44453f85
ILT
263// Analyzes the output file to check if incremental linking is possible and
264// (to be done) what files need to be relinked.
265
266bool
267Incremental_checker::can_incrementally_link_output_file()
268{
269 Output_file output(this->output_name_);
270 if (!output.open_for_modification())
271 return false;
c549a694
ILT
272 Incremental_binary* binary = open_incremental_binary(&output);
273 if (binary == NULL)
274 return false;
c4aa1e2d 275 return binary->check_inputs(this->incremental_inputs_);
44453f85
ILT
276}
277
3ce2c28e
ILT
278// Add the command line to the string table, setting
279// command_line_key_. In incremental builds, the command line is
280// stored in .gnu_incremental_inputs so that the next linker run can
281// check if the command line options didn't change.
282
283void
284Incremental_inputs::report_command_line(int argc, const char* const* argv)
285{
286 // Always store 'gold' as argv[0] to avoid a full relink if the user used a
287 // different path to the linker.
288 std::string args("gold");
289 // Copied from collect_argv in main.cc.
290 for (int i = 1; i < argc; ++i)
291 {
b19b0c6d
ILT
292 // Adding/removing these options should result in a full relink.
293 if (strcmp(argv[i], "--incremental-changed") == 0
294 || strcmp(argv[i], "--incremental-unchanged") == 0
295 || strcmp(argv[i], "--incremental-unknown") == 0)
296 continue;
297
3ce2c28e
ILT
298 args.append(" '");
299 // Now append argv[i], but with all single-quotes escaped
300 const char* argpos = argv[i];
301 while (1)
302 {
303 const int len = strcspn(argpos, "'");
304 args.append(argpos, len);
305 if (argpos[len] == '\0')
306 break;
307 args.append("'\"'\"'");
308 argpos += len + 1;
309 }
310 args.append("'");
311 }
c4aa1e2d
ILT
312
313 this->command_line_ = args;
314 this->strtab_->add(this->command_line_.c_str(), false,
315 &this->command_line_key_);
3ce2c28e
ILT
316}
317
072fe7ce
ILT
318// Record that the input argument INPUT is an achive ARCHIVE. This is
319// called by Read_symbols after finding out the type of the file.
320
321void
322Incremental_inputs::report_archive(const Input_argument* input,
323 Archive* archive)
324{
325 Hold_lock hl(*this->lock_);
326
327 Input_info info;
328 info.type = INCREMENTAL_INPUT_ARCHIVE;
329 info.archive = archive;
98fa85cb
ILT
330 info.mtime = archive->file().get_mtime();
331 this->inputs_map_.insert(std::make_pair(input, info));
072fe7ce
ILT
332}
333
334// Record that the input argument INPUT is an object OBJ. This is
335// called by Read_symbols after finding out the type of the file.
336
337void
338Incremental_inputs::report_object(const Input_argument* input,
339 Object* obj)
340{
341 Hold_lock hl(*this->lock_);
342
343 Input_info info;
344 info.type = (obj->is_dynamic()
345 ? INCREMENTAL_INPUT_SHARED_LIBRARY
346 : INCREMENTAL_INPUT_OBJECT);
347 info.object = obj;
98fa85cb
ILT
348 info.mtime = obj->input_file()->file().get_mtime();
349 this->inputs_map_.insert(std::make_pair(input, info));
072fe7ce
ILT
350}
351
352// Record that the input argument INPUT is an script SCRIPT. This is
353// called by read_script after parsing the script and reading the list
354// of inputs added by this script.
355
356void
357Incremental_inputs::report_script(const Input_argument* input,
98fa85cb 358 Timespec mtime,
072fe7ce
ILT
359 Script_info* script)
360{
361 Hold_lock hl(*this->lock_);
362
363 Input_info info;
364 info.type = INCREMENTAL_INPUT_SCRIPT;
365 info.script = script;
98fa85cb
ILT
366 info.mtime = mtime;
367 this->inputs_map_.insert(std::make_pair(input, info));
072fe7ce
ILT
368}
369
370// Compute indexes in the order in which the inputs should appear in
371// .gnu_incremental_inputs. This needs to be done after all the
372// scripts are parsed. The function is first called for the command
373// line inputs arguments and may call itself recursively for e.g. a
374// list of elements of a group or a list of inputs added by a script.
375// The [BEGIN; END) interval to analyze and *INDEX is the current
376// value of the index (that will be updated).
377
378void
379Incremental_inputs::finalize_inputs(
380 Input_argument_list::const_iterator begin,
381 Input_argument_list::const_iterator end,
382 unsigned int* index)
383{
384 for (Input_argument_list::const_iterator p = begin; p != end; ++p)
385 {
386 if (p->is_group())
387 {
388 finalize_inputs(p->group()->begin(), p->group()->end(), index);
389 continue;
390 }
391
98fa85cb 392 Inputs_info_map::iterator it = this->inputs_map_.find(&(*p));
072fe7ce 393 // TODO: turn it into an assert when the code will be more stable.
98fa85cb 394 if (it == this->inputs_map_.end())
072fe7ce
ILT
395 {
396 gold_error("internal error: %s: incremental build info not provided",
397 (p->is_file() ? p->file().name() : "[group]"));
398 continue;
399 }
400 Input_info* info = &it->second;
401 info->index = *index;
402 (*index)++;
403 this->strtab_->add(p->file().name(), false, &info->filename_key);
404 if (info->type == INCREMENTAL_INPUT_SCRIPT)
405 {
406 finalize_inputs(info->script->inputs()->begin(),
407 info->script->inputs()->end(),
408 index);
409 }
410 }
411}
412
3ce2c28e
ILT
413// Finalize the incremental link information. Called from
414// Layout::finalize.
415
416void
417Incremental_inputs::finalize()
418{
072fe7ce
ILT
419 unsigned int index = 0;
420 finalize_inputs(this->inputs_->begin(), this->inputs_->end(), &index);
421
422 // Sanity check.
98fa85cb
ILT
423 for (Inputs_info_map::const_iterator p = this->inputs_map_.begin();
424 p != this->inputs_map_.end();
072fe7ce
ILT
425 ++p)
426 {
427 gold_assert(p->second.filename_key != 0);
428 }
429
3ce2c28e
ILT
430 this->strtab_->set_string_offsets();
431}
432
433// Create the content of the .gnu_incremental_inputs section.
434
435Output_section_data*
436Incremental_inputs::create_incremental_inputs_section_data()
437{
438 switch (parameters->size_and_endianness())
439 {
440#ifdef HAVE_TARGET_32_LITTLE
441 case Parameters::TARGET_32_LITTLE:
442 return this->sized_create_inputs_section_data<32, false>();
443#endif
444#ifdef HAVE_TARGET_32_BIG
445 case Parameters::TARGET_32_BIG:
446 return this->sized_create_inputs_section_data<32, true>();
447#endif
448#ifdef HAVE_TARGET_64_LITTLE
449 case Parameters::TARGET_64_LITTLE:
450 return this->sized_create_inputs_section_data<64, false>();
451#endif
452#ifdef HAVE_TARGET_64_BIG
453 case Parameters::TARGET_64_BIG:
454 return this->sized_create_inputs_section_data<64, true>();
455#endif
456 default:
457 gold_unreachable();
072fe7ce 458 }
3ce2c28e
ILT
459}
460
461// Sized creation of .gnu_incremental_inputs section.
462
463template<int size, bool big_endian>
464Output_section_data*
465Incremental_inputs::sized_create_inputs_section_data()
072fe7ce
ILT
466{
467 const int entry_size =
468 Incremental_inputs_entry_write<size, big_endian>::data_size;
469 const int header_size =
3ce2c28e 470 Incremental_inputs_header_write<size, big_endian>::data_size;
072fe7ce
ILT
471
472 unsigned int sz = header_size + entry_size * this->inputs_map_.size();
3ce2c28e 473 unsigned char* buffer = new unsigned char[sz];
072fe7ce
ILT
474 unsigned char* inputs_base = buffer + header_size;
475
3ce2c28e 476 Incremental_inputs_header_write<size, big_endian> header_writer(buffer);
3ce2c28e
ILT
477 gold_assert(this->command_line_key_ > 0);
478 int cmd_offset = this->strtab_->get_offset_from_key(this->command_line_key_);
072fe7ce 479
3ce2c28e 480 header_writer.put_version(INCREMENTAL_LINK_VERSION);
072fe7ce 481 header_writer.put_input_file_count(this->inputs_map_.size());
3ce2c28e
ILT
482 header_writer.put_command_line_offset(cmd_offset);
483 header_writer.put_reserved(0);
072fe7ce
ILT
484
485 for (Inputs_info_map::const_iterator it = this->inputs_map_.begin();
486 it != this->inputs_map_.end();
487 ++it)
488 {
489 gold_assert(it->second.index < this->inputs_map_.size());
490
491 unsigned char* entry_buffer =
492 inputs_base + it->second.index * entry_size;
493 Incremental_inputs_entry_write<size, big_endian> entry(entry_buffer);
494 int filename_offset =
495 this->strtab_->get_offset_from_key(it->second.filename_key);
496 entry.put_filename_offset(filename_offset);
a45500ae
RÁE
497 switch (it->second.type)
498 {
499 case INCREMENTAL_INPUT_SCRIPT:
500 entry.put_data_offset(0);
501 break;
502 case INCREMENTAL_INPUT_ARCHIVE:
503 case INCREMENTAL_INPUT_OBJECT:
504 case INCREMENTAL_INPUT_SHARED_LIBRARY:
505 // TODO: add per input data. Currently we store
506 // an out-of-bounds offset for future version of gold to reject
507 // such an incremental_inputs section.
508 entry.put_data_offset(0xffffffff);
509 break;
510 default:
511 gold_unreachable();
512 }
98fa85cb
ILT
513 entry.put_timestamp_sec(it->second.mtime.seconds);
514 entry.put_timestamp_nsec(it->second.mtime.nanoseconds);
072fe7ce
ILT
515 entry.put_input_type(it->second.type);
516 entry.put_reserved(0);
517 }
518
3ce2c28e 519 return new Output_data_const_buffer(buffer, sz, 8,
072fe7ce 520 "** incremental link inputs list");
3ce2c28e
ILT
521}
522
c549a694
ILT
523// Instantiate the templates we need.
524
525#ifdef HAVE_TARGET_32_LITTLE
526template
527class Sized_incremental_binary<32, false>;
528#endif
529
530#ifdef HAVE_TARGET_32_BIG
531template
532class Sized_incremental_binary<32, true>;
533#endif
534
535#ifdef HAVE_TARGET_64_LITTLE
536template
537class Sized_incremental_binary<64, false>;
538#endif
539
540#ifdef HAVE_TARGET_64_BIG
541template
542class Sized_incremental_binary<64, true>;
543#endif
544
0e879927 545} // End namespace gold.
This page took 0.109158 seconds and 4 git commands to generate.