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.
27 #include "libiberty.h"
33 #include "incremental.h"
37 #include "target-select.h"
44 // Version information. Will change frequently during the development, later
45 // we could think about backward (and forward?) compatibility.
46 const unsigned int INCREMENTAL_LINK_VERSION = 1;
48 // This class manages the .gnu_incremental_inputs section, which holds
49 // the header information, a directory of input files, and separate
50 // entries for each input file.
52 template<int size, bool big_endian>
53 class Output_section_incremental_inputs : public Output_section_data
56 Output_section_incremental_inputs(const Incremental_inputs* inputs,
57 const Symbol_table* symtab)
58 : Output_section_data(size / 8), inputs_(inputs), symtab_(symtab)
62 // This is called to update the section size prior to assigning
63 // the address and file offset.
66 { this->set_final_data_size(); }
68 // Set the final data size.
70 set_final_data_size();
72 // Write the data to the file.
74 do_write(Output_file*);
76 // Write to a map file.
78 do_print_to_mapfile(Mapfile* mapfile) const
79 { mapfile->print_output_data(this, _("** incremental_inputs")); }
82 // Write the section header.
84 write_header(unsigned char* pov, unsigned int input_file_count,
85 section_offset_type command_line_offset);
87 // Write the input file entries.
89 write_input_files(unsigned char* oview, unsigned char* pov,
92 // Write the supplemental information blocks.
94 write_info_blocks(unsigned char* oview, unsigned char* pov,
95 Stringpool* strtab, unsigned int* global_syms,
96 unsigned int global_sym_count);
98 // Write the contents of the .gnu_incremental_symtab section.
100 write_symtab(unsigned char* pov, unsigned int* global_syms,
101 unsigned int global_sym_count);
103 // Write the contents of the .gnu_incremental_got_plt section.
105 write_got_plt(unsigned char* pov, off_t view_size);
107 // Typedefs for writing the data to the output sections.
108 typedef elfcpp::Swap<size, big_endian> Swap;
109 typedef elfcpp::Swap<16, big_endian> Swap16;
110 typedef elfcpp::Swap<32, big_endian> Swap32;
111 typedef elfcpp::Swap<64, big_endian> Swap64;
113 // Sizes of various structures.
114 static const int sizeof_addr = size / 8;
115 static const int header_size = 16;
116 static const int input_entry_size = 24;
118 // The Incremental_inputs object.
119 const Incremental_inputs* inputs_;
122 const Symbol_table* symtab_;
125 // Inform the user why we don't do an incremental link. Not called in
126 // the obvious case of missing output file. TODO: Is this helpful?
129 vexplain_no_incremental(const char* format, va_list args)
132 if (vasprintf(&buf, format, args) < 0)
134 gold_info(_("the link might take longer: "
135 "cannot perform incremental link: %s"), buf);
140 explain_no_incremental(const char* format, ...)
143 va_start(args, format);
144 vexplain_no_incremental(format, args);
151 Incremental_binary::error(const char* format, ...) const
154 va_start(args, format);
155 // Current code only checks if the file can be used for incremental linking,
156 // so errors shouldn't fail the build, but only result in a fallback to a
158 // TODO: when we implement incremental editing of the file, we may need a
159 // flag that will cause errors to be treated seriously.
160 vexplain_no_incremental(format, args);
164 // Find the .gnu_incremental_inputs section and related sections.
166 template<int size, bool big_endian>
168 Sized_incremental_binary<size, big_endian>::find_incremental_inputs_sections(
169 unsigned int* p_inputs_shndx,
170 unsigned int* p_symtab_shndx,
171 unsigned int* p_relocs_shndx,
172 unsigned int* p_got_plt_shndx,
173 unsigned int* p_strtab_shndx)
175 unsigned int inputs_shndx =
176 this->elf_file_.find_section_by_type(elfcpp::SHT_GNU_INCREMENTAL_INPUTS);
177 if (inputs_shndx == elfcpp::SHN_UNDEF) // Not found.
180 unsigned int symtab_shndx =
181 this->elf_file_.find_section_by_type(elfcpp::SHT_GNU_INCREMENTAL_SYMTAB);
182 if (symtab_shndx == elfcpp::SHN_UNDEF) // Not found.
184 if (this->elf_file_.section_link(symtab_shndx) != inputs_shndx)
187 unsigned int relocs_shndx =
188 this->elf_file_.find_section_by_type(elfcpp::SHT_GNU_INCREMENTAL_RELOCS);
189 if (relocs_shndx == elfcpp::SHN_UNDEF) // Not found.
191 if (this->elf_file_.section_link(relocs_shndx) != inputs_shndx)
194 unsigned int got_plt_shndx =
195 this->elf_file_.find_section_by_type(elfcpp::SHT_GNU_INCREMENTAL_GOT_PLT);
196 if (got_plt_shndx == elfcpp::SHN_UNDEF) // Not found.
198 if (this->elf_file_.section_link(got_plt_shndx) != inputs_shndx)
201 unsigned int strtab_shndx = this->elf_file_.section_link(inputs_shndx);
202 if (strtab_shndx == elfcpp::SHN_UNDEF
203 || strtab_shndx > this->elf_file_.shnum()
204 || this->elf_file_.section_type(strtab_shndx) != elfcpp::SHT_STRTAB)
207 if (p_inputs_shndx != NULL)
208 *p_inputs_shndx = inputs_shndx;
209 if (p_symtab_shndx != NULL)
210 *p_symtab_shndx = symtab_shndx;
211 if (p_relocs_shndx != NULL)
212 *p_relocs_shndx = relocs_shndx;
213 if (p_got_plt_shndx != NULL)
214 *p_got_plt_shndx = got_plt_shndx;
215 if (p_strtab_shndx != NULL)
216 *p_strtab_shndx = strtab_shndx;
220 // Set up the readers into the incremental info sections.
222 template<int size, bool big_endian>
224 Sized_incremental_binary<size, big_endian>::setup_readers()
226 unsigned int inputs_shndx;
227 unsigned int symtab_shndx;
228 unsigned int relocs_shndx;
229 unsigned int got_plt_shndx;
230 unsigned int strtab_shndx;
232 if (!this->find_incremental_inputs_sections(&inputs_shndx, &symtab_shndx,
233 &relocs_shndx, &got_plt_shndx,
237 Location inputs_location(this->elf_file_.section_contents(inputs_shndx));
238 Location symtab_location(this->elf_file_.section_contents(symtab_shndx));
239 Location relocs_location(this->elf_file_.section_contents(relocs_shndx));
240 Location got_plt_location(this->elf_file_.section_contents(got_plt_shndx));
241 Location strtab_location(this->elf_file_.section_contents(strtab_shndx));
243 View inputs_view = this->view(inputs_location);
244 View symtab_view = this->view(symtab_location);
245 View relocs_view = this->view(relocs_location);
246 View got_plt_view = this->view(got_plt_location);
247 View strtab_view = this->view(strtab_location);
249 elfcpp::Elf_strtab strtab(strtab_view.data(), strtab_location.data_size);
251 this->inputs_reader_ =
252 Incremental_inputs_reader<size, big_endian>(inputs_view.data(), strtab);
253 this->symtab_reader_ =
254 Incremental_symtab_reader<big_endian>(symtab_view.data(),
255 symtab_location.data_size);
256 this->relocs_reader_ =
257 Incremental_relocs_reader<size, big_endian>(relocs_view.data(),
258 relocs_location.data_size);
259 this->got_plt_reader_ =
260 Incremental_got_plt_reader<big_endian>(got_plt_view.data());
262 // Find the main symbol table.
263 unsigned int main_symtab_shndx =
264 this->elf_file_.find_section_by_type(elfcpp::SHT_SYMTAB);
265 gold_assert(main_symtab_shndx != elfcpp::SHN_UNDEF);
266 this->main_symtab_loc_ = this->elf_file_.section_contents(main_symtab_shndx);
268 // Find the main symbol string table.
269 unsigned int main_strtab_shndx =
270 this->elf_file_.section_link(main_symtab_shndx);
271 gold_assert(main_strtab_shndx != elfcpp::SHN_UNDEF
272 && main_strtab_shndx < this->elf_file_.shnum());
273 this->main_strtab_loc_ = this->elf_file_.section_contents(main_strtab_shndx);
275 // Walk the list of input files (a) to setup an Input_reader for each
276 // input file, and (b) to record maps of files added from archive
277 // libraries and scripts.
278 Incremental_inputs_reader<size, big_endian>& inputs = this->inputs_reader_;
279 unsigned int count = inputs.input_file_count();
280 this->input_objects_.resize(count);
281 this->input_entry_readers_.reserve(count);
282 this->library_map_.resize(count);
283 this->script_map_.resize(count);
284 for (unsigned int i = 0; i < count; i++)
286 Input_entry_reader input_file = inputs.input_file(i);
287 this->input_entry_readers_.push_back(Sized_input_reader(input_file));
288 switch (input_file.type())
290 case INCREMENTAL_INPUT_OBJECT:
291 case INCREMENTAL_INPUT_ARCHIVE_MEMBER:
292 case INCREMENTAL_INPUT_SHARED_LIBRARY:
293 // No special treatment necessary.
295 case INCREMENTAL_INPUT_ARCHIVE:
297 Incremental_library* lib =
298 new Incremental_library(input_file.filename(), i,
299 &this->input_entry_readers_[i]);
300 this->library_map_[i] = lib;
301 unsigned int member_count = input_file.get_member_count();
302 for (unsigned int j = 0; j < member_count; j++)
304 int member_offset = input_file.get_member_offset(j);
305 int member_index = inputs.input_file_index(member_offset);
306 this->library_map_[member_index] = lib;
310 case INCREMENTAL_INPUT_SCRIPT:
312 Script_info* script = new Script_info(input_file.filename());
313 this->script_map_[i] = script;
314 unsigned int object_count = input_file.get_object_count();
315 for (unsigned int j = 0; j < object_count; j++)
317 int object_offset = input_file.get_object_offset(j);
318 int object_index = inputs.input_file_index(object_offset);
319 this->script_map_[object_index] = script;
328 // Initialize the map of global symbols.
329 unsigned int nglobals = this->symtab_reader_.symbol_count();
330 this->symbol_map_.resize(nglobals);
332 this->has_incremental_info_ = true;
335 // Walk the list of input files given on the command line, and build
336 // a direct map of file index to the corresponding input argument.
339 check_input_args(std::vector<const Input_argument*>& input_args_map,
340 Input_arguments::const_iterator begin,
341 Input_arguments::const_iterator end)
343 for (Input_arguments::const_iterator p = begin;
349 const Input_file_group* group = p->group();
350 check_input_args(input_args_map, group->begin(), group->end());
352 else if (p->is_lib())
354 const Input_file_lib* lib = p->lib();
355 check_input_args(input_args_map, lib->begin(), lib->end());
359 gold_assert(p->is_file());
360 unsigned int arg_serial = p->file().arg_serial();
363 gold_assert(arg_serial <= input_args_map.size());
364 gold_assert(input_args_map[arg_serial - 1] == 0);
365 input_args_map[arg_serial - 1] = &*p;
371 // Determine whether an incremental link based on the existing output file
374 template<int size, bool big_endian>
376 Sized_incremental_binary<size, big_endian>::do_check_inputs(
377 const Command_line& cmdline,
378 Incremental_inputs* incremental_inputs)
380 Incremental_inputs_reader<size, big_endian>& inputs = this->inputs_reader_;
382 if (!this->has_incremental_info_)
384 explain_no_incremental(_("no incremental data from previous build"));
388 if (inputs.version() != INCREMENTAL_LINK_VERSION)
390 explain_no_incremental(_("different version of incremental build data"));
394 if (incremental_inputs->command_line() != inputs.command_line())
396 explain_no_incremental(_("command line changed"));
400 // Walk the list of input files given on the command line, and build
401 // a direct map of argument serial numbers to the corresponding input
403 this->input_args_map_.resize(cmdline.number_of_input_files());
404 check_input_args(this->input_args_map_, cmdline.begin(), cmdline.end());
406 // Walk the list of input files to check for conditions that prevent
407 // an incremental update link.
408 unsigned int count = inputs.input_file_count();
409 for (unsigned int i = 0; i < count; i++)
411 Input_entry_reader input_file = inputs.input_file(i);
412 switch (input_file.type())
414 case INCREMENTAL_INPUT_OBJECT:
415 case INCREMENTAL_INPUT_ARCHIVE_MEMBER:
416 case INCREMENTAL_INPUT_SHARED_LIBRARY:
417 case INCREMENTAL_INPUT_ARCHIVE:
418 // No special treatment necessary.
420 case INCREMENTAL_INPUT_SCRIPT:
421 if (this->do_file_has_changed(i))
423 explain_no_incremental(_("%s: script file changed"),
424 input_file.filename());
436 // Return TRUE if input file N has changed since the last incremental link.
438 template<int size, bool big_endian>
440 Sized_incremental_binary<size, big_endian>::do_file_has_changed(
441 unsigned int n) const
443 Input_entry_reader input_file = this->inputs_reader_.input_file(n);
444 Incremental_disposition disp = INCREMENTAL_CHECK;
445 const Input_argument* input_argument = this->get_input_argument(n);
446 if (input_argument != NULL)
447 disp = input_argument->file().options().incremental_disposition();
449 if (disp != INCREMENTAL_CHECK)
450 return disp == INCREMENTAL_CHANGED;
452 const char* filename = input_file.filename();
453 Timespec old_mtime = input_file.get_mtime();
455 if (!get_mtime(filename, &new_mtime))
457 // If we can't open get the current modification time, assume it has
458 // changed. If the file doesn't exist, we'll issue an error when we
459 // try to open it later.
463 if (new_mtime.seconds > old_mtime.seconds)
465 if (new_mtime.seconds == old_mtime.seconds
466 && new_mtime.nanoseconds > old_mtime.nanoseconds)
471 // Initialize the layout of the output file based on the existing
474 template<int size, bool big_endian>
476 Sized_incremental_binary<size, big_endian>::do_init_layout(Layout* layout)
478 typedef elfcpp::Shdr<size, big_endian> Shdr;
479 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
481 // Get views of the section headers and the section string table.
482 const off_t shoff = this->elf_file_.shoff();
483 const unsigned int shnum = this->elf_file_.shnum();
484 const unsigned int shstrndx = this->elf_file_.shstrndx();
485 Location shdrs_location(shoff, shnum * shdr_size);
486 Location shstrndx_location(this->elf_file_.section_contents(shstrndx));
487 View shdrs_view = this->view(shdrs_location);
488 View shstrndx_view = this->view(shstrndx_location);
489 elfcpp::Elf_strtab shstrtab(shstrndx_view.data(),
490 shstrndx_location.data_size);
492 layout->set_incremental_base(this);
494 // Initialize the layout.
495 this->section_map_.resize(shnum);
496 const unsigned char* pshdr = shdrs_view.data() + shdr_size;
497 for (unsigned int i = 1; i < shnum; i++)
501 if (!shstrtab.get_c_string(shdr.get_sh_name(), &name))
503 gold_debug(DEBUG_INCREMENTAL,
504 "Output section: %2d %08lx %08lx %08lx %3d %s",
506 static_cast<long>(shdr.get_sh_addr()),
507 static_cast<long>(shdr.get_sh_offset()),
508 static_cast<long>(shdr.get_sh_size()),
509 shdr.get_sh_type(), name ? name : "<null>");
510 this->section_map_[i] = layout->init_fixed_output_section(name, shdr);
515 // Mark regions of the input file that must be kept unchanged.
517 template<int size, bool big_endian>
519 Sized_incremental_binary<size, big_endian>::do_reserve_layout(
520 unsigned int input_file_index)
522 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
524 Input_entry_reader input_file =
525 this->inputs_reader_.input_file(input_file_index);
527 if (input_file.type() == INCREMENTAL_INPUT_SHARED_LIBRARY)
529 // Reserve the BSS space used for COPY relocations.
530 unsigned int nsyms = input_file.get_global_symbol_count();
531 Incremental_binary::View symtab_view(NULL);
532 unsigned int symtab_count;
533 elfcpp::Elf_strtab strtab(NULL, 0);
534 this->get_symtab_view(&symtab_view, &symtab_count, &strtab);
535 for (unsigned int i = 0; i < nsyms; ++i)
539 unsigned int output_symndx =
540 input_file.get_output_symbol_index(i, &is_def, &is_copy);
543 const unsigned char* sym_p = (symtab_view.data()
544 + output_symndx * sym_size);
545 elfcpp::Sym<size, big_endian> gsym(sym_p);
546 unsigned int shndx = gsym.get_st_shndx();
547 if (shndx < 1 || shndx >= this->section_map_.size())
549 Output_section* os = this->section_map_[shndx];
550 off_t offset = gsym.get_st_value() - os->address();
551 os->reserve(offset, gsym.get_st_size());
552 gold_debug(DEBUG_INCREMENTAL,
553 "Reserve for COPY reloc: %s, off %d, size %d",
555 static_cast<int>(offset),
556 static_cast<int>(gsym.get_st_size()));
562 unsigned int shnum = input_file.get_input_section_count();
563 for (unsigned int i = 0; i < shnum; i++)
565 typename Input_entry_reader::Input_section_info sect =
566 input_file.get_input_section(i);
567 if (sect.output_shndx == 0 || sect.sh_offset == -1)
569 Output_section* os = this->section_map_[sect.output_shndx];
570 gold_assert(os != NULL);
571 os->reserve(sect.sh_offset, sect.sh_size);
575 // Process the GOT and PLT entries from the existing output file.
577 template<int size, bool big_endian>
579 Sized_incremental_binary<size, big_endian>::do_process_got_plt(
580 Symbol_table* symtab,
583 Incremental_got_plt_reader<big_endian> got_plt_reader(this->got_plt_reader());
584 Sized_target<size, big_endian>* target =
585 parameters->sized_target<size, big_endian>();
587 // Get the number of symbols in the main symbol table and in the
588 // incremental symbol table. The difference between the two counts
589 // is the index of the first forced-local or global symbol in the
590 // main symbol table.
591 unsigned int symtab_count =
592 this->main_symtab_loc_.data_size / elfcpp::Elf_sizes<size>::sym_size;
593 unsigned int isym_count = this->symtab_reader_.symbol_count();
594 unsigned int first_global = symtab_count - isym_count;
596 // Tell the target how big the GOT and PLT sections are.
597 unsigned int got_count = got_plt_reader.get_got_entry_count();
598 unsigned int plt_count = got_plt_reader.get_plt_entry_count();
599 Output_data_got<size, big_endian>* got =
600 target->init_got_plt_for_update(symtab, layout, got_count, plt_count);
602 // Read the GOT entries from the base file and build the outgoing GOT.
603 for (unsigned int i = 0; i < got_count; ++i)
605 unsigned int got_type = got_plt_reader.get_got_type(i);
606 if ((got_type & 0x7f) == 0x7f)
608 // This is the second entry of a pair.
609 got->reserve_slot(i);
612 unsigned int symndx = got_plt_reader.get_got_symndx(i);
615 // This is an entry for a local symbol. Ignore this entry if
616 // the object file was replaced.
617 unsigned int input_index = got_plt_reader.get_got_input_index(i);
618 gold_debug(DEBUG_INCREMENTAL,
619 "GOT entry %d, type %02x: (local symbol)",
621 Sized_relobj_incr<size, big_endian>* obj =
622 this->input_object(input_index);
624 target->reserve_local_got_entry(i, obj, symndx, got_type & 0x7f);
628 // This is an entry for a global symbol. GOT_DESC is the symbol
630 // FIXME: This should really be a fatal error (corrupt input).
631 gold_assert(symndx >= first_global && symndx < symtab_count);
632 Symbol* sym = this->global_symbol(symndx - first_global);
633 // Add the GOT entry only if the symbol is still referenced.
634 if (sym != NULL && sym->in_reg())
636 gold_debug(DEBUG_INCREMENTAL,
637 "GOT entry %d, type %02x: %s",
638 i, got_type, sym->name());
639 target->reserve_global_got_entry(i, sym, got_type);
644 // Read the PLT entries from the base file and pass each to the target.
645 for (unsigned int i = 0; i < plt_count; ++i)
647 unsigned int plt_desc = got_plt_reader.get_plt_desc(i);
648 // FIXME: This should really be a fatal error (corrupt input).
649 gold_assert(plt_desc >= first_global && plt_desc < symtab_count);
650 Symbol* sym = this->global_symbol(plt_desc - first_global);
651 // Add the PLT entry only if the symbol is still referenced.
654 gold_debug(DEBUG_INCREMENTAL,
657 target->register_global_plt_entry(i, sym);
662 // Emit COPY relocations from the existing output file.
664 template<int size, bool big_endian>
666 Sized_incremental_binary<size, big_endian>::do_emit_copy_relocs(
667 Symbol_table* symtab)
669 Sized_target<size, big_endian>* target =
670 parameters->sized_target<size, big_endian>();
672 for (typename Copy_relocs::iterator p = this->copy_relocs_.begin();
673 p != this->copy_relocs_.end();
676 if (!(*p).symbol->is_copied_from_dynobj())
677 target->emit_copy_reloc(symtab, (*p).symbol, (*p).output_section,
682 // Apply incremental relocations for symbols whose values have changed.
684 template<int size, bool big_endian>
686 Sized_incremental_binary<size, big_endian>::do_apply_incremental_relocs(
687 const Symbol_table* symtab,
691 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
692 typedef typename elfcpp::Elf_types<size>::Elf_Swxword Addend;
693 Incremental_symtab_reader<big_endian> isymtab(this->symtab_reader());
694 Incremental_relocs_reader<size, big_endian> irelocs(this->relocs_reader());
695 unsigned int nglobals = isymtab.symbol_count();
696 const unsigned int incr_reloc_size = irelocs.reloc_size;
698 Relocate_info<size, big_endian> relinfo;
699 relinfo.symtab = symtab;
700 relinfo.layout = layout;
701 relinfo.object = NULL;
702 relinfo.reloc_shndx = 0;
703 relinfo.reloc_shdr = NULL;
704 relinfo.data_shndx = 0;
705 relinfo.data_shdr = NULL;
707 Sized_target<size, big_endian>* target =
708 parameters->sized_target<size, big_endian>();
710 for (unsigned int i = 0; i < nglobals; i++)
712 const Symbol* gsym = this->global_symbol(i);
714 // If the symbol is not referenced from any unchanged input files,
715 // we do not need to reapply any of its relocations.
719 // If the symbol is defined in an unchanged file, we do not need to
720 // reapply any of its relocations.
721 if (gsym->source() == Symbol::FROM_OBJECT
722 && gsym->object()->is_incremental())
725 gold_debug(DEBUG_INCREMENTAL,
726 "Applying incremental relocations for global symbol %s [%d]",
729 // Follow the linked list of input symbol table entries for this symbol.
730 // We don't bother to figure out whether the symbol table entry belongs
731 // to a changed or unchanged file because it's easier just to apply all
732 // the relocations -- although we might scribble over an area that has
733 // been reallocated, we do this before copying any new data into the
735 unsigned int offset = isymtab.get_list_head(i);
738 Incremental_global_symbol_reader<big_endian> sym_info =
739 this->inputs_reader().global_symbol_reader_at_offset(offset);
740 unsigned int r_base = sym_info.reloc_offset();
741 unsigned int r_count = sym_info.reloc_count();
743 // Apply each relocation for this symbol table entry.
744 for (unsigned int j = 0; j < r_count;
745 ++j, r_base += incr_reloc_size)
747 unsigned int r_type = irelocs.get_r_type(r_base);
748 unsigned int r_shndx = irelocs.get_r_shndx(r_base);
749 Address r_offset = irelocs.get_r_offset(r_base);
750 Addend r_addend = irelocs.get_r_addend(r_base);
751 Output_section* os = this->output_section(r_shndx);
752 Address address = os->address();
753 off_t section_offset = os->offset();
754 size_t view_size = os->data_size();
755 unsigned char* const view = of->get_output_view(section_offset,
758 gold_debug(DEBUG_INCREMENTAL,
759 " %08lx: %s + %d: type %d addend %ld",
760 (long)(section_offset + r_offset),
766 target->apply_relocation(&relinfo, r_offset, r_type, r_addend,
767 gsym, view, address, view_size);
769 // FIXME: Do something more efficient if write_output_view
770 // ever becomes more than a no-op.
771 of->write_output_view(section_offset, view_size, view);
773 offset = sym_info.next_offset();
778 // Get a view of the main symbol table and the symbol string table.
780 template<int size, bool big_endian>
782 Sized_incremental_binary<size, big_endian>::get_symtab_view(
785 elfcpp::Elf_strtab* strtab)
787 *symtab_view = this->view(this->main_symtab_loc_);
788 *nsyms = this->main_symtab_loc_.data_size / elfcpp::Elf_sizes<size>::sym_size;
790 View strtab_view(this->view(this->main_strtab_loc_));
791 *strtab = elfcpp::Elf_strtab(strtab_view.data(),
792 this->main_strtab_loc_.data_size);
798 // Create a Sized_incremental_binary object of the specified size and
799 // endianness. Fails if the target architecture is not supported.
801 template<int size, bool big_endian>
803 make_sized_incremental_binary(Output_file* file,
804 const elfcpp::Ehdr<size, big_endian>& ehdr)
806 Target* target = select_target(ehdr.get_e_machine(), size, big_endian,
807 ehdr.get_e_ident()[elfcpp::EI_OSABI],
808 ehdr.get_e_ident()[elfcpp::EI_ABIVERSION]);
811 explain_no_incremental(_("unsupported ELF machine number %d"),
812 ehdr.get_e_machine());
816 if (!parameters->target_valid())
817 set_parameters_target(target);
818 else if (target != ¶meters->target())
819 gold_error(_("%s: incompatible target"), file->filename());
821 return new Sized_incremental_binary<size, big_endian>(file, ehdr, target);
824 } // End of anonymous namespace.
826 // Create an Incremental_binary object for FILE. Returns NULL is this is not
827 // possible, e.g. FILE is not an ELF file or has an unsupported target. FILE
831 open_incremental_binary(Output_file* file)
833 off_t filesize = file->filesize();
834 int want = elfcpp::Elf_recognizer::max_header_size;
838 const unsigned char* p = file->get_input_view(0, want);
839 if (!elfcpp::Elf_recognizer::is_elf_file(p, want))
841 explain_no_incremental(_("output is not an ELF file."));
846 bool big_endian = false;
848 if (!elfcpp::Elf_recognizer::is_valid_header(p, want, &size, &big_endian,
851 explain_no_incremental(error.c_str());
855 Incremental_binary* result = NULL;
860 #ifdef HAVE_TARGET_32_BIG
861 result = make_sized_incremental_binary<32, true>(
862 file, elfcpp::Ehdr<32, true>(p));
864 explain_no_incremental(_("unsupported file: 32-bit, big-endian"));
869 #ifdef HAVE_TARGET_32_LITTLE
870 result = make_sized_incremental_binary<32, false>(
871 file, elfcpp::Ehdr<32, false>(p));
873 explain_no_incremental(_("unsupported file: 32-bit, little-endian"));
881 #ifdef HAVE_TARGET_64_BIG
882 result = make_sized_incremental_binary<64, true>(
883 file, elfcpp::Ehdr<64, true>(p));
885 explain_no_incremental(_("unsupported file: 64-bit, big-endian"));
890 #ifdef HAVE_TARGET_64_LITTLE
891 result = make_sized_incremental_binary<64, false>(
892 file, elfcpp::Ehdr<64, false>(p));
894 explain_no_incremental(_("unsupported file: 64-bit, little-endian"));
904 // Class Incremental_inputs.
906 // Add the command line to the string table, setting
907 // command_line_key_. In incremental builds, the command line is
908 // stored in .gnu_incremental_inputs so that the next linker run can
909 // check if the command line options didn't change.
912 Incremental_inputs::report_command_line(int argc, const char* const* argv)
914 // Always store 'gold' as argv[0] to avoid a full relink if the user used a
915 // different path to the linker.
916 std::string args("gold");
917 // Copied from collect_argv in main.cc.
918 for (int i = 1; i < argc; ++i)
920 // Adding/removing these options should not result in a full relink.
921 if (strcmp(argv[i], "--incremental") == 0
922 || strcmp(argv[i], "--incremental-full") == 0
923 || strcmp(argv[i], "--incremental-update") == 0
924 || strcmp(argv[i], "--incremental-changed") == 0
925 || strcmp(argv[i], "--incremental-unchanged") == 0
926 || strcmp(argv[i], "--incremental-unknown") == 0
927 || is_prefix_of("--incremental-base=", argv[i])
928 || is_prefix_of("--incremental-patch=", argv[i])
929 || is_prefix_of("--debug=", argv[i]))
931 if (strcmp(argv[i], "--incremental-base") == 0
932 || strcmp(argv[i], "--incremental-patch") == 0
933 || strcmp(argv[i], "--debug") == 0)
935 // When these options are used without the '=', skip the
936 // following parameter as well.
942 // Now append argv[i], but with all single-quotes escaped
943 const char* argpos = argv[i];
946 const int len = strcspn(argpos, "'");
947 args.append(argpos, len);
948 if (argpos[len] == '\0')
950 args.append("'\"'\"'");
956 this->command_line_ = args;
957 this->strtab_->add(this->command_line_.c_str(), false,
958 &this->command_line_key_);
961 // Record the input archive file ARCHIVE. This is called by the
962 // Add_archive_symbols task before determining which archive members
963 // to include. We create the Incremental_archive_entry here and
964 // attach it to the Archive, but we do not add it to the list of
965 // input objects until report_archive_end is called.
968 Incremental_inputs::report_archive_begin(Library_base* arch,
969 unsigned int arg_serial,
970 Script_info* script_info)
972 Stringpool::Key filename_key;
973 Timespec mtime = arch->get_mtime();
975 // For a file loaded from a script, don't record its argument serial number.
976 if (script_info != NULL)
979 this->strtab_->add(arch->filename().c_str(), false, &filename_key);
980 Incremental_archive_entry* entry =
981 new Incremental_archive_entry(filename_key, arg_serial, mtime);
982 arch->set_incremental_info(entry);
984 if (script_info != NULL)
986 Incremental_script_entry* script_entry = script_info->incremental_info();
987 gold_assert(script_entry != NULL);
988 script_entry->add_object(entry);
992 // Visitor class for processing the unused global symbols in a library.
993 // An instance of this class is passed to the library's
994 // for_all_unused_symbols() iterator, which will call the visit()
995 // function for each global symbol defined in each unused library
996 // member. We add those symbol names to the incremental info for the
999 class Unused_symbol_visitor : public Library_base::Symbol_visitor_base
1002 Unused_symbol_visitor(Incremental_archive_entry* entry, Stringpool* strtab)
1003 : entry_(entry), strtab_(strtab)
1007 visit(const char* sym)
1009 Stringpool::Key symbol_key;
1010 this->strtab_->add(sym, true, &symbol_key);
1011 this->entry_->add_unused_global_symbol(symbol_key);
1015 Incremental_archive_entry* entry_;
1016 Stringpool* strtab_;
1019 // Finish recording the input archive file ARCHIVE. This is called by the
1020 // Add_archive_symbols task after determining which archive members
1024 Incremental_inputs::report_archive_end(Library_base* arch)
1026 Incremental_archive_entry* entry = arch->incremental_info();
1028 gold_assert(entry != NULL);
1029 this->inputs_.push_back(entry);
1031 // Collect unused global symbols.
1032 Unused_symbol_visitor v(entry, this->strtab_);
1033 arch->for_all_unused_symbols(&v);
1036 // Record the input object file OBJ. If ARCH is not NULL, attach
1037 // the object file to the archive. This is called by the
1038 // Add_symbols task after finding out the type of the file.
1041 Incremental_inputs::report_object(Object* obj, unsigned int arg_serial,
1042 Library_base* arch, Script_info* script_info)
1044 Stringpool::Key filename_key;
1045 Timespec mtime = obj->get_mtime();
1047 // For a file loaded from a script, don't record its argument serial number.
1048 if (script_info != NULL)
1051 this->strtab_->add(obj->name().c_str(), false, &filename_key);
1053 Incremental_input_entry* input_entry;
1055 this->current_object_ = obj;
1057 if (!obj->is_dynamic())
1059 this->current_object_entry_ =
1060 new Incremental_object_entry(filename_key, obj, arg_serial, mtime);
1061 input_entry = this->current_object_entry_;
1064 Incremental_archive_entry* arch_entry = arch->incremental_info();
1065 gold_assert(arch_entry != NULL);
1066 arch_entry->add_object(this->current_object_entry_);
1071 this->current_object_entry_ = NULL;
1072 Stringpool::Key soname_key;
1073 Dynobj* dynobj = obj->dynobj();
1074 gold_assert(dynobj != NULL);
1075 this->strtab_->add(dynobj->soname(), false, &soname_key);
1076 input_entry = new Incremental_dynobj_entry(filename_key, soname_key, obj,
1080 if (obj->is_in_system_directory())
1081 input_entry->set_is_in_system_directory();
1083 if (obj->as_needed())
1084 input_entry->set_as_needed();
1086 this->inputs_.push_back(input_entry);
1088 if (script_info != NULL)
1090 Incremental_script_entry* script_entry = script_info->incremental_info();
1091 gold_assert(script_entry != NULL);
1092 script_entry->add_object(input_entry);
1096 // Record an input section SHNDX from object file OBJ.
1099 Incremental_inputs::report_input_section(Object* obj, unsigned int shndx,
1100 const char* name, off_t sh_size)
1102 Stringpool::Key key = 0;
1105 this->strtab_->add(name, true, &key);
1107 gold_assert(obj == this->current_object_);
1108 gold_assert(this->current_object_entry_ != NULL);
1109 this->current_object_entry_->add_input_section(shndx, key, sh_size);
1112 // Record a kept COMDAT group belonging to object file OBJ.
1115 Incremental_inputs::report_comdat_group(Object* obj, const char* name)
1117 Stringpool::Key key = 0;
1120 this->strtab_->add(name, true, &key);
1121 gold_assert(obj == this->current_object_);
1122 gold_assert(this->current_object_entry_ != NULL);
1123 this->current_object_entry_->add_comdat_group(key);
1126 // Record that the input argument INPUT is a script SCRIPT. This is
1127 // called by read_script after parsing the script and reading the list
1128 // of inputs added by this script.
1131 Incremental_inputs::report_script(Script_info* script,
1132 unsigned int arg_serial,
1135 Stringpool::Key filename_key;
1137 this->strtab_->add(script->filename().c_str(), false, &filename_key);
1138 Incremental_script_entry* entry =
1139 new Incremental_script_entry(filename_key, arg_serial, script, mtime);
1140 this->inputs_.push_back(entry);
1141 script->set_incremental_info(entry);
1144 // Finalize the incremental link information. Called from
1145 // Layout::finalize.
1148 Incremental_inputs::finalize()
1150 // Finalize the string table.
1151 this->strtab_->set_string_offsets();
1154 // Create the .gnu_incremental_inputs, _symtab, and _relocs input sections.
1157 Incremental_inputs::create_data_sections(Symbol_table* symtab)
1159 switch (parameters->size_and_endianness())
1161 #ifdef HAVE_TARGET_32_LITTLE
1162 case Parameters::TARGET_32_LITTLE:
1163 this->inputs_section_ =
1164 new Output_section_incremental_inputs<32, false>(this, symtab);
1167 #ifdef HAVE_TARGET_32_BIG
1168 case Parameters::TARGET_32_BIG:
1169 this->inputs_section_ =
1170 new Output_section_incremental_inputs<32, true>(this, symtab);
1173 #ifdef HAVE_TARGET_64_LITTLE
1174 case Parameters::TARGET_64_LITTLE:
1175 this->inputs_section_ =
1176 new Output_section_incremental_inputs<64, false>(this, symtab);
1179 #ifdef HAVE_TARGET_64_BIG
1180 case Parameters::TARGET_64_BIG:
1181 this->inputs_section_ =
1182 new Output_section_incremental_inputs<64, true>(this, symtab);
1188 this->symtab_section_ = new Output_data_space(4, "** incremental_symtab");
1189 this->relocs_section_ = new Output_data_space(4, "** incremental_relocs");
1190 this->got_plt_section_ = new Output_data_space(4, "** incremental_got_plt");
1193 // Return the sh_entsize value for the .gnu_incremental_relocs section.
1195 Incremental_inputs::relocs_entsize() const
1197 return 8 + 2 * parameters->target().get_size() / 8;
1200 // Class Output_section_incremental_inputs.
1202 // Finalize the offsets for each input section and supplemental info block,
1203 // and set the final data size of the incremental output sections.
1205 template<int size, bool big_endian>
1207 Output_section_incremental_inputs<size, big_endian>::set_final_data_size()
1209 const Incremental_inputs* inputs = this->inputs_;
1210 const unsigned int sizeof_addr = size / 8;
1211 const unsigned int rel_size = 8 + 2 * sizeof_addr;
1213 // Offset of each input entry.
1214 unsigned int input_offset = this->header_size;
1216 // Offset of each supplemental info block.
1217 unsigned int file_index = 0;
1218 unsigned int info_offset = this->header_size;
1219 info_offset += this->input_entry_size * inputs->input_file_count();
1221 // Count each input file and its supplemental information block.
1222 for (Incremental_inputs::Input_list::const_iterator p =
1223 inputs->input_files().begin();
1224 p != inputs->input_files().end();
1227 // Set the index and offset of the input file entry.
1228 (*p)->set_offset(file_index, input_offset);
1230 input_offset += this->input_entry_size;
1232 // Set the offset of the supplemental info block.
1233 switch ((*p)->type())
1235 case INCREMENTAL_INPUT_SCRIPT:
1237 Incremental_script_entry *entry = (*p)->script_entry();
1238 gold_assert(entry != NULL);
1239 (*p)->set_info_offset(info_offset);
1243 info_offset += (entry->get_object_count() * 4);
1246 case INCREMENTAL_INPUT_OBJECT:
1247 case INCREMENTAL_INPUT_ARCHIVE_MEMBER:
1249 Incremental_object_entry* entry = (*p)->object_entry();
1250 gold_assert(entry != NULL);
1251 (*p)->set_info_offset(info_offset);
1252 // Input section count, global symbol count, local symbol offset,
1253 // local symbol count, first dynamic reloc, dynamic reloc count,
1254 // comdat group count.
1256 // Each input section.
1257 info_offset += (entry->get_input_section_count()
1258 * (8 + 2 * sizeof_addr));
1259 // Each global symbol.
1260 const Object::Symbols* syms = entry->object()->get_global_symbols();
1261 info_offset += syms->size() * 20;
1262 // Each comdat group.
1263 info_offset += entry->get_comdat_group_count() * 4;
1266 case INCREMENTAL_INPUT_SHARED_LIBRARY:
1268 Incremental_dynobj_entry* entry = (*p)->dynobj_entry();
1269 gold_assert(entry != NULL);
1270 (*p)->set_info_offset(info_offset);
1271 // Global symbol count, soname index.
1273 // Each global symbol.
1274 const Object::Symbols* syms = entry->object()->get_global_symbols();
1275 gold_assert(syms != NULL);
1276 unsigned int nsyms = syms->size();
1277 unsigned int nsyms_out = 0;
1278 for (unsigned int i = 0; i < nsyms; ++i)
1280 const Symbol* sym = (*syms)[i];
1283 if (sym->is_forwarder())
1284 sym = this->symtab_->resolve_forwards(sym);
1285 if (sym->symtab_index() != -1U)
1288 info_offset += nsyms_out * 4;
1291 case INCREMENTAL_INPUT_ARCHIVE:
1293 Incremental_archive_entry* entry = (*p)->archive_entry();
1294 gold_assert(entry != NULL);
1295 (*p)->set_info_offset(info_offset);
1296 // Member count + unused global symbol count.
1299 info_offset += (entry->get_member_count() * 4);
1300 // Each global symbol.
1301 info_offset += (entry->get_unused_global_symbol_count() * 4);
1309 this->set_data_size(info_offset);
1311 // Set the size of the .gnu_incremental_symtab section.
1312 inputs->symtab_section()->set_current_data_size(this->symtab_->output_count()
1313 * sizeof(unsigned int));
1315 // Set the size of the .gnu_incremental_relocs section.
1316 inputs->relocs_section()->set_current_data_size(inputs->get_reloc_count()
1319 // Set the size of the .gnu_incremental_got_plt section.
1320 Sized_target<size, big_endian>* target =
1321 parameters->sized_target<size, big_endian>();
1322 unsigned int got_count = target->got_entry_count();
1323 unsigned int plt_count = target->plt_entry_count();
1324 unsigned int got_plt_size = 8; // GOT entry count, PLT entry count.
1325 got_plt_size = (got_plt_size + got_count + 3) & ~3; // GOT type array.
1326 got_plt_size += got_count * 8 + plt_count * 4; // GOT array, PLT array.
1327 inputs->got_plt_section()->set_current_data_size(got_plt_size);
1330 // Write the contents of the .gnu_incremental_inputs and
1331 // .gnu_incremental_symtab sections.
1333 template<int size, bool big_endian>
1335 Output_section_incremental_inputs<size, big_endian>::do_write(Output_file* of)
1337 const Incremental_inputs* inputs = this->inputs_;
1338 Stringpool* strtab = inputs->get_stringpool();
1340 // Get a view into the .gnu_incremental_inputs section.
1341 const off_t off = this->offset();
1342 const off_t oview_size = this->data_size();
1343 unsigned char* const oview = of->get_output_view(off, oview_size);
1344 unsigned char* pov = oview;
1346 // Get a view into the .gnu_incremental_symtab section.
1347 const off_t symtab_off = inputs->symtab_section()->offset();
1348 const off_t symtab_size = inputs->symtab_section()->data_size();
1349 unsigned char* const symtab_view = of->get_output_view(symtab_off,
1352 // Allocate an array of linked list heads for the .gnu_incremental_symtab
1353 // section. Each element corresponds to a global symbol in the output
1354 // symbol table, and points to the head of the linked list that threads
1355 // through the object file input entries. The value of each element
1356 // is the section-relative offset to a global symbol entry in a
1357 // supplemental information block.
1358 unsigned int global_sym_count = this->symtab_->output_count();
1359 unsigned int* global_syms = new unsigned int[global_sym_count];
1360 memset(global_syms, 0, global_sym_count * sizeof(unsigned int));
1362 // Write the section header.
1363 Stringpool::Key command_line_key = inputs->command_line_key();
1364 pov = this->write_header(pov, inputs->input_file_count(),
1365 strtab->get_offset_from_key(command_line_key));
1367 // Write the list of input files.
1368 pov = this->write_input_files(oview, pov, strtab);
1370 // Write the supplemental information blocks for each input file.
1371 pov = this->write_info_blocks(oview, pov, strtab, global_syms,
1374 gold_assert(pov - oview == oview_size);
1376 // Write the .gnu_incremental_symtab section.
1377 gold_assert(global_sym_count * 4 == symtab_size);
1378 this->write_symtab(symtab_view, global_syms, global_sym_count);
1380 delete[] global_syms;
1382 // Write the .gnu_incremental_got_plt section.
1383 const off_t got_plt_off = inputs->got_plt_section()->offset();
1384 const off_t got_plt_size = inputs->got_plt_section()->data_size();
1385 unsigned char* const got_plt_view = of->get_output_view(got_plt_off,
1387 this->write_got_plt(got_plt_view, got_plt_size);
1389 of->write_output_view(off, oview_size, oview);
1390 of->write_output_view(symtab_off, symtab_size, symtab_view);
1391 of->write_output_view(got_plt_off, got_plt_size, got_plt_view);
1394 // Write the section header: version, input file count, offset of command line
1395 // in the string table, and 4 bytes of padding.
1397 template<int size, bool big_endian>
1399 Output_section_incremental_inputs<size, big_endian>::write_header(
1401 unsigned int input_file_count,
1402 section_offset_type command_line_offset)
1404 Swap32::writeval(pov, INCREMENTAL_LINK_VERSION);
1405 Swap32::writeval(pov + 4, input_file_count);
1406 Swap32::writeval(pov + 8, command_line_offset);
1407 Swap32::writeval(pov + 12, 0);
1408 return pov + this->header_size;
1411 // Write the input file entries.
1413 template<int size, bool big_endian>
1415 Output_section_incremental_inputs<size, big_endian>::write_input_files(
1416 unsigned char* oview,
1420 const Incremental_inputs* inputs = this->inputs_;
1422 for (Incremental_inputs::Input_list::const_iterator p =
1423 inputs->input_files().begin();
1424 p != inputs->input_files().end();
1427 gold_assert(static_cast<unsigned int>(pov - oview) == (*p)->get_offset());
1428 section_offset_type filename_offset =
1429 strtab->get_offset_from_key((*p)->get_filename_key());
1430 const Timespec& mtime = (*p)->get_mtime();
1431 unsigned int flags = (*p)->type();
1432 if ((*p)->is_in_system_directory())
1433 flags |= INCREMENTAL_INPUT_IN_SYSTEM_DIR;
1434 if ((*p)->as_needed())
1435 flags |= INCREMENTAL_INPUT_AS_NEEDED;
1436 Swap32::writeval(pov, filename_offset);
1437 Swap32::writeval(pov + 4, (*p)->get_info_offset());
1438 Swap64::writeval(pov + 8, mtime.seconds);
1439 Swap32::writeval(pov + 16, mtime.nanoseconds);
1440 Swap16::writeval(pov + 20, flags);
1441 Swap16::writeval(pov + 22, (*p)->arg_serial());
1442 pov += this->input_entry_size;
1447 // Write the supplemental information blocks.
1449 template<int size, bool big_endian>
1451 Output_section_incremental_inputs<size, big_endian>::write_info_blocks(
1452 unsigned char* oview,
1455 unsigned int* global_syms,
1456 unsigned int global_sym_count)
1458 const Incremental_inputs* inputs = this->inputs_;
1459 unsigned int first_global_index = this->symtab_->first_global_index();
1461 for (Incremental_inputs::Input_list::const_iterator p =
1462 inputs->input_files().begin();
1463 p != inputs->input_files().end();
1466 switch ((*p)->type())
1468 case INCREMENTAL_INPUT_SCRIPT:
1470 gold_assert(static_cast<unsigned int>(pov - oview)
1471 == (*p)->get_info_offset());
1472 Incremental_script_entry* entry = (*p)->script_entry();
1473 gold_assert(entry != NULL);
1475 // Write the object count.
1476 unsigned int nobjects = entry->get_object_count();
1477 Swap32::writeval(pov, nobjects);
1480 // For each object, write the offset to its input file entry.
1481 for (unsigned int i = 0; i < nobjects; ++i)
1483 Incremental_input_entry* obj = entry->get_object(i);
1484 Swap32::writeval(pov, obj->get_offset());
1490 case INCREMENTAL_INPUT_OBJECT:
1491 case INCREMENTAL_INPUT_ARCHIVE_MEMBER:
1493 gold_assert(static_cast<unsigned int>(pov - oview)
1494 == (*p)->get_info_offset());
1495 Incremental_object_entry* entry = (*p)->object_entry();
1496 gold_assert(entry != NULL);
1497 const Object* obj = entry->object();
1498 const Relobj* relobj = static_cast<const Relobj*>(obj);
1499 const Object::Symbols* syms = obj->get_global_symbols();
1500 // Write the input section count and global symbol count.
1501 unsigned int nsections = entry->get_input_section_count();
1502 unsigned int nsyms = syms->size();
1503 off_t locals_offset = relobj->local_symbol_offset();
1504 unsigned int nlocals = relobj->output_local_symbol_count();
1505 unsigned int first_dynrel = relobj->first_dyn_reloc();
1506 unsigned int ndynrel = relobj->dyn_reloc_count();
1507 unsigned int ncomdat = entry->get_comdat_group_count();
1508 Swap32::writeval(pov, nsections);
1509 Swap32::writeval(pov + 4, nsyms);
1510 Swap32::writeval(pov + 8, static_cast<unsigned int>(locals_offset));
1511 Swap32::writeval(pov + 12, nlocals);
1512 Swap32::writeval(pov + 16, first_dynrel);
1513 Swap32::writeval(pov + 20, ndynrel);
1514 Swap32::writeval(pov + 24, ncomdat);
1517 // Build a temporary array to map input section indexes
1518 // from the original object file index to the index in the
1519 // incremental info table.
1520 unsigned int* index_map = new unsigned int[obj->shnum()];
1521 memset(index_map, 0, obj->shnum() * sizeof(unsigned int));
1523 // For each input section, write the name, output section index,
1524 // offset within output section, and input section size.
1525 for (unsigned int i = 0; i < nsections; i++)
1527 unsigned int shndx = entry->get_input_section_index(i);
1528 index_map[shndx] = i + 1;
1529 Stringpool::Key key = entry->get_input_section_name_key(i);
1530 off_t name_offset = 0;
1532 name_offset = strtab->get_offset_from_key(key);
1534 off_t out_offset = 0;
1536 Output_section* os = obj->output_section(shndx);
1539 out_shndx = os->out_shndx();
1540 out_offset = obj->output_section_offset(shndx);
1541 sh_size = entry->get_input_section_size(i);
1543 Swap32::writeval(pov, name_offset);
1544 Swap32::writeval(pov + 4, out_shndx);
1545 Swap::writeval(pov + 8, out_offset);
1546 Swap::writeval(pov + 8 + sizeof_addr, sh_size);
1547 pov += 8 + 2 * sizeof_addr;
1550 // For each global symbol, write its associated relocations,
1551 // add it to the linked list of globals, then write the
1552 // supplemental information: global symbol table index,
1553 // input section index, linked list chain pointer, relocation
1554 // count, and offset to the relocations.
1555 for (unsigned int i = 0; i < nsyms; i++)
1557 const Symbol* sym = (*syms)[i];
1558 if (sym->is_forwarder())
1559 sym = this->symtab_->resolve_forwards(sym);
1560 unsigned int shndx = 0;
1561 if (sym->source() != Symbol::FROM_OBJECT)
1563 // The symbol was defined by the linker (e.g., common).
1564 // We mark these symbols with a special SHNDX of -1,
1565 // but exclude linker-predefined symbols and symbols
1566 // copied from shared objects.
1567 if (!sym->is_predefined()
1568 && !sym->is_copied_from_dynobj())
1571 else if (sym->object() == obj && sym->is_defined())
1574 unsigned int orig_shndx = sym->shndx(&is_ordinary);
1576 shndx = index_map[orig_shndx];
1580 unsigned int symtab_index = sym->symtab_index();
1581 unsigned int chain = 0;
1582 unsigned int first_reloc = 0;
1583 unsigned int nrelocs = obj->get_incremental_reloc_count(i);
1586 gold_assert(symtab_index != -1U
1587 && (symtab_index - first_global_index
1588 < global_sym_count));
1589 first_reloc = obj->get_incremental_reloc_base(i);
1590 chain = global_syms[symtab_index - first_global_index];
1591 global_syms[symtab_index - first_global_index] =
1594 Swap32::writeval(pov, symtab_index);
1595 Swap32::writeval(pov + 4, shndx);
1596 Swap32::writeval(pov + 8, chain);
1597 Swap32::writeval(pov + 12, nrelocs);
1598 Swap32::writeval(pov + 16, first_reloc * 3 * sizeof_addr);
1602 // For each kept COMDAT group, write the group signature.
1603 for (unsigned int i = 0; i < ncomdat; i++)
1605 Stringpool::Key key = entry->get_comdat_signature_key(i);
1606 off_t name_offset = 0;
1608 name_offset = strtab->get_offset_from_key(key);
1609 Swap32::writeval(pov, name_offset);
1617 case INCREMENTAL_INPUT_SHARED_LIBRARY:
1619 gold_assert(static_cast<unsigned int>(pov - oview)
1620 == (*p)->get_info_offset());
1621 Incremental_dynobj_entry* entry = (*p)->dynobj_entry();
1622 gold_assert(entry != NULL);
1623 Object* obj = entry->object();
1624 Dynobj* dynobj = obj->dynobj();
1625 gold_assert(dynobj != NULL);
1626 const Object::Symbols* syms = obj->get_global_symbols();
1628 // Write the soname string table index.
1629 section_offset_type soname_offset =
1630 strtab->get_offset_from_key(entry->get_soname_key());
1631 Swap32::writeval(pov, soname_offset);
1634 // Skip the global symbol count for now.
1635 unsigned char* orig_pov = pov;
1638 // For each global symbol, write the global symbol table index.
1639 unsigned int nsyms = syms->size();
1640 unsigned int nsyms_out = 0;
1641 for (unsigned int i = 0; i < nsyms; i++)
1643 const Symbol* sym = (*syms)[i];
1646 if (sym->is_forwarder())
1647 sym = this->symtab_->resolve_forwards(sym);
1648 if (sym->symtab_index() == -1U)
1650 unsigned int flags = 0;
1651 if (sym->source() == Symbol::FROM_OBJECT
1652 && sym->object() == obj
1653 && sym->is_defined())
1654 flags = INCREMENTAL_SHLIB_SYM_DEF;
1655 else if (sym->is_copied_from_dynobj()
1656 && this->symtab_->get_copy_source(sym) == dynobj)
1657 flags = INCREMENTAL_SHLIB_SYM_COPY;
1658 flags <<= INCREMENTAL_SHLIB_SYM_FLAGS_SHIFT;
1659 Swap32::writeval(pov, sym->symtab_index() | flags);
1664 // Now write the global symbol count.
1665 Swap32::writeval(orig_pov, nsyms_out);
1669 case INCREMENTAL_INPUT_ARCHIVE:
1671 gold_assert(static_cast<unsigned int>(pov - oview)
1672 == (*p)->get_info_offset());
1673 Incremental_archive_entry* entry = (*p)->archive_entry();
1674 gold_assert(entry != NULL);
1676 // Write the member count and unused global symbol count.
1677 unsigned int nmembers = entry->get_member_count();
1678 unsigned int nsyms = entry->get_unused_global_symbol_count();
1679 Swap32::writeval(pov, nmembers);
1680 Swap32::writeval(pov + 4, nsyms);
1683 // For each member, write the offset to its input file entry.
1684 for (unsigned int i = 0; i < nmembers; ++i)
1686 Incremental_object_entry* member = entry->get_member(i);
1687 Swap32::writeval(pov, member->get_offset());
1691 // For each global symbol, write the name offset.
1692 for (unsigned int i = 0; i < nsyms; ++i)
1694 Stringpool::Key key = entry->get_unused_global_symbol(i);
1695 Swap32::writeval(pov, strtab->get_offset_from_key(key));
1708 // Write the contents of the .gnu_incremental_symtab section.
1710 template<int size, bool big_endian>
1712 Output_section_incremental_inputs<size, big_endian>::write_symtab(
1714 unsigned int* global_syms,
1715 unsigned int global_sym_count)
1717 for (unsigned int i = 0; i < global_sym_count; ++i)
1719 Swap32::writeval(pov, global_syms[i]);
1724 // This struct holds the view information needed to write the
1725 // .gnu_incremental_got_plt section.
1727 struct Got_plt_view_info
1729 // Start of the GOT type array in the output view.
1730 unsigned char* got_type_p;
1731 // Start of the GOT descriptor array in the output view.
1732 unsigned char* got_desc_p;
1733 // Start of the PLT descriptor array in the output view.
1734 unsigned char* plt_desc_p;
1735 // Number of GOT entries.
1736 unsigned int got_count;
1737 // Number of PLT entries.
1738 unsigned int plt_count;
1739 // Offset of the first non-reserved PLT entry (this is a target-dependent value).
1740 unsigned int first_plt_entry_offset;
1741 // Size of a PLT entry (this is a target-dependent value).
1742 unsigned int plt_entry_size;
1743 // Symbol index to write in the GOT descriptor array. For global symbols,
1744 // this is the global symbol table index; for local symbols, it is the
1745 // local symbol table index.
1746 unsigned int sym_index;
1747 // Input file index to write in the GOT descriptor array. For global
1748 // symbols, this is 0; for local symbols, it is the index of the input
1749 // file entry in the .gnu_incremental_inputs section.
1750 unsigned int input_index;
1753 // Functor class for processing a GOT offset list for local symbols.
1754 // Writes the GOT type and symbol index into the GOT type and descriptor
1755 // arrays in the output section.
1757 template<int size, bool big_endian>
1758 class Local_got_offset_visitor : public Got_offset_list::Visitor
1761 Local_got_offset_visitor(struct Got_plt_view_info& info)
1766 visit(unsigned int got_type, unsigned int got_offset)
1768 unsigned int got_index = got_offset / this->got_entry_size_;
1769 gold_assert(got_index < this->info_.got_count);
1770 // We can only handle GOT entry types in the range 0..0x7e
1771 // because we use a byte array to store them, and we use the
1772 // high bit to flag a local symbol.
1773 gold_assert(got_type < 0x7f);
1774 this->info_.got_type_p[got_index] = got_type | 0x80;
1775 unsigned char* pov = this->info_.got_desc_p + got_index * 8;
1776 elfcpp::Swap<32, big_endian>::writeval(pov, this->info_.sym_index);
1777 elfcpp::Swap<32, big_endian>::writeval(pov + 4, this->info_.input_index);
1781 static const unsigned int got_entry_size_ = size / 8;
1782 struct Got_plt_view_info& info_;
1785 // Functor class for processing a GOT offset list. Writes the GOT type
1786 // and symbol index into the GOT type and descriptor arrays in the output
1789 template<int size, bool big_endian>
1790 class Global_got_offset_visitor : public Got_offset_list::Visitor
1793 Global_got_offset_visitor(struct Got_plt_view_info& info)
1798 visit(unsigned int got_type, unsigned int got_offset)
1800 unsigned int got_index = got_offset / this->got_entry_size_;
1801 gold_assert(got_index < this->info_.got_count);
1802 // We can only handle GOT entry types in the range 0..0x7e
1803 // because we use a byte array to store them, and we use the
1804 // high bit to flag a local symbol.
1805 gold_assert(got_type < 0x7f);
1806 this->info_.got_type_p[got_index] = got_type;
1807 unsigned char* pov = this->info_.got_desc_p + got_index * 8;
1808 elfcpp::Swap<32, big_endian>::writeval(pov, this->info_.sym_index);
1809 elfcpp::Swap<32, big_endian>::writeval(pov + 4, 0);
1813 static const unsigned int got_entry_size_ = size / 8;
1814 struct Got_plt_view_info& info_;
1817 // Functor class for processing the global symbol table. Processes the
1818 // GOT offset list for the symbol, and writes the symbol table index
1819 // into the PLT descriptor array in the output section.
1821 template<int size, bool big_endian>
1822 class Global_symbol_visitor_got_plt
1825 Global_symbol_visitor_got_plt(struct Got_plt_view_info& info)
1830 operator()(const Sized_symbol<size>* sym)
1832 typedef Global_got_offset_visitor<size, big_endian> Got_visitor;
1833 const Got_offset_list* got_offsets = sym->got_offset_list();
1834 if (got_offsets != NULL)
1836 this->info_.sym_index = sym->symtab_index();
1837 this->info_.input_index = 0;
1838 Got_visitor v(this->info_);
1839 got_offsets->for_all_got_offsets(&v);
1841 if (sym->has_plt_offset())
1843 unsigned int plt_index =
1844 ((sym->plt_offset() - this->info_.first_plt_entry_offset)
1845 / this->info_.plt_entry_size);
1846 gold_assert(plt_index < this->info_.plt_count);
1847 unsigned char* pov = this->info_.plt_desc_p + plt_index * 4;
1848 elfcpp::Swap<32, big_endian>::writeval(pov, sym->symtab_index());
1853 struct Got_plt_view_info& info_;
1856 // Write the contents of the .gnu_incremental_got_plt section.
1858 template<int size, bool big_endian>
1860 Output_section_incremental_inputs<size, big_endian>::write_got_plt(
1864 Sized_target<size, big_endian>* target =
1865 parameters->sized_target<size, big_endian>();
1867 // Set up the view information for the functors.
1868 struct Got_plt_view_info view_info;
1869 view_info.got_count = target->got_entry_count();
1870 view_info.plt_count = target->plt_entry_count();
1871 view_info.first_plt_entry_offset = target->first_plt_entry_offset();
1872 view_info.plt_entry_size = target->plt_entry_size();
1873 view_info.got_type_p = pov + 8;
1874 view_info.got_desc_p = (view_info.got_type_p
1875 + ((view_info.got_count + 3) & ~3));
1876 view_info.plt_desc_p = view_info.got_desc_p + view_info.got_count * 8;
1878 gold_assert(pov + view_size ==
1879 view_info.plt_desc_p + view_info.plt_count * 4);
1881 // Write the section header.
1882 Swap32::writeval(pov, view_info.got_count);
1883 Swap32::writeval(pov + 4, view_info.plt_count);
1885 // Initialize the GOT type array to 0xff (reserved).
1886 memset(view_info.got_type_p, 0xff, view_info.got_count);
1888 // Write the incremental GOT descriptors for local symbols.
1889 typedef Local_got_offset_visitor<size, big_endian> Got_visitor;
1890 for (Incremental_inputs::Input_list::const_iterator p =
1891 this->inputs_->input_files().begin();
1892 p != this->inputs_->input_files().end();
1895 if ((*p)->type() != INCREMENTAL_INPUT_OBJECT
1896 && (*p)->type() != INCREMENTAL_INPUT_ARCHIVE_MEMBER)
1898 Incremental_object_entry* entry = (*p)->object_entry();
1899 gold_assert(entry != NULL);
1900 const Object* obj = entry->object();
1901 gold_assert(obj != NULL);
1902 view_info.input_index = (*p)->get_file_index();
1903 Got_visitor v(view_info);
1904 obj->for_all_local_got_entries(&v);
1907 // Write the incremental GOT and PLT descriptors for global symbols.
1908 typedef Global_symbol_visitor_got_plt<size, big_endian> Symbol_visitor;
1909 symtab_->for_all_symbols<size, Symbol_visitor>(Symbol_visitor(view_info));
1912 // Class Sized_relobj_incr. Most of these methods are not used for
1913 // Incremental objects, but are required to be implemented by the
1914 // base class Object.
1916 template<int size, bool big_endian>
1917 Sized_relobj_incr<size, big_endian>::Sized_relobj_incr(
1918 const std::string& name,
1919 Sized_incremental_binary<size, big_endian>* ibase,
1920 unsigned int input_file_index)
1921 : Sized_relobj<size, big_endian>(name, NULL), ibase_(ibase),
1922 input_file_index_(input_file_index),
1923 input_reader_(ibase->inputs_reader().input_file(input_file_index)),
1924 local_symbol_count_(0), output_local_dynsym_count_(0),
1925 local_symbol_index_(0), local_symbol_offset_(0), local_dynsym_offset_(0),
1926 symbols_(), incr_reloc_offset_(-1U), incr_reloc_count_(0),
1927 incr_reloc_output_index_(0), incr_relocs_(NULL), local_symbols_()
1929 if (this->input_reader_.is_in_system_directory())
1930 this->set_is_in_system_directory();
1931 const unsigned int shnum = this->input_reader_.get_input_section_count() + 1;
1932 this->set_shnum(shnum);
1933 ibase->set_input_object(input_file_index, this);
1936 // Read the symbols.
1938 template<int size, bool big_endian>
1940 Sized_relobj_incr<size, big_endian>::do_read_symbols(Read_symbols_data*)
1945 // Lay out the input sections.
1947 template<int size, bool big_endian>
1949 Sized_relobj_incr<size, big_endian>::do_layout(
1954 const unsigned int shnum = this->shnum();
1955 Incremental_inputs* incremental_inputs = layout->incremental_inputs();
1956 gold_assert(incremental_inputs != NULL);
1957 Output_sections& out_sections(this->output_sections());
1958 out_sections.resize(shnum);
1959 this->section_offsets().resize(shnum);
1960 for (unsigned int i = 1; i < shnum; i++)
1962 typename Input_entry_reader::Input_section_info sect =
1963 this->input_reader_.get_input_section(i - 1);
1964 // Add the section to the incremental inputs layout.
1965 incremental_inputs->report_input_section(this, i, sect.name,
1967 if (sect.output_shndx == 0 || sect.sh_offset == -1)
1969 Output_section* os = this->ibase_->output_section(sect.output_shndx);
1970 gold_assert(os != NULL);
1971 out_sections[i] = os;
1972 this->section_offsets()[i] = static_cast<Address>(sect.sh_offset);
1975 // Process the COMDAT groups.
1976 unsigned int ncomdat = this->input_reader_.get_comdat_group_count();
1977 for (unsigned int i = 0; i < ncomdat; i++)
1979 const char* signature = this->input_reader_.get_comdat_group_signature(i);
1980 if (signature == NULL || signature[0] == '\0')
1981 this->error(_("COMDAT group has no signature"));
1982 bool keep = layout->find_or_add_kept_section(signature, this, i, true,
1985 this->error(_("COMDAT group %s included twice in incremental link"),
1990 // Layout sections whose layout was deferred while waiting for
1991 // input files from a plugin.
1992 template<int size, bool big_endian>
1994 Sized_relobj_incr<size, big_endian>::do_layout_deferred_sections(Layout*)
1998 // Add the symbols to the symbol table.
2000 template<int size, bool big_endian>
2002 Sized_relobj_incr<size, big_endian>::do_add_symbols(
2003 Symbol_table* symtab,
2007 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
2008 unsigned char symbuf[sym_size];
2009 elfcpp::Sym<size, big_endian> sym(symbuf);
2010 elfcpp::Sym_write<size, big_endian> osym(symbuf);
2012 typedef typename elfcpp::Elf_types<size>::Elf_WXword Elf_size_type;
2014 unsigned int nsyms = this->input_reader_.get_global_symbol_count();
2015 this->symbols_.resize(nsyms);
2017 Incremental_binary::View symtab_view(NULL);
2018 unsigned int symtab_count;
2019 elfcpp::Elf_strtab strtab(NULL, 0);
2020 this->ibase_->get_symtab_view(&symtab_view, &symtab_count, &strtab);
2022 Incremental_symtab_reader<big_endian> isymtab(this->ibase_->symtab_reader());
2023 unsigned int isym_count = isymtab.symbol_count();
2024 unsigned int first_global = symtab_count - isym_count;
2026 const unsigned char* sym_p;
2027 for (unsigned int i = 0; i < nsyms; ++i)
2029 Incremental_global_symbol_reader<big_endian> info =
2030 this->input_reader_.get_global_symbol_reader(i);
2031 unsigned int output_symndx = info.output_symndx();
2032 sym_p = symtab_view.data() + output_symndx * sym_size;
2033 elfcpp::Sym<size, big_endian> gsym(sym_p);
2035 if (!strtab.get_c_string(gsym.get_st_name(), &name))
2038 typename elfcpp::Elf_types<size>::Elf_Addr v = gsym.get_st_value();
2039 unsigned int shndx = gsym.get_st_shndx();
2040 elfcpp::STB st_bind = gsym.get_st_bind();
2041 elfcpp::STT st_type = gsym.get_st_type();
2043 // Local hidden symbols start out as globals, but get converted to
2044 // to local during output.
2045 if (st_bind == elfcpp::STB_LOCAL)
2046 st_bind = elfcpp::STB_GLOBAL;
2048 unsigned int input_shndx = info.shndx();
2049 if (input_shndx == 0 || input_shndx == -1U)
2051 shndx = elfcpp::SHN_UNDEF;
2054 else if (shndx != elfcpp::SHN_ABS)
2056 // Find the input section and calculate the section-relative value.
2057 gold_assert(shndx != elfcpp::SHN_UNDEF);
2058 Output_section* os = this->ibase_->output_section(shndx);
2059 gold_assert(os != NULL && os->has_fixed_layout());
2060 typename Input_entry_reader::Input_section_info sect =
2061 this->input_reader_.get_input_section(input_shndx - 1);
2062 gold_assert(sect.output_shndx == shndx);
2063 if (st_type != elfcpp::STT_TLS)
2065 v -= sect.sh_offset;
2066 shndx = input_shndx;
2069 osym.put_st_name(0);
2070 osym.put_st_value(v);
2071 osym.put_st_size(gsym.get_st_size());
2072 osym.put_st_info(st_bind, st_type);
2073 osym.put_st_other(gsym.get_st_other());
2074 osym.put_st_shndx(shndx);
2076 Symbol* res = symtab->add_from_incrobj(this, name, NULL, &sym);
2078 // If this is a linker-defined symbol that hasn't yet been defined,
2080 if (input_shndx == -1U && !res->is_defined())
2082 shndx = gsym.get_st_shndx();
2083 v = gsym.get_st_value();
2084 Elf_size_type symsize = gsym.get_st_size();
2085 if (shndx == elfcpp::SHN_ABS)
2087 symtab->define_as_constant(name, NULL,
2088 Symbol_table::INCREMENTAL_BASE,
2089 v, symsize, st_type, st_bind,
2090 gsym.get_st_visibility(), 0,
2095 Output_section* os = this->ibase_->output_section(shndx);
2096 gold_assert(os != NULL && os->has_fixed_layout());
2099 os->reserve(v, symsize);
2100 symtab->define_in_output_data(name, NULL,
2101 Symbol_table::INCREMENTAL_BASE,
2102 os, v, symsize, st_type, st_bind,
2103 gsym.get_st_visibility(), 0,
2108 this->symbols_[i] = res;
2109 this->ibase_->add_global_symbol(output_symndx - first_global, res);
2113 // Return TRUE if we should include this object from an archive library.
2115 template<int size, bool big_endian>
2116 Archive::Should_include
2117 Sized_relobj_incr<size, big_endian>::do_should_include_member(
2126 // Iterate over global symbols, calling a visitor class V for each.
2128 template<int size, bool big_endian>
2130 Sized_relobj_incr<size, big_endian>::do_for_all_global_symbols(
2132 Library_base::Symbol_visitor_base*)
2134 // This routine is not used for incremental objects.
2137 // Get the size of a section.
2139 template<int size, bool big_endian>
2141 Sized_relobj_incr<size, big_endian>::do_section_size(unsigned int)
2146 // Get the name of a section.
2148 template<int size, bool big_endian>
2150 Sized_relobj_incr<size, big_endian>::do_section_name(unsigned int)
2155 // Return a view of the contents of a section.
2157 template<int size, bool big_endian>
2159 Sized_relobj_incr<size, big_endian>::do_section_contents(unsigned int)
2164 // Return section flags.
2166 template<int size, bool big_endian>
2168 Sized_relobj_incr<size, big_endian>::do_section_flags(unsigned int)
2173 // Return section entsize.
2175 template<int size, bool big_endian>
2177 Sized_relobj_incr<size, big_endian>::do_section_entsize(unsigned int)
2182 // Return section address.
2184 template<int size, bool big_endian>
2186 Sized_relobj_incr<size, big_endian>::do_section_address(unsigned int)
2191 // Return section type.
2193 template<int size, bool big_endian>
2195 Sized_relobj_incr<size, big_endian>::do_section_type(unsigned int)
2200 // Return the section link field.
2202 template<int size, bool big_endian>
2204 Sized_relobj_incr<size, big_endian>::do_section_link(unsigned int)
2209 // Return the section link field.
2211 template<int size, bool big_endian>
2213 Sized_relobj_incr<size, big_endian>::do_section_info(unsigned int)
2218 // Return the section alignment.
2220 template<int size, bool big_endian>
2222 Sized_relobj_incr<size, big_endian>::do_section_addralign(unsigned int)
2227 // Return the Xindex structure to use.
2229 template<int size, bool big_endian>
2231 Sized_relobj_incr<size, big_endian>::do_initialize_xindex()
2236 // Get symbol counts.
2238 template<int size, bool big_endian>
2240 Sized_relobj_incr<size, big_endian>::do_get_global_symbol_counts(
2241 const Symbol_table*, size_t*, size_t*) const
2248 template<int size, bool big_endian>
2250 Sized_relobj_incr<size, big_endian>::do_read_relocs(Read_relocs_data*)
2254 // Process the relocs to find list of referenced sections. Used only
2255 // during garbage collection.
2257 template<int size, bool big_endian>
2259 Sized_relobj_incr<size, big_endian>::do_gc_process_relocs(Symbol_table*,
2266 // Scan the relocs and adjust the symbol table.
2268 template<int size, bool big_endian>
2270 Sized_relobj_incr<size, big_endian>::do_scan_relocs(Symbol_table*,
2274 // Count the incremental relocations for this object.
2275 unsigned int nsyms = this->input_reader_.get_global_symbol_count();
2276 this->allocate_incremental_reloc_counts();
2277 for (unsigned int i = 0; i < nsyms; i++)
2279 Incremental_global_symbol_reader<big_endian> sym =
2280 this->input_reader_.get_global_symbol_reader(i);
2281 unsigned int reloc_count = sym.reloc_count();
2282 if (reloc_count > 0 && this->incr_reloc_offset_ == -1U)
2283 this->incr_reloc_offset_ = sym.reloc_offset();
2284 this->incr_reloc_count_ += reloc_count;
2285 for (unsigned int j = 0; j < reloc_count; j++)
2286 this->count_incremental_reloc(i);
2288 this->incr_reloc_output_index_ =
2289 layout->incremental_inputs()->get_reloc_count();
2290 this->finalize_incremental_relocs(layout, false);
2292 // The incoming incremental relocations may not end up in the same
2293 // location after the incremental update, because the incremental info
2294 // is regenerated in each link. Because the new location may overlap
2295 // with other data in the updated output file, we need to copy the
2296 // relocations into a buffer so that we can still read them safely
2297 // after we start writing updates to the output file.
2298 if (this->incr_reloc_count_ > 0)
2300 const Incremental_relocs_reader<size, big_endian>& relocs_reader =
2301 this->ibase_->relocs_reader();
2302 const unsigned int incr_reloc_size = relocs_reader.reloc_size;
2303 unsigned int len = this->incr_reloc_count_ * incr_reloc_size;
2304 this->incr_relocs_ = new unsigned char[len];
2305 memcpy(this->incr_relocs_,
2306 relocs_reader.data(this->incr_reloc_offset_),
2311 // Count the local symbols.
2313 template<int size, bool big_endian>
2315 Sized_relobj_incr<size, big_endian>::do_count_local_symbols(
2316 Stringpool_template<char>* pool,
2317 Stringpool_template<char>*)
2319 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
2321 // Set the count of local symbols based on the incremental info.
2322 unsigned int nlocals = this->input_reader_.get_local_symbol_count();
2323 this->local_symbol_count_ = nlocals;
2324 this->local_symbols_.reserve(nlocals);
2326 // Get views of the base file's symbol table and string table.
2327 Incremental_binary::View symtab_view(NULL);
2328 unsigned int symtab_count;
2329 elfcpp::Elf_strtab strtab(NULL, 0);
2330 this->ibase_->get_symtab_view(&symtab_view, &symtab_count, &strtab);
2332 // Read the local symbols from the base file's symbol table.
2333 off_t off = this->input_reader_.get_local_symbol_offset();
2334 const unsigned char* symp = symtab_view.data() + off;
2335 for (unsigned int i = 0; i < nlocals; ++i, symp += sym_size)
2337 elfcpp::Sym<size, big_endian> sym(symp);
2339 if (!strtab.get_c_string(sym.get_st_name(), &name))
2341 gold_debug(DEBUG_INCREMENTAL, "Local symbol %d: %s", i, name);
2342 name = pool->add(name, true, NULL);
2343 this->local_symbols_.push_back(Local_symbol(name,
2352 // Finalize the local symbols.
2354 template<int size, bool big_endian>
2356 Sized_relobj_incr<size, big_endian>::do_finalize_local_symbols(
2361 this->local_symbol_index_ = index;
2362 this->local_symbol_offset_ = off;
2363 return index + this->local_symbol_count_;
2366 // Set the offset where local dynamic symbol information will be stored.
2368 template<int size, bool big_endian>
2370 Sized_relobj_incr<size, big_endian>::do_set_local_dynsym_indexes(
2373 // FIXME: set local dynsym indexes.
2377 // Set the offset where local dynamic symbol information will be stored.
2379 template<int size, bool big_endian>
2381 Sized_relobj_incr<size, big_endian>::do_set_local_dynsym_offset(off_t)
2386 // Relocate the input sections and write out the local symbols.
2387 // We don't actually do any relocation here. For unchanged input files,
2388 // we reapply relocations only for symbols that have changed; that happens
2389 // in queue_final_tasks. We do need to rewrite the incremental relocations
2392 template<int size, bool big_endian>
2394 Sized_relobj_incr<size, big_endian>::do_relocate(const Symbol_table*,
2395 const Layout* layout,
2398 if (this->incr_reloc_count_ == 0)
2401 const unsigned int incr_reloc_size =
2402 Incremental_relocs_reader<size, big_endian>::reloc_size;
2404 // Get a view for the .gnu_incremental_relocs section.
2405 Incremental_inputs* inputs = layout->incremental_inputs();
2406 gold_assert(inputs != NULL);
2407 const off_t relocs_off = inputs->relocs_section()->offset();
2408 const off_t relocs_size = inputs->relocs_section()->data_size();
2409 unsigned char* const view = of->get_output_view(relocs_off, relocs_size);
2411 // Copy the relocations from the buffer.
2412 off_t off = this->incr_reloc_output_index_ * incr_reloc_size;
2413 unsigned int len = this->incr_reloc_count_ * incr_reloc_size;
2414 memcpy(view + off, this->incr_relocs_, len);
2416 // The output section table may have changed, so we need to map
2417 // the old section index to the new section index for each relocation.
2418 for (unsigned int i = 0; i < this->incr_reloc_count_; ++i)
2420 unsigned char* pov = view + off + i * incr_reloc_size;
2421 unsigned int shndx = elfcpp::Swap<32, big_endian>::readval(pov + 4);
2422 Output_section* os = this->ibase_->output_section(shndx);
2423 gold_assert(os != NULL);
2424 shndx = os->out_shndx();
2425 elfcpp::Swap<32, big_endian>::writeval(pov + 4, shndx);
2428 of->write_output_view(off, len, view);
2430 // Get views into the output file for the portions of the symbol table
2431 // and the dynamic symbol table that we will be writing.
2432 off_t symtab_off = layout->symtab_section()->offset();
2433 off_t output_size = this->local_symbol_count_ * This::sym_size;
2434 unsigned char* oview = NULL;
2435 if (output_size > 0)
2436 oview = of->get_output_view(symtab_off + this->local_symbol_offset_,
2439 off_t dyn_output_size = this->output_local_dynsym_count_ * sym_size;
2440 unsigned char* dyn_oview = NULL;
2441 if (dyn_output_size > 0)
2442 dyn_oview = of->get_output_view(this->local_dynsym_offset_,
2445 // Write the local symbols.
2446 unsigned char* ov = oview;
2447 unsigned char* dyn_ov = dyn_oview;
2448 const Stringpool* sympool = layout->sympool();
2449 const Stringpool* dynpool = layout->dynpool();
2450 Output_symtab_xindex* symtab_xindex = layout->symtab_xindex();
2451 Output_symtab_xindex* dynsym_xindex = layout->dynsym_xindex();
2452 for (unsigned int i = 0; i < this->local_symbol_count_; ++i)
2454 Local_symbol& lsym(this->local_symbols_[i]);
2457 unsigned int st_shndx = this->adjust_sym_shndx(i, lsym.st_shndx,
2461 Output_section* os = this->ibase_->output_section(st_shndx);
2462 st_shndx = os->out_shndx();
2463 if (st_shndx >= elfcpp::SHN_LORESERVE)
2465 symtab_xindex->add(this->local_symbol_index_ + i, st_shndx);
2466 if (lsym.needs_dynsym_entry)
2467 dynsym_xindex->add(lsym.output_dynsym_index, st_shndx);
2468 st_shndx = elfcpp::SHN_XINDEX;
2472 // Write the symbol to the output symbol table.
2474 elfcpp::Sym_write<size, big_endian> osym(ov);
2475 osym.put_st_name(sympool->get_offset(lsym.name));
2476 osym.put_st_value(lsym.st_value);
2477 osym.put_st_size(lsym.st_size);
2478 osym.put_st_info(elfcpp::STB_LOCAL,
2479 static_cast<elfcpp::STT>(lsym.st_type));
2480 osym.put_st_other(0);
2481 osym.put_st_shndx(st_shndx);
2485 // Write the symbol to the output dynamic symbol table.
2486 if (lsym.needs_dynsym_entry)
2488 gold_assert(dyn_ov < dyn_oview + dyn_output_size);
2489 elfcpp::Sym_write<size, big_endian> osym(dyn_ov);
2490 osym.put_st_name(dynpool->get_offset(lsym.name));
2491 osym.put_st_value(lsym.st_value);
2492 osym.put_st_size(lsym.st_size);
2493 osym.put_st_info(elfcpp::STB_LOCAL,
2494 static_cast<elfcpp::STT>(lsym.st_type));
2495 osym.put_st_other(0);
2496 osym.put_st_shndx(st_shndx);
2501 if (output_size > 0)
2503 gold_assert(ov - oview == output_size);
2504 of->write_output_view(symtab_off + this->local_symbol_offset_,
2505 output_size, oview);
2508 if (dyn_output_size > 0)
2510 gold_assert(dyn_ov - dyn_oview == dyn_output_size);
2511 of->write_output_view(this->local_dynsym_offset_, dyn_output_size,
2516 // Set the offset of a section.
2518 template<int size, bool big_endian>
2520 Sized_relobj_incr<size, big_endian>::do_set_section_offset(unsigned int,
2525 // Class Sized_incr_dynobj. Most of these methods are not used for
2526 // Incremental objects, but are required to be implemented by the
2527 // base class Object.
2529 template<int size, bool big_endian>
2530 Sized_incr_dynobj<size, big_endian>::Sized_incr_dynobj(
2531 const std::string& name,
2532 Sized_incremental_binary<size, big_endian>* ibase,
2533 unsigned int input_file_index)
2534 : Dynobj(name, NULL), ibase_(ibase),
2535 input_file_index_(input_file_index),
2536 input_reader_(ibase->inputs_reader().input_file(input_file_index)),
2539 if (this->input_reader_.is_in_system_directory())
2540 this->set_is_in_system_directory();
2541 if (this->input_reader_.as_needed())
2542 this->set_as_needed();
2543 this->set_soname_string(this->input_reader_.get_soname());
2547 // Read the symbols.
2549 template<int size, bool big_endian>
2551 Sized_incr_dynobj<size, big_endian>::do_read_symbols(Read_symbols_data*)
2556 // Lay out the input sections.
2558 template<int size, bool big_endian>
2560 Sized_incr_dynobj<size, big_endian>::do_layout(
2567 // Add the symbols to the symbol table.
2569 template<int size, bool big_endian>
2571 Sized_incr_dynobj<size, big_endian>::do_add_symbols(
2572 Symbol_table* symtab,
2576 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
2577 unsigned char symbuf[sym_size];
2578 elfcpp::Sym<size, big_endian> sym(symbuf);
2579 elfcpp::Sym_write<size, big_endian> osym(symbuf);
2581 typedef typename elfcpp::Elf_types<size>::Elf_WXword Elf_size_type;
2583 unsigned int nsyms = this->input_reader_.get_global_symbol_count();
2584 this->symbols_.resize(nsyms);
2586 Incremental_binary::View symtab_view(NULL);
2587 unsigned int symtab_count;
2588 elfcpp::Elf_strtab strtab(NULL, 0);
2589 this->ibase_->get_symtab_view(&symtab_view, &symtab_count, &strtab);
2591 Incremental_symtab_reader<big_endian> isymtab(this->ibase_->symtab_reader());
2592 unsigned int isym_count = isymtab.symbol_count();
2593 unsigned int first_global = symtab_count - isym_count;
2595 // We keep a set of symbols that we have generated COPY relocations
2596 // for, indexed by the symbol value. We do not need more than one
2597 // COPY relocation per address.
2598 typedef typename std::set<Address> Copied_symbols;
2599 Copied_symbols copied_symbols;
2601 const unsigned char* sym_p;
2602 for (unsigned int i = 0; i < nsyms; ++i)
2606 unsigned int output_symndx =
2607 this->input_reader_.get_output_symbol_index(i, &is_def, &is_copy);
2608 sym_p = symtab_view.data() + output_symndx * sym_size;
2609 elfcpp::Sym<size, big_endian> gsym(sym_p);
2611 if (!strtab.get_c_string(gsym.get_st_name(), &name))
2616 elfcpp::STB st_bind = gsym.get_st_bind();
2617 elfcpp::STT st_type = gsym.get_st_type();
2619 // Local hidden symbols start out as globals, but get converted to
2620 // to local during output.
2621 if (st_bind == elfcpp::STB_LOCAL)
2622 st_bind = elfcpp::STB_GLOBAL;
2626 shndx = elfcpp::SHN_UNDEF;
2631 // For a symbol defined in a shared object, the section index
2632 // is meaningless, as long as it's not SHN_UNDEF.
2634 v = gsym.get_st_value();
2637 osym.put_st_name(0);
2638 osym.put_st_value(v);
2639 osym.put_st_size(gsym.get_st_size());
2640 osym.put_st_info(st_bind, st_type);
2641 osym.put_st_other(gsym.get_st_other());
2642 osym.put_st_shndx(shndx);
2644 Sized_symbol<size>* res =
2645 symtab->add_from_incrobj<size, big_endian>(this, name, NULL, &sym);
2646 this->symbols_[i] = res;
2647 this->ibase_->add_global_symbol(output_symndx - first_global,
2652 std::pair<typename Copied_symbols::iterator, bool> ins =
2653 copied_symbols.insert(v);
2656 unsigned int shndx = gsym.get_st_shndx();
2657 Output_section* os = this->ibase_->output_section(shndx);
2658 off_t offset = v - os->address();
2659 this->ibase_->add_copy_reloc(this->symbols_[i], os, offset);
2665 // Return TRUE if we should include this object from an archive library.
2667 template<int size, bool big_endian>
2668 Archive::Should_include
2669 Sized_incr_dynobj<size, big_endian>::do_should_include_member(
2678 // Iterate over global symbols, calling a visitor class V for each.
2680 template<int size, bool big_endian>
2682 Sized_incr_dynobj<size, big_endian>::do_for_all_global_symbols(
2684 Library_base::Symbol_visitor_base*)
2686 // This routine is not used for dynamic libraries.
2689 // Iterate over local symbols, calling a visitor class V for each GOT offset
2690 // associated with a local symbol.
2692 template<int size, bool big_endian>
2694 Sized_incr_dynobj<size, big_endian>::do_for_all_local_got_entries(
2695 Got_offset_list::Visitor*) const
2699 // Get the size of a section.
2701 template<int size, bool big_endian>
2703 Sized_incr_dynobj<size, big_endian>::do_section_size(unsigned int)
2708 // Get the name of a section.
2710 template<int size, bool big_endian>
2712 Sized_incr_dynobj<size, big_endian>::do_section_name(unsigned int)
2717 // Return a view of the contents of a section.
2719 template<int size, bool big_endian>
2721 Sized_incr_dynobj<size, big_endian>::do_section_contents(unsigned int)
2726 // Return section flags.
2728 template<int size, bool big_endian>
2730 Sized_incr_dynobj<size, big_endian>::do_section_flags(unsigned int)
2735 // Return section entsize.
2737 template<int size, bool big_endian>
2739 Sized_incr_dynobj<size, big_endian>::do_section_entsize(unsigned int)
2744 // Return section address.
2746 template<int size, bool big_endian>
2748 Sized_incr_dynobj<size, big_endian>::do_section_address(unsigned int)
2753 // Return section type.
2755 template<int size, bool big_endian>
2757 Sized_incr_dynobj<size, big_endian>::do_section_type(unsigned int)
2762 // Return the section link field.
2764 template<int size, bool big_endian>
2766 Sized_incr_dynobj<size, big_endian>::do_section_link(unsigned int)
2771 // Return the section link field.
2773 template<int size, bool big_endian>
2775 Sized_incr_dynobj<size, big_endian>::do_section_info(unsigned int)
2780 // Return the section alignment.
2782 template<int size, bool big_endian>
2784 Sized_incr_dynobj<size, big_endian>::do_section_addralign(unsigned int)
2789 // Return the Xindex structure to use.
2791 template<int size, bool big_endian>
2793 Sized_incr_dynobj<size, big_endian>::do_initialize_xindex()
2798 // Get symbol counts.
2800 template<int size, bool big_endian>
2802 Sized_incr_dynobj<size, big_endian>::do_get_global_symbol_counts(
2803 const Symbol_table*, size_t*, size_t*) const
2808 // Allocate an incremental object of the appropriate size and endianness.
2811 make_sized_incremental_object(
2812 Incremental_binary* ibase,
2813 unsigned int input_file_index,
2814 Incremental_input_type input_type,
2815 const Incremental_binary::Input_reader* input_reader)
2818 std::string name(input_reader->filename());
2820 switch (parameters->size_and_endianness())
2822 #ifdef HAVE_TARGET_32_LITTLE
2823 case Parameters::TARGET_32_LITTLE:
2825 Sized_incremental_binary<32, false>* sized_ibase =
2826 static_cast<Sized_incremental_binary<32, false>*>(ibase);
2827 if (input_type == INCREMENTAL_INPUT_SHARED_LIBRARY)
2828 obj = new Sized_incr_dynobj<32, false>(name, sized_ibase,
2831 obj = new Sized_relobj_incr<32, false>(name, sized_ibase,
2836 #ifdef HAVE_TARGET_32_BIG
2837 case Parameters::TARGET_32_BIG:
2839 Sized_incremental_binary<32, true>* sized_ibase =
2840 static_cast<Sized_incremental_binary<32, true>*>(ibase);
2841 if (input_type == INCREMENTAL_INPUT_SHARED_LIBRARY)
2842 obj = new Sized_incr_dynobj<32, true>(name, sized_ibase,
2845 obj = new Sized_relobj_incr<32, true>(name, sized_ibase,
2850 #ifdef HAVE_TARGET_64_LITTLE
2851 case Parameters::TARGET_64_LITTLE:
2853 Sized_incremental_binary<64, false>* sized_ibase =
2854 static_cast<Sized_incremental_binary<64, false>*>(ibase);
2855 if (input_type == INCREMENTAL_INPUT_SHARED_LIBRARY)
2856 obj = new Sized_incr_dynobj<64, false>(name, sized_ibase,
2859 obj = new Sized_relobj_incr<64, false>(name, sized_ibase,
2864 #ifdef HAVE_TARGET_64_BIG
2865 case Parameters::TARGET_64_BIG:
2867 Sized_incremental_binary<64, true>* sized_ibase =
2868 static_cast<Sized_incremental_binary<64, true>*>(ibase);
2869 if (input_type == INCREMENTAL_INPUT_SHARED_LIBRARY)
2870 obj = new Sized_incr_dynobj<64, true>(name, sized_ibase,
2873 obj = new Sized_relobj_incr<64, true>(name, sized_ibase,
2882 gold_assert(obj != NULL);
2886 // Copy the unused symbols from the incremental input info.
2887 // We need to do this because we may be overwriting the incremental
2888 // input info in the base file before we write the new incremental
2891 Incremental_library::copy_unused_symbols()
2893 unsigned int symcount = this->input_reader_->get_unused_symbol_count();
2894 this->unused_symbols_.reserve(symcount);
2895 for (unsigned int i = 0; i < symcount; ++i)
2897 std::string name(this->input_reader_->get_unused_symbol(i));
2898 this->unused_symbols_.push_back(name);
2902 // Iterator for unused global symbols in the library.
2904 Incremental_library::do_for_all_unused_symbols(Symbol_visitor_base* v) const
2906 for (Symbol_list::const_iterator p = this->unused_symbols_.begin();
2907 p != this->unused_symbols_.end();
2909 v->visit(p->c_str());
2912 // Instantiate the templates we need.
2914 #ifdef HAVE_TARGET_32_LITTLE
2916 class Sized_incremental_binary<32, false>;
2919 class Sized_relobj_incr<32, false>;
2922 class Sized_incr_dynobj<32, false>;
2925 #ifdef HAVE_TARGET_32_BIG
2927 class Sized_incremental_binary<32, true>;
2930 class Sized_relobj_incr<32, true>;
2933 class Sized_incr_dynobj<32, true>;
2936 #ifdef HAVE_TARGET_64_LITTLE
2938 class Sized_incremental_binary<64, false>;
2941 class Sized_relobj_incr<64, false>;
2944 class Sized_incr_dynobj<64, false>;
2947 #ifdef HAVE_TARGET_64_BIG
2949 class Sized_incremental_binary<64, true>;
2952 class Sized_relobj_incr<64, true>;
2955 class Sized_incr_dynobj<64, true>;
2958 } // End namespace gold.