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