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.
26 #include "libiberty.h"
32 #include "incremental.h"
36 #include "target-select.h"
43 // Version information. Will change frequently during the development, later
44 // we could think about backward (and forward?) compatibility.
45 const unsigned int INCREMENTAL_LINK_VERSION = 1;
47 // This class manages the .gnu_incremental_inputs section, which holds
48 // the header information, a directory of input files, and separate
49 // entries for each input file.
51 template<int size, bool big_endian>
52 class Output_section_incremental_inputs : public Output_section_data
55 Output_section_incremental_inputs(const Incremental_inputs* inputs,
56 const Symbol_table* symtab)
57 : Output_section_data(size / 8), inputs_(inputs), symtab_(symtab)
61 // This is called to update the section size prior to assigning
62 // the address and file offset.
65 { this->set_final_data_size(); }
67 // Set the final data size.
69 set_final_data_size();
71 // Write the data to the file.
73 do_write(Output_file*);
75 // Write to a map file.
77 do_print_to_mapfile(Mapfile* mapfile) const
78 { mapfile->print_output_data(this, _("** incremental_inputs")); }
81 // Write the section header.
83 write_header(unsigned char* pov, unsigned int input_file_count,
84 section_offset_type command_line_offset);
86 // Write the input file entries.
88 write_input_files(unsigned char* oview, unsigned char* pov,
91 // Write the supplemental information blocks.
93 write_info_blocks(unsigned char* oview, unsigned char* pov,
94 Stringpool* strtab, unsigned int* global_syms,
95 unsigned int global_sym_count);
97 // Write the contents of the .gnu_incremental_symtab section.
99 write_symtab(unsigned char* pov, unsigned int* global_syms,
100 unsigned int global_sym_count);
102 // Write the contents of the .gnu_incremental_got_plt section.
104 write_got_plt(unsigned char* pov, off_t view_size);
106 // Typedefs for writing the data to the output sections.
107 typedef elfcpp::Swap<size, big_endian> Swap;
108 typedef elfcpp::Swap<16, big_endian> Swap16;
109 typedef elfcpp::Swap<32, big_endian> Swap32;
110 typedef elfcpp::Swap<64, big_endian> Swap64;
112 // Sizes of various structures.
113 static const int sizeof_addr = size / 8;
114 static const int header_size = 16;
115 static const int input_entry_size = 24;
117 // The Incremental_inputs object.
118 const Incremental_inputs* inputs_;
121 const Symbol_table* symtab_;
124 // Inform the user why we don't do an incremental link. Not called in
125 // the obvious case of missing output file. TODO: Is this helpful?
128 vexplain_no_incremental(const char* format, va_list args)
131 if (vasprintf(&buf, format, args) < 0)
133 gold_info(_("the link might take longer: "
134 "cannot perform incremental link: %s"), buf);
139 explain_no_incremental(const char* format, ...)
142 va_start(args, format);
143 vexplain_no_incremental(format, args);
150 Incremental_binary::error(const char* format, ...) const
153 va_start(args, format);
154 // Current code only checks if the file can be used for incremental linking,
155 // so errors shouldn't fail the build, but only result in a fallback to a
157 // TODO: when we implement incremental editing of the file, we may need a
158 // flag that will cause errors to be treated seriously.
159 vexplain_no_incremental(format, args);
163 // Find the .gnu_incremental_inputs section and related sections.
165 template<int size, bool big_endian>
167 Sized_incremental_binary<size, big_endian>::find_incremental_inputs_sections(
168 unsigned int* p_inputs_shndx,
169 unsigned int* p_symtab_shndx,
170 unsigned int* p_relocs_shndx,
171 unsigned int* p_got_plt_shndx,
172 unsigned int* p_strtab_shndx)
174 unsigned int inputs_shndx =
175 this->elf_file_.find_section_by_type(elfcpp::SHT_GNU_INCREMENTAL_INPUTS);
176 if (inputs_shndx == elfcpp::SHN_UNDEF) // Not found.
179 unsigned int symtab_shndx =
180 this->elf_file_.find_section_by_type(elfcpp::SHT_GNU_INCREMENTAL_SYMTAB);
181 if (symtab_shndx == elfcpp::SHN_UNDEF) // Not found.
183 if (this->elf_file_.section_link(symtab_shndx) != inputs_shndx)
186 unsigned int relocs_shndx =
187 this->elf_file_.find_section_by_type(elfcpp::SHT_GNU_INCREMENTAL_RELOCS);
188 if (relocs_shndx == elfcpp::SHN_UNDEF) // Not found.
190 if (this->elf_file_.section_link(relocs_shndx) != inputs_shndx)
193 unsigned int got_plt_shndx =
194 this->elf_file_.find_section_by_type(elfcpp::SHT_GNU_INCREMENTAL_GOT_PLT);
195 if (got_plt_shndx == elfcpp::SHN_UNDEF) // Not found.
197 if (this->elf_file_.section_link(got_plt_shndx) != inputs_shndx)
200 unsigned int strtab_shndx = this->elf_file_.section_link(inputs_shndx);
201 if (strtab_shndx == elfcpp::SHN_UNDEF
202 || strtab_shndx > this->elf_file_.shnum()
203 || this->elf_file_.section_type(strtab_shndx) != elfcpp::SHT_STRTAB)
206 if (p_inputs_shndx != NULL)
207 *p_inputs_shndx = inputs_shndx;
208 if (p_symtab_shndx != NULL)
209 *p_symtab_shndx = symtab_shndx;
210 if (p_relocs_shndx != NULL)
211 *p_relocs_shndx = relocs_shndx;
212 if (p_got_plt_shndx != NULL)
213 *p_got_plt_shndx = got_plt_shndx;
214 if (p_strtab_shndx != NULL)
215 *p_strtab_shndx = strtab_shndx;
219 // Set up the readers into the incremental info sections.
221 template<int size, bool big_endian>
223 Sized_incremental_binary<size, big_endian>::setup_readers()
225 unsigned int inputs_shndx;
226 unsigned int symtab_shndx;
227 unsigned int relocs_shndx;
228 unsigned int got_plt_shndx;
229 unsigned int strtab_shndx;
231 if (!this->find_incremental_inputs_sections(&inputs_shndx, &symtab_shndx,
232 &relocs_shndx, &got_plt_shndx,
236 Location inputs_location(this->elf_file_.section_contents(inputs_shndx));
237 Location symtab_location(this->elf_file_.section_contents(symtab_shndx));
238 Location relocs_location(this->elf_file_.section_contents(relocs_shndx));
239 Location got_plt_location(this->elf_file_.section_contents(got_plt_shndx));
240 Location strtab_location(this->elf_file_.section_contents(strtab_shndx));
242 View inputs_view = this->view(inputs_location);
243 View symtab_view = this->view(symtab_location);
244 View relocs_view = this->view(relocs_location);
245 View got_plt_view = this->view(got_plt_location);
246 View strtab_view = this->view(strtab_location);
248 elfcpp::Elf_strtab strtab(strtab_view.data(), strtab_location.data_size);
250 this->inputs_reader_ =
251 Incremental_inputs_reader<size, big_endian>(inputs_view.data(), strtab);
252 this->symtab_reader_ =
253 Incremental_symtab_reader<big_endian>(symtab_view.data(),
254 symtab_location.data_size);
255 this->relocs_reader_ =
256 Incremental_relocs_reader<size, big_endian>(relocs_view.data(),
257 relocs_location.data_size);
258 this->got_plt_reader_ =
259 Incremental_got_plt_reader<big_endian>(got_plt_view.data());
261 // Walk the list of input files (a) to setup an Input_reader for each
262 // input file, and (b) to record maps of files added from archive
263 // libraries and scripts.
264 Incremental_inputs_reader<size, big_endian>& inputs = this->inputs_reader_;
265 unsigned int count = inputs.input_file_count();
266 this->input_entry_readers_.reserve(count);
267 this->library_map_.resize(count);
268 this->script_map_.resize(count);
269 for (unsigned int i = 0; i < count; i++)
271 Input_entry_reader input_file = inputs.input_file(i);
272 this->input_entry_readers_.push_back(Sized_input_reader(input_file));
273 switch (input_file.type())
275 case INCREMENTAL_INPUT_OBJECT:
276 case INCREMENTAL_INPUT_ARCHIVE_MEMBER:
277 case INCREMENTAL_INPUT_SHARED_LIBRARY:
278 // No special treatment necessary.
280 case INCREMENTAL_INPUT_ARCHIVE:
282 Incremental_library* lib =
283 new Incremental_library(input_file.filename(), i,
284 &this->input_entry_readers_[i]);
285 this->library_map_[i] = lib;
286 unsigned int member_count = input_file.get_member_count();
287 for (unsigned int j = 0; j < member_count; j++)
289 int member_offset = input_file.get_member_offset(j);
290 int member_index = inputs.input_file_index(member_offset);
291 this->library_map_[member_index] = lib;
295 case INCREMENTAL_INPUT_SCRIPT:
297 Script_info* script = new Script_info(input_file.filename());
298 this->script_map_[i] = script;
299 unsigned int object_count = input_file.get_object_count();
300 for (unsigned int j = 0; j < object_count; j++)
302 int object_offset = input_file.get_object_offset(j);
303 int object_index = inputs.input_file_index(object_offset);
304 this->script_map_[object_index] = script;
313 this->has_incremental_info_ = true;
316 // Walk the list of input files given on the command line, and build
317 // a direct map of file index to the corresponding input argument.
320 check_input_args(std::vector<const Input_argument*>& input_args_map,
321 Input_arguments::const_iterator begin,
322 Input_arguments::const_iterator end)
324 for (Input_arguments::const_iterator p = begin;
330 const Input_file_group* group = p->group();
331 check_input_args(input_args_map, group->begin(), group->end());
333 else if (p->is_lib())
335 const Input_file_lib* lib = p->lib();
336 check_input_args(input_args_map, lib->begin(), lib->end());
340 gold_assert(p->is_file());
341 unsigned int arg_serial = p->file().arg_serial();
344 gold_assert(arg_serial <= input_args_map.size());
345 gold_assert(input_args_map[arg_serial - 1] == 0);
346 input_args_map[arg_serial - 1] = &*p;
352 // Determine whether an incremental link based on the existing output file
355 template<int size, bool big_endian>
357 Sized_incremental_binary<size, big_endian>::do_check_inputs(
358 const Command_line& cmdline,
359 Incremental_inputs* incremental_inputs)
361 Incremental_inputs_reader<size, big_endian>& inputs = this->inputs_reader_;
363 if (!this->has_incremental_info_)
365 explain_no_incremental(_("no incremental data from previous build"));
369 if (inputs.version() != INCREMENTAL_LINK_VERSION)
371 explain_no_incremental(_("different version of incremental build data"));
375 if (incremental_inputs->command_line() != inputs.command_line())
377 explain_no_incremental(_("command line changed"));
381 // Walk the list of input files given on the command line, and build
382 // a direct map of argument serial numbers to the corresponding input
384 this->input_args_map_.resize(cmdline.number_of_input_files());
385 check_input_args(this->input_args_map_, cmdline.begin(), cmdline.end());
387 // Walk the list of input files to check for conditions that prevent
388 // an incremental update link.
389 unsigned int count = inputs.input_file_count();
390 for (unsigned int i = 0; i < count; i++)
392 Input_entry_reader input_file = inputs.input_file(i);
393 switch (input_file.type())
395 case INCREMENTAL_INPUT_OBJECT:
396 case INCREMENTAL_INPUT_ARCHIVE_MEMBER:
397 case INCREMENTAL_INPUT_SHARED_LIBRARY:
398 case INCREMENTAL_INPUT_ARCHIVE:
399 // No special treatment necessary.
401 case INCREMENTAL_INPUT_SCRIPT:
402 if (this->do_file_has_changed(i))
404 explain_no_incremental(_("%s: script file changed"),
405 input_file.filename());
417 // Return TRUE if input file N has changed since the last incremental link.
419 template<int size, bool big_endian>
421 Sized_incremental_binary<size, big_endian>::do_file_has_changed(
422 unsigned int n) const
424 Input_entry_reader input_file = this->inputs_reader_.input_file(n);
425 Incremental_disposition disp = INCREMENTAL_CHECK;
426 const Input_argument* input_argument = this->get_input_argument(n);
427 if (input_argument != NULL)
428 disp = input_argument->file().options().incremental_disposition();
430 if (disp != INCREMENTAL_CHECK)
431 return disp == INCREMENTAL_CHANGED;
433 const char* filename = input_file.filename();
434 Timespec old_mtime = input_file.get_mtime();
436 if (!get_mtime(filename, &new_mtime))
438 // If we can't open get the current modification time, assume it has
439 // changed. If the file doesn't exist, we'll issue an error when we
440 // try to open it later.
444 if (new_mtime.seconds > old_mtime.seconds)
446 if (new_mtime.seconds == old_mtime.seconds
447 && new_mtime.nanoseconds > old_mtime.nanoseconds)
452 // Initialize the layout of the output file based on the existing
455 template<int size, bool big_endian>
457 Sized_incremental_binary<size, big_endian>::do_init_layout(Layout* layout)
459 typedef elfcpp::Shdr<size, big_endian> Shdr;
460 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
462 // Get views of the section headers and the section string table.
463 const off_t shoff = this->elf_file_.shoff();
464 const unsigned int shnum = this->elf_file_.shnum();
465 const unsigned int shstrndx = this->elf_file_.shstrndx();
466 Location shdrs_location(shoff, shnum * shdr_size);
467 Location shstrndx_location(this->elf_file_.section_contents(shstrndx));
468 View shdrs_view = this->view(shdrs_location);
469 View shstrndx_view = this->view(shstrndx_location);
470 elfcpp::Elf_strtab shstrtab(shstrndx_view.data(),
471 shstrndx_location.data_size);
473 layout->set_incremental_base(this);
475 // Initialize the layout.
476 this->section_map_.resize(shnum);
477 const unsigned char* pshdr = shdrs_view.data() + shdr_size;
478 for (unsigned int i = 1; i < shnum; i++)
482 if (!shstrtab.get_c_string(shdr.get_sh_name(), &name))
484 gold_debug(DEBUG_INCREMENTAL,
485 "Output section: %2d %08lx %08lx %08lx %3d %s",
487 static_cast<long>(shdr.get_sh_addr()),
488 static_cast<long>(shdr.get_sh_offset()),
489 static_cast<long>(shdr.get_sh_size()),
490 shdr.get_sh_type(), name ? name : "<null>");
491 this->section_map_[i] = layout->init_fixed_output_section(name, shdr);
496 // Mark regions of the input file that must be kept unchanged.
498 template<int size, bool big_endian>
500 Sized_incremental_binary<size, big_endian>::do_reserve_layout(
501 unsigned int input_file_index)
503 Input_entry_reader input_file =
504 this->inputs_reader_.input_file(input_file_index);
506 if (input_file.type() == INCREMENTAL_INPUT_SHARED_LIBRARY)
509 unsigned int shnum = input_file.get_input_section_count();
510 for (unsigned int i = 0; i < shnum; i++)
512 typename Input_entry_reader::Input_section_info sect =
513 input_file.get_input_section(i);
514 if (sect.output_shndx == 0 || sect.sh_offset == -1)
516 Output_section* os = this->section_map_[sect.output_shndx];
517 gold_assert(os != NULL);
518 os->reserve(sect.sh_offset, sect.sh_size);
522 // Get a view of the main symbol table and the symbol string table.
524 template<int size, bool big_endian>
526 Sized_incremental_binary<size, big_endian>::get_symtab_view(
529 elfcpp::Elf_strtab* strtab)
531 unsigned int symtab_shndx =
532 this->elf_file_.find_section_by_type(elfcpp::SHT_SYMTAB);
533 gold_assert(symtab_shndx != elfcpp::SHN_UNDEF);
534 Location symtab_location(this->elf_file_.section_contents(symtab_shndx));
535 *symtab_view = this->view(symtab_location);
536 *nsyms = symtab_location.data_size / elfcpp::Elf_sizes<size>::sym_size;
538 unsigned int strtab_shndx = this->elf_file_.section_link(symtab_shndx);
539 gold_assert(strtab_shndx != elfcpp::SHN_UNDEF
540 && strtab_shndx < this->elf_file_.shnum());
542 Location strtab_location(this->elf_file_.section_contents(strtab_shndx));
543 View strtab_view(this->view(strtab_location));
544 *strtab = elfcpp::Elf_strtab(strtab_view.data(), strtab_location.data_size);
550 // Create a Sized_incremental_binary object of the specified size and
551 // endianness. Fails if the target architecture is not supported.
553 template<int size, bool big_endian>
555 make_sized_incremental_binary(Output_file* file,
556 const elfcpp::Ehdr<size, big_endian>& ehdr)
558 Target* target = select_target(ehdr.get_e_machine(), size, big_endian,
559 ehdr.get_e_ident()[elfcpp::EI_OSABI],
560 ehdr.get_e_ident()[elfcpp::EI_ABIVERSION]);
563 explain_no_incremental(_("unsupported ELF machine number %d"),
564 ehdr.get_e_machine());
568 if (!parameters->target_valid())
569 set_parameters_target(target);
570 else if (target != ¶meters->target())
571 gold_error(_("%s: incompatible target"), file->filename());
573 return new Sized_incremental_binary<size, big_endian>(file, ehdr, target);
576 } // End of anonymous namespace.
578 // Create an Incremental_binary object for FILE. Returns NULL is this is not
579 // possible, e.g. FILE is not an ELF file or has an unsupported target. FILE
583 open_incremental_binary(Output_file* file)
585 off_t filesize = file->filesize();
586 int want = elfcpp::Elf_recognizer::max_header_size;
590 const unsigned char* p = file->get_input_view(0, want);
591 if (!elfcpp::Elf_recognizer::is_elf_file(p, want))
593 explain_no_incremental(_("output is not an ELF file."));
598 bool big_endian = false;
600 if (!elfcpp::Elf_recognizer::is_valid_header(p, want, &size, &big_endian,
603 explain_no_incremental(error.c_str());
607 Incremental_binary* result = NULL;
612 #ifdef HAVE_TARGET_32_BIG
613 result = make_sized_incremental_binary<32, true>(
614 file, elfcpp::Ehdr<32, true>(p));
616 explain_no_incremental(_("unsupported file: 32-bit, big-endian"));
621 #ifdef HAVE_TARGET_32_LITTLE
622 result = make_sized_incremental_binary<32, false>(
623 file, elfcpp::Ehdr<32, false>(p));
625 explain_no_incremental(_("unsupported file: 32-bit, little-endian"));
633 #ifdef HAVE_TARGET_64_BIG
634 result = make_sized_incremental_binary<64, true>(
635 file, elfcpp::Ehdr<64, true>(p));
637 explain_no_incremental(_("unsupported file: 64-bit, big-endian"));
642 #ifdef HAVE_TARGET_64_LITTLE
643 result = make_sized_incremental_binary<64, false>(
644 file, elfcpp::Ehdr<64, false>(p));
646 explain_no_incremental(_("unsupported file: 64-bit, little-endian"));
656 // Class Incremental_inputs.
658 // Add the command line to the string table, setting
659 // command_line_key_. In incremental builds, the command line is
660 // stored in .gnu_incremental_inputs so that the next linker run can
661 // check if the command line options didn't change.
664 Incremental_inputs::report_command_line(int argc, const char* const* argv)
666 // Always store 'gold' as argv[0] to avoid a full relink if the user used a
667 // different path to the linker.
668 std::string args("gold");
669 // Copied from collect_argv in main.cc.
670 for (int i = 1; i < argc; ++i)
672 // Adding/removing these options should not result in a full relink.
673 if (strcmp(argv[i], "--incremental") == 0
674 || strcmp(argv[i], "--incremental-full") == 0
675 || strcmp(argv[i], "--incremental-update") == 0
676 || strcmp(argv[i], "--incremental-changed") == 0
677 || strcmp(argv[i], "--incremental-unchanged") == 0
678 || strcmp(argv[i], "--incremental-unknown") == 0
679 || is_prefix_of("--debug=", argv[i]))
683 // Now append argv[i], but with all single-quotes escaped
684 const char* argpos = argv[i];
687 const int len = strcspn(argpos, "'");
688 args.append(argpos, len);
689 if (argpos[len] == '\0')
691 args.append("'\"'\"'");
697 this->command_line_ = args;
698 this->strtab_->add(this->command_line_.c_str(), false,
699 &this->command_line_key_);
702 // Record the input archive file ARCHIVE. This is called by the
703 // Add_archive_symbols task before determining which archive members
704 // to include. We create the Incremental_archive_entry here and
705 // attach it to the Archive, but we do not add it to the list of
706 // input objects until report_archive_end is called.
709 Incremental_inputs::report_archive_begin(Library_base* arch,
710 unsigned int arg_serial,
711 Script_info* script_info)
713 Stringpool::Key filename_key;
714 Timespec mtime = arch->get_mtime();
716 // For a file loaded from a script, don't record its argument serial number.
717 if (script_info != NULL)
720 this->strtab_->add(arch->filename().c_str(), false, &filename_key);
721 Incremental_archive_entry* entry =
722 new Incremental_archive_entry(filename_key, arg_serial, mtime);
723 arch->set_incremental_info(entry);
725 if (script_info != NULL)
727 Incremental_script_entry* script_entry = script_info->incremental_info();
728 gold_assert(script_entry != NULL);
729 script_entry->add_object(entry);
733 // Visitor class for processing the unused global symbols in a library.
734 // An instance of this class is passed to the library's
735 // for_all_unused_symbols() iterator, which will call the visit()
736 // function for each global symbol defined in each unused library
737 // member. We add those symbol names to the incremental info for the
740 class Unused_symbol_visitor : public Library_base::Symbol_visitor_base
743 Unused_symbol_visitor(Incremental_archive_entry* entry, Stringpool* strtab)
744 : entry_(entry), strtab_(strtab)
748 visit(const char* sym)
750 Stringpool::Key symbol_key;
751 this->strtab_->add(sym, true, &symbol_key);
752 this->entry_->add_unused_global_symbol(symbol_key);
756 Incremental_archive_entry* entry_;
760 // Finish recording the input archive file ARCHIVE. This is called by the
761 // Add_archive_symbols task after determining which archive members
765 Incremental_inputs::report_archive_end(Library_base* arch)
767 Incremental_archive_entry* entry = arch->incremental_info();
769 gold_assert(entry != NULL);
770 this->inputs_.push_back(entry);
772 // Collect unused global symbols.
773 Unused_symbol_visitor v(entry, this->strtab_);
774 arch->for_all_unused_symbols(&v);
777 // Record the input object file OBJ. If ARCH is not NULL, attach
778 // the object file to the archive. This is called by the
779 // Add_symbols task after finding out the type of the file.
782 Incremental_inputs::report_object(Object* obj, unsigned int arg_serial,
783 Library_base* arch, Script_info* script_info)
785 Stringpool::Key filename_key;
786 Timespec mtime = obj->get_mtime();
788 // For a file loaded from a script, don't record its argument serial number.
789 if (script_info != NULL)
792 this->strtab_->add(obj->name().c_str(), false, &filename_key);
793 Incremental_object_entry* obj_entry =
794 new Incremental_object_entry(filename_key, obj, arg_serial, mtime);
795 if (obj->is_in_system_directory())
796 obj_entry->set_is_in_system_directory();
797 this->inputs_.push_back(obj_entry);
801 Incremental_archive_entry* arch_entry = arch->incremental_info();
802 gold_assert(arch_entry != NULL);
803 arch_entry->add_object(obj_entry);
806 if (script_info != NULL)
808 Incremental_script_entry* script_entry = script_info->incremental_info();
809 gold_assert(script_entry != NULL);
810 script_entry->add_object(obj_entry);
813 this->current_object_ = obj;
814 this->current_object_entry_ = obj_entry;
817 // Record the input object file OBJ. If ARCH is not NULL, attach
818 // the object file to the archive. This is called by the
819 // Add_symbols task after finding out the type of the file.
822 Incremental_inputs::report_input_section(Object* obj, unsigned int shndx,
823 const char* name, off_t sh_size)
825 Stringpool::Key key = 0;
828 this->strtab_->add(name, true, &key);
830 gold_assert(obj == this->current_object_);
831 this->current_object_entry_->add_input_section(shndx, key, sh_size);
834 // Record that the input argument INPUT is a script SCRIPT. This is
835 // called by read_script after parsing the script and reading the list
836 // of inputs added by this script.
839 Incremental_inputs::report_script(Script_info* script,
840 unsigned int arg_serial,
843 Stringpool::Key filename_key;
845 this->strtab_->add(script->filename().c_str(), false, &filename_key);
846 Incremental_script_entry* entry =
847 new Incremental_script_entry(filename_key, arg_serial, script, mtime);
848 this->inputs_.push_back(entry);
849 script->set_incremental_info(entry);
852 // Finalize the incremental link information. Called from
856 Incremental_inputs::finalize()
858 // Finalize the string table.
859 this->strtab_->set_string_offsets();
862 // Create the .gnu_incremental_inputs, _symtab, and _relocs input sections.
865 Incremental_inputs::create_data_sections(Symbol_table* symtab)
867 switch (parameters->size_and_endianness())
869 #ifdef HAVE_TARGET_32_LITTLE
870 case Parameters::TARGET_32_LITTLE:
871 this->inputs_section_ =
872 new Output_section_incremental_inputs<32, false>(this, symtab);
875 #ifdef HAVE_TARGET_32_BIG
876 case Parameters::TARGET_32_BIG:
877 this->inputs_section_ =
878 new Output_section_incremental_inputs<32, true>(this, symtab);
881 #ifdef HAVE_TARGET_64_LITTLE
882 case Parameters::TARGET_64_LITTLE:
883 this->inputs_section_ =
884 new Output_section_incremental_inputs<64, false>(this, symtab);
887 #ifdef HAVE_TARGET_64_BIG
888 case Parameters::TARGET_64_BIG:
889 this->inputs_section_ =
890 new Output_section_incremental_inputs<64, true>(this, symtab);
896 this->symtab_section_ = new Output_data_space(4, "** incremental_symtab");
897 this->relocs_section_ = new Output_data_space(4, "** incremental_relocs");
898 this->got_plt_section_ = new Output_data_space(4, "** incremental_got_plt");
901 // Return the sh_entsize value for the .gnu_incremental_relocs section.
903 Incremental_inputs::relocs_entsize() const
905 return 8 + 2 * parameters->target().get_size() / 8;
908 // Class Output_section_incremental_inputs.
910 // Finalize the offsets for each input section and supplemental info block,
911 // and set the final data size of the incremental output sections.
913 template<int size, bool big_endian>
915 Output_section_incremental_inputs<size, big_endian>::set_final_data_size()
917 const Incremental_inputs* inputs = this->inputs_;
918 const unsigned int sizeof_addr = size / 8;
919 const unsigned int rel_size = 8 + 2 * sizeof_addr;
921 // Offset of each input entry.
922 unsigned int input_offset = this->header_size;
924 // Offset of each supplemental info block.
925 unsigned int info_offset = this->header_size;
926 info_offset += this->input_entry_size * inputs->input_file_count();
928 // Count each input file and its supplemental information block.
929 for (Incremental_inputs::Input_list::const_iterator p =
930 inputs->input_files().begin();
931 p != inputs->input_files().end();
934 // Set the offset of the input file entry.
935 (*p)->set_offset(input_offset);
936 input_offset += this->input_entry_size;
938 // Set the offset of the supplemental info block.
939 switch ((*p)->type())
941 case INCREMENTAL_INPUT_SCRIPT:
943 Incremental_script_entry *entry = (*p)->script_entry();
944 gold_assert(entry != NULL);
945 (*p)->set_info_offset(info_offset);
949 info_offset += (entry->get_object_count() * 4);
952 case INCREMENTAL_INPUT_OBJECT:
953 case INCREMENTAL_INPUT_ARCHIVE_MEMBER:
955 Incremental_object_entry* entry = (*p)->object_entry();
956 gold_assert(entry != NULL);
957 (*p)->set_info_offset(info_offset);
958 // Input section count + global symbol count.
960 // Each input section.
961 info_offset += (entry->get_input_section_count()
962 * (8 + 2 * sizeof_addr));
963 // Each global symbol.
964 const Object::Symbols* syms = entry->object()->get_global_symbols();
965 info_offset += syms->size() * 20;
968 case INCREMENTAL_INPUT_SHARED_LIBRARY:
970 Incremental_object_entry* entry = (*p)->object_entry();
971 gold_assert(entry != NULL);
972 (*p)->set_info_offset(info_offset);
973 // Global symbol count.
975 // Each global symbol.
976 const Object::Symbols* syms = entry->object()->get_global_symbols();
977 gold_assert(syms != NULL);
978 unsigned int nsyms = syms->size();
979 unsigned int nsyms_out = 0;
980 for (unsigned int i = 0; i < nsyms; ++i)
982 const Symbol* sym = (*syms)[i];
985 if (sym->is_forwarder())
986 sym = this->symtab_->resolve_forwards(sym);
987 if (sym->symtab_index() != -1U)
990 info_offset += nsyms_out * 4;
993 case INCREMENTAL_INPUT_ARCHIVE:
995 Incremental_archive_entry* entry = (*p)->archive_entry();
996 gold_assert(entry != NULL);
997 (*p)->set_info_offset(info_offset);
998 // Member count + unused global symbol count.
1001 info_offset += (entry->get_member_count() * 4);
1002 // Each global symbol.
1003 info_offset += (entry->get_unused_global_symbol_count() * 4);
1011 this->set_data_size(info_offset);
1013 // Set the size of the .gnu_incremental_symtab section.
1014 inputs->symtab_section()->set_current_data_size(this->symtab_->output_count()
1015 * sizeof(unsigned int));
1017 // Set the size of the .gnu_incremental_relocs section.
1018 inputs->relocs_section()->set_current_data_size(inputs->get_reloc_count()
1021 // Set the size of the .gnu_incremental_got_plt section.
1022 Sized_target<size, big_endian>* target =
1023 parameters->sized_target<size, big_endian>();
1024 unsigned int got_count = target->got_entry_count();
1025 unsigned int plt_count = target->plt_entry_count();
1026 unsigned int got_plt_size = 8; // GOT entry count, PLT entry count.
1027 got_plt_size = (got_plt_size + got_count + 3) & ~3; // GOT type array.
1028 got_plt_size += got_count * 4 + plt_count * 4; // GOT array, PLT array.
1029 inputs->got_plt_section()->set_current_data_size(got_plt_size);
1032 // Write the contents of the .gnu_incremental_inputs and
1033 // .gnu_incremental_symtab sections.
1035 template<int size, bool big_endian>
1037 Output_section_incremental_inputs<size, big_endian>::do_write(Output_file* of)
1039 const Incremental_inputs* inputs = this->inputs_;
1040 Stringpool* strtab = inputs->get_stringpool();
1042 // Get a view into the .gnu_incremental_inputs section.
1043 const off_t off = this->offset();
1044 const off_t oview_size = this->data_size();
1045 unsigned char* const oview = of->get_output_view(off, oview_size);
1046 unsigned char* pov = oview;
1048 // Get a view into the .gnu_incremental_symtab section.
1049 const off_t symtab_off = inputs->symtab_section()->offset();
1050 const off_t symtab_size = inputs->symtab_section()->data_size();
1051 unsigned char* const symtab_view = of->get_output_view(symtab_off,
1054 // Allocate an array of linked list heads for the .gnu_incremental_symtab
1055 // section. Each element corresponds to a global symbol in the output
1056 // symbol table, and points to the head of the linked list that threads
1057 // through the object file input entries. The value of each element
1058 // is the section-relative offset to a global symbol entry in a
1059 // supplemental information block.
1060 unsigned int global_sym_count = this->symtab_->output_count();
1061 unsigned int* global_syms = new unsigned int[global_sym_count];
1062 memset(global_syms, 0, global_sym_count * sizeof(unsigned int));
1064 // Write the section header.
1065 Stringpool::Key command_line_key = inputs->command_line_key();
1066 pov = this->write_header(pov, inputs->input_file_count(),
1067 strtab->get_offset_from_key(command_line_key));
1069 // Write the list of input files.
1070 pov = this->write_input_files(oview, pov, strtab);
1072 // Write the supplemental information blocks for each input file.
1073 pov = this->write_info_blocks(oview, pov, strtab, global_syms,
1076 gold_assert(pov - oview == oview_size);
1078 // Write the .gnu_incremental_symtab section.
1079 gold_assert(global_sym_count * 4 == symtab_size);
1080 this->write_symtab(symtab_view, global_syms, global_sym_count);
1082 delete[] global_syms;
1084 // Write the .gnu_incremental_got_plt section.
1085 const off_t got_plt_off = inputs->got_plt_section()->offset();
1086 const off_t got_plt_size = inputs->got_plt_section()->data_size();
1087 unsigned char* const got_plt_view = of->get_output_view(got_plt_off,
1089 this->write_got_plt(got_plt_view, got_plt_size);
1091 of->write_output_view(off, oview_size, oview);
1092 of->write_output_view(symtab_off, symtab_size, symtab_view);
1093 of->write_output_view(got_plt_off, got_plt_size, got_plt_view);
1096 // Write the section header: version, input file count, offset of command line
1097 // in the string table, and 4 bytes of padding.
1099 template<int size, bool big_endian>
1101 Output_section_incremental_inputs<size, big_endian>::write_header(
1103 unsigned int input_file_count,
1104 section_offset_type command_line_offset)
1106 Swap32::writeval(pov, INCREMENTAL_LINK_VERSION);
1107 Swap32::writeval(pov + 4, input_file_count);
1108 Swap32::writeval(pov + 8, command_line_offset);
1109 Swap32::writeval(pov + 12, 0);
1110 return pov + this->header_size;
1113 // Write the input file entries.
1115 template<int size, bool big_endian>
1117 Output_section_incremental_inputs<size, big_endian>::write_input_files(
1118 unsigned char* oview,
1122 const Incremental_inputs* inputs = this->inputs_;
1124 for (Incremental_inputs::Input_list::const_iterator p =
1125 inputs->input_files().begin();
1126 p != inputs->input_files().end();
1129 gold_assert(static_cast<unsigned int>(pov - oview) == (*p)->get_offset());
1130 section_offset_type filename_offset =
1131 strtab->get_offset_from_key((*p)->get_filename_key());
1132 const Timespec& mtime = (*p)->get_mtime();
1133 unsigned int flags = (*p)->type();
1134 if ((*p)->is_in_system_directory())
1135 flags |= INCREMENTAL_INPUT_IN_SYSTEM_DIR;
1136 Swap32::writeval(pov, filename_offset);
1137 Swap32::writeval(pov + 4, (*p)->get_info_offset());
1138 Swap64::writeval(pov + 8, mtime.seconds);
1139 Swap32::writeval(pov + 16, mtime.nanoseconds);
1140 Swap16::writeval(pov + 20, flags);
1141 Swap16::writeval(pov + 22, (*p)->arg_serial());
1142 pov += this->input_entry_size;
1147 // Write the supplemental information blocks.
1149 template<int size, bool big_endian>
1151 Output_section_incremental_inputs<size, big_endian>::write_info_blocks(
1152 unsigned char* oview,
1155 unsigned int* global_syms,
1156 unsigned int global_sym_count)
1158 const Incremental_inputs* inputs = this->inputs_;
1159 unsigned int first_global_index = this->symtab_->first_global_index();
1161 for (Incremental_inputs::Input_list::const_iterator p =
1162 inputs->input_files().begin();
1163 p != inputs->input_files().end();
1166 switch ((*p)->type())
1168 case INCREMENTAL_INPUT_SCRIPT:
1170 gold_assert(static_cast<unsigned int>(pov - oview)
1171 == (*p)->get_info_offset());
1172 Incremental_script_entry* entry = (*p)->script_entry();
1173 gold_assert(entry != NULL);
1175 // Write the object count.
1176 unsigned int nobjects = entry->get_object_count();
1177 Swap32::writeval(pov, nobjects);
1180 // For each object, write the offset to its input file entry.
1181 for (unsigned int i = 0; i < nobjects; ++i)
1183 Incremental_input_entry* obj = entry->get_object(i);
1184 Swap32::writeval(pov, obj->get_offset());
1190 case INCREMENTAL_INPUT_OBJECT:
1191 case INCREMENTAL_INPUT_ARCHIVE_MEMBER:
1193 gold_assert(static_cast<unsigned int>(pov - oview)
1194 == (*p)->get_info_offset());
1195 Incremental_object_entry* entry = (*p)->object_entry();
1196 gold_assert(entry != NULL);
1197 const Object* obj = entry->object();
1198 const Object::Symbols* syms = obj->get_global_symbols();
1199 // Write the input section count and global symbol count.
1200 unsigned int nsections = entry->get_input_section_count();
1201 unsigned int nsyms = syms->size();
1202 Swap32::writeval(pov, nsections);
1203 Swap32::writeval(pov + 4, nsyms);
1206 // Build a temporary array to map input section indexes
1207 // from the original object file index to the index in the
1208 // incremental info table.
1209 unsigned int* index_map = new unsigned int[obj->shnum()];
1210 memset(index_map, 0, obj->shnum() * sizeof(unsigned int));
1212 // For each input section, write the name, output section index,
1213 // offset within output section, and input section size.
1214 for (unsigned int i = 0; i < nsections; i++)
1216 unsigned int shndx = entry->get_input_section_index(i);
1217 index_map[shndx] = i + 1;
1218 Stringpool::Key key = entry->get_input_section_name_key(i);
1219 off_t name_offset = 0;
1221 name_offset = strtab->get_offset_from_key(key);
1223 off_t out_offset = 0;
1225 Output_section* os = obj->output_section(shndx);
1228 out_shndx = os->out_shndx();
1229 out_offset = obj->output_section_offset(shndx);
1230 sh_size = entry->get_input_section_size(i);
1232 Swap32::writeval(pov, name_offset);
1233 Swap32::writeval(pov + 4, out_shndx);
1234 Swap::writeval(pov + 8, out_offset);
1235 Swap::writeval(pov + 8 + sizeof_addr, sh_size);
1236 pov += 8 + 2 * sizeof_addr;
1239 // For each global symbol, write its associated relocations,
1240 // add it to the linked list of globals, then write the
1241 // supplemental information: global symbol table index,
1242 // input section index, linked list chain pointer, relocation
1243 // count, and offset to the relocations.
1244 for (unsigned int i = 0; i < nsyms; i++)
1246 const Symbol* sym = (*syms)[i];
1247 if (sym->is_forwarder())
1248 sym = this->symtab_->resolve_forwards(sym);
1249 unsigned int shndx = 0;
1250 if (sym->source() == Symbol::FROM_OBJECT
1251 && sym->object() == obj
1252 && sym->is_defined())
1255 unsigned int orig_shndx = sym->shndx(&is_ordinary);
1257 shndx = index_map[orig_shndx];
1259 unsigned int symtab_index = sym->symtab_index();
1260 unsigned int chain = 0;
1261 unsigned int first_reloc = 0;
1262 unsigned int nrelocs = obj->get_incremental_reloc_count(i);
1265 gold_assert(symtab_index != -1U
1266 && (symtab_index - first_global_index
1267 < global_sym_count));
1268 first_reloc = obj->get_incremental_reloc_base(i);
1269 chain = global_syms[symtab_index - first_global_index];
1270 global_syms[symtab_index - first_global_index] =
1273 Swap32::writeval(pov, symtab_index);
1274 Swap32::writeval(pov + 4, shndx);
1275 Swap32::writeval(pov + 8, chain);
1276 Swap32::writeval(pov + 12, nrelocs);
1277 Swap32::writeval(pov + 16, first_reloc * 3 * sizeof_addr);
1285 case INCREMENTAL_INPUT_SHARED_LIBRARY:
1287 gold_assert(static_cast<unsigned int>(pov - oview)
1288 == (*p)->get_info_offset());
1289 Incremental_object_entry* entry = (*p)->object_entry();
1290 gold_assert(entry != NULL);
1291 const Object* obj = entry->object();
1292 const Object::Symbols* syms = obj->get_global_symbols();
1294 // Skip the global symbol count for now.
1295 unsigned char* orig_pov = pov;
1298 // For each global symbol, write the global symbol table index.
1299 unsigned int nsyms = syms->size();
1300 unsigned int nsyms_out = 0;
1301 for (unsigned int i = 0; i < nsyms; i++)
1303 const Symbol* sym = (*syms)[i];
1306 if (sym->is_forwarder())
1307 sym = this->symtab_->resolve_forwards(sym);
1308 if (sym->symtab_index() == -1U)
1310 unsigned int def_flag = 0;
1311 if (sym->source() == Symbol::FROM_OBJECT
1312 && sym->object() == obj
1313 && sym->is_defined())
1314 def_flag = 1U << 31;
1315 Swap32::writeval(pov, sym->symtab_index() | def_flag);
1320 // Now write the global symbol count.
1321 Swap32::writeval(orig_pov, nsyms_out);
1325 case INCREMENTAL_INPUT_ARCHIVE:
1327 gold_assert(static_cast<unsigned int>(pov - oview)
1328 == (*p)->get_info_offset());
1329 Incremental_archive_entry* entry = (*p)->archive_entry();
1330 gold_assert(entry != NULL);
1332 // Write the member count and unused global symbol count.
1333 unsigned int nmembers = entry->get_member_count();
1334 unsigned int nsyms = entry->get_unused_global_symbol_count();
1335 Swap32::writeval(pov, nmembers);
1336 Swap32::writeval(pov + 4, nsyms);
1339 // For each member, write the offset to its input file entry.
1340 for (unsigned int i = 0; i < nmembers; ++i)
1342 Incremental_object_entry* member = entry->get_member(i);
1343 Swap32::writeval(pov, member->get_offset());
1347 // For each global symbol, write the name offset.
1348 for (unsigned int i = 0; i < nsyms; ++i)
1350 Stringpool::Key key = entry->get_unused_global_symbol(i);
1351 Swap32::writeval(pov, strtab->get_offset_from_key(key));
1364 // Write the contents of the .gnu_incremental_symtab section.
1366 template<int size, bool big_endian>
1368 Output_section_incremental_inputs<size, big_endian>::write_symtab(
1370 unsigned int* global_syms,
1371 unsigned int global_sym_count)
1373 for (unsigned int i = 0; i < global_sym_count; ++i)
1375 Swap32::writeval(pov, global_syms[i]);
1380 // This struct holds the view information needed to write the
1381 // .gnu_incremental_got_plt section.
1383 struct Got_plt_view_info
1385 // Start of the GOT type array in the output view.
1386 unsigned char* got_type_p;
1387 // Start of the GOT descriptor array in the output view.
1388 unsigned char* got_desc_p;
1389 // Start of the PLT descriptor array in the output view.
1390 unsigned char* plt_desc_p;
1391 // Number of GOT entries.
1392 unsigned int got_count;
1393 // Number of PLT entries.
1394 unsigned int plt_count;
1395 // Offset of the first non-reserved PLT entry (this is a target-dependent value).
1396 unsigned int first_plt_entry_offset;
1397 // Size of a PLT entry (this is a target-dependent value).
1398 unsigned int plt_entry_size;
1399 // Value to write in the GOT descriptor array. For global symbols,
1400 // this is the global symbol table index; for local symbols, it is
1401 // the offset of the input file entry in the .gnu_incremental_inputs
1403 unsigned int got_descriptor;
1406 // Functor class for processing a GOT offset list for local symbols.
1407 // Writes the GOT type and symbol index into the GOT type and descriptor
1408 // arrays in the output section.
1410 template<int size, bool big_endian>
1411 class Local_got_offset_visitor : public Got_offset_list::Visitor
1414 Local_got_offset_visitor(struct Got_plt_view_info& info)
1419 visit(unsigned int got_type, unsigned int got_offset)
1421 unsigned int got_index = got_offset / this->got_entry_size_;
1422 gold_assert(got_index < this->info_.got_count);
1423 // We can only handle GOT entry types in the range 0..0x7e
1424 // because we use a byte array to store them, and we use the
1425 // high bit to flag a local symbol.
1426 gold_assert(got_type < 0x7f);
1427 this->info_.got_type_p[got_index] = got_type | 0x80;
1428 unsigned char* pov = this->info_.got_desc_p + got_index * 4;
1429 elfcpp::Swap<32, big_endian>::writeval(pov, this->info_.got_descriptor);
1433 static const unsigned int got_entry_size_ = size / 8;
1434 struct Got_plt_view_info& info_;
1437 // Functor class for processing a GOT offset list. Writes the GOT type
1438 // and symbol index into the GOT type and descriptor arrays in the output
1441 template<int size, bool big_endian>
1442 class Global_got_offset_visitor : public Got_offset_list::Visitor
1445 Global_got_offset_visitor(struct Got_plt_view_info& info)
1450 visit(unsigned int got_type, unsigned int got_offset)
1452 unsigned int got_index = got_offset / this->got_entry_size_;
1453 gold_assert(got_index < this->info_.got_count);
1454 // We can only handle GOT entry types in the range 0..0x7e
1455 // because we use a byte array to store them, and we use the
1456 // high bit to flag a local symbol.
1457 gold_assert(got_type < 0x7f);
1458 this->info_.got_type_p[got_index] = got_type;
1459 unsigned char* pov = this->info_.got_desc_p + got_index * 4;
1460 elfcpp::Swap<32, big_endian>::writeval(pov, this->info_.got_descriptor);
1464 static const unsigned int got_entry_size_ = size / 8;
1465 struct Got_plt_view_info& info_;
1468 // Functor class for processing the global symbol table. Processes the
1469 // GOT offset list for the symbol, and writes the symbol table index
1470 // into the PLT descriptor array in the output section.
1472 template<int size, bool big_endian>
1473 class Global_symbol_visitor_got_plt
1476 Global_symbol_visitor_got_plt(struct Got_plt_view_info& info)
1481 operator()(const Sized_symbol<size>* sym)
1483 typedef Global_got_offset_visitor<size, big_endian> Got_visitor;
1484 const Got_offset_list* got_offsets = sym->got_offset_list();
1485 if (got_offsets != NULL)
1487 this->info_.got_descriptor = sym->symtab_index();
1488 Got_visitor v(this->info_);
1489 got_offsets->for_all_got_offsets(&v);
1491 if (sym->has_plt_offset())
1493 unsigned int plt_index =
1494 ((sym->plt_offset() - this->info_.first_plt_entry_offset)
1495 / this->info_.plt_entry_size);
1496 gold_assert(plt_index < this->info_.plt_count);
1497 unsigned char* pov = this->info_.plt_desc_p + plt_index * 4;
1498 elfcpp::Swap<32, big_endian>::writeval(pov, sym->symtab_index());
1503 struct Got_plt_view_info& info_;
1506 // Write the contents of the .gnu_incremental_got_plt section.
1508 template<int size, bool big_endian>
1510 Output_section_incremental_inputs<size, big_endian>::write_got_plt(
1514 Sized_target<size, big_endian>* target =
1515 parameters->sized_target<size, big_endian>();
1517 // Set up the view information for the functors.
1518 struct Got_plt_view_info view_info;
1519 view_info.got_count = target->got_entry_count();
1520 view_info.plt_count = target->plt_entry_count();
1521 view_info.first_plt_entry_offset = target->first_plt_entry_offset();
1522 view_info.plt_entry_size = target->plt_entry_size();
1523 view_info.got_type_p = pov + 8;
1524 view_info.got_desc_p = (view_info.got_type_p
1525 + ((view_info.got_count + 3) & ~3));
1526 view_info.plt_desc_p = view_info.got_desc_p + view_info.got_count * 4;
1528 gold_assert(pov + view_size ==
1529 view_info.plt_desc_p + view_info.plt_count * 4);
1531 // Write the section header.
1532 Swap32::writeval(pov, view_info.got_count);
1533 Swap32::writeval(pov + 4, view_info.plt_count);
1535 // Initialize the GOT type array to 0xff (reserved).
1536 memset(view_info.got_type_p, 0xff, view_info.got_count);
1538 // Write the incremental GOT descriptors for local symbols.
1539 typedef Local_got_offset_visitor<size, big_endian> Got_visitor;
1540 for (Incremental_inputs::Input_list::const_iterator p =
1541 this->inputs_->input_files().begin();
1542 p != this->inputs_->input_files().end();
1545 if ((*p)->type() != INCREMENTAL_INPUT_OBJECT
1546 && (*p)->type() != INCREMENTAL_INPUT_ARCHIVE_MEMBER)
1548 Incremental_object_entry* entry = (*p)->object_entry();
1549 gold_assert(entry != NULL);
1550 const Object* obj = entry->object();
1551 gold_assert(obj != NULL);
1552 view_info.got_descriptor = (*p)->get_offset();
1553 Got_visitor v(view_info);
1554 obj->for_all_local_got_entries(&v);
1557 // Write the incremental GOT and PLT descriptors for global symbols.
1558 typedef Global_symbol_visitor_got_plt<size, big_endian> Symbol_visitor;
1559 symtab_->for_all_symbols<size, Symbol_visitor>(Symbol_visitor(view_info));
1562 // Class Sized_incr_relobj. Most of these methods are not used for
1563 // Incremental objects, but are required to be implemented by the
1564 // base class Object.
1566 template<int size, bool big_endian>
1567 Sized_incr_relobj<size, big_endian>::Sized_incr_relobj(
1568 const std::string& name,
1569 Sized_incremental_binary<size, big_endian>* ibase,
1570 unsigned int input_file_index)
1571 : Sized_relobj_base<size, big_endian>(name, NULL), ibase_(ibase),
1572 input_file_index_(input_file_index),
1573 input_reader_(ibase->inputs_reader().input_file(input_file_index)),
1574 symbols_(), section_offsets_(), incr_reloc_offset_(-1U),
1575 incr_reloc_count_(0), incr_reloc_output_index_(0), incr_relocs_(NULL)
1577 if (this->input_reader_.is_in_system_directory())
1578 this->set_is_in_system_directory();
1579 const unsigned int shnum = this->input_reader_.get_input_section_count() + 1;
1580 this->set_shnum(shnum);
1583 // Read the symbols.
1585 template<int size, bool big_endian>
1587 Sized_incr_relobj<size, big_endian>::do_read_symbols(Read_symbols_data*)
1592 // Lay out the input sections.
1594 template<int size, bool big_endian>
1596 Sized_incr_relobj<size, big_endian>::do_layout(
1601 const unsigned int shnum = this->shnum();
1602 Incremental_inputs* incremental_inputs = layout->incremental_inputs();
1603 gold_assert(incremental_inputs != NULL);
1604 Output_sections& out_sections(this->output_sections());
1605 out_sections.resize(shnum);
1606 this->section_offsets_.resize(shnum);
1607 for (unsigned int i = 1; i < shnum; i++)
1609 typename Input_entry_reader::Input_section_info sect =
1610 this->input_reader_.get_input_section(i - 1);
1611 // Add the section to the incremental inputs layout.
1612 incremental_inputs->report_input_section(this, i, sect.name,
1614 if (sect.output_shndx == 0 || sect.sh_offset == -1)
1616 Output_section* os = this->ibase_->output_section(sect.output_shndx);
1617 gold_assert(os != NULL);
1618 out_sections[i] = os;
1619 this->section_offsets_[i] = static_cast<Address>(sect.sh_offset);
1623 // Layout sections whose layout was deferred while waiting for
1624 // input files from a plugin.
1625 template<int size, bool big_endian>
1627 Sized_incr_relobj<size, big_endian>::do_layout_deferred_sections(Layout*)
1631 // Add the symbols to the symbol table.
1633 template<int size, bool big_endian>
1635 Sized_incr_relobj<size, big_endian>::do_add_symbols(
1636 Symbol_table* symtab,
1640 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1641 unsigned char symbuf[sym_size];
1642 elfcpp::Sym<size, big_endian> sym(symbuf);
1643 elfcpp::Sym_write<size, big_endian> osym(symbuf);
1645 typedef typename elfcpp::Elf_types<size>::Elf_WXword Elf_size_type;
1647 unsigned int nsyms = this->input_reader_.get_global_symbol_count();
1648 this->symbols_.resize(nsyms);
1650 Incremental_binary::View symtab_view(NULL);
1651 unsigned int symtab_count;
1652 elfcpp::Elf_strtab strtab(NULL, 0);
1653 this->ibase_->get_symtab_view(&symtab_view, &symtab_count, &strtab);
1655 // Incremental_symtab_reader<big_endian> isymtab(this->ibase_->symtab_reader());
1656 // Incremental_relocs_reader<size, big_endian> irelocs(this->ibase_->relocs_reader());
1657 // unsigned int isym_count = isymtab.symbol_count();
1658 // unsigned int first_global = symtab_count - isym_count;
1660 unsigned const char* sym_p;
1661 for (unsigned int i = 0; i < nsyms; ++i)
1663 Incremental_global_symbol_reader<big_endian> info =
1664 this->input_reader_.get_global_symbol_reader(i);
1665 sym_p = symtab_view.data() + info.output_symndx() * sym_size;
1666 elfcpp::Sym<size, big_endian> gsym(sym_p);
1668 if (!strtab.get_c_string(gsym.get_st_name(), &name))
1671 typename elfcpp::Elf_types<size>::Elf_Addr v = gsym.get_st_value();
1672 unsigned int shndx = gsym.get_st_shndx();
1673 elfcpp::STB st_bind = gsym.get_st_bind();
1674 elfcpp::STT st_type = gsym.get_st_type();
1676 // Local hidden symbols start out as globals, but get converted to
1677 // to local during output.
1678 if (st_bind == elfcpp::STB_LOCAL)
1679 st_bind = elfcpp::STB_GLOBAL;
1681 unsigned int input_shndx = info.shndx();
1682 if (input_shndx == 0)
1684 shndx = elfcpp::SHN_UNDEF;
1687 else if (shndx != elfcpp::SHN_ABS)
1689 // Find the input section and calculate the section-relative value.
1690 gold_assert(shndx != elfcpp::SHN_UNDEF);
1691 Output_section* os = this->ibase_->output_section(shndx);
1692 gold_assert(os != NULL && os->has_fixed_layout());
1693 typename Input_entry_reader::Input_section_info sect =
1694 this->input_reader_.get_input_section(input_shndx - 1);
1695 gold_assert(sect.output_shndx == shndx);
1696 if (st_type != elfcpp::STT_TLS)
1698 v -= sect.sh_offset;
1699 shndx = input_shndx;
1702 osym.put_st_name(0);
1703 osym.put_st_value(v);
1704 osym.put_st_size(gsym.get_st_size());
1705 osym.put_st_info(st_bind, st_type);
1706 osym.put_st_other(gsym.get_st_other());
1707 osym.put_st_shndx(shndx);
1710 symtab->add_from_incrobj(this, name, NULL, &sym);
1714 // Return TRUE if we should include this object from an archive library.
1716 template<int size, bool big_endian>
1717 Archive::Should_include
1718 Sized_incr_relobj<size, big_endian>::do_should_include_member(
1727 // Iterate over global symbols, calling a visitor class V for each.
1729 template<int size, bool big_endian>
1731 Sized_incr_relobj<size, big_endian>::do_for_all_global_symbols(
1733 Library_base::Symbol_visitor_base*)
1735 // This routine is not used for incremental objects.
1738 // Iterate over local symbols, calling a visitor class V for each GOT offset
1739 // associated with a local symbol.
1741 template<int size, bool big_endian>
1743 Sized_incr_relobj<size, big_endian>::do_for_all_local_got_entries(
1744 Got_offset_list::Visitor*) const
1746 // FIXME: Implement Sized_incr_relobj::do_for_all_local_got_entries.
1749 // Get the size of a section.
1751 template<int size, bool big_endian>
1753 Sized_incr_relobj<size, big_endian>::do_section_size(unsigned int)
1758 // Get the name of a section.
1760 template<int size, bool big_endian>
1762 Sized_incr_relobj<size, big_endian>::do_section_name(unsigned int)
1767 // Return a view of the contents of a section.
1769 template<int size, bool big_endian>
1771 Sized_incr_relobj<size, big_endian>::do_section_contents(unsigned int)
1776 // Return section flags.
1778 template<int size, bool big_endian>
1780 Sized_incr_relobj<size, big_endian>::do_section_flags(unsigned int)
1785 // Return section entsize.
1787 template<int size, bool big_endian>
1789 Sized_incr_relobj<size, big_endian>::do_section_entsize(unsigned int)
1794 // Return section address.
1796 template<int size, bool big_endian>
1798 Sized_incr_relobj<size, big_endian>::do_section_address(unsigned int)
1803 // Return section type.
1805 template<int size, bool big_endian>
1807 Sized_incr_relobj<size, big_endian>::do_section_type(unsigned int)
1812 // Return the section link field.
1814 template<int size, bool big_endian>
1816 Sized_incr_relobj<size, big_endian>::do_section_link(unsigned int)
1821 // Return the section link field.
1823 template<int size, bool big_endian>
1825 Sized_incr_relobj<size, big_endian>::do_section_info(unsigned int)
1830 // Return the section alignment.
1832 template<int size, bool big_endian>
1834 Sized_incr_relobj<size, big_endian>::do_section_addralign(unsigned int)
1839 // Return the Xindex structure to use.
1841 template<int size, bool big_endian>
1843 Sized_incr_relobj<size, big_endian>::do_initialize_xindex()
1848 // Get symbol counts.
1850 template<int size, bool big_endian>
1852 Sized_incr_relobj<size, big_endian>::do_get_global_symbol_counts(
1853 const Symbol_table*, size_t*, size_t*) const
1860 template<int size, bool big_endian>
1862 Sized_incr_relobj<size, big_endian>::do_read_relocs(Read_relocs_data*)
1866 // Process the relocs to find list of referenced sections. Used only
1867 // during garbage collection.
1869 template<int size, bool big_endian>
1871 Sized_incr_relobj<size, big_endian>::do_gc_process_relocs(Symbol_table*,
1878 // Scan the relocs and adjust the symbol table.
1880 template<int size, bool big_endian>
1882 Sized_incr_relobj<size, big_endian>::do_scan_relocs(Symbol_table*,
1886 // Count the incremental relocations for this object.
1887 unsigned int nsyms = this->input_reader_.get_global_symbol_count();
1888 this->allocate_incremental_reloc_counts();
1889 for (unsigned int i = 0; i < nsyms; i++)
1891 Incremental_global_symbol_reader<big_endian> sym =
1892 this->input_reader_.get_global_symbol_reader(i);
1893 unsigned int reloc_count = sym.reloc_count();
1894 if (reloc_count > 0 && this->incr_reloc_offset_ == -1U)
1895 this->incr_reloc_offset_ = sym.reloc_offset();
1896 this->incr_reloc_count_ += reloc_count;
1897 for (unsigned int j = 0; j < reloc_count; j++)
1898 this->count_incremental_reloc(i);
1900 this->incr_reloc_output_index_ =
1901 layout->incremental_inputs()->get_reloc_count();
1902 this->finalize_incremental_relocs(layout, false);
1904 // The incoming incremental relocations may not end up in the same
1905 // location after the incremental update, because the incremental info
1906 // is regenerated in each link. Because the new location may overlap
1907 // with other data in the updated output file, we need to copy the
1908 // relocations into a buffer so that we can still read them safely
1909 // after we start writing updates to the output file.
1910 if (this->incr_reloc_count_ > 0)
1912 const Incremental_relocs_reader<size, big_endian>& relocs_reader =
1913 this->ibase_->relocs_reader();
1914 const unsigned int incr_reloc_size = relocs_reader.reloc_size;
1915 unsigned int len = this->incr_reloc_count_ * incr_reloc_size;
1916 this->incr_relocs_ = new unsigned char[len];
1917 memcpy(this->incr_relocs_,
1918 relocs_reader.data(this->incr_reloc_offset_),
1923 // Count the local symbols.
1925 template<int size, bool big_endian>
1927 Sized_incr_relobj<size, big_endian>::do_count_local_symbols(
1928 Stringpool_template<char>*,
1929 Stringpool_template<char>*)
1931 // FIXME: Count local symbols.
1934 // Finalize the local symbols.
1936 template<int size, bool big_endian>
1938 Sized_incr_relobj<size, big_endian>::do_finalize_local_symbols(
1943 // FIXME: Finalize local symbols.
1947 // Set the offset where local dynamic symbol information will be stored.
1949 template<int size, bool big_endian>
1951 Sized_incr_relobj<size, big_endian>::do_set_local_dynsym_indexes(
1954 // FIXME: set local dynsym indexes.
1958 // Set the offset where local dynamic symbol information will be stored.
1960 template<int size, bool big_endian>
1962 Sized_incr_relobj<size, big_endian>::do_set_local_dynsym_offset(off_t)
1967 // Relocate the input sections and write out the local symbols.
1968 // We don't actually do any relocation here. For unchanged input files,
1969 // we reapply relocations only for symbols that have changed; that happens
1970 // in queue_final_tasks. We do need to rewrite the incremental relocations
1973 template<int size, bool big_endian>
1975 Sized_incr_relobj<size, big_endian>::do_relocate(const Symbol_table*,
1976 const Layout* layout,
1979 if (this->incr_reloc_count_ == 0)
1982 const unsigned int incr_reloc_size =
1983 Incremental_relocs_reader<size, big_endian>::reloc_size;
1985 // Get a view for the .gnu_incremental_relocs section.
1986 Incremental_inputs* inputs = layout->incremental_inputs();
1987 gold_assert(inputs != NULL);
1988 const off_t relocs_off = inputs->relocs_section()->offset();
1989 const off_t relocs_size = inputs->relocs_section()->data_size();
1990 unsigned char* const view = of->get_output_view(relocs_off, relocs_size);
1992 // Copy the relocations from the buffer.
1993 off_t off = this->incr_reloc_output_index_ * incr_reloc_size;
1994 unsigned int len = this->incr_reloc_count_ * incr_reloc_size;
1995 memcpy(view + off, this->incr_relocs_, len);
1996 of->write_output_view(off, len, view);
1999 // Set the offset of a section.
2001 template<int size, bool big_endian>
2003 Sized_incr_relobj<size, big_endian>::do_set_section_offset(unsigned int,
2008 // Class Sized_incr_dynobj. Most of these methods are not used for
2009 // Incremental objects, but are required to be implemented by the
2010 // base class Object.
2012 template<int size, bool big_endian>
2013 Sized_incr_dynobj<size, big_endian>::Sized_incr_dynobj(
2014 const std::string& name,
2015 Sized_incremental_binary<size, big_endian>* ibase,
2016 unsigned int input_file_index)
2017 : Dynobj(name, NULL), ibase_(ibase),
2018 input_file_index_(input_file_index),
2019 input_reader_(ibase->inputs_reader().input_file(input_file_index)),
2022 if (this->input_reader_.is_in_system_directory())
2023 this->set_is_in_system_directory();
2027 // Read the symbols.
2029 template<int size, bool big_endian>
2031 Sized_incr_dynobj<size, big_endian>::do_read_symbols(Read_symbols_data*)
2036 // Lay out the input sections.
2038 template<int size, bool big_endian>
2040 Sized_incr_dynobj<size, big_endian>::do_layout(
2047 // Add the symbols to the symbol table.
2049 template<int size, bool big_endian>
2051 Sized_incr_dynobj<size, big_endian>::do_add_symbols(
2052 Symbol_table* symtab,
2056 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
2057 unsigned char symbuf[sym_size];
2058 elfcpp::Sym<size, big_endian> sym(symbuf);
2059 elfcpp::Sym_write<size, big_endian> osym(symbuf);
2061 typedef typename elfcpp::Elf_types<size>::Elf_WXword Elf_size_type;
2063 unsigned int nsyms = this->input_reader_.get_global_symbol_count();
2064 this->symbols_.resize(nsyms);
2066 Incremental_binary::View symtab_view(NULL);
2067 unsigned int symtab_count;
2068 elfcpp::Elf_strtab strtab(NULL, 0);
2069 this->ibase_->get_symtab_view(&symtab_view, &symtab_count, &strtab);
2071 // Incremental_symtab_reader<big_endian> isymtab(this->ibase_->symtab_reader());
2072 // Incremental_relocs_reader<size, big_endian> irelocs(this->ibase_->relocs_reader());
2073 // unsigned int isym_count = isymtab.symbol_count();
2074 // unsigned int first_global = symtab_count - isym_count;
2076 unsigned const char* sym_p;
2077 for (unsigned int i = 0; i < nsyms; ++i)
2080 unsigned int output_symndx =
2081 this->input_reader_.get_output_symbol_index(i, &is_def);
2082 sym_p = symtab_view.data() + output_symndx * sym_size;
2083 elfcpp::Sym<size, big_endian> gsym(sym_p);
2085 if (!strtab.get_c_string(gsym.get_st_name(), &name))
2088 typename elfcpp::Elf_types<size>::Elf_Addr v;
2090 elfcpp::STB st_bind = gsym.get_st_bind();
2091 elfcpp::STT st_type = gsym.get_st_type();
2093 // Local hidden symbols start out as globals, but get converted to
2094 // to local during output.
2095 if (st_bind == elfcpp::STB_LOCAL)
2096 st_bind = elfcpp::STB_GLOBAL;
2100 shndx = elfcpp::SHN_UNDEF;
2105 // For a symbol defined in a shared object, the section index
2106 // is meaningless, as long as it's not SHN_UNDEF.
2108 v = gsym.get_st_value();
2111 osym.put_st_name(0);
2112 osym.put_st_value(v);
2113 osym.put_st_size(gsym.get_st_size());
2114 osym.put_st_info(st_bind, st_type);
2115 osym.put_st_other(gsym.get_st_other());
2116 osym.put_st_shndx(shndx);
2119 symtab->add_from_incrobj<size, big_endian>(this, name, NULL, &sym);
2123 // Return TRUE if we should include this object from an archive library.
2125 template<int size, bool big_endian>
2126 Archive::Should_include
2127 Sized_incr_dynobj<size, big_endian>::do_should_include_member(
2136 // Iterate over global symbols, calling a visitor class V for each.
2138 template<int size, bool big_endian>
2140 Sized_incr_dynobj<size, big_endian>::do_for_all_global_symbols(
2142 Library_base::Symbol_visitor_base*)
2144 // This routine is not used for dynamic libraries.
2147 // Iterate over local symbols, calling a visitor class V for each GOT offset
2148 // associated with a local symbol.
2150 template<int size, bool big_endian>
2152 Sized_incr_dynobj<size, big_endian>::do_for_all_local_got_entries(
2153 Got_offset_list::Visitor*) const
2155 // FIXME: Implement Sized_incr_dynobj::do_for_all_local_got_entries.
2158 // Get the size of a section.
2160 template<int size, bool big_endian>
2162 Sized_incr_dynobj<size, big_endian>::do_section_size(unsigned int)
2167 // Get the name of a section.
2169 template<int size, bool big_endian>
2171 Sized_incr_dynobj<size, big_endian>::do_section_name(unsigned int)
2176 // Return a view of the contents of a section.
2178 template<int size, bool big_endian>
2180 Sized_incr_dynobj<size, big_endian>::do_section_contents(unsigned int)
2185 // Return section flags.
2187 template<int size, bool big_endian>
2189 Sized_incr_dynobj<size, big_endian>::do_section_flags(unsigned int)
2194 // Return section entsize.
2196 template<int size, bool big_endian>
2198 Sized_incr_dynobj<size, big_endian>::do_section_entsize(unsigned int)
2203 // Return section address.
2205 template<int size, bool big_endian>
2207 Sized_incr_dynobj<size, big_endian>::do_section_address(unsigned int)
2212 // Return section type.
2214 template<int size, bool big_endian>
2216 Sized_incr_dynobj<size, big_endian>::do_section_type(unsigned int)
2221 // Return the section link field.
2223 template<int size, bool big_endian>
2225 Sized_incr_dynobj<size, big_endian>::do_section_link(unsigned int)
2230 // Return the section link field.
2232 template<int size, bool big_endian>
2234 Sized_incr_dynobj<size, big_endian>::do_section_info(unsigned int)
2239 // Return the section alignment.
2241 template<int size, bool big_endian>
2243 Sized_incr_dynobj<size, big_endian>::do_section_addralign(unsigned int)
2248 // Return the Xindex structure to use.
2250 template<int size, bool big_endian>
2252 Sized_incr_dynobj<size, big_endian>::do_initialize_xindex()
2257 // Get symbol counts.
2259 template<int size, bool big_endian>
2261 Sized_incr_dynobj<size, big_endian>::do_get_global_symbol_counts(
2262 const Symbol_table*, size_t*, size_t*) const
2267 // Allocate an incremental object of the appropriate size and endianness.
2270 make_sized_incremental_object(
2271 Incremental_binary* ibase,
2272 unsigned int input_file_index,
2273 Incremental_input_type input_type,
2274 const Incremental_binary::Input_reader* input_reader)
2277 std::string name(input_reader->filename());
2279 switch (parameters->size_and_endianness())
2281 #ifdef HAVE_TARGET_32_LITTLE
2282 case Parameters::TARGET_32_LITTLE:
2284 Sized_incremental_binary<32, false>* sized_ibase =
2285 static_cast<Sized_incremental_binary<32, false>*>(ibase);
2286 if (input_type == INCREMENTAL_INPUT_SHARED_LIBRARY)
2287 obj = new Sized_incr_dynobj<32, false>(name, sized_ibase,
2290 obj = new Sized_incr_relobj<32, false>(name, sized_ibase,
2295 #ifdef HAVE_TARGET_32_BIG
2296 case Parameters::TARGET_32_BIG:
2298 Sized_incremental_binary<32, true>* sized_ibase =
2299 static_cast<Sized_incremental_binary<32, true>*>(ibase);
2300 if (input_type == INCREMENTAL_INPUT_SHARED_LIBRARY)
2301 obj = new Sized_incr_dynobj<32, true>(name, sized_ibase,
2304 obj = new Sized_incr_relobj<32, true>(name, sized_ibase,
2309 #ifdef HAVE_TARGET_64_LITTLE
2310 case Parameters::TARGET_64_LITTLE:
2312 Sized_incremental_binary<64, false>* sized_ibase =
2313 static_cast<Sized_incremental_binary<64, false>*>(ibase);
2314 if (input_type == INCREMENTAL_INPUT_SHARED_LIBRARY)
2315 obj = new Sized_incr_dynobj<64, false>(name, sized_ibase,
2318 obj = new Sized_incr_relobj<64, false>(name, sized_ibase,
2323 #ifdef HAVE_TARGET_64_BIG
2324 case Parameters::TARGET_64_BIG:
2326 Sized_incremental_binary<64, true>* sized_ibase =
2327 static_cast<Sized_incremental_binary<64, true>*>(ibase);
2328 if (input_type == INCREMENTAL_INPUT_SHARED_LIBRARY)
2329 obj = new Sized_incr_dynobj<64, true>(name, sized_ibase,
2332 obj = new Sized_incr_relobj<64, true>(name, sized_ibase,
2341 gold_assert(obj != NULL);
2345 // Copy the unused symbols from the incremental input info.
2346 // We need to do this because we may be overwriting the incremental
2347 // input info in the base file before we write the new incremental
2350 Incremental_library::copy_unused_symbols()
2352 unsigned int symcount = this->input_reader_->get_unused_symbol_count();
2353 this->unused_symbols_.reserve(symcount);
2354 for (unsigned int i = 0; i < symcount; ++i)
2356 std::string name(this->input_reader_->get_unused_symbol(i));
2357 this->unused_symbols_.push_back(name);
2361 // Iterator for unused global symbols in the library.
2363 Incremental_library::do_for_all_unused_symbols(Symbol_visitor_base* v) const
2365 for (Symbol_list::const_iterator p = this->unused_symbols_.begin();
2366 p != this->unused_symbols_.end();
2368 v->visit(p->c_str());
2371 // Instantiate the templates we need.
2373 #ifdef HAVE_TARGET_32_LITTLE
2375 class Sized_incremental_binary<32, false>;
2378 class Sized_incr_relobj<32, false>;
2381 class Sized_incr_dynobj<32, false>;
2384 #ifdef HAVE_TARGET_32_BIG
2386 class Sized_incremental_binary<32, true>;
2389 class Sized_incr_relobj<32, true>;
2392 class Sized_incr_dynobj<32, true>;
2395 #ifdef HAVE_TARGET_64_LITTLE
2397 class Sized_incremental_binary<64, false>;
2400 class Sized_incr_relobj<64, false>;
2403 class Sized_incr_dynobj<64, false>;
2406 #ifdef HAVE_TARGET_64_BIG
2408 class Sized_incremental_binary<64, true>;
2411 class Sized_incr_relobj<64, true>;
2414 class Sized_incr_dynobj<64, true>;
2417 } // End namespace gold.