2009-11-24 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 using elfcpp::Convert;
35
36 namespace gold {
37
38 // Version information. Will change frequently during the development, later
39 // we could think about backward (and forward?) compatibility.
40 const unsigned int INCREMENTAL_LINK_VERSION = 1;
41
42 namespace internal {
43
44 // Header of the .gnu_incremental_input section.
45 struct Incremental_inputs_header_data
46 {
47 // Incremental linker version.
48 elfcpp::Elf_Word version;
49
50 // Numer of input files in the link.
51 elfcpp::Elf_Word input_file_count;
52
53 // Offset of command line options in .gnu_incremental_strtab.
54 elfcpp::Elf_Word command_line_offset;
55
56 // Padding.
57 elfcpp::Elf_Word reserved;
58 };
59
60 // Data stored in .gnu_incremental_input after the header for each of the
61 // Incremental_input_header_data::input_file_count input entries.
62 struct Incremental_inputs_entry_data
63 {
64 // Offset of file name in .gnu_incremental_strtab section.
65 elfcpp::Elf_Word filename_offset;
66
67 // Offset of data in .gnu_incremental_input.
68 elfcpp::Elf_Word data_offset;
69
70 // Timestamp (in seconds).
71 elfcpp::Elf_Xword timestamp_sec;
72
73 // Nano-second part of timestamp (if supported).
74 elfcpp::Elf_Word timestamp_nsec;
75
76 // Type of the input entry.
77 elfcpp::Elf_Half input_type;
78
79 // Padding.
80 elfcpp::Elf_Half reserved;
81 };
82
83 }
84
85 // Accessors.
86
87 // Reader class for .gnu_incremental_inputs header. See
88 // internal::Incremental_inputs_header_data for fields descriptions.
89
90 template<int size, bool big_endian>
91 class Incremental_inputs_header
92 {
93 private:
94 typedef internal::Incremental_inputs_header_data Data_type;
95
96 public:
97 Incremental_inputs_header(const unsigned char *p)
98 : p_(reinterpret_cast<const Data_type*>(p))
99 { }
100
101 static const int data_size = sizeof(Data_type);
102
103 elfcpp::Elf_Word
104 get_version() const
105 { return Convert<32, big_endian>::convert_host(this->p_->version); }
106
107 elfcpp::Elf_Word
108 get_input_file_count() const
109 { return Convert<32, big_endian>::convert_host(this->p_->input_file_count); }
110
111 elfcpp::Elf_Word
112 get_command_line_offset() const
113 { return Convert<32, big_endian>::convert_host(this->p_->command_line_offset); }
114
115 elfcpp::Elf_Word
116 get_reserved() const
117 { return Convert<32, big_endian>::convert_host(this->p_->reserved); }
118
119 private:
120 const Data_type* p_;
121 };
122
123 // Writer class for .gnu_incremental_inputs header. See
124 // internal::Incremental_inputs_header_data for fields descriptions.
125
126 template<int size, bool big_endian>
127 class Incremental_inputs_header_write
128 {
129 private:
130 typedef internal::Incremental_inputs_header_data Data_type;
131
132 public:
133 Incremental_inputs_header_write(unsigned char *p)
134 : p_(reinterpret_cast<Data_type*>(p))
135 { }
136
137 static const int data_size = sizeof(Data_type);
138
139 void
140 put_version(elfcpp::Elf_Word v)
141 { this->p_->version = Convert<32, big_endian>::convert_host(v); }
142
143 void
144 put_input_file_count(elfcpp::Elf_Word v)
145 { this->p_->input_file_count = Convert<32, big_endian>::convert_host(v); }
146
147 void
148 put_command_line_offset(elfcpp::Elf_Word v)
149 { this->p_->command_line_offset = Convert<32, big_endian>::convert_host(v); }
150
151 void
152 put_reserved(elfcpp::Elf_Word v)
153 { this->p_->reserved = Convert<32, big_endian>::convert_host(v); }
154
155 private:
156 Data_type* p_;
157 };
158
159 // Reader class for an .gnu_incremental_inputs entry. See
160 // internal::Incremental_inputs_entry_data for fields descriptions.
161 template<int size, bool big_endian>
162 class Incremental_inputs_entry
163 {
164 private:
165 typedef internal::Incremental_inputs_entry_data Data_type;
166
167 public:
168 Incremental_inputs_entry(const unsigned char *p)
169 : p_(reinterpret_cast<const Data_type*>(p))
170 { }
171
172 static const int data_size = sizeof(Data_type);
173
174 elfcpp::Elf_Word
175 get_filename_offset(elfcpp::Elf_Word v)
176 { return Convert<32, big_endian>::convert_host(this->p_->filename_offset); }
177
178 elfcpp::Elf_Word
179 get_data_offset(elfcpp::Elf_Word v)
180 { return Convert<32, big_endian>::convert_host(this->p_->data_offset); }
181
182 elfcpp::Elf_Xword
183 get_timestamp_sec(elfcpp::Elf_Xword v)
184 { return Convert<64, big_endian>::convert_host(this->p_->timestamp_sec); }
185
186 elfcpp::Elf_Word
187 get_timestamp_nsec(elfcpp::Elf_Word v)
188 { return Convert<32, big_endian>::convert_host(this->p_->timestamp_nsec); }
189
190 elfcpp::Elf_Word
191 get_input_type(elfcpp::Elf_Word v)
192 { return Convert<32, big_endian>::convert_host(this->p_->input_type); }
193
194 elfcpp::Elf_Word
195 get_reserved(elfcpp::Elf_Word v)
196 { return Convert<32, big_endian>::convert_host(this->p_->reserved); }
197
198 private:
199 const Data_type* p_;
200 };
201
202 // Writer class for an .gnu_incremental_inputs entry. See
203 // internal::Incremental_inputs_entry_data for fields descriptions.
204 template<int size, bool big_endian>
205 class Incremental_inputs_entry_write
206 {
207 private:
208 typedef internal::Incremental_inputs_entry_data Data_type;
209
210 public:
211 Incremental_inputs_entry_write(unsigned char *p)
212 : p_(reinterpret_cast<Data_type*>(p))
213 { }
214
215 static const int data_size = sizeof(Data_type);
216
217 void
218 put_filename_offset(elfcpp::Elf_Word v)
219 { this->p_->filename_offset = Convert<32, big_endian>::convert_host(v); }
220
221 void
222 put_data_offset(elfcpp::Elf_Word v)
223 { this->p_->data_offset = Convert<32, big_endian>::convert_host(v); }
224
225 void
226 put_timestamp_sec(elfcpp::Elf_Xword v)
227 { this->p_->timestamp_sec = Convert<64, big_endian>::convert_host(v); }
228
229 void
230 put_timestamp_nsec(elfcpp::Elf_Word v)
231 { this->p_->timestamp_nsec = Convert<32, big_endian>::convert_host(v); }
232
233 void
234 put_input_type(elfcpp::Elf_Word v)
235 { this->p_->input_type = Convert<32, big_endian>::convert_host(v); }
236
237 void
238 put_reserved(elfcpp::Elf_Word v)
239 { this->p_->reserved = Convert<32, big_endian>::convert_host(v); }
240
241 private:
242 Data_type* p_;
243 };
244
245 // Inform the user why we don't do an incremental link. Not called in
246 // the obvious case of missing output file. TODO: Is this helpful?
247
248 void
249 vexplain_no_incremental(const char* format, va_list args)
250 {
251 char* buf = NULL;
252 if (vasprintf(&buf, format, args) < 0)
253 gold_nomem();
254 gold_info(_("the link might take longer: "
255 "cannot perform incremental link: %s"), buf);
256 free(buf);
257 }
258
259 void
260 explain_no_incremental(const char* format, ...)
261 {
262 va_list args;
263 va_start(args, format);
264 vexplain_no_incremental(format, args);
265 va_end(args);
266 }
267
268 // Report an error.
269
270 void
271 Incremental_binary::error(const char* format, ...) const
272 {
273 va_list args;
274 va_start(args, format);
275 // Current code only checks if the file can be used for incremental linking,
276 // so errors shouldn't fail the build, but only result in a fallback to a
277 // full build.
278 // TODO: when we implement incremental editing of the file, we may need a
279 // flag that will cause errors to be treated seriously.
280 vexplain_no_incremental(format, args);
281 va_end(args);
282 }
283
284 template<int size, bool big_endian>
285 bool
286 Sized_incremental_binary<size, big_endian>::do_find_incremental_inputs_section(
287 Location* location,
288 unsigned int* strtab_shndx)
289 {
290 unsigned int shndx = this->elf_file_.find_section_by_type(
291 elfcpp::SHT_GNU_INCREMENTAL_INPUTS);
292 if (shndx == elfcpp::SHN_UNDEF) // Not found.
293 return false;
294 *strtab_shndx = this->elf_file_.section_link(shndx);
295 *location = this->elf_file_.section_contents(shndx);
296 return true;
297 }
298
299 template<int size, bool big_endian>
300 bool
301 Sized_incremental_binary<size, big_endian>::do_check_inputs(
302 Incremental_inputs* incremental_inputs)
303 {
304 const int entry_size =
305 Incremental_inputs_entry_write<size, big_endian>::data_size;
306 const int header_size =
307 Incremental_inputs_header_write<size, big_endian>::data_size;
308
309 unsigned int strtab_shndx;
310 Location location;
311
312 if (!do_find_incremental_inputs_section(&location, &strtab_shndx))
313 {
314 explain_no_incremental(_("no incremental data from previous build"));
315 return false;
316 }
317 if (location.data_size < header_size
318 || strtab_shndx >= this->elf_file_.shnum()
319 || this->elf_file_.section_type(strtab_shndx) != elfcpp::SHT_STRTAB)
320 {
321 explain_no_incremental(_("invalid incremental build data"));
322 return false;
323 }
324
325 Location strtab_location(this->elf_file_.section_contents(strtab_shndx));
326 View data_view(view(location));
327 View strtab_view(view(strtab_location));
328 elfcpp::Elf_strtab strtab(strtab_view.data(), strtab_location.data_size);
329 Incremental_inputs_header<size, big_endian> header(data_view.data());
330
331 if (header.get_version() != INCREMENTAL_LINK_VERSION)
332 {
333 explain_no_incremental(_("different version of incremental build data"));
334 return false;
335 }
336
337 const char* command_line;
338 // We divide instead of multiplying to make sure there is no integer
339 // overflow.
340 size_t max_input_entries = (location.data_size - header_size) / entry_size;
341 if (header.get_input_file_count() > max_input_entries
342 || !strtab.get_c_string(header.get_command_line_offset(), &command_line))
343 {
344 explain_no_incremental(_("invalid incremental build data"));
345 return false;
346 }
347
348 if (incremental_inputs->command_line() != command_line)
349 {
350 explain_no_incremental(_("command line changed"));
351 return false;
352 }
353
354 // TODO: compare incremental_inputs->inputs() with entries in data_view.
355 return true;
356 }
357
358 namespace
359 {
360
361 // Create a Sized_incremental_binary object of the specified size and
362 // endianness. Fails if the target architecture is not supported.
363
364 template<int size, bool big_endian>
365 Incremental_binary*
366 make_sized_incremental_binary(Output_file* file,
367 const elfcpp::Ehdr<size, big_endian>& ehdr)
368 {
369 Target* target = select_target(ehdr.get_e_machine(), size, big_endian,
370 ehdr.get_e_ident()[elfcpp::EI_OSABI],
371 ehdr.get_e_ident()[elfcpp::EI_ABIVERSION]);
372 if (target == NULL)
373 {
374 explain_no_incremental(_("unsupported ELF machine number %d"),
375 ehdr.get_e_machine());
376 return NULL;
377 }
378
379 return new Sized_incremental_binary<size, big_endian>(file, ehdr, target);
380 }
381
382 } // End of anonymous namespace.
383
384 // Create an Incremental_binary object for FILE. Returns NULL is this is not
385 // possible, e.g. FILE is not an ELF file or has an unsupported target. FILE
386 // should be opened.
387
388 Incremental_binary*
389 open_incremental_binary(Output_file* file)
390 {
391 off_t filesize = file->filesize();
392 int want = elfcpp::Elf_recognizer::max_header_size;
393 if (filesize < want)
394 want = filesize;
395
396 const unsigned char* p = file->get_input_view(0, want);
397 if (!elfcpp::Elf_recognizer::is_elf_file(p, want))
398 {
399 explain_no_incremental(_("output is not an ELF file."));
400 return NULL;
401 }
402
403 int size = 0;
404 bool big_endian = false;
405 std::string error;
406 if (!elfcpp::Elf_recognizer::is_valid_header(p, want, &size, &big_endian,
407 &error))
408 {
409 explain_no_incremental(error.c_str());
410 return NULL;
411 }
412
413 Incremental_binary* result = NULL;
414 if (size == 32)
415 {
416 if (big_endian)
417 {
418 #ifdef HAVE_TARGET_32_BIG
419 result = make_sized_incremental_binary<32, true>(
420 file, elfcpp::Ehdr<32, true>(p));
421 #else
422 explain_no_incremental(_("unsupported file: 32-bit, big-endian"));
423 #endif
424 }
425 else
426 {
427 #ifdef HAVE_TARGET_32_LITTLE
428 result = make_sized_incremental_binary<32, false>(
429 file, elfcpp::Ehdr<32, false>(p));
430 #else
431 explain_no_incremental(_("unsupported file: 32-bit, little-endian"));
432 #endif
433 }
434 }
435 else if (size == 64)
436 {
437 if (big_endian)
438 {
439 #ifdef HAVE_TARGET_64_BIG
440 result = make_sized_incremental_binary<64, true>(
441 file, elfcpp::Ehdr<64, true>(p));
442 #else
443 explain_no_incremental(_("unsupported file: 64-bit, big-endian"));
444 #endif
445 }
446 else
447 {
448 #ifdef HAVE_TARGET_64_LITTLE
449 result = make_sized_incremental_binary<64, false>(
450 file, elfcpp::Ehdr<64, false>(p));
451 #else
452 explain_no_incremental(_("unsupported file: 64-bit, little-endian"));
453 #endif
454 }
455 }
456 else
457 gold_unreachable();
458
459 return result;
460 }
461
462 // Analyzes the output file to check if incremental linking is possible and
463 // (to be done) what files need to be relinked.
464
465 bool
466 Incremental_checker::can_incrementally_link_output_file()
467 {
468 Output_file output(this->output_name_);
469 if (!output.open_for_modification())
470 return false;
471 Incremental_binary* binary = open_incremental_binary(&output);
472 if (binary == NULL)
473 return false;
474 return binary->check_inputs(this->incremental_inputs_);
475 }
476
477 // Add the command line to the string table, setting
478 // command_line_key_. In incremental builds, the command line is
479 // stored in .gnu_incremental_inputs so that the next linker run can
480 // check if the command line options didn't change.
481
482 void
483 Incremental_inputs::report_command_line(int argc, const char* const* argv)
484 {
485 // Always store 'gold' as argv[0] to avoid a full relink if the user used a
486 // different path to the linker.
487 std::string args("gold");
488 // Copied from collect_argv in main.cc.
489 for (int i = 1; i < argc; ++i)
490 {
491 // Adding/removing these options should result in a full relink.
492 if (strcmp(argv[i], "--incremental-changed") == 0
493 || strcmp(argv[i], "--incremental-unchanged") == 0
494 || strcmp(argv[i], "--incremental-unknown") == 0)
495 continue;
496
497 args.append(" '");
498 // Now append argv[i], but with all single-quotes escaped
499 const char* argpos = argv[i];
500 while (1)
501 {
502 const int len = strcspn(argpos, "'");
503 args.append(argpos, len);
504 if (argpos[len] == '\0')
505 break;
506 args.append("'\"'\"'");
507 argpos += len + 1;
508 }
509 args.append("'");
510 }
511
512 this->command_line_ = args;
513 this->strtab_->add(this->command_line_.c_str(), false,
514 &this->command_line_key_);
515 }
516
517 // Record that the input argument INPUT is an achive ARCHIVE. This is
518 // called by Read_symbols after finding out the type of the file.
519
520 void
521 Incremental_inputs::report_archive(const Input_argument* input,
522 Archive* archive)
523 {
524 Hold_lock hl(*this->lock_);
525
526 Input_info info;
527 info.type = INCREMENTAL_INPUT_ARCHIVE;
528 info.archive = archive;
529 info.mtime = archive->file().get_mtime();
530 this->inputs_map_.insert(std::make_pair(input, info));
531 }
532
533 // Record that the input argument INPUT is an object OBJ. This is
534 // called by Read_symbols after finding out the type of the file.
535
536 void
537 Incremental_inputs::report_object(const Input_argument* input,
538 Object* obj)
539 {
540 Hold_lock hl(*this->lock_);
541
542 Input_info info;
543 info.type = (obj->is_dynamic()
544 ? INCREMENTAL_INPUT_SHARED_LIBRARY
545 : INCREMENTAL_INPUT_OBJECT);
546 info.object = obj;
547 info.mtime = obj->input_file()->file().get_mtime();
548 this->inputs_map_.insert(std::make_pair(input, info));
549 }
550
551 // Record that the input argument INPUT is an script SCRIPT. This is
552 // called by read_script after parsing the script and reading the list
553 // of inputs added by this script.
554
555 void
556 Incremental_inputs::report_script(const Input_argument* input,
557 Timespec mtime,
558 Script_info* script)
559 {
560 Hold_lock hl(*this->lock_);
561
562 Input_info info;
563 info.type = INCREMENTAL_INPUT_SCRIPT;
564 info.script = script;
565 info.mtime = mtime;
566 this->inputs_map_.insert(std::make_pair(input, info));
567 }
568
569 // Compute indexes in the order in which the inputs should appear in
570 // .gnu_incremental_inputs. This needs to be done after all the
571 // scripts are parsed. The function is first called for the command
572 // line inputs arguments and may call itself recursively for e.g. a
573 // list of elements of a group or a list of inputs added by a script.
574 // The [BEGIN; END) interval to analyze and *INDEX is the current
575 // value of the index (that will be updated).
576
577 void
578 Incremental_inputs::finalize_inputs(
579 Input_argument_list::const_iterator begin,
580 Input_argument_list::const_iterator end,
581 unsigned int* index)
582 {
583 for (Input_argument_list::const_iterator p = begin; p != end; ++p)
584 {
585 if (p->is_group())
586 {
587 finalize_inputs(p->group()->begin(), p->group()->end(), index);
588 continue;
589 }
590
591 Inputs_info_map::iterator it = this->inputs_map_.find(&(*p));
592 // TODO: turn it into an assert when the code will be more stable.
593 if (it == this->inputs_map_.end())
594 {
595 gold_error("internal error: %s: incremental build info not provided",
596 (p->is_file() ? p->file().name() : "[group]"));
597 continue;
598 }
599 Input_info* info = &it->second;
600 info->index = *index;
601 (*index)++;
602 this->strtab_->add(p->file().name(), false, &info->filename_key);
603 if (info->type == INCREMENTAL_INPUT_SCRIPT)
604 {
605 finalize_inputs(info->script->inputs()->begin(),
606 info->script->inputs()->end(),
607 index);
608 }
609 }
610 }
611
612 // Finalize the incremental link information. Called from
613 // Layout::finalize.
614
615 void
616 Incremental_inputs::finalize()
617 {
618 unsigned int index = 0;
619 finalize_inputs(this->inputs_->begin(), this->inputs_->end(), &index);
620
621 // Sanity check.
622 for (Inputs_info_map::const_iterator p = this->inputs_map_.begin();
623 p != this->inputs_map_.end();
624 ++p)
625 {
626 gold_assert(p->second.filename_key != 0);
627 }
628
629 this->strtab_->set_string_offsets();
630 }
631
632 // Create the content of the .gnu_incremental_inputs section.
633
634 Output_section_data*
635 Incremental_inputs::create_incremental_inputs_section_data()
636 {
637 switch (parameters->size_and_endianness())
638 {
639 #ifdef HAVE_TARGET_32_LITTLE
640 case Parameters::TARGET_32_LITTLE:
641 return this->sized_create_inputs_section_data<32, false>();
642 #endif
643 #ifdef HAVE_TARGET_32_BIG
644 case Parameters::TARGET_32_BIG:
645 return this->sized_create_inputs_section_data<32, true>();
646 #endif
647 #ifdef HAVE_TARGET_64_LITTLE
648 case Parameters::TARGET_64_LITTLE:
649 return this->sized_create_inputs_section_data<64, false>();
650 #endif
651 #ifdef HAVE_TARGET_64_BIG
652 case Parameters::TARGET_64_BIG:
653 return this->sized_create_inputs_section_data<64, true>();
654 #endif
655 default:
656 gold_unreachable();
657 }
658 }
659
660 // Sized creation of .gnu_incremental_inputs section.
661
662 template<int size, bool big_endian>
663 Output_section_data*
664 Incremental_inputs::sized_create_inputs_section_data()
665 {
666 const int entry_size =
667 Incremental_inputs_entry_write<size, big_endian>::data_size;
668 const int header_size =
669 Incremental_inputs_header_write<size, big_endian>::data_size;
670
671 unsigned int sz = header_size + entry_size * this->inputs_map_.size();
672 unsigned char* buffer = new unsigned char[sz];
673 unsigned char* inputs_base = buffer + header_size;
674
675 Incremental_inputs_header_write<size, big_endian> header_writer(buffer);
676 gold_assert(this->command_line_key_ > 0);
677 int cmd_offset = this->strtab_->get_offset_from_key(this->command_line_key_);
678
679 header_writer.put_version(INCREMENTAL_LINK_VERSION);
680 header_writer.put_input_file_count(this->inputs_map_.size());
681 header_writer.put_command_line_offset(cmd_offset);
682 header_writer.put_reserved(0);
683
684 for (Inputs_info_map::const_iterator it = this->inputs_map_.begin();
685 it != this->inputs_map_.end();
686 ++it)
687 {
688 gold_assert(it->second.index < this->inputs_map_.size());
689
690 unsigned char* entry_buffer =
691 inputs_base + it->second.index * entry_size;
692 Incremental_inputs_entry_write<size, big_endian> entry(entry_buffer);
693 int filename_offset =
694 this->strtab_->get_offset_from_key(it->second.filename_key);
695 entry.put_filename_offset(filename_offset);
696 // TODO: add per input data and timestamp. Currently we store
697 // an out-of-bounds offset for future version of gold to reject
698 // such an incremental_inputs section.
699 entry.put_data_offset(0xffffffff);
700 entry.put_timestamp_sec(it->second.mtime.seconds);
701 entry.put_timestamp_nsec(it->second.mtime.nanoseconds);
702 entry.put_input_type(it->second.type);
703 entry.put_reserved(0);
704 }
705
706 return new Output_data_const_buffer(buffer, sz, 8,
707 "** incremental link inputs list");
708 }
709
710 // Instantiate the templates we need.
711
712 #ifdef HAVE_TARGET_32_LITTLE
713 template
714 class Sized_incremental_binary<32, false>;
715 #endif
716
717 #ifdef HAVE_TARGET_32_BIG
718 template
719 class Sized_incremental_binary<32, true>;
720 #endif
721
722 #ifdef HAVE_TARGET_64_LITTLE
723 template
724 class Sized_incremental_binary<64, false>;
725 #endif
726
727 #ifdef HAVE_TARGET_64_BIG
728 template
729 class Sized_incremental_binary<64, true>;
730 #endif
731
732 } // End namespace gold.
This page took 0.045101 seconds and 5 git commands to generate.