PR ld/13258
[binutils.git] / gold / incremental.cc
blobaa9d7d3a7fe99c9f6e1686ae8cd158622d6a74d2
1 // inremental.cc -- incremental linking support for gold
3 // Copyright 2009, 2010 Free Software Foundation, Inc.
4 // Written by Mikolaj Zalewski <mikolajz@google.com>.
6 // This file is part of gold.
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.
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.
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.
23 #include "gold.h"
25 #include <cstdarg>
26 #include "libiberty.h"
28 #include "elfcpp.h"
29 #include "output.h"
30 #include "symtab.h"
31 #include "incremental.h"
32 #include "archive.h"
33 #include "output.h"
34 #include "target-select.h"
35 #include "target.h"
37 namespace gold {
39 // Version information. Will change frequently during the development, later
40 // we could think about backward (and forward?) compatibility.
41 const unsigned int INCREMENTAL_LINK_VERSION = 1;
43 // This class manages the .gnu_incremental_inputs section, which holds
44 // the header information, a directory of input files, and separate
45 // entries for each input file.
47 template<int size, bool big_endian>
48 class Output_section_incremental_inputs : public Output_section_data
50 public:
51 Output_section_incremental_inputs(const Incremental_inputs* inputs,
52 const Symbol_table* symtab)
53 : Output_section_data(size / 8), inputs_(inputs), symtab_(symtab)
54 { }
56 protected:
57 // Set the final data size.
58 void
59 set_final_data_size();
61 // Write the data to the file.
62 void
63 do_write(Output_file*);
65 // Write to a map file.
66 void
67 do_print_to_mapfile(Mapfile* mapfile) const
68 { mapfile->print_output_data(this, _("** incremental_inputs")); }
70 private:
71 // Write the section header.
72 unsigned char*
73 write_header(unsigned char* pov, unsigned int input_file_count,
74 section_offset_type command_line_offset);
76 // Write the input file entries.
77 unsigned char*
78 write_input_files(unsigned char* oview, unsigned char* pov,
79 Stringpool* strtab);
81 // Write the supplemental information blocks.
82 unsigned char*
83 write_info_blocks(unsigned char* oview, unsigned char* pov,
84 Stringpool* strtab, unsigned int* global_syms,
85 unsigned int global_sym_count);
87 // Write the contents of the .gnu_incremental_symtab section.
88 void
89 write_symtab(unsigned char* pov, unsigned int* global_syms,
90 unsigned int global_sym_count);
92 // Write the contents of the .gnu_incremental_got_plt section.
93 void
94 write_got_plt(unsigned char* pov, off_t view_size);
96 // Typedefs for writing the data to the output sections.
97 typedef elfcpp::Swap<size, big_endian> Swap;
98 typedef elfcpp::Swap<16, big_endian> Swap16;
99 typedef elfcpp::Swap<32, big_endian> Swap32;
100 typedef elfcpp::Swap<64, big_endian> Swap64;
102 // Sizes of various structures.
103 static const int sizeof_addr = size / 8;
104 static const int header_size = 16;
105 static const int input_entry_size = 24;
107 // The Incremental_inputs object.
108 const Incremental_inputs* inputs_;
110 // The symbol table.
111 const Symbol_table* symtab_;
114 // Inform the user why we don't do an incremental link. Not called in
115 // the obvious case of missing output file. TODO: Is this helpful?
117 void
118 vexplain_no_incremental(const char* format, va_list args)
120 char* buf = NULL;
121 if (vasprintf(&buf, format, args) < 0)
122 gold_nomem();
123 gold_info(_("the link might take longer: "
124 "cannot perform incremental link: %s"), buf);
125 free(buf);
128 void
129 explain_no_incremental(const char* format, ...)
131 va_list args;
132 va_start(args, format);
133 vexplain_no_incremental(format, args);
134 va_end(args);
137 // Report an error.
139 void
140 Incremental_binary::error(const char* format, ...) const
142 va_list args;
143 va_start(args, format);
144 // Current code only checks if the file can be used for incremental linking,
145 // so errors shouldn't fail the build, but only result in a fallback to a
146 // full build.
147 // TODO: when we implement incremental editing of the file, we may need a
148 // flag that will cause errors to be treated seriously.
149 vexplain_no_incremental(format, args);
150 va_end(args);
153 // Find the .gnu_incremental_inputs section and related sections.
155 template<int size, bool big_endian>
156 bool
157 Sized_incremental_binary<size, big_endian>::do_find_incremental_inputs_sections(
158 unsigned int* p_inputs_shndx,
159 unsigned int* p_symtab_shndx,
160 unsigned int* p_relocs_shndx,
161 unsigned int* p_got_plt_shndx,
162 unsigned int* p_strtab_shndx)
164 unsigned int inputs_shndx =
165 this->elf_file_.find_section_by_type(elfcpp::SHT_GNU_INCREMENTAL_INPUTS);
166 if (inputs_shndx == elfcpp::SHN_UNDEF) // Not found.
167 return false;
169 unsigned int symtab_shndx =
170 this->elf_file_.find_section_by_type(elfcpp::SHT_GNU_INCREMENTAL_SYMTAB);
171 if (symtab_shndx == elfcpp::SHN_UNDEF) // Not found.
172 return false;
173 if (this->elf_file_.section_link(symtab_shndx) != inputs_shndx)
174 return false;
176 unsigned int relocs_shndx =
177 this->elf_file_.find_section_by_type(elfcpp::SHT_GNU_INCREMENTAL_RELOCS);
178 if (relocs_shndx == elfcpp::SHN_UNDEF) // Not found.
179 return false;
180 if (this->elf_file_.section_link(relocs_shndx) != inputs_shndx)
181 return false;
183 unsigned int got_plt_shndx =
184 this->elf_file_.find_section_by_type(elfcpp::SHT_GNU_INCREMENTAL_GOT_PLT);
185 if (got_plt_shndx == elfcpp::SHN_UNDEF) // Not found.
186 return false;
187 if (this->elf_file_.section_link(got_plt_shndx) != inputs_shndx)
188 return false;
190 unsigned int strtab_shndx = this->elf_file_.section_link(inputs_shndx);
191 if (strtab_shndx == elfcpp::SHN_UNDEF
192 || strtab_shndx > this->elf_file_.shnum()
193 || this->elf_file_.section_type(strtab_shndx) != elfcpp::SHT_STRTAB)
194 return false;
196 if (p_inputs_shndx != NULL)
197 *p_inputs_shndx = inputs_shndx;
198 if (p_symtab_shndx != NULL)
199 *p_symtab_shndx = symtab_shndx;
200 if (p_relocs_shndx != NULL)
201 *p_relocs_shndx = relocs_shndx;
202 if (p_got_plt_shndx != NULL)
203 *p_got_plt_shndx = got_plt_shndx;
204 if (p_strtab_shndx != NULL)
205 *p_strtab_shndx = strtab_shndx;
206 return true;
209 // Determine whether an incremental link based on the existing output file
210 // can be done.
212 template<int size, bool big_endian>
213 bool
214 Sized_incremental_binary<size, big_endian>::do_check_inputs(
215 Incremental_inputs* incremental_inputs)
217 unsigned int inputs_shndx;
218 unsigned int symtab_shndx;
219 unsigned int relocs_shndx;
220 unsigned int plt_got_shndx;
221 unsigned int strtab_shndx;
223 if (!do_find_incremental_inputs_sections(&inputs_shndx, &symtab_shndx,
224 &relocs_shndx, &plt_got_shndx,
225 &strtab_shndx))
227 explain_no_incremental(_("no incremental data from previous build"));
228 return false;
231 Location inputs_location(this->elf_file_.section_contents(inputs_shndx));
232 Location symtab_location(this->elf_file_.section_contents(symtab_shndx));
233 Location relocs_location(this->elf_file_.section_contents(relocs_shndx));
234 Location strtab_location(this->elf_file_.section_contents(strtab_shndx));
236 View inputs_view(view(inputs_location));
237 View symtab_view(view(symtab_location));
238 View relocs_view(view(relocs_location));
239 View strtab_view(view(strtab_location));
241 elfcpp::Elf_strtab strtab(strtab_view.data(), strtab_location.data_size);
243 Incremental_inputs_reader<size, big_endian>
244 incoming_inputs(inputs_view.data(), strtab);
246 if (incoming_inputs.version() != INCREMENTAL_LINK_VERSION)
248 explain_no_incremental(_("different version of incremental build data"));
249 return false;
252 if (incremental_inputs->command_line() != incoming_inputs.command_line())
254 explain_no_incremental(_("command line changed"));
255 return false;
258 // TODO: compare incremental_inputs->inputs() with entries in data_view.
260 return true;
263 namespace
266 // Create a Sized_incremental_binary object of the specified size and
267 // endianness. Fails if the target architecture is not supported.
269 template<int size, bool big_endian>
270 Incremental_binary*
271 make_sized_incremental_binary(Output_file* file,
272 const elfcpp::Ehdr<size, big_endian>& ehdr)
274 Target* target = select_target(ehdr.get_e_machine(), size, big_endian,
275 ehdr.get_e_ident()[elfcpp::EI_OSABI],
276 ehdr.get_e_ident()[elfcpp::EI_ABIVERSION]);
277 if (target == NULL)
279 explain_no_incremental(_("unsupported ELF machine number %d"),
280 ehdr.get_e_machine());
281 return NULL;
284 if (!parameters->target_valid())
285 set_parameters_target(target);
286 else if (target != &parameters->target())
287 gold_error(_("%s: incompatible target"), file->filename());
289 return new Sized_incremental_binary<size, big_endian>(file, ehdr, target);
292 } // End of anonymous namespace.
294 // Create an Incremental_binary object for FILE. Returns NULL is this is not
295 // possible, e.g. FILE is not an ELF file or has an unsupported target. FILE
296 // should be opened.
298 Incremental_binary*
299 open_incremental_binary(Output_file* file)
301 off_t filesize = file->filesize();
302 int want = elfcpp::Elf_recognizer::max_header_size;
303 if (filesize < want)
304 want = filesize;
306 const unsigned char* p = file->get_input_view(0, want);
307 if (!elfcpp::Elf_recognizer::is_elf_file(p, want))
309 explain_no_incremental(_("output is not an ELF file."));
310 return NULL;
313 int size = 0;
314 bool big_endian = false;
315 std::string error;
316 if (!elfcpp::Elf_recognizer::is_valid_header(p, want, &size, &big_endian,
317 &error))
319 explain_no_incremental(error.c_str());
320 return NULL;
323 Incremental_binary* result = NULL;
324 if (size == 32)
326 if (big_endian)
328 #ifdef HAVE_TARGET_32_BIG
329 result = make_sized_incremental_binary<32, true>(
330 file, elfcpp::Ehdr<32, true>(p));
331 #else
332 explain_no_incremental(_("unsupported file: 32-bit, big-endian"));
333 #endif
335 else
337 #ifdef HAVE_TARGET_32_LITTLE
338 result = make_sized_incremental_binary<32, false>(
339 file, elfcpp::Ehdr<32, false>(p));
340 #else
341 explain_no_incremental(_("unsupported file: 32-bit, little-endian"));
342 #endif
345 else if (size == 64)
347 if (big_endian)
349 #ifdef HAVE_TARGET_64_BIG
350 result = make_sized_incremental_binary<64, true>(
351 file, elfcpp::Ehdr<64, true>(p));
352 #else
353 explain_no_incremental(_("unsupported file: 64-bit, big-endian"));
354 #endif
356 else
358 #ifdef HAVE_TARGET_64_LITTLE
359 result = make_sized_incremental_binary<64, false>(
360 file, elfcpp::Ehdr<64, false>(p));
361 #else
362 explain_no_incremental(_("unsupported file: 64-bit, little-endian"));
363 #endif
366 else
367 gold_unreachable();
369 return result;
372 // Analyzes the output file to check if incremental linking is possible and
373 // (to be done) what files need to be relinked.
375 bool
376 Incremental_checker::can_incrementally_link_output_file()
378 Output_file output(this->output_name_);
379 if (!output.open_for_modification())
380 return false;
381 Incremental_binary* binary = open_incremental_binary(&output);
382 if (binary == NULL)
383 return false;
384 return binary->check_inputs(this->incremental_inputs_);
387 // Class Incremental_inputs.
389 // Add the command line to the string table, setting
390 // command_line_key_. In incremental builds, the command line is
391 // stored in .gnu_incremental_inputs so that the next linker run can
392 // check if the command line options didn't change.
394 void
395 Incremental_inputs::report_command_line(int argc, const char* const* argv)
397 // Always store 'gold' as argv[0] to avoid a full relink if the user used a
398 // different path to the linker.
399 std::string args("gold");
400 // Copied from collect_argv in main.cc.
401 for (int i = 1; i < argc; ++i)
403 // Adding/removing these options should not result in a full relink.
404 if (strcmp(argv[i], "--incremental") == 0
405 || strcmp(argv[i], "--incremental-full") == 0
406 || strcmp(argv[i], "--incremental-update") == 0
407 || strcmp(argv[i], "--incremental-changed") == 0
408 || strcmp(argv[i], "--incremental-unchanged") == 0
409 || strcmp(argv[i], "--incremental-unknown") == 0)
410 continue;
412 args.append(" '");
413 // Now append argv[i], but with all single-quotes escaped
414 const char* argpos = argv[i];
415 while (1)
417 const int len = strcspn(argpos, "'");
418 args.append(argpos, len);
419 if (argpos[len] == '\0')
420 break;
421 args.append("'\"'\"'");
422 argpos += len + 1;
424 args.append("'");
427 this->command_line_ = args;
428 this->strtab_->add(this->command_line_.c_str(), false,
429 &this->command_line_key_);
432 // Record the input archive file ARCHIVE. This is called by the
433 // Add_archive_symbols task before determining which archive members
434 // to include. We create the Incremental_archive_entry here and
435 // attach it to the Archive, but we do not add it to the list of
436 // input objects until report_archive_end is called.
438 void
439 Incremental_inputs::report_archive_begin(Archive* arch)
441 Stringpool::Key filename_key;
442 Timespec mtime = arch->file().get_mtime();
444 this->strtab_->add(arch->filename().c_str(), false, &filename_key);
445 Incremental_archive_entry* entry =
446 new Incremental_archive_entry(filename_key, arch, mtime);
447 arch->set_incremental_info(entry);
450 // Finish recording the input archive file ARCHIVE. This is called by the
451 // Add_archive_symbols task after determining which archive members
452 // to include.
454 void
455 Incremental_inputs::report_archive_end(Archive* arch)
457 Incremental_archive_entry* entry = arch->incremental_info();
459 gold_assert(entry != NULL);
461 // Collect unused global symbols.
462 for (Archive::Unused_symbol_iterator p = arch->unused_symbols_begin();
463 p != arch->unused_symbols_end();
464 ++p)
466 Stringpool::Key symbol_key;
467 this->strtab_->add(*p, true, &symbol_key);
468 entry->add_unused_global_symbol(symbol_key);
470 this->inputs_.push_back(entry);
473 // Record the input object file OBJ. If ARCH is not NULL, attach
474 // the object file to the archive. This is called by the
475 // Add_symbols task after finding out the type of the file.
477 void
478 Incremental_inputs::report_object(Object* obj, Archive* arch)
480 Stringpool::Key filename_key;
481 Timespec mtime = obj->input_file()->file().get_mtime();
483 this->strtab_->add(obj->name().c_str(), false, &filename_key);
484 Incremental_object_entry* obj_entry =
485 new Incremental_object_entry(filename_key, obj, mtime);
486 this->inputs_.push_back(obj_entry);
488 if (arch != NULL)
490 Incremental_archive_entry* arch_entry = arch->incremental_info();
491 gold_assert(arch_entry != NULL);
492 arch_entry->add_object(obj_entry);
495 this->current_object_ = obj;
496 this->current_object_entry_ = obj_entry;
499 // Record the input object file OBJ. If ARCH is not NULL, attach
500 // the object file to the archive. This is called by the
501 // Add_symbols task after finding out the type of the file.
503 void
504 Incremental_inputs::report_input_section(Object* obj, unsigned int shndx,
505 const char* name, off_t sh_size)
507 Stringpool::Key key = 0;
509 if (name != NULL)
510 this->strtab_->add(name, true, &key);
512 gold_assert(obj == this->current_object_);
513 this->current_object_entry_->add_input_section(shndx, key, sh_size);
516 // Record that the input argument INPUT is a script SCRIPT. This is
517 // called by read_script after parsing the script and reading the list
518 // of inputs added by this script.
520 void
521 Incremental_inputs::report_script(const std::string& filename,
522 Script_info* script, Timespec mtime)
524 Stringpool::Key filename_key;
526 this->strtab_->add(filename.c_str(), false, &filename_key);
527 Incremental_script_entry* entry =
528 new Incremental_script_entry(filename_key, script, mtime);
529 this->inputs_.push_back(entry);
532 // Finalize the incremental link information. Called from
533 // Layout::finalize.
535 void
536 Incremental_inputs::finalize()
538 // Finalize the string table.
539 this->strtab_->set_string_offsets();
542 // Create the .gnu_incremental_inputs, _symtab, and _relocs input sections.
544 void
545 Incremental_inputs::create_data_sections(Symbol_table* symtab)
547 switch (parameters->size_and_endianness())
549 #ifdef HAVE_TARGET_32_LITTLE
550 case Parameters::TARGET_32_LITTLE:
551 this->inputs_section_ =
552 new Output_section_incremental_inputs<32, false>(this, symtab);
553 break;
554 #endif
555 #ifdef HAVE_TARGET_32_BIG
556 case Parameters::TARGET_32_BIG:
557 this->inputs_section_ =
558 new Output_section_incremental_inputs<32, true>(this, symtab);
559 break;
560 #endif
561 #ifdef HAVE_TARGET_64_LITTLE
562 case Parameters::TARGET_64_LITTLE:
563 this->inputs_section_ =
564 new Output_section_incremental_inputs<64, false>(this, symtab);
565 break;
566 #endif
567 #ifdef HAVE_TARGET_64_BIG
568 case Parameters::TARGET_64_BIG:
569 this->inputs_section_ =
570 new Output_section_incremental_inputs<64, true>(this, symtab);
571 break;
572 #endif
573 default:
574 gold_unreachable();
576 this->symtab_section_ = new Output_data_space(4, "** incremental_symtab");
577 this->relocs_section_ = new Output_data_space(4, "** incremental_relocs");
578 this->got_plt_section_ = new Output_data_space(4, "** incremental_got_plt");
581 // Return the sh_entsize value for the .gnu_incremental_relocs section.
582 unsigned int
583 Incremental_inputs::relocs_entsize() const
585 return 8 + 2 * parameters->target().get_size() / 8;
588 // Class Output_section_incremental_inputs.
590 // Finalize the offsets for each input section and supplemental info block,
591 // and set the final data size of the incremental output sections.
593 template<int size, bool big_endian>
594 void
595 Output_section_incremental_inputs<size, big_endian>::set_final_data_size()
597 const Incremental_inputs* inputs = this->inputs_;
598 const unsigned int sizeof_addr = size / 8;
599 const unsigned int rel_size = 8 + 2 * sizeof_addr;
601 // Offset of each input entry.
602 unsigned int input_offset = this->header_size;
604 // Offset of each supplemental info block.
605 unsigned int info_offset = this->header_size;
606 info_offset += this->input_entry_size * inputs->input_file_count();
608 // Count each input file and its supplemental information block.
609 for (Incremental_inputs::Input_list::const_iterator p =
610 inputs->input_files().begin();
611 p != inputs->input_files().end();
612 ++p)
614 // Set the offset of the input file entry.
615 (*p)->set_offset(input_offset);
616 input_offset += this->input_entry_size;
618 // Set the offset of the supplemental info block.
619 switch ((*p)->type())
621 case INCREMENTAL_INPUT_SCRIPT:
622 // No supplemental info for a script.
623 (*p)->set_info_offset(0);
624 break;
625 case INCREMENTAL_INPUT_OBJECT:
626 case INCREMENTAL_INPUT_ARCHIVE_MEMBER:
628 Incremental_object_entry* entry = (*p)->object_entry();
629 gold_assert(entry != NULL);
630 (*p)->set_info_offset(info_offset);
631 // Input section count + global symbol count.
632 info_offset += 8;
633 // Each input section.
634 info_offset += (entry->get_input_section_count()
635 * (8 + 2 * sizeof_addr));
636 // Each global symbol.
637 const Object::Symbols* syms = entry->object()->get_global_symbols();
638 info_offset += syms->size() * 16;
640 break;
641 case INCREMENTAL_INPUT_SHARED_LIBRARY:
643 Incremental_object_entry* entry = (*p)->object_entry();
644 gold_assert(entry != NULL);
645 (*p)->set_info_offset(info_offset);
646 // Global symbol count.
647 info_offset += 4;
648 // Each global symbol.
649 const Object::Symbols* syms = entry->object()->get_global_symbols();
650 unsigned int nsyms = syms != NULL ? syms->size() : 0;
651 info_offset += nsyms * 4;
653 break;
654 case INCREMENTAL_INPUT_ARCHIVE:
656 Incremental_archive_entry* entry = (*p)->archive_entry();
657 gold_assert(entry != NULL);
658 (*p)->set_info_offset(info_offset);
659 // Member count + unused global symbol count.
660 info_offset += 8;
661 // Each member.
662 info_offset += (entry->get_member_count() * 4);
663 // Each global symbol.
664 info_offset += (entry->get_unused_global_symbol_count() * 4);
666 break;
667 default:
668 gold_unreachable();
672 this->set_data_size(info_offset);
674 // Set the size of the .gnu_incremental_symtab section.
675 inputs->symtab_section()->set_current_data_size(this->symtab_->output_count()
676 * sizeof(unsigned int));
678 // Set the size of the .gnu_incremental_relocs section.
679 inputs->relocs_section()->set_current_data_size(inputs->get_reloc_count()
680 * rel_size);
682 // Set the size of the .gnu_incremental_got_plt section.
683 Sized_target<size, big_endian>* target =
684 parameters->sized_target<size, big_endian>();
685 unsigned int got_count = target->got_entry_count();
686 unsigned int plt_count = target->plt_entry_count();
687 unsigned int got_plt_size = 8; // GOT entry count, PLT entry count.
688 got_plt_size = (got_plt_size + got_count + 3) & ~3; // GOT type array.
689 got_plt_size += got_count * 4 + plt_count * 4; // GOT array, PLT array.
690 inputs->got_plt_section()->set_current_data_size(got_plt_size);
693 // Write the contents of the .gnu_incremental_inputs and
694 // .gnu_incremental_symtab sections.
696 template<int size, bool big_endian>
697 void
698 Output_section_incremental_inputs<size, big_endian>::do_write(Output_file* of)
700 const Incremental_inputs* inputs = this->inputs_;
701 Stringpool* strtab = inputs->get_stringpool();
703 // Get a view into the .gnu_incremental_inputs section.
704 const off_t off = this->offset();
705 const off_t oview_size = this->data_size();
706 unsigned char* const oview = of->get_output_view(off, oview_size);
707 unsigned char* pov = oview;
709 // Get a view into the .gnu_incremental_symtab section.
710 const off_t symtab_off = inputs->symtab_section()->offset();
711 const off_t symtab_size = inputs->symtab_section()->data_size();
712 unsigned char* const symtab_view = of->get_output_view(symtab_off,
713 symtab_size);
715 // Allocate an array of linked list heads for the .gnu_incremental_symtab
716 // section. Each element corresponds to a global symbol in the output
717 // symbol table, and points to the head of the linked list that threads
718 // through the object file input entries. The value of each element
719 // is the section-relative offset to a global symbol entry in a
720 // supplemental information block.
721 unsigned int global_sym_count = this->symtab_->output_count();
722 unsigned int* global_syms = new unsigned int[global_sym_count];
723 memset(global_syms, 0, global_sym_count * sizeof(unsigned int));
725 // Write the section header.
726 Stringpool::Key command_line_key = inputs->command_line_key();
727 pov = this->write_header(pov, inputs->input_file_count(),
728 strtab->get_offset_from_key(command_line_key));
730 // Write the list of input files.
731 pov = this->write_input_files(oview, pov, strtab);
733 // Write the supplemental information blocks for each input file.
734 pov = this->write_info_blocks(oview, pov, strtab, global_syms,
735 global_sym_count);
737 gold_assert(pov - oview == oview_size);
739 // Write the .gnu_incremental_symtab section.
740 gold_assert(global_sym_count * 4 == symtab_size);
741 this->write_symtab(symtab_view, global_syms, global_sym_count);
743 delete[] global_syms;
745 // Write the .gnu_incremental_got_plt section.
746 const off_t got_plt_off = inputs->got_plt_section()->offset();
747 const off_t got_plt_size = inputs->got_plt_section()->data_size();
748 unsigned char* const got_plt_view = of->get_output_view(got_plt_off,
749 got_plt_size);
750 this->write_got_plt(got_plt_view, got_plt_size);
752 of->write_output_view(off, oview_size, oview);
753 of->write_output_view(symtab_off, symtab_size, symtab_view);
754 of->write_output_view(got_plt_off, got_plt_size, got_plt_view);
757 // Write the section header: version, input file count, offset of command line
758 // in the string table, and 4 bytes of padding.
760 template<int size, bool big_endian>
761 unsigned char*
762 Output_section_incremental_inputs<size, big_endian>::write_header(
763 unsigned char* pov,
764 unsigned int input_file_count,
765 section_offset_type command_line_offset)
767 Swap32::writeval(pov, INCREMENTAL_LINK_VERSION);
768 Swap32::writeval(pov + 4, input_file_count);
769 Swap32::writeval(pov + 8, command_line_offset);
770 Swap32::writeval(pov + 12, 0);
771 return pov + this->header_size;
774 // Write the input file entries.
776 template<int size, bool big_endian>
777 unsigned char*
778 Output_section_incremental_inputs<size, big_endian>::write_input_files(
779 unsigned char* oview,
780 unsigned char* pov,
781 Stringpool* strtab)
783 const Incremental_inputs* inputs = this->inputs_;
785 for (Incremental_inputs::Input_list::const_iterator p =
786 inputs->input_files().begin();
787 p != inputs->input_files().end();
788 ++p)
790 gold_assert(static_cast<unsigned int>(pov - oview) == (*p)->get_offset());
791 section_offset_type filename_offset =
792 strtab->get_offset_from_key((*p)->get_filename_key());
793 const Timespec& mtime = (*p)->get_mtime();
794 Swap32::writeval(pov, filename_offset);
795 Swap32::writeval(pov + 4, (*p)->get_info_offset());
796 Swap64::writeval(pov + 8, mtime.seconds);
797 Swap32::writeval(pov + 16, mtime.nanoseconds);
798 Swap16::writeval(pov + 20, (*p)->type());
799 Swap16::writeval(pov + 22, 0);
800 pov += this->input_entry_size;
802 return pov;
805 // Write the supplemental information blocks.
807 template<int size, bool big_endian>
808 unsigned char*
809 Output_section_incremental_inputs<size, big_endian>::write_info_blocks(
810 unsigned char* oview,
811 unsigned char* pov,
812 Stringpool* strtab,
813 unsigned int* global_syms,
814 unsigned int global_sym_count)
816 const Incremental_inputs* inputs = this->inputs_;
817 unsigned int first_global_index = this->symtab_->first_global_index();
819 for (Incremental_inputs::Input_list::const_iterator p =
820 inputs->input_files().begin();
821 p != inputs->input_files().end();
822 ++p)
824 switch ((*p)->type())
826 case INCREMENTAL_INPUT_SCRIPT:
827 // No supplemental info for a script.
828 break;
830 case INCREMENTAL_INPUT_OBJECT:
831 case INCREMENTAL_INPUT_ARCHIVE_MEMBER:
833 gold_assert(static_cast<unsigned int>(pov - oview)
834 == (*p)->get_info_offset());
835 Incremental_object_entry* entry = (*p)->object_entry();
836 gold_assert(entry != NULL);
837 const Object* obj = entry->object();
838 const Object::Symbols* syms = obj->get_global_symbols();
839 // Write the input section count and global symbol count.
840 unsigned int nsections = entry->get_input_section_count();
841 unsigned int nsyms = syms->size();
842 Swap32::writeval(pov, nsections);
843 Swap32::writeval(pov + 4, nsyms);
844 pov += 8;
846 // For each input section, write the name, output section index,
847 // offset within output section, and input section size.
848 for (unsigned int i = 0; i < nsections; i++)
850 Stringpool::Key key = entry->get_input_section_name_key(i);
851 off_t name_offset = 0;
852 if (key != 0)
853 name_offset = strtab->get_offset_from_key(key);
854 int out_shndx = 0;
855 off_t out_offset = 0;
856 off_t sh_size = 0;
857 Output_section* os = obj->output_section(i);
858 if (os != NULL)
860 out_shndx = os->out_shndx();
861 out_offset = obj->output_section_offset(i);
862 sh_size = entry->get_input_section_size(i);
864 Swap32::writeval(pov, name_offset);
865 Swap32::writeval(pov + 4, out_shndx);
866 Swap::writeval(pov + 8, out_offset);
867 Swap::writeval(pov + 8 + sizeof_addr, sh_size);
868 pov += 8 + 2 * sizeof_addr;
871 // For each global symbol, write its associated relocations,
872 // add it to the linked list of globals, then write the
873 // supplemental information: global symbol table index,
874 // linked list chain pointer, relocation count, and offset
875 // to the relocations.
876 for (unsigned int i = 0; i < nsyms; i++)
878 const Symbol* sym = (*syms)[i];
879 if (sym->is_forwarder())
880 sym = this->symtab_->resolve_forwards(sym);
881 unsigned int symtab_index = sym->symtab_index();
882 unsigned int chain = 0;
883 unsigned int first_reloc = 0;
884 unsigned int nrelocs = obj->get_incremental_reloc_count(i);
885 if (nrelocs > 0)
887 gold_assert(symtab_index != -1U
888 && (symtab_index - first_global_index
889 < global_sym_count));
890 first_reloc = obj->get_incremental_reloc_base(i);
891 chain = global_syms[symtab_index - first_global_index];
892 global_syms[symtab_index - first_global_index] =
893 pov - oview;
895 Swap32::writeval(pov, symtab_index);
896 Swap32::writeval(pov + 4, chain);
897 Swap32::writeval(pov + 8, nrelocs);
898 Swap32::writeval(pov + 12, first_reloc * 3 * sizeof_addr);
899 pov += 16;
902 break;
904 case INCREMENTAL_INPUT_SHARED_LIBRARY:
906 gold_assert(static_cast<unsigned int>(pov - oview)
907 == (*p)->get_info_offset());
908 Incremental_object_entry* entry = (*p)->object_entry();
909 gold_assert(entry != NULL);
910 const Object* obj = entry->object();
911 const Object::Symbols* syms = obj->get_global_symbols();
913 // Write the global symbol count.
914 unsigned int nsyms = syms != NULL ? syms->size() : 0;
915 Swap32::writeval(pov, nsyms);
916 pov += 4;
918 // For each global symbol, write the global symbol table index.
919 for (unsigned int i = 0; i < nsyms; i++)
921 const Symbol* sym = (*syms)[i];
922 Swap32::writeval(pov, sym->symtab_index());
923 pov += 4;
926 break;
928 case INCREMENTAL_INPUT_ARCHIVE:
930 gold_assert(static_cast<unsigned int>(pov - oview)
931 == (*p)->get_info_offset());
932 Incremental_archive_entry* entry = (*p)->archive_entry();
933 gold_assert(entry != NULL);
935 // Write the member count and unused global symbol count.
936 unsigned int nmembers = entry->get_member_count();
937 unsigned int nsyms = entry->get_unused_global_symbol_count();
938 Swap32::writeval(pov, nmembers);
939 Swap32::writeval(pov + 4, nsyms);
940 pov += 8;
942 // For each member, write the offset to its input file entry.
943 for (unsigned int i = 0; i < nmembers; ++i)
945 Incremental_object_entry* member = entry->get_member(i);
946 Swap32::writeval(pov, member->get_offset());
947 pov += 4;
950 // For each global symbol, write the name offset.
951 for (unsigned int i = 0; i < nsyms; ++i)
953 Stringpool::Key key = entry->get_unused_global_symbol(i);
954 Swap32::writeval(pov, strtab->get_offset_from_key(key));
955 pov += 4;
958 break;
960 default:
961 gold_unreachable();
964 return pov;
967 // Write the contents of the .gnu_incremental_symtab section.
969 template<int size, bool big_endian>
970 void
971 Output_section_incremental_inputs<size, big_endian>::write_symtab(
972 unsigned char* pov,
973 unsigned int* global_syms,
974 unsigned int global_sym_count)
976 for (unsigned int i = 0; i < global_sym_count; ++i)
978 Swap32::writeval(pov, global_syms[i]);
979 pov += 4;
983 // This struct holds the view information needed to write the
984 // .gnu_incremental_got_plt section.
986 struct Got_plt_view_info
988 // Start of the GOT type array in the output view.
989 unsigned char* got_type_p;
990 // Start of the GOT descriptor array in the output view.
991 unsigned char* got_desc_p;
992 // Start of the PLT descriptor array in the output view.
993 unsigned char* plt_desc_p;
994 // Number of GOT entries.
995 unsigned int got_count;
996 // Number of PLT entries.
997 unsigned int plt_count;
998 // Offset of the first non-reserved PLT entry (this is a target-dependent value).
999 unsigned int first_plt_entry_offset;
1000 // Size of a PLT entry (this is a target-dependent value).
1001 unsigned int plt_entry_size;
1002 // Value to write in the GOT descriptor array. For global symbols,
1003 // this is the global symbol table index; for local symbols, it is
1004 // the offset of the input file entry in the .gnu_incremental_inputs
1005 // section.
1006 unsigned int got_descriptor;
1009 // Functor class for processing a GOT offset list for local symbols.
1010 // Writes the GOT type and symbol index into the GOT type and descriptor
1011 // arrays in the output section.
1013 template<int size, bool big_endian>
1014 class Local_got_offset_visitor
1016 public:
1017 Local_got_offset_visitor(struct Got_plt_view_info& info)
1018 : info_(info)
1021 void
1022 operator()(unsigned int got_type, unsigned int got_offset)
1024 unsigned int got_index = got_offset / this->got_entry_size_;
1025 gold_assert(got_index < this->info_.got_count);
1026 // We can only handle GOT entry types in the range 0..0x7e
1027 // because we use a byte array to store them, and we use the
1028 // high bit to flag a local symbol.
1029 gold_assert(got_type < 0x7f);
1030 this->info_.got_type_p[got_index] = got_type | 0x80;
1031 unsigned char* pov = this->info_.got_desc_p + got_index * 4;
1032 elfcpp::Swap<32, big_endian>::writeval(pov, this->info_.got_descriptor);
1035 private:
1036 static const unsigned int got_entry_size_ = size / 8;
1037 struct Got_plt_view_info& info_;
1040 // Functor class for processing a GOT offset list. Writes the GOT type
1041 // and symbol index into the GOT type and descriptor arrays in the output
1042 // section.
1044 template<int size, bool big_endian>
1045 class Global_got_offset_visitor
1047 public:
1048 Global_got_offset_visitor(struct Got_plt_view_info& info)
1049 : info_(info)
1052 void
1053 operator()(unsigned int got_type, unsigned int got_offset)
1055 unsigned int got_index = got_offset / this->got_entry_size_;
1056 gold_assert(got_index < this->info_.got_count);
1057 // We can only handle GOT entry types in the range 0..0x7e
1058 // because we use a byte array to store them, and we use the
1059 // high bit to flag a local symbol.
1060 gold_assert(got_type < 0x7f);
1061 this->info_.got_type_p[got_index] = got_type;
1062 unsigned char* pov = this->info_.got_desc_p + got_index * 4;
1063 elfcpp::Swap<32, big_endian>::writeval(pov, this->info_.got_descriptor);
1066 private:
1067 static const unsigned int got_entry_size_ = size / 8;
1068 struct Got_plt_view_info& info_;
1071 // Functor class for processing the global symbol table. Processes the
1072 // GOT offset list for the symbol, and writes the symbol table index
1073 // into the PLT descriptor array in the output section.
1075 template<int size, bool big_endian>
1076 class Global_symbol_visitor_got_plt
1078 public:
1079 Global_symbol_visitor_got_plt(struct Got_plt_view_info& info)
1080 : info_(info)
1083 void
1084 operator()(const Sized_symbol<size>* sym)
1086 typedef Global_got_offset_visitor<size, big_endian> Got_visitor;
1087 const Got_offset_list* got_offsets = sym->got_offset_list();
1088 if (got_offsets != NULL)
1090 info_.got_descriptor = sym->symtab_index();
1091 got_offsets->for_all_got_offsets(Got_visitor(info_));
1093 if (sym->has_plt_offset())
1095 unsigned int plt_index =
1096 ((sym->plt_offset() - this->info_.first_plt_entry_offset)
1097 / this->info_.plt_entry_size);
1098 gold_assert(plt_index < this->info_.plt_count);
1099 unsigned char* pov = this->info_.plt_desc_p + plt_index * 4;
1100 elfcpp::Swap<32, big_endian>::writeval(pov, sym->symtab_index());
1104 private:
1105 struct Got_plt_view_info& info_;
1108 // Write the contents of the .gnu_incremental_got_plt section.
1110 template<int size, bool big_endian>
1111 void
1112 Output_section_incremental_inputs<size, big_endian>::write_got_plt(
1113 unsigned char* pov,
1114 off_t view_size)
1116 Sized_target<size, big_endian>* target =
1117 parameters->sized_target<size, big_endian>();
1119 // Set up the view information for the functors.
1120 struct Got_plt_view_info view_info;
1121 view_info.got_count = target->got_entry_count();
1122 view_info.plt_count = target->plt_entry_count();
1123 view_info.first_plt_entry_offset = target->first_plt_entry_offset();
1124 view_info.plt_entry_size = target->plt_entry_size();
1125 view_info.got_type_p = pov + 8;
1126 view_info.got_desc_p = (view_info.got_type_p
1127 + ((view_info.got_count + 3) & ~3));
1128 view_info.plt_desc_p = view_info.got_desc_p + view_info.got_count * 4;
1130 gold_assert(pov + view_size ==
1131 view_info.plt_desc_p + view_info.plt_count * 4);
1133 // Write the section header.
1134 Swap32::writeval(pov, view_info.got_count);
1135 Swap32::writeval(pov + 4, view_info.plt_count);
1137 // Initialize the GOT type array to 0xff (reserved).
1138 memset(view_info.got_type_p, 0xff, view_info.got_count);
1140 // Write the incremental GOT descriptors for local symbols.
1141 for (Incremental_inputs::Input_list::const_iterator p =
1142 this->inputs_->input_files().begin();
1143 p != this->inputs_->input_files().end();
1144 ++p)
1146 if ((*p)->type() != INCREMENTAL_INPUT_OBJECT
1147 && (*p)->type() != INCREMENTAL_INPUT_ARCHIVE_MEMBER)
1148 continue;
1149 Incremental_object_entry* entry = (*p)->object_entry();
1150 gold_assert(entry != NULL);
1151 const Sized_relobj<size, big_endian>* obj =
1152 static_cast<Sized_relobj<size, big_endian>*>(entry->object());
1153 gold_assert(obj != NULL);
1154 unsigned int nsyms = obj->local_symbol_count();
1155 for (unsigned int i = 0; i < nsyms; i++)
1157 const Got_offset_list* got_offsets = obj->local_got_offset_list(i);
1158 if (got_offsets != NULL)
1160 typedef Local_got_offset_visitor<size, big_endian> Got_visitor;
1161 view_info.got_descriptor = (*p)->get_offset();
1162 got_offsets->for_all_got_offsets(Got_visitor(view_info));
1167 // Write the incremental GOT and PLT descriptors for global symbols.
1168 typedef Global_symbol_visitor_got_plt<size, big_endian> Symbol_visitor;
1169 symtab_->for_all_symbols<size, Symbol_visitor>(Symbol_visitor(view_info));
1172 // Instantiate the templates we need.
1174 #ifdef HAVE_TARGET_32_LITTLE
1175 template
1176 class Sized_incremental_binary<32, false>;
1177 #endif
1179 #ifdef HAVE_TARGET_32_BIG
1180 template
1181 class Sized_incremental_binary<32, true>;
1182 #endif
1184 #ifdef HAVE_TARGET_64_LITTLE
1185 template
1186 class Sized_incremental_binary<64, false>;
1187 #endif
1189 #ifdef HAVE_TARGET_64_BIG
1190 template
1191 class Sized_incremental_binary<64, true>;
1192 #endif
1194 } // End namespace gold.